[pypy-svn] r50869 - pypy/dist/pypy/lib/_ctypes
fijal at codespeak.net
fijal at codespeak.net
Tue Jan 22 13:33:02 CET 2008
Author: fijal
Date: Tue Jan 22 13:33:01 2008
New Revision: 50869
Added:
pypy/dist/pypy/lib/_ctypes/union.py (contents, props changed)
Modified:
pypy/dist/pypy/lib/_ctypes/__init__.py
pypy/dist/pypy/lib/_ctypes/dummy.py
pypy/dist/pypy/lib/_ctypes/structure.py
Log:
Rudimentary support for unions (they still does not work)
Modified: pypy/dist/pypy/lib/_ctypes/__init__.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/__init__.py (original)
+++ pypy/dist/pypy/lib/_ctypes/__init__.py Tue Jan 22 13:33:01 2008
@@ -1,4 +1,3 @@
-from _ctypes.dummy import Union
from _ctypes.dummy import resize
from _ctypes.basics import _CData, sizeof, alignment, byref, addressof,\
ArgumentError
@@ -9,6 +8,7 @@
from _ctypes.structure import Structure
from _ctypes.array import Array
from _ctypes.builtin import _memmove_addr, _string_at_addr, _memset_addr
+from _ctypes.union import Union
__version__ = '1.0.2'
#XXX platform dependant?
Modified: pypy/dist/pypy/lib/_ctypes/dummy.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/dummy.py (original)
+++ pypy/dist/pypy/lib/_ctypes/dummy.py Tue Jan 22 13:33:01 2008
@@ -1,9 +1,3 @@
-class UnionType(type):
- pass
-
-class Union(object):
- __metaclass__ = UnionType
-
def dummyfunc(*args, **kwargs):
EXPLODE
Modified: pypy/dist/pypy/lib/_ctypes/structure.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/structure.py (original)
+++ pypy/dist/pypy/lib/_ctypes/structure.py Tue Jan 22 13:33:01 2008
@@ -22,7 +22,13 @@
size = round_up(size, alignment)
return size, alignment, pos
-def names_and_fields(_fields_, superclass):
+
+def struct_getattr(self, name):
+ if hasattr(self, '_fieldtypes') and name in self._fieldtypes:
+ return self._fieldtypes[name]
+ return _CDataMeta.__getattribute__(self, name)
+
+def names_and_fields(_fields_, superclass, zero_offset=False):
for _, tp in _fields_:
if not isinstance(tp, _CDataMeta):
raise TypeError("Expected CData subclass, got %s" % (tp,))
@@ -33,7 +39,10 @@
names = [name for name, ctype in all_fields]
rawfields = [(name, ctype._ffishape)
for name, ctype in all_fields]
- _, _, pos = size_alignment_pos(all_fields)
+ if not zero_offset:
+ _, _, pos = size_alignment_pos(all_fields)
+ else:
+ pos = [0] * len(all_fields)
fields = {}
for i, (name, ctype) in enumerate(all_fields):
fields[name] = Field(name, pos[i], ctypes.sizeof(ctype), ctype)
@@ -78,6 +87,8 @@
return res
+ __getattr__ = struct_getattr
+
def __setattr__(self, name, value):
if name == '_fields_':
if self.__dict__.get('_fields_', None):
@@ -92,12 +103,6 @@
return
_CDataMeta.__setattr__(self, name, value)
- def __getattr__(self, name):
- if hasattr(self, '_fieldtypes') and name in self._fieldtypes:
- return self._fieldtypes[name]
- return _CDataMeta.__getattribute__(self, name)
- #return Field(name,
-
def from_address(self, address):
instance = self.__new__(self)
instance.__dict__['_buffer'] = self._ffistruct.fromaddress(address)
Added: pypy/dist/pypy/lib/_ctypes/union.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/_ctypes/union.py Tue Jan 22 13:33:01 2008
@@ -0,0 +1,29 @@
+
+
+import _rawffi
+from _ctypes.basics import _CData, _CDataMeta
+from _ctypes.structure import round_up, names_and_fields, struct_getattr
+import inspect
+
+class UnionMeta(_CDataMeta):
+ def __new__(self, name, cls, typedict):
+ res = type.__new__(self, name, cls, typedict)
+ if '_fields_' in typedict:
+ res._names, rawfields, res._fieldtypes = names_and_fields(
+ typedict['_fields_'], cls[0], True)
+
+ return res
+
+ def _sizeofinstances(self):
+ return max([field.size for field in self._fieldtypes.values()]
+ + [0])
+
+ def _alignmentofinstances(self):
+ return max([field.alignment for field in self._fieldtypes.values()]
+ + [1])
+
+ __getattr__ = struct_getattr
+
+class Union(_CData):
+ __metaclass__ = UnionMeta
+ _ffiletter = 'P'
More information about the Pypy-commit
mailing list