Commits

holger krekel  committed c8e9760

fix issue7 - raise a proper error if no valid statementrange can be found

  • Participants
  • Parent commits 07ca410

Comments (0)

Files changed (5)

+Changes between 1.4.5 and 1.4.x
+==================================================
+
+- fix issue7: source.getstatementrange() now raises proper error
+  if no valid statement can be found
+
 Changes between 1.4.4 and 1.4.5
 ==================================================
 

File py/__init__.py

 
 (c) Holger Krekel and others, 2004-2010
 """
-__version__ = '1.4.5'
+__version__ = '1.4.6.dev1'
 
 from py import _apipkg
 

File py/_code/source.py

     def getstatementrange(self, lineno, assertion=False):
         """ return (start, end) tuple which spans the minimal
             statement region which containing the given lineno.
+            raise a ValueError if no such statementrange can be found.
         """
         # XXX there must be a better than these heuristic ways ...
         # XXX there may even be better heuristics :-)
 
         # 1. find the start of the statement
         from codeop import compile_command
+        end = None
         for start in range(lineno, -1, -1):
             if assertion:
                 line = self.lines[start]
                 trysource = self[start:end]
                 if trysource.isparseable():
                     return start, end
+        if end is None:
+            raise ValueError("no valid source range around line %d " % (lineno,))
         return start, end
 
     def getblockend(self, lineno):
         name='py',
         description='library with cross-python path, ini-parsing, io, code, log facilities',
         long_description = open('README.txt').read(),
-        version='1.4.5',
+        version='1.4.6.dev1',
         url='http://pylib.org',
         license='MIT license',
         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

File testing/code/test_source.py

         r = source.getstatementrange(1)
         assert r == (1,2)
 
+    def test_getstatementrange_with_syntaxerror_issue7(self):
+        source = Source(":")
+        py.test.raises(ValueError, lambda: source.getstatementrange(0))
+
     @py.test.mark.skipif("sys.version_info < (2,6)")
     def test_compile_to_ast(self):
         import ast