Bug in Getsource?

Konstantin Veretennicov kveretennicov at yahoo.com
Fri Jun 25 12:33:07 EDT 2004


"Chris S." <chrisks at NOSPAMudel.edu> wrote in message news:<cba6uq$5eo$1 at 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



More information about the Python-list mailing list