This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: inspect.getsource() misses single line blocks.
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: arigo Nosy List: arigo, ronadam
Priority: normal Keywords:

Created on 2005-09-20 01:00 by ronadam, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg26329 - (view) Author: Ron Adam (ronadam) Date: 2005-09-20 01:00
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

msg26330 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2005-09-25 10:57
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.
msg26331 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2005-09-25 11:51
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.
History
Date User Action Args
2022-04-11 14:56:13adminsetgithub: 42386
2005-09-20 01:00:04ronadamcreate