diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-06-18 22:08:13 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-06-18 22:08:13 (GMT) |
commit | 5ca576ed0a0c697c7e7547adfd0b3af010fd2053 (patch) | |
tree | 0b0db361191363b3c168a6c105030f53e181d3e5 /Lib/inspect.py | |
parent | 1dad6a86de55c38da5c356c2c6d81be8ff7884b1 (diff) | |
download | cpython-5ca576ed0a0c697c7e7547adfd0b3af010fd2053.zip cpython-5ca576ed0a0c697c7e7547adfd0b3af010fd2053.tar.gz cpython-5ca576ed0a0c697c7e7547adfd0b3af010fd2053.tar.bz2 |
Merging the gen-branch into the main line, at Guido's direction. Yay!
Bugfix candidate in inspect.py: it was referencing "self" outside of
a method.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 42 |
1 files changed, 19 insertions, 23 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 2d88bc1..eeb54d2 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -349,32 +349,28 @@ class ListReader: return self.lines[i] else: return '' -class EndOfBlock(Exception): pass - -class BlockFinder: - """Provide a tokeneater() method to detect the end of a code block.""" - def __init__(self): - self.indent = 0 - self.started = 0 - self.last = 0 - - def tokeneater(self, type, token, (srow, scol), (erow, ecol), line): - if not self.started: - if type == tokenize.NAME: self.started = 1 +def getblock(lines): + """Extract the block of code at the top of the given list of lines.""" + + indent = 0 + started = 0 + last = 0 + tokens = tokenize.generate_tokens(ListReader(lines).readline) + + for (type, token, (srow, scol), (erow, ecol), line) in tokens: + if not started: + if type == tokenize.NAME: + started = 1 elif type == tokenize.NEWLINE: - self.last = srow + last = srow elif type == tokenize.INDENT: - self.indent = self.indent + 1 + indent = indent + 1 elif type == tokenize.DEDENT: - self.indent = self.indent - 1 - if self.indent == 0: raise EndOfBlock, self.last - -def getblock(lines): - """Extract the block of code at the top of the given list of lines.""" - try: - tokenize.tokenize(ListReader(lines).readline, BlockFinder().tokeneater) - except EndOfBlock, eob: - return lines[:eob.args[0]] + indent = indent - 1 + if indent == 0: + return lines[:last] + else: + raise ValueError, "unable to find block" def getsourcelines(object): """Return a list of source lines and starting line number for an object. |