[pypy-svn] r78640 - in pypy/branch/fast-forward: lib_pypy/_ctypes pypy/module/test_lib_pypy/ctypes_tests

afa at codespeak.net afa at codespeak.net
Tue Nov 2 11:18:31 CET 2010


Author: afa
Date: Tue Nov  2 11:18:27 2010
New Revision: 78640

Modified:
   pypy/branch/fast-forward/lib_pypy/_ctypes/array.py
   pypy/branch/fast-forward/lib_pypy/_ctypes/primitive.py
   pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_array.py
   pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py
Log:
ctypes objects should not reallocate the buffer when __init__ is called again.
Issue found in multiprocessing tests


Modified: pypy/branch/fast-forward/lib_pypy/_ctypes/array.py
==============================================================================
--- pypy/branch/fast-forward/lib_pypy/_ctypes/array.py	(original)
+++ pypy/branch/fast-forward/lib_pypy/_ctypes/array.py	Tue Nov  2 11:18:27 2010
@@ -145,8 +145,12 @@
     __metaclass__ = ArrayMeta
     _ffiargshape = 'P'
 
-    def __init__(self, *args):
+    def __new__(cls, *args):
+        self = _CData.__new__(cls, *args)
         self._buffer = self._ffiarray(self._length_, autofree=True)
+        return self
+
+    def __init__(self, *args):
         for i, arg in enumerate(args):
             self[i] = arg
 

Modified: pypy/branch/fast-forward/lib_pypy/_ctypes/primitive.py
==============================================================================
--- pypy/branch/fast-forward/lib_pypy/_ctypes/primitive.py	(original)
+++ pypy/branch/fast-forward/lib_pypy/_ctypes/primitive.py	Tue Nov  2 11:18:27 2010
@@ -288,8 +288,12 @@
     __metaclass__ = SimpleType
     _type_ = 'i'
 
-    def __init__(self, value=DEFAULT_VALUE):
+    def __new__(cls, *args, **kwds):
+        self = _CData.__new__(cls, *args, **kwds)
         self._buffer = self._ffiarray(1, autofree=True)
+        return self
+
+    def __init__(self, value=DEFAULT_VALUE):
         if value is not DEFAULT_VALUE:
             self.value = value
 

Modified: pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_array.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_array.py	(original)
+++ pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_array.py	Tue Nov  2 11:18:27 2010
@@ -109,6 +109,13 @@
         assert sz[:] == "foo"
         assert sz.value == "foo"
 
+    def test_init_again(self):
+        sz = (c_char * 3)()
+        addr1 = addressof(sz)
+        sz.__init__(*"foo")
+        addr2 = addressof(sz)
+        assert addr1 == addr2
+
     try:
         create_unicode_buffer
     except NameError:

Modified: pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py	(original)
+++ pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py	Tue Nov  2 11:18:27 2010
@@ -89,6 +89,13 @@
             parm = byref(t())
             assert ArgType == type(parm)
 
+    def test_init_again(self):
+        for t in signed_types + unsigned_types + float_types:
+            parm = t()
+            addr1 = addressof(parm)
+            parm.__init__(0)
+            addr2 = addressof(parm)
+            assert addr1 == addr2
 
     def test_floats(self):
         # c_float and c_double can be created from



More information about the Pypy-commit mailing list