[pypy-svn] r71107 - in pypy/trunk/pypy/lib: _ctypes app_test/ctypes_tests

afa at codespeak.net afa at codespeak.net
Fri Feb 5 14:43:25 CET 2010


Author: afa
Date: Fri Feb  5 14:43:23 2010
New Revision: 71107

Modified:
   pypy/trunk/pypy/lib/_ctypes/structure.py
   pypy/trunk/pypy/lib/_ctypes/union.py
   pypy/trunk/pypy/lib/app_test/ctypes_tests/test_structures.py
Log:
Another fix in ctypes when a Structure also defines __slots__:
getattr(self.__class__, name) won't call __getattribute__.
Always use _fieldtypes, this will even be faster.

This fixes the pyglet "Hello world" sample, on Windows at least


Modified: pypy/trunk/pypy/lib/_ctypes/structure.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/structure.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/structure.py	Fri Feb  5 14:43:23 2010
@@ -180,12 +180,13 @@
 
     def __setattr__(self, name, value):
         try:
-            fieldtype = self._fieldtypes[name].ctype
+            field = self._fieldtypes[name]
         except KeyError:
             return _CData.__setattr__(self, name, value)
+        fieldtype = field.ctype
         cobj = fieldtype.from_param(value)
         if ensure_objects(cobj) is not None:
-            key = keepalive_key(getattr(self.__class__, name).num)
+            key = keepalive_key(field.num)
             store_reference(self, key, cobj._objects)
         arg = cobj._get_buffer_value()
         if fieldtype._fficompositesize is not None:
@@ -199,10 +200,11 @@
         if name == '_fieldtypes':
             return _CData.__getattribute__(self, '_fieldtypes')
         try:
-            fieldtype = self._fieldtypes[name].ctype
+            field = self._fieldtypes[name]
         except KeyError:
             return _CData.__getattribute__(self, name)
-        offset = self.__class__._fieldtypes[name].num
+        fieldtype = field.ctype
+        offset = field.num
         suba = self._subarray(fieldtype, name)
         return fieldtype._CData_output(suba, self, offset)
 

Modified: pypy/trunk/pypy/lib/_ctypes/union.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/union.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/union.py	Fri Feb  5 14:43:23 2010
@@ -87,21 +87,23 @@
 
     def __getattr__(self, name):
         try:
-            fieldtype = self._fieldtypes[name].ctype
+            field = self._fieldtypes[name]
         except KeyError:
             raise AttributeError(name)
+        fieldtype = field.ctype
         val = self._ffiarrays[name].fromaddress(self._buffer.buffer, 1)
-        offset = self.__class__._fieldtypes[name].num        
+        offset = field.num
         return fieldtype._CData_output(val, self, offset)
 
     def __setattr__(self, name, value):
         try:
-            fieldtype = self._fieldtypes[name].ctype
+            field = self._fieldtypes[name]
         except KeyError:
             raise AttributeError(name)
+        fieldtype = field.ctype
         cobj = fieldtype.from_param(value)
         if ensure_objects(cobj) is not None:
-            key = keepalive_key(getattr(self.__class__, name).num)
+            key = keepalive_key(field.num)
             store_reference(self, key, cobj._objects)
         arg = cobj._get_buffer_value()
         if fieldtype._fficompositesize is not None:

Modified: pypy/trunk/pypy/lib/app_test/ctypes_tests/test_structures.py
==============================================================================
--- pypy/trunk/pypy/lib/app_test/ctypes_tests/test_structures.py	(original)
+++ pypy/trunk/pypy/lib/app_test/ctypes_tests/test_structures.py	Fri Feb  5 14:43:23 2010
@@ -392,6 +392,15 @@
         x.other = 42
         assert x.other == 42
 
+    def test_withslots(self):
+        class X(Structure):
+            _fields_ = [("a", c_int * 2)]
+            __slots__ = ['a']
+
+        x = X()
+        x.a = (42, 43)
+        assert tuple(x.a) == (42, 43)
+
     def test_getattr_recursion(self):
         # Structure.__getattr__ used to call itself recursively
         # and hit the recursion limit.



More information about the Pypy-commit mailing list