[pypy-svn] r52250 - pypy/branch/rawffi-shape-cleanup/pypy/lib/_ctypes
pedronis at codespeak.net
pedronis at codespeak.net
Fri Mar 7 16:11:52 CET 2008
Author: pedronis
Date: Fri Mar 7 16:11:51 2008
New Revision: 52250
Modified:
pypy/branch/rawffi-shape-cleanup/pypy/lib/_ctypes/structure.py
pypy/branch/rawffi-shape-cleanup/pypy/lib/_ctypes/union.py
Log:
make test_anon passes, now unions use an opaque _rawffi.Structure as their backstore
some refactorings to avoid code duplication
the branch is ready to be merged
Modified: pypy/branch/rawffi-shape-cleanup/pypy/lib/_ctypes/structure.py
==============================================================================
--- pypy/branch/rawffi-shape-cleanup/pypy/lib/_ctypes/structure.py (original)
+++ pypy/branch/rawffi-shape-cleanup/pypy/lib/_ctypes/structure.py Fri Mar 7 16:11:51 2008
@@ -22,27 +22,6 @@
size = round_up(size, alignment)
return size, alignment, pos
-def struct_getattr(self, name):
- if hasattr(self, '_fieldtypes') and name in self._fieldtypes:
- return self._fieldtypes[name]
- return _CDataMeta.__getattribute__(self, name)
-
-def struct_setattr(self, name, value):
- if name == '_fields_':
- if self.__dict__.get('_fields_', None):
- raise AttributeError("_fields_ is final")
- if self in [v for k, v in value]:
- raise AttributeError("Structure or union cannot contain itself")
- self._names, rawfields, self._fieldtypes = names_and_fields(
- value, self.__bases__[0], False,
- self.__dict__.get('_anonymous_', None))
- self._ffistruct = _rawffi.Structure(rawfields)
- _CDataMeta.__setattr__(self, '_fields_', value)
- self._ffiargshape = self._ffishape = (self._ffistruct, 1)
- self._fficompositesize = self._ffistruct.size
- return
- _CDataMeta.__setattr__(self, name, value)
-
def names_and_fields(_fields_, superclass, zero_offset=False, anon=None):
if isinstance(_fields_, tuple):
_fields_ = list(_fields_)
@@ -92,6 +71,32 @@
return "<Field '%s' offset=%d size=%d>" % (self.name, self.offset,
self.size)
+# ________________________________________________________________
+
+def _set_shape(tp, rawfields):
+ tp._ffistruct = _rawffi.Structure(rawfields)
+ tp._ffiargshape = tp._ffishape = (tp._ffistruct, 1)
+ tp._fficompositesize = tp._ffistruct.size
+
+def struct_getattr(self, name):
+ if hasattr(self, '_fieldtypes') and name in self._fieldtypes:
+ return self._fieldtypes[name]
+ return _CDataMeta.__getattribute__(self, name)
+
+def struct_setattr(self, name, value):
+ if name == '_fields_':
+ if self.__dict__.get('_fields_', None):
+ raise AttributeError("_fields_ is final")
+ if self in [v for k, v in value]:
+ raise AttributeError("Structure or union cannot contain itself")
+ self._names, rawfields, self._fieldtypes = names_and_fields(
+ value, self.__bases__[0], False,
+ self.__dict__.get('_anonymous_', None))
+ _CDataMeta.__setattr__(self, '_fields_', value)
+ _set_shape(self, rawfields)
+ return
+ _CDataMeta.__setattr__(self, name, value)
+
class StructureMeta(_CDataMeta):
def __new__(self, name, cls, typedict):
res = type.__new__(self, name, cls, typedict)
@@ -104,10 +109,7 @@
res._names, rawfields, res._fieldtypes = names_and_fields(
typedict['_fields_'], cls[0], False,
typedict.get('_anonymous_', None))
- res._ffistruct = _rawffi.Structure(rawfields)
- res._ffishape = (res._ffistruct, 1)
- res._ffiargshape = res._ffishape
- res._fficompositesize = res._ffistruct.size
+ _set_shape(res, rawfields)
def __init__(self, *args, **kwds):
if not hasattr(self, '_ffistruct'):
Modified: pypy/branch/rawffi-shape-cleanup/pypy/lib/_ctypes/union.py
==============================================================================
--- pypy/branch/rawffi-shape-cleanup/pypy/lib/_ctypes/union.py (original)
+++ pypy/branch/rawffi-shape-cleanup/pypy/lib/_ctypes/union.py Fri Mar 7 16:11:51 2008
@@ -6,6 +6,19 @@
struct_setattr
import inspect
+
+def _set_shape(tp):
+ size = tp._sizeofinstances()
+ alignment = tp._alignmentofinstances()
+ tp._ffiopaque = _rawffi.Structure((size, alignment)) # opaque
+ tp._ffiargshape = tp._ffishape = (tp._ffiopaque, 1)
+ tp._fficompositesize = tp._ffiopaque.size
+ # we need to create an array of size one for each
+ # of our elements
+ tp._ffiarrays = {}
+ for name, field in tp._fieldtypes.iteritems():
+ tp._ffiarrays[name] = _rawffi.Array(field.ctype._ffishape)
+
class UnionMeta(_CDataMeta):
def __new__(self, name, cls, typedict):
res = type.__new__(self, name, cls, typedict)
@@ -13,21 +26,14 @@
res._names, rawfields, res._fieldtypes = names_and_fields(
typedict['_fields_'], cls[0], True,
typedict.get('_anonymous_', None))
- res._ffishape = (res._sizeofinstances(),
- res._alignmentofinstances())
- res._ffiargshape = res._ffishape
- res._fficompositesize = res._sizeofinstances()
- # we need to create an array of size one for each
- # of our elements
- res._ffiarrays = {}
- for name, field in res._fieldtypes.iteritems():
- res._ffiarrays[name] = _rawffi.Array(field.ctype._ffishape)
+ _set_shape(res)
+
def __init__(self): # don't allow arguments by now
if not hasattr(self, '_ffiarrays'):
raise TypeError("Cannot instantiate union, has no type")
# malloc size
size = self.__class__._sizeofinstances()
- self.__dict__['_buffer'] = _rawffi.Array('c')(size)
+ self.__dict__['_buffer'] = self._ffiopaque()
self.__dict__['_needs_free'] = True
res.__init__ = __init__
return res
@@ -56,14 +62,8 @@
self._names, rawfields, self._fieldtypes = names_and_fields(
value, self.__bases__[0], True,
self.__dict__.get('_anonymous_', None))
- self._ffiarrays = {}
- for name, field in self._fieldtypes.iteritems():
- self._ffiarrays[name] = _rawffi.Array(field.ctype._ffishape)
_CDataMeta.__setattr__(self, '_fields_', value)
- self._ffiargshape = self._ffishape = (self._sizeofinstances(),
- self._alignmentofinstances())
- self._fficompositesize = self._sizeofinstances()
- return
+ _set_shape(self)
_CDataMeta.__setattr__(self, name, value)
class Union(_CData):
More information about the Pypy-commit
mailing list