[pypy-svn] r51614 - in pypy/dist/pypy/lib: _ctypes app_test/ctypes

fijal at codespeak.net fijal at codespeak.net
Mon Feb 18 23:42:40 CET 2008


Author: fijal
Date: Mon Feb 18 23:42:38 2008
New Revision: 51614

Modified:
   pypy/dist/pypy/lib/_ctypes/array.py
   pypy/dist/pypy/lib/_ctypes/structure.py
   pypy/dist/pypy/lib/app_test/ctypes/test_keepalive.py
Log:
array in structure. Test and a fix.


Modified: pypy/dist/pypy/lib/_ctypes/array.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/array.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/array.py	Mon Feb 18 23:42:38 2008
@@ -2,7 +2,7 @@
 import _rawffi
 
 from _ctypes.basics import _CData, cdata_from_address, _CDataMeta, sizeof,\
-     keepalive_key, CArgObject
+     keepalive_key, store_reference, CArgObject
 
 def _create_unicode(buffer, maxlength):
     res = []
@@ -70,6 +70,14 @@
     def _alignmentofinstances(self):
         return self._type_._alignmentofinstances()
 
+    def _CData_output(self, resarray, base=None, index=-1):
+        res = self.__new__(self)
+        ffiarray = self._ffiarray.fromaddress(resarray.buffer, self._length_)
+        res._buffer = ffiarray
+        res._base = base
+        res._index = index
+        return res.__ctypes_from_outparam__()
+
 def array_get_slice_params(self, index):
     if index.step is not None:
         raise TypeError("3 arg slices not supported (for no reason)")
@@ -128,7 +136,7 @@
             return
         index = self._fix_index(index)
         if getattr(value, '_objects', None):
-            self._objects[keepalive_key(index)] = value._objects
+            store_reference(self, index, value._objects)
         arg = self._type_._CData_value(value)
         if not isinstance(self._type_._ffishape, tuple):
             self._buffer[index] = arg

Modified: pypy/dist/pypy/lib/_ctypes/structure.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/structure.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/structure.py	Mon Feb 18 23:42:38 2008
@@ -60,7 +60,7 @@
         pos = [0] * len(all_fields)
     fields = {}
     for i, (name, ctype) in enumerate(all_fields):
-        fields[name] = Field(name, pos[i], ctypes.sizeof(ctype), ctype)
+        fields[name] = Field(name, pos[i], ctypes.sizeof(ctype), ctype, i)
     if anon:
         resnames = []
         for i, (name, value) in enumerate(all_fields):
@@ -70,7 +70,8 @@
                     relpos = pos[i] + value._fieldtypes[subname].offset
                     subvalue = value._fieldtypes[subname].ctype
                     fields[subname] = Field(subname, relpos,
-                                            ctypes.sizeof(subvalue), subvalue)
+                                            ctypes.sizeof(subvalue), subvalue,
+                                            i)
                     # XXX we never set rawfields here, let's wait for a test
             else:
                 resnames.append(name)
@@ -78,8 +79,8 @@
     return names, rawfields, fields
 
 class Field(object):
-    def __init__(self, name, offset, size, ctype):
-        for k in ('name', 'offset', 'size', 'ctype'):
+    def __init__(self, name, offset, size, ctype, num):
+        for k in ('name', 'offset', 'size', 'ctype', 'num'):
             self.__dict__[k] = locals()[k]
 
     def __setattr__(self, name, value):
@@ -177,7 +178,7 @@
             fieldtype = self._fieldtypes[name].ctype
         except KeyError:
             return _CData.__getattribute__(self, name)
-        offset = self.__class__._fieldtypes[name].offset
+        offset = self.__class__._fieldtypes[name].num
         suba = self._subarray(fieldtype, name)
         return fieldtype._CData_output(suba, self, offset)
 

Modified: pypy/dist/pypy/lib/app_test/ctypes/test_keepalive.py
==============================================================================
--- pypy/dist/pypy/lib/app_test/ctypes/test_keepalive.py	(original)
+++ pypy/dist/pypy/lib/app_test/ctypes/test_keepalive.py	Mon Feb 18 23:42:38 2008
@@ -1,6 +1,7 @@
 import py
 
 from ctypes import *
+import sys
 
 class TestKeepalive:
     """ Tests whether various objects land in _objects
@@ -39,6 +40,8 @@
         assert p._objects['1'].value == 3
 
     def test_primitive(self):
+        if not hasattr(sys, 'pypy_translation_info'):
+            py.test.skip("whitebox test")
         assert c_char_p("abc")._objects['0']._buffer[0] == "a"
         assert c_int(3)._objects is None
 
@@ -74,3 +77,12 @@
 
         assert a._objects['0:3']['1'] is s
 
+    def test_struct_with_inlined_array(self):
+        class S(Structure):
+            _fields_ = [('b', c_int),
+                        ('a', POINTER(c_int) * 2)]
+
+        s = S()
+        stuff = c_int(2)
+        s.a[1] = pointer(stuff)
+        assert s._objects == {'1:1': {'1': stuff}}



More information about the Pypy-commit mailing list