getsource returns incomplete code when lambda passed to decorator
Closed this issue · 3 comments
mmckerns commented
In the code below, the results for fun_2
are incomplete:
class Decorator(object):
def __init__(self, item):
self.item = item
self.func = None
def __call__(self, func):
self.func = func
return self
@Decorator(42)
def fun_1():
pass
@Decorator(lambda x: 42)
def fun_2():
pass
@Decorator(fun_2.item)
def fun_3():
pass
import inspect
print("Inspect results")
print(inspect.getsource(fun_1.func))
print(inspect.getsource(fun_2.func))
print(inspect.getsource(fun_3.func))
import dill
print("Dill results")
print(dill.source.getsource(fun_1.func))
print(dill.source.getsource(fun_2.func))
print(dill.source.getsource(fun_3.func))
The expected results are:
@Decorator(lambda x: 42)
def fun_2():
pass
but what is obtained is
@Decorator(lambda x: 42)
Both dill
and inspect
fail with the same results.
Note this issue was reported on StackOverflow: http://stackoverflow.com/questions/43063658
mmckerns commented
This, however, does work:
>>> f = lambda x: 42
>>> @Decorator(f)
... def fun_4():
... pass
...
>>>
>>> print dill.source.getsource(fun_4.func)
@Decorator(f)
def fun_4():
pass
>>>
mmckerns commented
interestingly, the original code fails in python2.x through python3.4... and works in python3.5 and later.