[pypy-svn] r9981 - pypy/dist/pypy/module/builtin

tismer at codespeak.net tismer at codespeak.net
Mon Mar 21 04:23:05 CET 2005


Author: tismer
Date: Mon Mar 21 04:23:05 2005
New Revision: 9981

Modified:
   pypy/dist/pypy/module/builtin/app_complex.py
Log:
augmented complex a little to become subclassable for test_pickle.
For that reason, it could not inheritfrom object,
or it would not grow a __base__other than object.
Do we want to special-case this in typeobject?

Modified: pypy/dist/pypy/module/builtin/app_complex.py
==============================================================================
--- pypy/dist/pypy/module/builtin/app_complex.py	(original)
+++ pypy/dist/pypy/module/builtin/app_complex.py	Mon Mar 21 04:23:05 2005
@@ -2,7 +2,15 @@
 Plain Python definition of the 'complex' type.
 """
 
-class complex(object):
+# XXX this has been object before,
+# but we need something different, or
+# the __base__ will never become different from object.
+# note that this is real Python behavior :-)
+
+# XXX would be eventually try tospecial-case this
+# in typeobject to be handled as a base class?
+
+class complex(float):
     """complex(real[, imag]) -> complex number
 
     Create a complex number from a real part and an optional imaginary part.
@@ -12,6 +20,14 @@
 
     # XXX this class is not well tested
 
+    # provide __new__to prevend the default which has no parameters
+    def __new__(typ, *args, **kwds):
+        ret = float.__new__(typ)
+        return ret
+
+    def __reduce__(self):
+        return self.__class__, (), self.__dict__
+
     def __init__(self, real=0.0, imag=None):
         if isinstance(real, str): 
             if imag is not None:
@@ -40,8 +56,9 @@
     def __setattr__(self, name, value):
         if name in ('real', 'imag'):
             raise AttributeError, "readonly attribute"
-        else:
+        elif self.__class__ is complex:
             raise AttributeError, "'complex' object has no attribute %s" % name
+        self.__dict__[name] = value
 
     def _makeComplexFromString(self, string):
         import re



More information about the Pypy-commit mailing list