Bug in Getsource?

C

Chris S.

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

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?
 
K

Konstantin Veretennicov

Chris S. said:
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
 
C

Chris S.

Konstantin said:
Yep, same here.




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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top