[py-svn] r51651 - in py/branch/event/py/test2: . testing

hpk at codespeak.net hpk at codespeak.net
Tue Feb 19 20:29:37 CET 2008


Author: hpk
Date: Tue Feb 19 20:29:35 2008
New Revision: 51651

Modified:
   py/branch/event/py/test2/present.py
   py/branch/event/py/test2/testing/test_present.py
Log:
test and streamline short tracebacks 


Modified: py/branch/event/py/test2/present.py
==============================================================================
--- py/branch/event/py/test2/present.py	(original)
+++ py/branch/event/py/test2/present.py	Tue Feb 19 20:29:35 2008
@@ -57,8 +57,10 @@
         """ This one represents piece of source with possible
         marker at requested position
         """
+        if source is None:
+            self.out.line("???") 
+            return
         if isinstance(source, str):
-            # why the hell, string is iterable?
             source = source.split("\n")
         if marker_location < 0:
             marker_location += len(source)
@@ -85,12 +87,16 @@
             s = str(source.getstatement(len(source)-1))
         except KeyboardInterrupt: 
             raise 
-        except: 
+        except:
             s = str(source[-1])
-        indent = " " * (4 + (len(s) - len(s.lstrip())))
+        indent = 4 + (len(s) - len(s.lstrip()))
+        self.repr_exconly(excinfo, indent=indent) 
+
+    def repr_exconly(self, excinfo, indent):
+        indent = " " * indent 
         # get the real exception information out 
         lines = excinfo.exconly(tryshort=True).split('\n')
-        self.out.line('>' + indent[:-1] + lines.pop(0))
+        self.out.line('E' + indent[:-1] + lines.pop(0))
         for x in lines:
             self.out.line(indent + x)
 
@@ -133,12 +139,6 @@
         self.repr_failure_headline(item) 
         self.repr_tb(item, excinfo) 
         
-    def repr_tb(self, item, excinfo): 
-        traceback = self.filtertraceback(item, excinfo.traceback) 
-        recursionindex = traceback.recursionindex()
-        repr_tb = getattr(self, "repr_tb_" + self.config.option.tbstyle)
-        repr_tb(item, excinfo, traceback, recursionindex)
-
     def repr_out_err(self, colitem): 
         for parent in colitem.listchain(): 
             for name, obj in zip(['out', 'err'], parent._getouterr()): 
@@ -146,34 +146,44 @@
                     self.out.sep("- ", "%s: recorded std%s" % (parent.name, name))
                     self.out.line(obj)
 
+    def repr_tb_entry(self, entry, excinfo=None):
+        # excinfo is None if this is the last tb entry 
+        source = self.getentrysource(entry)
+        firstsourceline = entry.getfirstlinesource()
+        marker_location = entry.lineno - firstsourceline
+        self.repr_source(source, '>', marker_location)
+        if excinfo: 
+            self.repr_failure_explanation(excinfo, source) 
+        self.out.line("") 
+        self.out.line("[%s:%d]" %(entry.path, entry.lineno+1))
+        self.repr_locals(entry.locals)
+
+    def repr_tb_entry_last(self, item, entry, excinfo):
+        self.repr_tb_entry(entry, excinfo) 
+        self.repr_out_err(item) 
+
+    def repr_tb(self, item, excinfo): 
+        traceback = self.filtertraceback(item, excinfo.traceback) 
+        recursionindex = traceback.recursionindex()
+        repr_tb = getattr(self, "repr_tb_" + self.config.option.tbstyle)
+        repr_tb(item, excinfo, traceback, recursionindex)
+
     def repr_tb_long(self, item, excinfo, traceback, recursionindex):
         last = traceback[-1]
-        first = traceback[0]
         for index, entry in py.builtin.enumerate(traceback): 
