return (start, end) tuple which spans the minimal
statement region which containing the given lineno.
def getstatementrange(self, lineno):
origin: code.Source
where:
exceptions that might appear during execution:
function source:
def getstatementrange(self, lineno):
""" return (start, end) tuple which spans the minimal
statement region which containing the given lineno.
"""
# XXX there must be a better than these heuristic ways ...
# XXX there may even be better heuristics :-)
if not (0 <= lineno < len(self)):
raise IndexError("lineno out of range")
# 1. find the start of the statement
from codeop import compile_command
for start in range(lineno, -1, -1):
trylines = self.lines[start:lineno+1]
# quick hack to indent the source and get it as a string in one go
trylines.insert(0, 'if 0:')
trysource = '\n '.join(trylines)
# ^ space here
try:
compile_command(trysource)
except (SyntaxError, OverflowError, ValueError):
pass
else:
break # got a valid or incomplete statement
# 2. find the end of the statement
for end in range(lineno+1, len(self)+1):
trysource = self[start:end]
if trysource.isparseable():
break
return start, end
called in /home/johnny/projects/merlinux/py/dist/py/code/traceback2.py
traceback code.Source.getstatementrange.0
called in /home/johnny/projects/merlinux/py/dist/py/code/source.py
traceback code.Source.getstatementrange.1
called in /home/johnny/projects/merlinux/py/dist/py/code/testing/test_source.py
traceback code.Source.getstatementrange.2
called in /home/johnny/projects/merlinux/py/dist/py/code/testing/test_source.py
traceback code.Source.getstatementrange.3
called in /home/johnny/projects/merlinux/py/dist/py/code/source.py
traceback code.Source.getstatementrange.4
called in /home/johnny/projects/merlinux/py/dist/py/code/traceback2.py
traceback code.Source.getstatementrange.5
called in /home/johnny/projects/merlinux/py/dist/py/code/traceback2.py
traceback code.Source.getstatementrange.6
called in /home/johnny/projects/merlinux/py/dist/py/code/source.py
traceback code.Source.getstatementrange.7
called in /home/johnny/projects/merlinux/py/dist/py/code/source.py
traceback code.Source.getstatementrange.8
called in /home/johnny/projects/merlinux/py/dist/py/code/testing/test_source.py
traceback code.Source.getstatementrange.9
called in /home/johnny/projects/merlinux/py/dist/py/code/source.py
traceback code.Source.getstatementrange.10
called in /home/johnny/projects/merlinux/py/dist/py/code/testing/test_source.py
traceback code.Source.getstatementrange.11
called in /home/johnny/projects/merlinux/py/dist/py/code/source.py
traceback code.Source.getstatementrange.12
called in /home/johnny/projects/merlinux/py/dist/py/code/source.py
traceback code.Source.getstatementrange.13
called in /home/johnny/projects/merlinux/py/dist/py/code/source.py
traceback code.Source.getstatementrange.14
called in /home/johnny/projects/merlinux/py/dist/py/code/source.py
traceback code.Source.getstatementrange.15
called in /home/johnny/projects/merlinux/py/dist/py/code/testing/test_source.py
traceback code.Source.getstatementrange.16
called in /home/johnny/projects/merlinux/py/dist/py/code/testing/test_source.py
traceback code.Source.getstatementrange.17
called in /home/johnny/projects/merlinux/py/dist/py/code/testing/test_source.py
traceback code.Source.getstatementrange.18
called in /home/johnny/projects/merlinux/py/dist/py/code/source.py
traceback code.Source.getstatementrange.19
called in /home/johnny/projects/merlinux/py/dist/py/code/testing/test_source.py