[py-svn] r6954 - in py/dist: example/test py/magic py/test py/test/report/text

hpk at codespeak.net hpk at codespeak.net
Fri Oct 15 23:14:00 CEST 2004


Author: hpk
Date: Fri Oct 15 23:14:00 2004
New Revision: 6954

Added:
   py/dist/example/test/failure_demo.py
      - copied, changed from r6953, py/dist/py/test/test/demo.py
Modified:
   py/dist/py/magic/assertion.py
   py/dist/py/magic/dyncode.py
   py/dist/py/magic/exprinfo.py
   py/dist/py/magic/test_assertion.py
   py/dist/py/magic/test_dyncode.py
   py/dist/py/magic/test_exprinfo.py
   py/dist/py/magic/test_invoke.py
   py/dist/py/test/report/text/reporter.py
   py/dist/py/test/test_collect.py
Log:

- fixed a fundamental flaw in assertion/error handling 
  in try/finally statements.  Now i begin to understand
  the traceback/frame structure better ... 
  which is somewhat involved because py.test provides 
  proper traceback information even if you are running
  dynamically generated code ... 

- created/moved an example/test/failure_demo.py 
  which you can execute with "py.test failure_demo.py | less"
  to see all the nice output you get when you have
  tests failing.  the demo contains a set of like 40 
  differently failing tests. 

- fixed a couple of minor bugs

- added some tests while hunting for the real problem 
  (see first item)



Copied: py/dist/example/test/failure_demo.py (from r6953, py/dist/py/test/test/demo.py)
==============================================================================
--- py/dist/py/test/test/demo.py	(original)
+++ py/dist/example/test/failure_demo.py	Fri Oct 15 23:14:00 2004
@@ -6,7 +6,7 @@
 def somefunc(x,y):
     otherfunc(x,y)
 
-class FailingTests(object):
+class TestFailing(object):
     def test_simple(self):
         def f():
             return 42

Modified: py/dist/py/magic/assertion.py
==============================================================================
--- py/dist/py/magic/assertion.py	(original)
+++ py/dist/py/magic/assertion.py	Fri Oct 15 23:14:00 2004
@@ -5,14 +5,11 @@
 BuiltinAssertionError = __builtin__.AssertionError
 
 class AssertionError(BuiltinAssertionError):
-    __module__ = 'py.magic'
-    
     def __init__(self, *args):
         BuiltinAssertionError.__init__(self, *args)
         f = sys._getframe(1)
         source = dyncode.getparseablestartingblock(f)
         #print "f.f_lineno", f.f_lineno 
-        #print "source = %r" % source 
         if source:
             self.msg = exprinfo.interpret(source, f)
             if self.msg is None:

Modified: py/dist/py/magic/dyncode.py
==============================================================================
--- py/dist/py/magic/dyncode.py	(original)
+++ py/dist/py/magic/dyncode.py	Fri Oct 15 23:14:00 2004
@@ -28,11 +28,19 @@
         tb = tb.tb_next
     return tblist
 
-def getparseablestartingblock(frame): 
-    lineno_hint = frame.f_lineno - 1 
+def getparseablestartingblock(obj): 
+    if hasattr(obj, 'tb_lineno'): 
+        lineno = obj.tb_lineno-1
+        frame = obj.tb_frame
+    elif hasattr(obj, 'f_lineno'):
+        lineno = obj.f_lineno-1
+        frame = obj
+    else:
+        raise ValueError, "can only grok frame and traceback objects" 
+    #lineno_hint = frame.f_lineno - 1 
     #print "getstartingblock: lineno_hint is %d" % lineno_hint 
     lines = magic.dyncode.getlines(frame.f_code.co_filename) 
-    source = getsource_tryparsing(lines, lineno_hint) 
+    source = getsource_tryparsing(lines, lineno) 
     #print "getstartingblock: returning %r" % source
     return source 
 

Modified: py/dist/py/magic/exprinfo.py
==============================================================================
--- py/dist/py/magic/exprinfo.py	(original)
+++ py/dist/py/magic/exprinfo.py	Fri Oct 15 23:14:00 2004
@@ -443,8 +443,13 @@
     return f, getframeline(f, tb.tb_lineno)
 
 def getmsg((typ, val, tb)):
