[pypy-svn] r50631 - pypy/branch/applevel-ctypes2/pypy/lib/_ctypes
arigo at codespeak.net
arigo at codespeak.net
Tue Jan 15 15:30:04 CET 2008
Author: arigo
Date: Tue Jan 15 15:29:59 2008
New Revision: 50631
Modified:
pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/array.py
pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/basics.py
pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/pointer.py
pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/primitive.py
pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/structure.py
Log:
(fijal, arigo)
Some general progress. More tests pass.
Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/array.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/array.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/array.py Tue Jan 15 15:29:59 2008
@@ -12,7 +12,7 @@
res._ffiarray = ffiarray
if typedict['_type_']._type_ == 'c':
def getvalue(self):
- return _rawffi.charp2string(self._array.buffer,
+ return _rawffi.charp2string(self._buffer.buffer,
self._length_)
def setvalue(self, val):
# we don't want to have buffers here
@@ -36,7 +36,7 @@
return res
def _CData_input(self, value):
- return self.from_param(value)._array.byptr()
+ return self.from_param(value)._buffer.byptr()
from_address = cdata_from_address
@@ -44,7 +44,7 @@
__metaclass__ = ArrayMeta
def __init__(self, *args):
- self._array = self._ffiarray(self._length_)
+ self._buffer = self._ffiarray(self._length_)
for i, arg in enumerate(args):
self[i] = arg
@@ -73,9 +73,9 @@
return "".join([self[i] for i in range(start, stop)])
def _subarray(self, index):
- """Return an _array of length 1 whose address is the same as
+ """Return a _rawffi array of length 1 whose address is the same as
the index'th item of self."""
- address = self._array.itemaddress(index)
+ address = self._buffer.itemaddress(index)
return self._ffiarray.fromaddress(address, 1)
def __setitem__(self, index, value):
@@ -84,7 +84,7 @@
return
value = self._type_._CData_input(value)
index = self._fix_index(index)
- self._array[index] = value[0]
+ self._buffer[index] = value[0]
def __getitem__(self, index):
if isinstance(index, slice):
Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/basics.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/basics.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/basics.py Tue Jan 15 15:29:59 2008
@@ -15,21 +15,25 @@
def _CData_input(self, value):
"""Used when data enters into ctypes from user code. 'value' is
- some user-specified Python object, which is converted into an
- _array of length 1 containing the same value according to the
+ some user-specified Python object, which is converted into a _rawffi
+ array of length 1 containing the same value according to the
type 'self'.
"""
- return self.from_param(value)._array
+ return self.from_param(value)._buffer
def _CData_output(self, resarray):
"""Used when data exits ctypes and goes into user code.
- 'resarray' is an _array of length 1 containing the value,
+ 'resarray' is a _rawffi array of length 1 containing the value,
and this returns a general Python object that corresponds.
"""
res = self.__new__(self)
- res._array = resarray
+ res._buffer = resarray
return res.__ctypes_from_outparam__()
+ def __mul__(self, other):
+ from _ctypes.array import create_array_type
+ return create_array_type(self, other)
+
class _CData(object):
""" The most basic object for all ctypes types
"""
@@ -53,7 +57,11 @@
def sizeof(tp):
if not isinstance(tp, _CDataMeta):
- raise TypeError("ctypes type expected, got %r" % (type(tp).__name__,))
+ if isinstance(tp, _CData):
+ tp = type(tp)
+ else:
+ raise TypeError("ctypes type or instance expected, got %r" % (
+ type(tp).__name__,))
return tp._sizeofinstances()
def alignment(tp):
@@ -67,9 +75,8 @@
def cdata_from_address(self, address):
instance = self.__new__(self)
lgt = getattr(self, '_length_', 1)
- instance._array = self._ffiarray.fromaddress(address, lgt)
+ instance._buffer = self._ffiarray.fromaddress(address, lgt)
return instance
def addressof(tp):
- # XXX we should have a method on each..
- return tp._array.buffer
+ return tp._buffer.buffer
Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/pointer.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/pointer.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/pointer.py Tue Jan 15 15:29:59 2008
@@ -22,7 +22,7 @@
if '_type_' in typedict:
ffiarray = _rawffi.Array('P')
def __init__(self, value=0):
- self._array = ffiarray(1)
+ self._buffer = ffiarray(1)
self.contents = value
obj._ffiarray = ffiarray
else:
@@ -52,24 +52,19 @@
class _Pointer(_CData):
__metaclass__ = PointerType
- def getvalue(self):
- return self._array
-
- value = property(getvalue)
-
def getcontents(self):
- return self._type_.from_address(self._array[0])
+ return self._type_.from_address(self._buffer[0])
def setcontents(self, value):
- if isinstance(value, int):
- self._array[0] = value
- else:
- self._array[0] = value._array
+ if not isinstance(value, self._type_):
+ raise TypeError("expected %s instead of %s" % (
+ self._type_.__name__, type(value).__name__))
+ self._buffer[0] = value._buffer
def _subarray(self, index=0):
- """Return an _array of length 1 whose address is the same as
+ """Return a _rawffi array of length 1 whose address is the same as
the index'th item to which self is pointing."""
- address = self._array[0]
+ address = self._buffer[0]
address += index * sizeof(self._type_)
return self._type_._ffiarray.fromaddress(address, 1)
Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/primitive.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/primitive.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/primitive.py Tue Jan 15 15:29:59 2008
@@ -3,7 +3,6 @@
SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv"
from _ctypes.basics import _CData, _CDataMeta, cdata_from_address
-from _ctypes.array import create_array_type
class NULL(object):
pass
@@ -48,13 +47,13 @@
from _ctypes import Array, _Pointer
def _getvalue(self):
- return _rawffi.charp2string(self._array[0])
+ return _rawffi.charp2string(self._buffer[0])
def _setvalue(self, value):
if isinstance(value, str):
array = _rawffi.Array('c')(len(value)+1, value)
value = array.buffer
# XXX free 'array' later
- self._array[0] = value
+ self._buffer[0] = value
result.value = property(_getvalue, _setvalue)
def from_param(self, value):
@@ -74,9 +73,6 @@
from_address = cdata_from_address
- def __mul__(self, other):
- return create_array_type(self, other)
-
def from_param(self, value):
if isinstance(value, self):
return value
@@ -93,15 +89,15 @@
_type_ = 'i'
def __init__(self, value=DEFAULT_VALUE):
- self._array = self._ffiarray(1)
+ self._buffer = self._ffiarray(1)
if value is not DEFAULT_VALUE:
self.value = value
def _getvalue(self):
- return self._array[0]
+ return self._buffer[0]
def _setvalue(self, value):
- self._array[0] = value
+ self._buffer[0] = value
value = property(_getvalue, _setvalue)
del _getvalue, _setvalue
Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/structure.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/structure.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/structure.py Tue Jan 15 15:29:59 2008
@@ -14,7 +14,7 @@
res._ffistruct = ffistruct
def __init__(self, *args, **kwds):
- self.__dict__['_struct'] = ffistruct()
+ self.__dict__['_buffer'] = ffistruct()
if len(args) > len(names):
raise TypeError("too many arguments")
for name, arg in zip(names, args):
@@ -29,16 +29,21 @@
return res
def _CData_input(self, value):
- return self.from_param(value)._struct.byptr()
+ return self.from_param(value)._buffer.byptr()
+
+ def from_address(self, address):
+ instance = self.__new__(self)
+ instance.__dict__['_buffer'] = self._ffistruct.fromaddress(address)
+ return instance
class Structure(_CData):
__metaclass__ = StructureMeta
def _subarray(self, fieldtype, name):
- """Return an _array of length 1 whose address is the same as
+ """Return a _rawffi array of length 1 whose address is the same as
the address of the field 'name' of self."""
- address = self._struct.fieldaddress(name)
+ address = self._buffer.fieldaddress(name)
A = _rawffi.Array(fieldtype._ffiletter)
return A.fromaddress(address, 1)
@@ -48,7 +53,7 @@
except KeyError:
raise AttributeError(name)
value = fieldtype._CData_input(value)
- self._struct.__setattr__(name, value[0])
+ self._buffer.__setattr__(name, value[0])
def __getattr__(self, name):
try:
More information about the Pypy-commit
mailing list