[pypy-svn] r7940 - pypy/branch/src-pytest/pypy/tool/pytest

arigo at codespeak.net arigo at codespeak.net
Sun Dec 19 22:59:39 CET 2004


Author: arigo
Date: Sun Dec 19 22:59:38 2004
New Revision: 7940

Added:
   pypy/branch/src-pytest/pypy/tool/pytest/autopath.py
      - copied unchanged from r7938, pypy/branch/src-pytest/pypy/tool/autopath.py
Modified:
   pypy/branch/src-pytest/pypy/tool/pytest/support.py
Log:
Finished a replacement AssertionError for the app-level of PyPy.
Comes with a few tests (in the file, so you have to run them explicitely as
"py.test support.py").


Modified: pypy/branch/src-pytest/pypy/tool/pytest/support.py
==============================================================================
--- pypy/branch/src-pytest/pypy/tool/pytest/support.py	(original)
+++ pypy/branch/src-pytest/pypy/tool/pytest/support.py	Sun Dec 19 22:59:38 2004
@@ -1,16 +1,20 @@
-from py.__impl__.magic import exprinfo
+import autopath
 import py
-py.magic.autopath()
+from py.__impl__.magic import exprinfo
+from py.code import InspectableFrame
 from pypy.objspace.std import StdObjSpace
 from pypy.interpreter.gateway import app2interp, interp2app
 from pypy.interpreter.argument import Arguments
+from pypy.interpreter.error import OperationError
 
-class AppRunnerFrame:
+class AppRunnerFrame(InspectableFrame):
 
-    def __init__(self, space, w_globals, w_locals):
-        self.space = space
-        self.w_globals = w_globals
-        self.w_locals = w_locals
+    def __init__(self, pyframe):
+        lineno = pyframe.get_last_lineno()
+        super(AppRunnerFrame, self).__init__(pyframe.code, lineno)
+        self.space = pyframe.space
+        self.w_globals = pyframe.w_globals
+        self.w_locals = pyframe.getdictscope()
 
     def eval(self, code, **vars):
         space = self.space
@@ -30,45 +34,72 @@
     def is_true(self, w_value):
         return self.space.is_true(w_value)
 
-def build_pytest_assertion(space): 
-    def app_getmyassertion():
-        class MyAssertionError(AssertionError):
-            def __init__(self, *args):
-                AssertionError.__init__(self, *args) 
-                self.inspect_assertion()
-        return MyAssertionError 
-    getmyassertion = app2interp(app_getmyassertion)
-    w_myassertion = getmyassertion(space) 
-
-    def inspect_assertion(space, w_self): 
-        pass 
-        #print w_self 
-        #print "got to interp-level inspect_assertion" 
-
-    w_inspect_assertion = space.wrap(interp2app(inspect_assertion))
-    space.setattr(w_myassertion, 
-                  space.wrap('inspect_assertion'), 
-                  w_inspect_assertion)
-    return w_myassertion 
+def build_pytest_assertion(space):
+    def my_init(space, w_self, __args__):
+        "Our new AssertionError.__init__()."
+        w_parent_init = space.getattr(w_BuiltinAssertionError,
+                                      space.wrap('__init__'))
+        space.call_args(w_parent_init, __args__.prepend(w_self))
+        framestack = space.getexecutioncontext().framestack
+        frame = framestack.top(0)
+        # Argh! we see app-level helpers in the frame stack!
+        #       that's very probably very bad...
+        assert frame.code.co_name == 'app_normalize_exception'
+        frame = framestack.top(1)
+        
+        runner = AppRunnerFrame(frame)
+        # XXX what if the source is not available?
+        msg = exprinfo.interpret(str(runner.statement.deindent()), runner)
+        if msg is None:
+            msg = "(inconsistenty failed then succeeded)"
+        elif msg.startswith('AssertionError: '):
+            msg = msg[16:]
+        space.setattr(w_self, space.wrap('args'),
+                      space.newtuple([space.wrap(msg)]))
+
+    # build a new AssertionError class to replace the original one.
+    w_BuiltinAssertionError = space.getitem(space.w_builtins,
+                                            space.wrap('AssertionError'))
+    w_metaclass = space.type(w_BuiltinAssertionError)
+    w_init = space.wrap(interp2app(my_init))
+    w_dict = space.newdict([])
+    space.setitem(w_dict, space.wrap('__init__'), w_init)
+    return space.call_function(w_metaclass,
+                               space.wrap('AssertionError'),
+                               space.newtuple([w_BuiltinAssertionError]),
+                               w_dict)
 
 def setup_module(mod):
     mod.space = StdObjSpace()
 
+def somefunc(x):
+    print x
+
 def test_AppRunnerFrame():
-    w_glob = space.newdict([])
-    w_loc = space.newdict([])
-    runner = AppRunnerFrame(space, w_glob, w_loc)
+    from pypy.interpreter.pycode import PyCode
+    from pypy.interpreter.pyframe import PyFrame
+    import sys
+    co = PyCode()._from_code(somefunc.func_code)
+    pyframe = PyFrame(space, co, space.newdict([]), None)
+    runner = AppRunnerFrame(pyframe)
     exprinfo.run("f = lambda x: x+1", runner)
-    exprinfo.check("isinstance(f(2), float)", runner)
+    msg = exprinfo.interpret("assert isinstance(f(2), float)", runner)
+    assert msg.startswith("AssertionError: assert isinstance(3, float)\n"
+                          " +  where 3 = ")
+
 
 def test_myexception():
     def app_test_func():
-        assert 42 == 43 
+        x = 6*7
+        assert x == 43
     t = app2interp(app_test_func)
     f = t.get_function(space)
-    #space.setitem(space.w_builtins, space.wrap('AssertionError'), 
-    #              build_pytest_assertion(space)) 
-    f.call_args(Arguments([]))
-
-if __name__ == '__main__':
-    test()
+    space.setitem(space.w_builtins, space.wrap('AssertionError'), 
+                  build_pytest_assertion(space))
+    try:
+        f.call_args(Arguments([]))
+    except OperationError, e:
+        assert e.match(space, space.w_AssertionError)
+        assert space.unwrap(space.str(e.w_value)) == 'assert 42 == 43'
+    else:
+        assert False, "got no exception!"



More information about the Pypy-commit mailing list