[pypy-svn] r26021 - in pypy/dist/pypy/rpython/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Thu Apr 20 11:31:01 CEST 2006


Author: arigo
Date: Thu Apr 20 11:30:59 2006
New Revision: 26021

Modified:
   pypy/dist/pypy/rpython/rctypes/afunc.py
   pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
   pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py
Log:
For all calls to pythonapi.PyXxx(), generate a PyErr_Occurred() check
in genc.


Modified: pypy/dist/pypy/rpython/rctypes/afunc.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/afunc.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/afunc.py	Thu Apr 20 11:30:59 2006
@@ -62,12 +62,17 @@
     else:
         RESTYPE = lltype.Void
 
-    ll_func = getattr(cfuncptr, 'llinterp_friendly_version', None)
-    includes = getattr(cfuncptr, 'includes', ())
+    kwds = {}
+    if hasattr(cfuncptr, 'llinterp_friendly_version'):
+        kwds['_callable'] = cfuncptr.llinterp_friendly_version
+    if (cfuncptr._flags_ & ctypes._FUNCFLAG_PYTHONAPI) == 0:
+        kwds['includes'] = getattr(cfuncptr, 'includes', ())
+    #else:
+    #   no 'includes': hack to trigger in GenC a PyErr_Occurred() check
+
     v_result = hop.llops.gencapicall(fnname, unwrapped_args_v,
                                      resulttype = RESTYPE,
-                                     _callable = ll_func,
-                                     includes = includes)
+                                     **kwds)
     # XXX hack! hack! temporary! I promize!
     FUNCTYPE = lltype.FuncType(ARGTYPES, RESTYPE)
     last_op = hop.llops[-1]

Modified: pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py	Thu Apr 20 11:30:59 2006
@@ -163,3 +163,9 @@
     assert x.value is TAG
     x.value = 42
     assert x.value == 42
+
+def test_pythonapi():
+    pythonapi.PyInt_AsLong.argtypes = [py_object]
+    pythonapi.PyInt_AsLong.restype = c_long
+    assert pythonapi.PyInt_AsLong(py_object(17L)) == 17
+    py.test.raises(TypeError, "pythonapi.PyInt_AsLong(py_object('hello'))")

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py	Thu Apr 20 11:30:59 2006
@@ -12,9 +12,9 @@
 from pypy.rpython.rstr import string_repr
 from pypy.rpython.lltypesystem import lltype
 
-from ctypes import cdll
+from ctypes import cdll, pythonapi, _FUNCFLAG_PYTHONAPI
 from ctypes import c_int, c_long, c_char_p, c_char, create_string_buffer
-from ctypes import POINTER
+from ctypes import POINTER, py_object
 
 # __________ the standard C library __________
 
@@ -163,3 +163,29 @@
         t2 = fn()
         t3 = time.time()
         assert int(t1) <= t2 <= int(t3 + 1.0)
+
+    def test_compile_pythonapi(self):
+        PyInt_AsLong = pythonapi.PyInt_AsLong
+        PyInt_AsLong.argtypes = [py_object]
+        PyInt_AsLong.restype = c_long
+        assert PyInt_AsLong._flags_ & _FUNCFLAG_PYTHONAPI
+
+        PyNumber_Add = pythonapi.PyNumber_Add
+        PyNumber_Add.argtypes = [py_object, py_object]
+        PyNumber_Add.restype = py_object
+
+        def fn1(x, crash):
+            pyobj = py_object(x)
+            pyobj = PyNumber_Add(pyobj, pyobj)
+            x = PyInt_AsLong(pyobj)
+            if crash:
+                # fn(sys.maxint, 1) should crash on PyInt_AsLong before
+                # it arrives here.  If by mistake it arrives here then
+                # we get a TypeError instead of the OverflowError
+                PyNumber_Add(py_object(5), py_object("x"))
+            return x
+
+        fn = compile(fn1, [int, int])
+        res = fn(17, 0)
+        assert res == 34
+        py.test.raises(OverflowError, 'fn(sys.maxint, 1)')



More information about the Pypy-commit mailing list