[ python-Bugs-1295909 ] inspect.getsource() misses single line blocks.

SourceForge.net noreply at sourceforge.net
Sun Sep 25 13:51:51 CEST 2005


Bugs item #1295909, was opened at 2005-09-20 01:00
Message generated for change (Comment added) made by arigo
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1295909&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.4
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Ron Adam (ronadam)
Assigned to: Armin Rigo (arigo)
Summary: inspect.getsource() misses single line blocks.

Initial Comment:
While playing around with the inspect module I found
that the Blockfinder doesn't recognize single line
function definitions.

Adding the following two lines to it fixes it, but I'm
not sure if it causes problems anywhere else.


C:\Python24\Lib>diff.py inspect.py inspect_.py
*** inspect.py Tue Mar 15 13:22:02 2005
--- inspect_.py Mon Sep 19 14:26:26 2005
***************
*** 531,536 ****
--- 531,538 ----
                  raise EndOfBlock, self.last
          elif type == tokenize.NAME and scol == 0:
              raise EndOfBlock, self.last
+         elif self.indent == 0:
+             raise EndOfBlock, self.last

  def getblock(lines):
      """Extract the block of code at the top of the
given list of lines."""


Version info:
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310
32 bit (Intel)] on win32


def test(t):
    print '**',t,'**'
    print "Line:"
    def f(): pass
    """ This line shouldn't be visible """
    print inspect.getsource(f)

    print "Block:"
    def f():
        pass
        pass
    """This line should not be visible."""
    print inspect.getsource(f)

import inspect
test("before")

import inspect_ as inspect
test("after")


#-- output --

** before **
Line:
    def f(): pass
    """ This line shouldn't be visible """
    print inspect.getsource(f)

    print "Block:"
    def f():
        pass
        pass

Block:
    def f():
        pass
        pass


** after **
Line:
    def f(): pass

Block:
    def f():
        pass
        pass



----------------------------------------------------------------------

>Comment By: Armin Rigo (arigo)
Date: 2005-09-25 11:51

Message:
Logged In: YES 
user_id=4771

Checked in:
  Lib/inspect.py               1.63
  Lib/test/inspect_fodder2.py  1.5
  Lib/test/test_inspect.py     1.21

Not backported to 2.4 for now (the branch is frozen for 2.4.2 release and it's unclear if this is critical enough).

Thanks!  The patch I applied is essentially what you proposed, with the exception of special tokens (NL and COMMENT) that should not end a block even at indentation level zero.

----------------------------------------------------------------------

Comment By: Armin Rigo (arigo)
Date: 2005-09-25 10:57

Message:
Logged In: YES 
user_id=4771

While working on PyPy we stubled over a couple of bugs in getsource() as well, and thought about some fix.  I will add your example and a few more to test_inspect.py and fix/clean up a bit getblock() in inspect.py.  I will also try to stress-test getsource() on some example programs.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1295909&group_id=5470


More information about the Python-bugs-list mailing list