-    frame = magic.dyncode.listtb(tb)[-1].tb_frame 
-    source = dyncode.getparseablestartingblock(frame)
+    #frame, line = gettbline(tb)
+    #frame = RunnerFrame(frame.f_globals, frame.f_locals)
+    #return interpret(line, frame)
+
+    tb = magic.dyncode.listtb(tb)[-1] 
+    source = dyncode.getparseablestartingblock(tb)
+    frame = tb.tb_frame 
     x = interpret(source, frame)
     if not isinstance(x, str):
         raise TypeError, "interpret returned non-string %r" % (x,)

Modified: py/dist/py/magic/test_assertion.py
==============================================================================
--- py/dist/py/magic/test_assertion.py	(original)
+++ py/dist/py/magic/test_assertion.py	Fri Oct 15 23:14:00 2004
@@ -1,43 +1,53 @@
+import py
 
-from py.test import main
-from py.__impl__.magic import assertion
+def setup_module(mod):
+    py.magic.invoke(assertion=1) 
+
+def teardown_module(mod):
+    py.magic.revoke(assertion=1) 
 
 def f():
     return 2
 
 def test_assert():
-    assertion.invoke()
     try:
+        assert f() == 3
+    except AssertionError, e:
+        s = str(e)
+        assert s.startswith('assert 2 == 3\n')
+
+def test_assert_within_finally(): 
+    class A: 
+        def f():
+            pass 
+    excinfo = py.test.raises(TypeError, """
         try:
-            assert f() == 3
-        except AssertionError, e:
-            s = str(e)
-            assert s.startswith('assert 2 == 3\n')
-    finally:
-        assertion.revoke()
+            A().f()
+        finally:
+            i = 42
+    """)
+    s = str(excinfo[1])
+    assert s.find("takes no argument") != -1
+
+    #def g():
+    #    A.f()
+    #excinfo = getexcinfo(TypeError, g)
+    #msg = getmsg(excinfo)
+    #assert msg.find("must be called with A") != -1
+
 
 def test_assert_multiline_1():
-    assertion.invoke()
     try:
-        try:
-            assert (f() ==
-                    3)
-        except AssertionError, e:
-            s = str(e)
-            assert s.startswith('assert 2 == 3\n')
-    finally:
-        assertion.revoke()
+        assert (f() ==
+                3)
+    except AssertionError, e:
+        s = str(e)
+        assert s.startswith('assert 2 == 3\n')
 
 def test_assert_multiline_2():
-    assertion.invoke()
     try:
-        try:
-            assert (f() == (4,
-                       3)[-1])
-        except AssertionError, e:
-            s = str(e)
-            assert s.startswith('assert 2 ==')
-    finally:
-        assertion.revoke()
-
-main()
+        assert (f() == (4,
+                   3)[-1])
+    except AssertionError, e:
+        s = str(e)
+        assert s.startswith('assert 2 ==')

Modified: py/dist/py/magic/test_dyncode.py
==============================================================================
--- py/dist/py/magic/test_dyncode.py	(original)
+++ py/dist/py/magic/test_dyncode.py	Fri Oct 15 23:14:00 2004
@@ -1,8 +1,13 @@
 import sys
 import os
 #print "dyncode_test: __name__ ==", __name__
-from py import test
-from py.__impl__.magic import dyncode 
+from py.__impl__.magic import dyncode, exprinfo
+import py
+
+def setup_module(mod):
+    py.magic.invoke(dyncode=1)
+def teardown_module(mod):
+    py.magic.revoke(dyncode=1)
 
 def test_dyncode_trace():
     source = """
@@ -11,7 +16,7 @@
     """
     co = dyncode.compile2(source)
     exec co 
-    excinfo = test.raises(ValueError, f)
+    excinfo = py.test.raises(ValueError, f)
     filename, lineno = dyncode.tbinfo(excinfo[2])
     line = dyncode.getline(filename, lineno)
     assert line.strip() == 'raise ValueError'
@@ -26,7 +31,7 @@
     assert fn1 != fn2
 
 def test_syntaxerror_rerepresentation():
-    ex = test.raises(SyntaxError, dyncode.compile2, 'x x')[1]
+    ex = py.test.raises(SyntaxError, dyncode.compile2, 'x x')[1]
     assert ex.lineno == 1
     assert ex.offset == 3
     assert ex.text.strip(), 'x x'
@@ -63,7 +68,7 @@
     class A:
         def __init__(self, *args):
             frame = sys._getframe(1)
