[pypy-svn] r51595 - pypy/dist/pypy/lib/_ctypes

fijal at codespeak.net fijal at codespeak.net
Mon Feb 18 17:31:08 CET 2008


Author: fijal
Date: Mon Feb 18 17:31:08 2008
New Revision: 51595

Modified:
   pypy/dist/pypy/lib/_ctypes/array.py
   pypy/dist/pypy/lib/_ctypes/basics.py
   pypy/dist/pypy/lib/_ctypes/builtin.py
   pypy/dist/pypy/lib/_ctypes/pointer.py
   pypy/dist/pypy/lib/_ctypes/structure.py
   pypy/dist/pypy/lib/_ctypes/union.py
Log:
Simplify stuff a bit by having a direct way of accessing 0th element of buffer
as buffer is usually not needed. Quite a bit easier.


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 17:31:08 2008
@@ -129,15 +129,14 @@
         index = self._fix_index(index)
         if getattr(value, '_objects', None):
             self._objects[keepalive_key(index)] = value._objects
-        cobj, arg = self._type_._CData_input(value)
+        arg = self._type_._CData_value(value)
         if not isinstance(self._type_._ffishape, tuple):
-            self._buffer[index] = arg._buffer[0]
+            self._buffer[index] = arg
             # something more sophisticated, cannot set field directly
         else:
             from ctypes import memmove
             dest = self._buffer.itemaddress(index)
-            source = arg._buffer[0]
-            memmove(dest, source, self._type_._ffishape[0])
+            memmove(dest, arg, self._type_._ffishape[0])
 
     def __getitem__(self, index):
         if isinstance(index, slice):

Modified: pypy/dist/pypy/lib/_ctypes/basics.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/basics.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/basics.py	Mon Feb 18 17:31:08 2008
@@ -41,6 +41,12 @@
         cobj = self.from_param(value)
         return cobj, cobj._get_buffer_for_param()
 
+    def _CData_value(self, value):
+        cobj = self.from_param(value)
+        # we don't care here if this stuff will live afterwards, as we're
+        # interested only in value anyway
+        return cobj._get_buffer_for_param()._buffer[0]
+
     def _CData_output(self, resarray, base=None, index=-1, needs_free=False):
         assert isinstance(resarray, _rawffi.ArrayInstance)
         """Used when data exits ctypes and goes into user code.

Modified: pypy/dist/pypy/lib/_ctypes/builtin.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/builtin.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/builtin.py	Mon Feb 18 17:31:08 2008
@@ -11,9 +11,8 @@
 def _string_at_addr(addr, lgt):
     # address here can be almost anything
     import ctypes
-    cobj, arg = ctypes.c_char_p._CData_input(addr)
-    obj = arg._buffer[0]
-    return _rawffi.charp2rawstring(obj, lgt)
+    arg = ctypes.c_char_p._CData_value(addr)
+    return _rawffi.charp2rawstring(arg, lgt)
 
 def set_conversion_mode(encoding, errors):
     old_cm = ConvMode.encoding, ConvMode.errors
@@ -23,12 +22,11 @@
 
 def _wstring_at_addr(addr, lgt):
     import ctypes
-    cobj, arg = ctypes.c_wchar_p._CData_input(addr)
-    obj = arg._buffer[0]
+    arg = ctypes.c_wchar_p._CData_value(addr)
     # XXX purely applevel
     if lgt == -1:
         lgt = sys.maxint
-    a = _rawffi.Array('u').fromaddress(obj, lgt)
+    a = _rawffi.Array('u').fromaddress(arg, lgt)
     res = []
     for i in xrange(lgt):
         if lgt == sys.maxint and a[i] == '\x00':

Modified: pypy/dist/pypy/lib/_ctypes/pointer.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/pointer.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/pointer.py	Mon Feb 18 17:31:08 2008
@@ -100,8 +100,7 @@
         return self._type_._CData_output(self._subarray(index), self, index)
 
     def __setitem__(self, index, value):
-        cobj, arg = self._type_._CData_input(value)
-        self._subarray(index)[0] = arg._buffer[0]
+        self._subarray(index)[0] = self._type_._CData_value(value)
 
     def __nonzero__(self):
         return self._buffer[0] != 0

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 17:31:08 2008
@@ -168,8 +168,7 @@
         if getattr(value, '_objects', None):
             key = keepalive_key(getattr(self.__class__, name).offset)
             store_reference(self, key, value._objects)
-        cobj, arg = fieldtype._CData_input(value)
-        self._buffer.__setattr__(name, arg._buffer[0])
+        self._buffer.__setattr__(name, fieldtype._CData_value(value))
 
     def __getattribute__(self, name):
         if name == '_fieldtypes':

Modified: pypy/dist/pypy/lib/_ctypes/union.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/union.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/union.py	Mon Feb 18 17:31:08 2008
@@ -81,9 +81,8 @@
             fieldtype = self._fieldtypes[name].ctype
         except KeyError:
             raise AttributeError(name)
-        cobj, arg = fieldtype._CData_input(value)
         buf = self._ffiarrays[name].fromaddress(self._buffer.buffer, 1)
-        buf[0] = arg._buffer[0]
+        buf[0] = fieldtype._CData_value(value)
 
     def __del__(self):
         if self._needs_free:



More information about the Pypy-commit mailing list