Bug in Getsource?

Discussion in 'Python' started by Chris S., Jun 22, 2004.

  1. Chris S.

    Chris S. Guest

    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?
    Chris S., Jun 22, 2004
    #1
    1. Advertising

  2. "Chris S." <> wrote in message news:<cba6uq$5eo$>...
    > 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
    Konstantin Veretennicov, Jun 25, 2004
    #2
    1. Advertising

  3. Chris S.

    Chris S. Guest

    Konstantin Veretennicov wrote:

    > "Chris S." <> wrote in message news:<cba6uq$5eo$>...
    >
    >>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?
    Chris S., Jun 26, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    Re: BUG? OR NOT A BUG?

    John, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    545
  2. RedEye
    Replies:
    2
    Views:
    578
    Jason Kester
    Dec 13, 2005
  3. Ron Adam

    inspect getsource() minor fix?

    Ron Adam, Sep 19, 2005, in forum: Python
    Replies:
    0
    Views:
    287
    Ron Adam
    Sep 19, 2005
  4. Peng Yu
    Replies:
    0
    Views:
    285
    Peng Yu
    Jun 22, 2010
  5. Ciccio

    inspect.getsource bug?

    Ciccio, Dec 26, 2010, in forum: Python
    Replies:
    1
    Views:
    261
    Ciccio
    Dec 26, 2010
Loading...

Share This Page