]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport, at the reqest of Kevin Jacobs:
authorGuido van Rossum <guido@python.org>
Mon, 7 Oct 2002 18:08:27 +0000 (18:08 +0000)
committerGuido van Rossum <guido@python.org>
Mon, 7 Oct 2002 18:08:27 +0000 (18:08 +0000)
- Changed new-style class instantiation so that when C's __new__
  method returns something that's not a C instance, its __init__ is
  not called.  [SF bug #537450]

XXX This is arguably a semantic change, but it's hard to imagine a
reason for wanting to depend on the old behavior.  If problems with
this are reported within a week of the release of 2.2.2 beta 1, we may
revert this change.

Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index 84f3da965a770759ec970baedd7261a179f0ddeb..74e33499d09393c35939726b7915a3ce8289560b 100644 (file)
@@ -3017,6 +3017,27 @@ def subtype_resurrection():
     # it as a leak.
     del C.__del__
 
+def funnynew():
+    if verbose: print "Testing __new__ returning something unexpected..."
+    class C(object):
+        def __new__(cls, arg):
+            if isinstance(arg, str): return [1, 2, 3]
+            elif isinstance(arg, int): return object.__new__(D)
+            else: return object.__new__(cls)
+    class D(C):
+        def __init__(self, arg):
+            self.foo = arg
+    vereq(C("1"), [1, 2, 3])
+    vereq(D("1"), [1, 2, 3])
+    d = D(None)
+    veris(d.foo, None)
+    d = C(1)
+    vereq(isinstance(d, D), True)
+    vereq(d.foo, 1)
+    d = D(1)
+    vereq(isinstance(d, D), True)
+    vereq(d.foo, 1)
+
 def test_main():
     class_docstrings()
     lists()
@@ -3078,6 +3099,7 @@ def test_main():
     imulbug()
     copy_setstate()
     subtype_resurrection()
+    funnynew()
     if verbose: print "All OK"
 
 if __name__ == "__main__":
index 6e849da1b9b9b862980ee4ac8d9549f897efc0b2..8ab75cc49630edf85e6aec1d00abac6ec51dd414 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1,9 +1,16 @@
-What's New in Python 2.2.2?
-Release date: dd-mmm-2002
-===========================
+What's New in Python 2.2.2b1?
+Release date: 7-Oct-2002
+=============================
 
 Core and builtins
 
+- Changed new-style class instantiation so that when C's __new__
+  method returns something that's not a C instance, its __init__ is
+  not called.  [SF bug #537450]  (This is arguably a semantic change,
+  but it's hard to imagine a reason for wanting to depend on the old
+  behavior.  If problems with this are reported within a week of the
+  release of 2.2.2 beta 1, we may revert this change.)
+
 - u'%c' will now raise a ValueError in case the argument is an
   integer outside the valid range of Unicode code point ordinals.
 
index c9c29fe6f300c581e144d486746d79d05ccfc5db..d134e0c407b14db54ca878ccbd43a045e7f77584 100644 (file)
@@ -189,6 +189,10 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
                    (kwds == NULL ||
                     (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
                        return obj;
+               /* If the returned object is not an instance of type,
+                  it won't be initialized. */
+               if (!PyType_IsSubtype(obj->ob_type, type))
+                       return obj;
                type = obj->ob_type;
                if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
                    type->tp_init != NULL &&