[pypy-svn] r61546 - in pypy/trunk/pypy/lib: _ctypes app_test/ctypes_tests
afa at codespeak.net
afa at codespeak.net
Wed Feb 4 13:43:10 CET 2009
Author: afa
Date: Wed Feb 4 13:43:08 2009
New Revision: 61546
Modified:
pypy/trunk/pypy/lib/_ctypes/structure.py
pypy/trunk/pypy/lib/app_test/ctypes_tests/test_structures.py
Log:
Don't smash the __init__ method of classes derived from ctypes.Structure
This new test used to fail when run with pypy-c.
Modified: pypy/trunk/pypy/lib/_ctypes/structure.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/structure.py (original)
+++ pypy/trunk/pypy/lib/_ctypes/structure.py Wed Feb 4 13:43:08 2009
@@ -111,22 +111,6 @@
typedict.get('_anonymous_', None))
_set_shape(res, rawfields)
- def __init__(self, *args, **kwds):
- if not hasattr(self, '_ffistruct'):
- raise TypeError("Cannot instantiate structure, has no _fields_")
- self.__dict__['_buffer'] = self._ffistruct(autofree=True)
- if len(args) > len(self._names):
- raise TypeError("too many arguments")
- for name, arg in zip(self._names, args):
- if name in kwds:
- raise TypeError("duplicate value for argument %r" % (
- name,))
- self.__setattr__(name, arg)
- for name, arg in kwds.items():
- self.__setattr__(name, arg)
- res.__init__ = __init__
-
-
return res
__getattr__ = struct_getattr
@@ -168,6 +152,24 @@
class Structure(_CData):
__metaclass__ = StructureMeta
+ def __new__(cls, *args, **kwds):
+ if not hasattr(cls, '_ffistruct'):
+ raise TypeError("Cannot instantiate structure, has no _fields_")
+ self = super(_CData, cls).__new__(cls, *args, **kwds)
+ self.__dict__['_buffer'] = self._ffistruct(autofree=True)
+ return self
+
+ def __init__(self, *args, **kwds):
+ if len(args) > len(self._names):
+ raise TypeError("too many arguments")
+ for name, arg in zip(self._names, args):
+ if name in kwds:
+ raise TypeError("duplicate value for argument %r" % (
+ name,))
+ self.__setattr__(name, arg)
+ for name, arg in kwds.items():
+ self.__setattr__(name, arg)
+
def _subarray(self, fieldtype, name):
"""Return a _rawffi array of length 1 whose address is the same as
the address of the field 'name' of self."""
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 Wed Feb 4 13:43:08 2009
@@ -371,6 +371,19 @@
assert p.age == 6
assert p.income == 5
+ def test___init__(self):
+ class Person(Structure):
+ _fields_ = (("name", c_char*10),
+ ("age", c_int))
+
+ def __init__(self, name, surname, age):
+ self.name = name + ' ' + surname
+ self.age = age
+
+ p = Person("John", "Doe", 25)
+ assert p.name == "John Doe"
+ assert p.age == 25
+
class TestPointerMember(BaseCTypesTestChecker):
More information about the Pypy-commit
mailing list