[pypy-svn] r74886 - in pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem: . test

arigo at codespeak.net arigo at codespeak.net
Sat May 29 12:09:21 CEST 2010


Author: arigo
Date: Sat May 29 12:09:20 2010
New Revision: 74886

Modified:
   pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/ll2ctypes.py
   pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/llmemory.py
   pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/lltype.py
   pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
Log:
Fixes to ll2ctypes handling of AddressAsInt.


Modified: pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/ll2ctypes.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/ll2ctypes.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/ll2ctypes.py	Sat May 29 12:09:20 2010
@@ -494,7 +494,7 @@
 # additionally, this adds mess to __del__ "semantics"
 _all_callbacks = {}
 _all_callbacks_results = []
-_callback2obj = {}
+_int2obj = {}
 _callback_exc_info = None
 
 def get_rtyper():
@@ -512,7 +512,10 @@
     if isinstance(llobj, lltype._uninitialized):
         return uninitialized2ctypes(llobj.TYPE)
     if isinstance(llobj, llmemory.AddressAsInt):
-        return ctypes.cast(lltype2ctypes(llobj.adr), ctypes.c_void_p).value
+        cobj = ctypes.cast(lltype2ctypes(llobj.adr), ctypes.c_void_p)
+        res = intmask(cobj.value)
+        _int2obj[res] = llobj.adr.ptr._obj
+        return res
     if isinstance(llobj, llmemory.fakeaddress):
         llobj = llobj.ptr or 0
 
@@ -607,8 +610,9 @@
             else:
                 ctypes_func_type = get_ctypes_type(T)
                 res = ctypes_func_type(callback)
-            _callback2obj[ctypes.cast(res, ctypes.c_void_p).value] = container
             _all_callbacks[key] = res
+            key2 = intmask(ctypes.cast(res, ctypes.c_void_p).value)
+            _int2obj[key2] = container
             return res
 
         index = 0
@@ -711,9 +715,9 @@
                 container = _array_of_known_length(T.TO)
                 container._storage = cobj.contents
         elif isinstance(T.TO, lltype.FuncType):
-            cobjkey = ctypes.cast(cobj, ctypes.c_void_p).value
-            if cobjkey in _callback2obj:
-                container = _callback2obj[cobjkey]
+            cobjkey = intmask(ctypes.cast(cobj, ctypes.c_void_p).value)
+            if cobjkey in _int2obj:
+                container = _int2obj[cobjkey]
             else:
                 _callable = get_ctypes_trampoline(T.TO, cobj)
                 return lltype.functionptr(T.TO, getattr(cobj, '__name__', '?'),

Modified: pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/llmemory.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/llmemory.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/llmemory.py	Sat May 29 12:09:20 2010
@@ -632,7 +632,11 @@
 def cast_int_to_adr(int):
     if isinstance(int, AddressAsInt):
         return int.adr
-    ptr = lltype.cast_int_to_ptr(_NONGCREF, int)
+    try:
+        ptr = lltype.cast_int_to_ptr(_NONGCREF, int)
+    except ValueError:
+        from pypy.rpython.lltypesystem import ll2ctypes
+        ptr = ll2ctypes._int2obj[int]._as_ptr()
     return cast_ptr_to_adr(ptr)
 
 # ____________________________________________________________

Modified: pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/lltype.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/lltype.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/lltype.py	Sat May 29 12:09:20 2010
@@ -1840,7 +1840,8 @@
 def cast_int_to_ptr(PTRTYPE, oddint):
     if oddint == 0:
         return nullptr(PTRTYPE.TO)
-    assert oddint & 1, "only odd integers can be cast back to ptr"
+    if not (oddint & 1):
+        raise ValueError("only odd integers can be cast back to ptr")
     return _ptr(PTRTYPE, oddint, solid=True)
 
 def attachRuntimeTypeInfo(GCSTRUCT, funcptr=None, destrptr=None):

Modified: pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/test/test_ll2ctypes.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/test/test_ll2ctypes.py	Sat May 29 12:09:20 2010
@@ -1204,6 +1204,18 @@
         res = interpret(f, [])
         assert res == 6
 
+    def test_force_to_int_and_back(self):
+        S = lltype.Struct('S')
+        p = lltype.malloc(S, flavor='raw')
+        a = llmemory.cast_ptr_to_adr(p)
+        i = llmemory.cast_adr_to_int(a)
+        i = rffi.get_real_int(i)
+        #
+        a2 = llmemory.cast_int_to_adr(i)
+        assert a2 == a
+        #
+        lltype.free(p)
+
 class TestPlatform(object):
     def test_lib_on_libpaths(self):
         from pypy.translator.platform import platform



More information about the Pypy-commit mailing list