]> granicus.if.org Git - python/commitdiff
Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
authorGuido van Rossum <guido@python.org>
Thu, 18 Oct 2001 15:49:21 +0000 (15:49 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 18 Oct 2001 15:49:21 +0000 (15:49 +0000)
The fix is a band-aid: type_call() now makes the same exception for a
single-argument call to type() as type_new() was already making.

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

index c9235c8b0e5aa65eee9285cf77795c2a03cbc8d9..962c1cc7a040f358b0d8e56e9a4af7311e304d99 100644 (file)
@@ -716,6 +716,18 @@ def metaclass():
             return "D" + self.__super._get_x()
     vereq(D().x, "DCBA")
 
+    # Make sure type(x) doesn't call x.__class__.__init__
+    class T(type):
+        counter = 0
+        def __init__(self, *args):
+            T.counter += 1
+    class C:
+        __metaclass__ = T
+    vereq(T.counter, 1)
+    a = C()
+    vereq(type(a), C)
+    vereq(T.counter, 1)
+
 def pymods():
     if verbose: print "Testing Python subclass of module..."
     log = []
index 0ae72c34d4e12e32f32867ddfca09aa9ba15aad1..e7400e9e51a72837e709064b8dd30fd5a7dc9405 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -337,6 +337,7 @@ Sean Reifschneider
 Michael P. Reilly
 Bernhard Reiter
 Steven Reiz
+Roeland Rengelink
 Jan Pieter Riegel
 Armin Rigo
 Nicholas Riley
index c37e54f900f8fd95ca79af7d4e81c2746438a876..66eecec6d167f62e206afa751a3d20bc372a7b7a 100644 (file)
@@ -147,6 +147,13 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
        obj = type->tp_new(type, args, kwds);
        if (obj != NULL) {
+               /* Ugly exception: when the call was type(something),
+                  don't call tp_init on the result. */
+               if (type == &PyType_Type &&
+                   PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
+                   (kwds == NULL ||
+                    (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
+                       return obj;
                type = obj->ob_type;
                if (type->tp_init != NULL &&
                    type->tp_init(obj, args, kwds) < 0) {