inspect getsource() minor fix?

R

Ron Adam

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 any problems anywhere else.

elif self.indent == 0:
raise EndOfBlock, self.last

Cheers,
Ron


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




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
 

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,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top