-            self.source = dyncode.getparseablestartingblock(frame)
+            self.source = dyncode.getparseablestartingblock(frame) 
            
     x = A('x', 
           'y' \
@@ -72,7 +77,17 @@
 
     l = [i for i in x.source.split('\n') if i.strip()]
     assert len(l) == 4
-     
-if __name__ == '__main__':
-    test.main()
 
+def test_getline_finally():
+    def c(): pass
+    excinfo = py.test.raises(TypeError, """
+           teardown = None
+           try:
+                c(1) 
+           finally:
+                if teardown: 
+                    teardown() 
+    """)
+    tb = dyncode.gettb(excinfo[2], -1)
+    source = dyncode.getparseablestartingblock(tb) 
+    assert source.strip() == 'c(1)' 

Modified: py/dist/py/magic/test_exprinfo.py
==============================================================================
--- py/dist/py/magic/test_exprinfo.py	(original)
+++ py/dist/py/magic/test_exprinfo.py	Fri Oct 15 23:14:00 2004
@@ -23,6 +23,30 @@
     msg = getmsg(excinfo)
     assert msg == 'AssertionError: assert 1 == 2'
 
+def test_assert_func_argument_type_error(): 
+    def f (): 
+        pass
+    def g():
+        f(1) 
+    excinfo = getexcinfo(TypeError, g)
+    msg = getmsg(excinfo)
+    assert msg.find("takes no argument") != -1
+
+    class A: 
+        def f():
+            pass 
+    def g():
+        A().f()
+    excinfo = getexcinfo(TypeError, g)
+    msg = getmsg(excinfo)
+    assert msg.find("takes no argument") != -1
+
+    def g():
+        A.f()
+    excinfo = getexcinfo(TypeError, g)
+    msg = getmsg(excinfo)
+    assert msg.find("must be called with A") != -1
+
 def global_f():
     return 42
 

Modified: py/dist/py/magic/test_invoke.py
==============================================================================
--- py/dist/py/magic/test_invoke.py	(original)
+++ py/dist/py/magic/test_invoke.py	Fri Oct 15 23:14:00 2004
@@ -3,7 +3,6 @@
 import inspect
 
 def check_dyncode():
-    print compile
     co = compile('x=3', 'bogus', 'exec') 
     s = inspect.getfile(co) 
     assert s
@@ -13,7 +12,6 @@
 def check_assertion():
     excinfo = py.test.raises(AssertionError, "assert 1 == 2")
     value = excinfo[1]
-    print value
     assert str(value) == "assert 1 == 2" 
 
 def test_invoke_dyncode():

Modified: py/dist/py/test/report/text/reporter.py
==============================================================================
--- py/dist/py/test/report/text/reporter.py	(original)
+++ py/dist/py/test/report/text/reporter.py	Fri Oct 15 23:14:00 2004
@@ -75,7 +75,7 @@
             #    self.out.write('%s[%d]' % (collector.pypath, 
             #                           numunits))
             #else:
-            self.out.line(collector.pypath.getfile())
+            #self.out.line("collector.fspy.modpath) 
             return self.out.line 
 
     def open_Directory(self, collector):
@@ -99,7 +99,7 @@
             unit.iocapture = SimpleOutErrCapture()
         if self.out.tty:
             realpath, lineno = unit.pypath.getfilelineno()
-            location = "running %s:%d %s" % (realpath.basename, lineno, str(unit.pypath))
+            location = "running %s:%d %s" % (realpath.basename, lineno, str(unit.pypath.modpath))
             self.out.rewrite(location) 
         self._started[unit] = now() 
 
@@ -131,7 +131,7 @@
             location = "%s:%d" % (realpath.basename, lineno)
             resultstring = self.namemap.get(restype, result.__class__.__name__)
             self.out.rewrite("%.3f %-2s %-20s %s%s" % (
-                elapsed, resultstring, location, str(unit.pypath), writeinfo
+                elapsed, resultstring, location, str(unit.pypath.modpath), writeinfo
                 ))
         if self.option.usepdb:
             if (issubclass(restype, collect.Error) or

Modified: py/dist/py/test/test_collect.py
==============================================================================
--- py/dist/py/test/test_collect.py	(original)
+++ py/dist/py/test/test_collect.py	Fri Oct 15 23:14:00 2004
@@ -32,8 +32,6 @@
     l = list(collect.Module(fn))
     assert len(l) == 1
     assert isinstance(l[0], collect.Error)
-    import traceback
-    print traceback.print_exception(*l[0].excinfo)
     assert isinstance(l[0].excinfo[1], path.NotFound) 
 
 def test_syntax_error_in_module():



More information about the pytest-commit mailing list