[pypy-svn] r74557 - in pypy/trunk/pypy/module/cpyext: . test

agaynor at codespeak.net agaynor at codespeak.net
Wed May 19 02:49:25 CEST 2010


Author: agaynor
Date: Wed May 19 02:49:23 2010
New Revision: 74557

Modified:
   pypy/trunk/pypy/module/cpyext/pyerrors.py
   pypy/trunk/pypy/module/cpyext/stubs.py
   pypy/trunk/pypy/module/cpyext/stubsactive.py
   pypy/trunk/pypy/module/cpyext/test/test_pyerrors.py
Log:
Implemented PyErr_Print

Modified: pypy/trunk/pypy/module/cpyext/pyerrors.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/pyerrors.py	(original)
+++ pypy/trunk/pypy/module/cpyext/pyerrors.py	Wed May 19 02:49:23 2010
@@ -53,10 +53,11 @@
     if operror:
         ptype[0] = make_ref(space, operror.w_type)
         pvalue[0] = make_ref(space, operror.get_w_value(space))
+        ptraceback[0] = make_ref(space, space.wrap(operror.application_traceback))
     else:
         ptype[0] = lltype.nullptr(PyObject.TO)
         pvalue[0] = lltype.nullptr(PyObject.TO)
-    ptraceback[0] = lltype.nullptr(PyObject.TO)
+        ptraceback[0] = lltype.nullptr(PyObject.TO)
 
 @cpython_api([PyObject, PyObject, PyObject], lltype.Void)
 def PyErr_Restore(space, w_type, w_value, w_traceback):
@@ -208,4 +209,36 @@
     Deprecated; use PyErr_WarnEx() instead."""
     return PyErr_WarnEx(space, w_category, message, 1)
 
+ at cpython_api([rffi.INT_real], lltype.Void)
+def PyErr_PrintEx(space, set_sys_last_vars):
+    """Print a standard traceback to sys.stderr and clear the error indicator.
+    Call this function only when the error indicator is set.  (Otherwise it will
+    cause a fatal error!)
+    
+    If set_sys_last_vars is nonzero, the variables sys.last_type,
+    sys.last_value and sys.last_traceback will be set to the
+    type, value and traceback of the printed exception, respectively."""
+    if not PyErr_Occurred(space):
+        PyErr_BadInternalCall(space)
+    state = space.fromcache(State)
+    operror = state.clear_exception()
+    
+    w_type = operror.w_type
+    w_value = operror.get_w_value(space)
+    w_tb = space.wrap(operror.application_traceback)
+    space.call_function(space.sys.get("excepthook"),
+        w_type,
+        w_value,
+        w_tb
+    )
+    
+    if set_sys_last_vars:
+        w_dict = space.sys.getdict()
+        w_dict.setitem_str("last_type", w_type)
+        w_dict.setitem_str("last_value", w_value)
+        w_dict.setitem_str("last_traceback", w_tb)
 
+ at cpython_api([], lltype.Void)
+def PyErr_Print(space):
+    """Alias for PyErr_PrintEx(1)."""
+    PyErr_PrintEx(space, 1)

Modified: pypy/trunk/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stubs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stubs.py	Wed May 19 02:49:23 2010
@@ -826,17 +826,6 @@
     """
     raise NotImplementedError
 
- at cpython_api([rffi.INT_real], lltype.Void)
-def PyErr_PrintEx(space, set_sys_last_vars):
-    """Print a standard traceback to sys.stderr and clear the error indicator.
-    Call this function only when the error indicator is set.  (Otherwise it will
-    cause a fatal error!)
-    
-    If set_sys_last_vars is nonzero, the variables sys.last_type,
-    sys.last_value and sys.last_traceback will be set to the
-    type, value and traceback of the printed exception, respectively."""
-    raise NotImplementedError
-
 @cpython_api([PyObjectP, PyObjectP, PyObjectP], lltype.Void)
 def PyErr_NormalizeException(space, exc, val, tb):
     """Under certain circumstances, the values returned by PyErr_Fetch() below

Modified: pypy/trunk/pypy/module/cpyext/stubsactive.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stubsactive.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stubsactive.py	Wed May 19 02:49:23 2010
@@ -23,11 +23,6 @@
     instead of the repr()."""
     raise NotImplementedError
 
- at cpython_api([], lltype.Void)
-def PyErr_Print(space):
-    """Alias for PyErr_PrintEx(1)."""
-    raise NotImplementedError
-
 @cpython_api([PyInterpreterState], PyThreadState, error=CANNOT_FAIL)
 def PyThreadState_New(space, interp):
     """Create a new thread state object belonging to the given interpreter object.

Modified: pypy/trunk/pypy/module/cpyext/test/test_pyerrors.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_pyerrors.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_pyerrors.py	Wed May 19 02:49:23 2010
@@ -1,3 +1,6 @@
+import sys
+import StringIO
+
 from pypy.module.cpyext.state import State
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
@@ -79,6 +82,13 @@
         assert ": UserWarning: this is a warning" in err
         rffi.free_charp(message)
 
+    def test_print_err(self, space, api, capfd):
+        api.PyErr_SetObject(space.w_Exception, space.wrap("cpyext is cool"))
+        api.PyErr_Print()
+        out, err = capfd.readouterr()
+        assert "cpyext is cool" in err
+        assert not api.PyErr_Occurred()
+
 class AppTestFetch(AppTestCpythonExtensionBase):
     def setup_class(cls):
         AppTestCpythonExtensionBase.setup_class.im_func(cls)



More information about the Pypy-commit mailing list