-            source = self.getentrysource(entry)
-            firstsourceline = entry.getfirstlinesource()
-            marker_location = entry.lineno - firstsourceline
-            if entry == last: 
-                self.repr_source(source, 'E', marker_location)
-                self.repr_failure_explanation(excinfo, source) 
+            if last != entry: 
+                self.repr_tb_entry(entry)
+                self.out.sep("_ ")
             else:
-                self.repr_source(source, '>', marker_location)
-            self.out.line("") 
-            self.out.line("[%s:%d]" %(entry.path, entry.lineno+1))
-            self.repr_locals(entry.locals)
-
-            # trailing info 
-            if entry == last:
-                self.repr_out_err(item) 
+                self.repr_tb_entry_last(item, entry, excinfo) 
                 self.out.sep("_")
-            else: 
-                self.out.sep("_ ")
-                if index == recursionindex:
-                    self.out.line("Recursion detected (same locals & position)")
-                    self.out.sep("!")
-                    break
+            if index == recursionindex:
+                self.out.line("Recursion detected (same locals & position)")
+                self.out.sep("!")
+                break
 
     def repr_tb_short(self, item, excinfo, traceback, recursionindex): 
+        # XXX refactor
         self.out.line()
         last = traceback[-1]
         for index, entry in py.builtin.enumerate(traceback):
@@ -184,21 +194,13 @@
                 path, entry.lineno+1, entry.name))
             try:
                 source = entry.getsource().lines
-            except py.error.ENOENT:
-                source = ["?"]
+                source = source[relline].lstrip()
+            except (IndexError, py.error.ENOENT):
+                source = None
             else:
-                try:
-                    if len(source) > 1:
-                        source = source[relline]
-                except IndexError:
-                    source = []
+                self.out.line("    " + source) 
             if entry == last:
-                if source:
-                    self.repr_source(source, 'E')
-                self.repr_failure_explanation(excinfo, source) 
-            else:
-                if source:
-                    self.repr_source(source, ' ')
+                self.repr_exconly(excinfo, indent=4)
             self.repr_locals(entry.locals) 
 
             # trailing info 

Modified: py/branch/event/py/test2/testing/test_present.py
==============================================================================
--- py/branch/event/py/test2/testing/test_present.py	(original)
+++ py/branch/event/py/test2/testing/test_present.py	Tue Feb 19 20:29:35 2008
@@ -79,7 +79,7 @@
         source = py.code.Source(f)
         e = f()
         p.repr_failure_explanation(e, source)
-        assert p.stringio.getvalue().startswith(">   ")
+        assert p.stringio.getvalue().startswith("E   ")
 
     def test_repr_local(self):
         p = self.getpresenter("--showlocals") 
@@ -137,7 +137,7 @@
             p.repr_failure(item, excinfo)
             s = p.stringio.getvalue()
             print s
-            assert re.search(">.*xyz", s) 
+            assert re.search("   xyz", s) 
             assert re.search("NameError:.*xyz", s) 
 
         self.gentest(check, tbstyle="short")
@@ -160,5 +160,30 @@
         s = item.repr_failure(excinfo) 
         lines = s.split("\n")
         assert lines[2].find("test_gen[0] -> check(0,)") == -1
-        
 
+    def test_repr_tb_short(self):
+        item, excinfo = suptest.getfailing("""
+            def f(x):
+                assert x 
+            def test_f():
+                f(0)
+        """)
+        item._config.option.tbstyle = "short" 
+        s = item.repr_failure(excinfo)
+        print s
+        index = -1 
+        basename = item.fspath.basename 
+        lines = s.split("\n")[3:]
+        for line in (
+              '  File "%s", line 5, in test_f' % basename, 
+              '    f(0)', 
+              '  File "%s", line 3, in f' % basename, 
+              '    assert x',
+              'E   assert 0'
+        ):
+            actual = lines.pop(0)
+            actual = actual.rstrip()
+            if line != actual:
+                print "expected:", repr(line)
+                print "got     :", repr(actual)
+                assert 0



More information about the pytest-commit mailing list