[pypy-svn] r71214 - in pypy/trunk/pypy/translator: . c/test

arigo at codespeak.net arigo at codespeak.net
Sat Feb 13 11:33:06 CET 2010


Author: arigo
Date: Sat Feb 13 11:33:05 2010
New Revision: 71214

Modified:
   pypy/trunk/pypy/translator/c/test/test_standalone.py
   pypy/trunk/pypy/translator/exceptiontransform.py
Log:
Compile differently AssertionErrors/NotImplementedErrors
in debug mode and in non-debug mode.  Test and explanation.


Modified: pypy/trunk/pypy/translator/c/test/test_standalone.py
==============================================================================
--- pypy/trunk/pypy/translator/c/test/test_standalone.py	(original)
+++ pypy/trunk/pypy/translator/c/test/test_standalone.py	Sat Feb 13 11:33:05 2010
@@ -16,13 +16,16 @@
 class StandaloneTests(object):
     config = None
 
-    def compile(self, entry_point):
+    def compile(self, entry_point, debug=True):
         t = TranslationContext(self.config)
         t.buildannotator().build_types(entry_point, [s_list_of_strings])
         t.buildrtyper().specialize()
 
         cbuilder = CStandaloneBuilder(t, entry_point, t.config)
-        cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES)
+        if debug:
+            cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES)
+        else:
+            cbuilder.generate_source()
         cbuilder.compile()
         if option.view:
             t.view()
@@ -511,7 +514,17 @@
         assert re.match(r'  File "\w+.c", line \d+, in h', l2)
         assert re.match(r'  File "\w+.c", line \d+, in raiseme', l3)
 
-    def test_assertion_error(self):
+    def test_assertion_error_debug(self):
+        def entry_point(argv):
+            assert len(argv) != 1
+            return 0
+        t, cbuilder = self.compile(entry_point, debug=True)
+        out, err = cbuilder.cmdexec("", expect_crash=True)
+        assert out.strip() == ''
+        lines = err.strip().splitlines()
+        assert lines[-1] == 'in pypy_g_RPyRaiseException: AssertionError'
+
+    def test_assertion_error_nondebug(self):
         def g(x):
             assert x != 1
         def f(argv):
@@ -522,7 +535,7 @@
         def entry_point(argv):
             f(argv)
             return 0
-        t, cbuilder = self.compile(entry_point)
+        t, cbuilder = self.compile(entry_point, debug=False)
         out, err = cbuilder.cmdexec("", expect_crash=True)
         assert out.strip() == ''
         lines = err.strip().splitlines()
@@ -535,7 +548,17 @@
         # The traceback stops at f() because it's the first function that
         # captures the AssertionError, which makes the program abort.
 
-    def test_ll_assert_error(self):
+    def test_ll_assert_error_debug(self):
+        def entry_point(argv):
+            ll_assert(len(argv) != 1, "foobar")
+            return 0
+        t, cbuilder = self.compile(entry_point, debug=True)
+        out, err = cbuilder.cmdexec("", expect_crash=True)
+        assert out.strip() == ''
+        lines = err.strip().splitlines()
+        assert lines[-1] == 'in pypy_g_entry_point: foobar'
+
+    def test_ll_assert_error_nondebug(self):
         py.test.skip("implement later, maybe: tracebacks even with ll_assert")
         def g(x):
             ll_assert(x != 1, "foobar")

Modified: pypy/trunk/pypy/translator/exceptiontransform.py
==============================================================================
--- pypy/trunk/pypy/translator/exceptiontransform.py	(original)
+++ pypy/trunk/pypy/translator/exceptiontransform.py	Sat Feb 13 11:33:05 2010
@@ -86,7 +86,15 @@
             exc_data.exc_value = null_value
 
         def rpyexc_raise(etype, evalue):
-            # assert(!RPyExceptionOccurred());
+            # When compiling in debug mode, the following ll_asserts will
+            # crash the program as soon as it raises AssertionError or
+            # NotImplementedError.  Useful when you are in a debugger.
+            # When compiling in release mode, AssertionErrors and
+            # NotImplementedErrors are raised normally, and only later
+            # caught by debug_catch_exception and printed, which allows
+            # us to see at least part of the traceback for them.
+            ll_assert(etype != assertion_error_ll_exc_type, "AssertionError")
+            ll_assert(etype != n_i_error_ll_exc_type, "NotImplementedError")
             exc_data.exc_type = etype
             exc_data.exc_value = evalue
             lloperation.llop.debug_start_traceback(lltype.Void, etype)



More information about the Pypy-commit mailing list