[pypy-svn] pypy default: (kleptog) Implement PyErr_WriteUnraisable

amauryfa commits-noreply at bitbucket.org
Sun Feb 27 22:43:01 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r42326:9adf3462fae3
Date: 2011-02-27 22:42 +0100
http://bitbucket.org/pypy/pypy/changeset/9adf3462fae3/

Log:	(kleptog) Implement PyErr_WriteUnraisable

diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -763,18 +763,6 @@
     only be called from the main thread."""
     raise NotImplementedError
 
- at cpython_api([PyObject], lltype.Void)
-def PyErr_WriteUnraisable(space, obj):
-    """This utility function prints a warning message to sys.stderr when an
-    exception has been set but it is impossible for the interpreter to actually
-    raise the exception.  It is used, for example, when an exception occurs in an
-    __del__() method.
-    
-    The function is called with a single argument obj that identifies the context
-    in which the unraisable exception occurred. The repr of obj will be printed in
-    the warning message."""
-    raise NotImplementedError
-
 @cpython_api([rffi.CCHARP, rffi.CCHARP, Py_ssize_t, Py_ssize_t, Py_ssize_t, rffi.CCHARP], PyObject)
 def PyUnicodeDecodeError_Create(space, encoding, object, length, start, end, reason):
     """Create a UnicodeDecodeError object with the attributes encoding,

diff --git a/pypy/module/cpyext/test/test_pyerrors.py b/pypy/module/cpyext/test/test_pyerrors.py
--- a/pypy/module/cpyext/test/test_pyerrors.py
+++ b/pypy/module/cpyext/test/test_pyerrors.py
@@ -89,6 +89,14 @@
         assert "cpyext is cool" in err
         assert not api.PyErr_Occurred()
 
+    def test_WriteUnraisable(self, space, api, capfd):
+        api.PyErr_SetObject(space.w_ValueError, space.wrap("message"))
+        w_where = space.wrap("location")
+        api.PyErr_WriteUnraisable(w_where)
+        out, err = capfd.readouterr()
+        assert ("Exception ValueError: 'message' "
+                "in W_StringObject('location') ignored") == err.strip()
+
 class AppTestFetch(AppTestCpythonExtensionBase):
     def setup_class(cls):
         AppTestCpythonExtensionBase.setup_class.im_func(cls)

diff --git a/pypy/module/cpyext/pyerrors.py b/pypy/module/cpyext/pyerrors.py
--- a/pypy/module/cpyext/pyerrors.py
+++ b/pypy/module/cpyext/pyerrors.py
@@ -284,3 +284,19 @@
                                     space.wrap("traceback"))
     space.call_method(w_traceback, "print_tb", w_tb, space.w_None, w_file)
     return 0
+
+ at cpython_api([PyObject], lltype.Void)
+def PyErr_WriteUnraisable(space, w_where):
+    """This utility function prints a warning message to sys.stderr when an
+    exception has been set but it is impossible for the interpreter to actually
+    raise the exception.  It is used, for example, when an exception occurs in
+    an __del__() method.
+    
+    The function is called with a single argument obj that identifies the
+    context in which the unraisable exception occurred. The repr of obj will be
+    printed in the warning message."""
+    
+    state = space.fromcache(State)
+    operror = state.clear_exception()
+    if operror:
+        operror.write_unraisable(space, w_where)


More information about the Pypy-commit mailing list