Bug in Getsource?

Chris S. chrisks at NOSPAMudel.edu
Sat Jun 26 00:19:23 EDT 2004


Konstantin Veretennicov wrote:

> "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

How is your current fix not satisfactory? Is that not the correct code?



More information about the Python-list mailing list