[pypy-svn] r14471 - in pypy/dist/pypy: rpython translator/c/test

arigo at codespeak.net arigo at codespeak.net
Sun Jul 10 14:34:58 CEST 2005


Author: arigo
Date: Sun Jul 10 14:34:56 2005
New Revision: 14471

Modified:
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/translator/c/test/test_exception.py
Log:
Support for raising AssertionErrors (e.g. with 'assert' statements)
in ll functions.  That was easy, but testing it is more fun because
it confuses py.test quite a bit.  We might have later to write some
conftest.py code that handles AssertionErrors coming from compiled
functions, or try to see how exactly py.test is confused.



Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Sun Jul 10 14:34:56 2005
@@ -194,6 +194,7 @@
 BUILTIN_TYPER[math.floor] = rtype_math_floor
 BUILTIN_TYPER[math.fmod] = rtype_math_fmod
 BUILTIN_TYPER[Exception.__init__.im_func] = rtype_Exception__init__
+BUILTIN_TYPER[AssertionError.__init__.im_func] = rtype_Exception__init__
 BUILTIN_TYPER[OSError.__init__.im_func] = rtype_OSError__init__
 # annotation of low-level types
 

Modified: pypy/dist/pypy/translator/c/test/test_exception.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_exception.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_exception.py	Sun Jul 10 14:34:56 2005
@@ -41,3 +41,36 @@
         assert str(e) == 'MyException'   # which is genc's best effort
     else:
         py.test.fail("f1(1) did not raise anything")
+
+def test_assert():
+    def testfn(n):
+        assert n >= 0
+
+    # big confusion with py.test's AssertionError handling here...
+    # some hacks to try to disable it for the current test.
+    saved = no_magic()
+    try:
+        t = Translator(testfn)
+        t.annotate([int]).simplify()
+        t.specialize()
+        f1 = t.ccompile()
+        res = f1(0)
+        assert res is None, repr(res)
+        res = f1(42)
+        assert res is None, repr(res)
+        py.test.raises(AssertionError, f1, -2)
+    finally:
+        restore_magic(saved)
+
+
+def no_magic():
+    import __builtin__
+    try:
+        py.magic.revert(__builtin__, 'AssertionError')
+        return True
+    except ValueError:
+        return False
+
+def restore_magic(saved):
+    if saved:
+        py.magic.invoke(assertion=True)



More information about the Pypy-commit mailing list