Author: Amaury Forgeot d'Arc <[email protected]>
Branch: 
Changeset: r56647:76c0dcd40f93
Date: 2012-08-08 02:48 +0200
http://bitbucket.org/pypy/pypy/changeset/76c0dcd40f93/

Log:    issue1232: ctypes: Subclasses of primitive types don't call their
        __new__ when the value is extracted from a struct member. Yes, this
        is confusing.

diff --git a/lib_pypy/_ctypes/basics.py b/lib_pypy/_ctypes/basics.py
--- a/lib_pypy/_ctypes/basics.py
+++ b/lib_pypy/_ctypes/basics.py
@@ -59,7 +59,8 @@
         'resbuffer' is a _rawffi array of length 1 containing the value,
         and this returns a general Python object that corresponds.
         """
-        res = self.__new__(self)
+        res = object.__new__(self)
+        res.__class__ = self
         res.__dict__['_buffer'] = resbuffer
         res.__dict__['_base'] = base
         res.__dict__['_index'] = index
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py
@@ -187,6 +187,14 @@
         # probably be changed:
         raises(TypeError, c_int, c_long(42))
 
+    def test_subclass(self):
+        class enum(c_int):
+            def __new__(cls, value):
+                dont_call_me
+        class S(Structure):
+            _fields_ = [('t', enum)]
+        assert isinstance(S().t, enum)
+
 ##    def test_perf(self):
 ##        check_perf()
 
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to