[py-svn] r57054 - in py/branch/event/py/code: . testing

hpk at codespeak.net hpk at codespeak.net
Thu Aug 7 12:09:43 CEST 2008


Author: hpk
Date: Thu Aug  7 12:09:41 2008
New Revision: 57054

Modified:
   py/branch/event/py/code/code.py
   py/branch/event/py/code/excinfo.py
   py/branch/event/py/code/testing/test_code.py
   py/branch/event/py/code/testing/test_excinfo.py
   py/branch/event/py/code/traceback2.py
Log:
try harder to get at source code for traceback entries


Modified: py/branch/event/py/code/code.py
==============================================================================
--- py/branch/event/py/code/code.py	(original)
+++ py/branch/event/py/code/code.py	Thu Aug  7 12:09:41 2008
@@ -58,13 +58,15 @@
         )
 
     def path(self):
-        """ return a py.path.local object wrapping the source of the code """
+        """ return a py.path.local object pointing to the source code """
         fn = self.raw.co_filename 
         try:
             return fn.__path__
         except AttributeError:
             p = py.path.local(self.raw.co_filename)
-            if not p.check():
+            if not p.check(file=1):
+                # XXX maybe try harder like the weird logic 
+                # in the standard lib [linecache.updatecache] does? 
                 p = self.raw.co_filename
             return p
                 
@@ -77,6 +79,9 @@
         try:
             return fn.__source__
         except AttributeError:
+            path = self.path
+            if not isinstance(path, py.path.local):
+                return None
             return py.code.Source(self.path.read(mode="rU"))
     fullsource = property(fullsource, None, None,
                           "full source containing this code object")

Modified: py/branch/event/py/code/excinfo.py
==============================================================================
--- py/branch/event/py/code/excinfo.py	(original)
+++ py/branch/event/py/code/excinfo.py	Thu Aug  7 12:09:41 2008
@@ -95,9 +95,8 @@
         return 4 + (len(s) - len(s.lstrip()))
 
     def _getentrysource(self, entry):
-        try:
-            source = entry.getsource()
-        except py.error.ENOENT:
+        source = entry.getsource()
+        if source is None:
             source = py.code.Source("???")
         return source.deindent()
 

Modified: py/branch/event/py/code/testing/test_code.py
==============================================================================
--- py/branch/event/py/code/testing/test_code.py	(original)
+++ py/branch/event/py/code/testing/test_code.py	Thu Aug  7 12:09:41 2008
@@ -77,3 +77,5 @@
     assert co_code.co_filename == name
     code = py.code.Code(co_code)
     assert str(code.path) == name 
+    assert code.fullsource is None
+    

Modified: py/branch/event/py/code/testing/test_excinfo.py
==============================================================================
--- py/branch/event/py/code/testing/test_excinfo.py	(original)
+++ py/branch/event/py/code/testing/test_excinfo.py	Thu Aug  7 12:09:41 2008
@@ -217,6 +217,31 @@
     s = str(excinfo.traceback[-1])
     assert s == "  File '<string>':1 in <module>\n  ???\n"
 
+def test_entrysource_Queue_example():
+    import Queue
+    try:
+        Queue.Queue().get(timeout=0.001)
+    except Queue.Empty:
+        excinfo = py.code.ExceptionInfo()
+    entry = excinfo.traceback[-1]
+    source = entry.getsource()
+    assert source is not None
+    s = str(source).strip()
+    assert s.startswith("def get")
+
+def test_codepath_Queue_example():
+    py.test.skip("try harder to get at the paths of code objects.")
+    import Queue
+    try:
+        Queue.Queue().get(timeout=0.001)
+    except Queue.Empty:
+        excinfo = py.code.ExceptionInfo()
+    entry = excinfo.traceback[-1]
+    path = entry.path
+    assert isinstance(path, py.path.local)
+    assert path.basename == "Queue.py"
+    assert path.check()
+
 class TestFormattedExcinfo: 
     def setup_method(self, method):
         self.tmpdir = py.test2.ensuretemp("%s_%s" %(
@@ -269,6 +294,17 @@
             'E       assert 0'
         ]
 
+
+    def test_repr_source_not_existing(self):
+        pr = FormattedExcinfo()
+        co = compile("raise ValueError()", "", "exec")
+        try:
+            exec co 
+        except ValueError:
+            excinfo = py.code.ExceptionInfo()
+        repr = pr.repr_excinfo(excinfo)
+        assert repr.reprtraceback.reprentries[1].lines[0] == ">   ???"
+
     def test_repr_local(self):
         p = FormattedExcinfo(showlocals=True)
         loc = {'y': 5, 'z': 7, 'x': 3, '__builtins__': __builtins__}

Modified: py/branch/event/py/code/traceback2.py
==============================================================================
--- py/branch/event/py/code/traceback2.py	(original)
+++ py/branch/event/py/code/traceback2.py	Thu Aug  7 12:09:41 2008
@@ -52,6 +52,13 @@
     def getsource(self): 
         """ return failing source code. """ 
         source = self.frame.code.fullsource
+        if source is None:
+            try:
+                sourcelines, lineno = py.std.inspect.findsource(self.frame.code.raw)
+            except IOError:
+                return None
+            source = py.code.Source()
+            source.lines = map(str.rstrip, sourcelines)
         start = self.getfirstlinesource()
         end = self.lineno
         try:



More information about the pytest-commit mailing list