"Chris S." <ch*****@NOSPAMudel.edu> wrote in message news:<cb**********@scrotar.nss.udel.edu>...
I've noticed inspect.getsource acts strangely for lambda expressions.
For instance:
from inspect import getsource
somefunc = lambda(a):abs(a)
print 'somefunc source:',getsource(somefunc)
results in:
somefunc source: from inspect import getsource
Yep, same here.
I'd understand if inspect can't handle lambda expressions, but claiming
something is an object's source when it is obviously not is a bug IMO.
Is this correct? Is there any way to fix this?
Looks like the problem is in inspect.findsource(), line 433
pat = re.compile(r'^(\s*def\s)|(.*\slambda(:|\s))')
As you can see, this pattern doesn't match (perfectly legal) "lambda(a):"
(note the parentheses).
If I change pattern to r'^(\s*def\s)|(.*\slambda(:|\s|\())',
getsource returns "somefunc = lambda(a): abs(a)",
which is better, but still not satisfactory.
Alas, I'm too lazy and/or stupid and/or busy to conceive a good patch ;)
- kv