Skip to content

dill.source.getsource returns partial source when called inside a decorator in a REPL #603

@benclifford

Description

@benclifford

I noticed this behaviour - I'm able to easily work around in my case, but noting it here.

In a REPL, Python 3.11, when getsource is invoked inside a decorator function (so trying to get the source of the function that is being decorated right now), partial source is returned (rather than the whole source, or an error). This problem does not happen with the same code in a Python file executed from the command line.

>>> import demo
>>> @demo.d
... def m():
...   print("hi")
...   return 7
... 
@demo.d
def m():
  print("hi")

>>> 

but in this case, with getsource running much later (getsource is called when the decorated function is invoked), I get the full source:

>>> import demo
>>> @demo.e
... def m():
...   print("hi")
...   return 8
... 
>>> m()
@demo.e
def m():
  print("hi")
  return 8

with demo.py:

import dill.source

def d(f):
    print(dill.source.getsource(f))
    return f

def e(f):
    def wrapped():
      print(dill.source.getsource(f))
    return wrapped

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions