[pypy-svn] r73190 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test

trundle at codespeak.net trundle at codespeak.net
Tue Mar 30 20:37:54 CEST 2010


Author: trundle
Date: Tue Mar 30 20:37:51 2010
New Revision: 73190

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/pyerrors.py
   pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pyerrors.py
Log:
Implement PyErr_GivenExceptionMatches.


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/pyerrors.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/pyerrors.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/pyerrors.py	Tue Mar 30 20:37:51 2010
@@ -1,7 +1,7 @@
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.interpreter.error import OperationError
 from pypy.module.cpyext.api import cpython_api, PyObject, make_ref,\
-        register_container
+        register_container, CANNOT_FAIL
 from pypy.module.cpyext.state import State
 
 @cpython_api([PyObject, rffi.CCHARP], lltype.Void)
@@ -25,3 +25,15 @@
 def PyErr_BadInternalCall(space):
     raise OperationError(space.w_SystemError, space.wrap("Bad internal call!"))
 
+ at cpython_api([PyObject, PyObject], rffi.INT_real, error=CANNOT_FAIL)
+def PyErr_GivenExceptionMatches(space, w_given, w_exc):
+    """Return true if the given exception matches the exception in exc.  If
+    exc is a class object, this also returns true when given is an instance
+    of a subclass.  If exc is a tuple, all exceptions in the tuple (and
+    recursively in subtuples) are searched for a match."""
+    if (space.is_true(space.isinstance(w_given, space.w_BaseException)) or
+        space.is_oldstyle_instance(w_given)):
+        w_given_type = space.exception_getclass(w_given)
+    else:
+        w_given_type = w_given
+    return space.exception_match(w_given_type, w_exc)

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	Tue Mar 30 20:37:51 2010
@@ -1523,14 +1523,6 @@
     violation will occur if no exception has been raised."""
     raise NotImplementedError
 
- at cpython_api([PyObject, PyObject], rffi.INT_real)
-def PyErr_GivenExceptionMatches(space, given, exc):
-    """Return true if the given exception matches the exception in exc.  If
-    exc is a class object, this also returns true when given is an instance
-    of a subclass.  If exc is a tuple, all exceptions in the tuple (and
-    recursively in subtuples) are searched for a match."""
-    raise NotImplementedError
-
 @cpython_api([{PyObject**exc}, {PyObject**val}, {PyObject**tb}], lltype.Void)
 def PyErr_NormalizeException(space, , , ):
     """Under certain circumstances, the values returned by PyErr_Fetch() below

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pyerrors.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pyerrors.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pyerrors.py	Tue Mar 30 20:37:51 2010
@@ -3,6 +3,29 @@
 from pypy.rpython.lltypesystem import rffi
 
 class TestExceptions(BaseApiTest):
+    def test_GivenExceptionMatches(self, space, api):
+        old_style_exception = space.appexec([], """():
+            class OldStyle:
+                pass
+            return OldStyle
+        """)
+        exc_matches = api.PyErr_GivenExceptionMatches
+
+        string_exception = space.wrap('exception')
+        instance = space.call_function(space.w_ValueError)
+        old_style_instance = space.call_function(old_style_exception)
+        assert exc_matches(string_exception, string_exception)
+        assert exc_matches(old_style_exception, old_style_exception)
+        assert not exc_matches(old_style_exception, space.w_Exception)
+        assert exc_matches(instance, space.w_ValueError)
+        assert exc_matches(old_style_instance, old_style_exception)
+        assert exc_matches(space.w_ValueError, space.w_ValueError)
+        assert exc_matches(space.w_IndexError, space.w_LookupError)
+        assert not exc_matches(space.w_ValueError, space.w_LookupError)
+
+        exceptions = space.newtuple([space.w_LookupError, space.w_ValueError])
+        assert exc_matches(space.w_ValueError, exceptions)
+
     def test_Occurred(self, space, api):
         assert not api.PyErr_Occurred()
         string = rffi.str2charp("spam and eggs")



More information about the Pypy-commit mailing list