By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

inspect getsource() minor fix?

P: n/a

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
Sep 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.