[Python-checkins] cpython (2.7): Issue #26906: Resolving special methods of uninitialized type now causes

serhiy.storchaka python-checkins at python.org
Sat Oct 8 05:28:51 EDT 2016


https://hg.python.org/cpython/rev/3119f08802a5
changeset:   104366:3119f08802a5
branch:      2.7
parent:      104364:4ed5f0e9bba2
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Oct 08 12:24:09 2016 +0300
summary:
  Issue #26906: Resolving special methods of uninitialized type now causes
implicit initialization of the type instead of a fail.

files:
  Misc/NEWS            |   3 +++
  Objects/typeobject.c |  24 +++++++++++++++++++-----
  2 files changed, 22 insertions(+), 5 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #26906: Resolving special methods of uninitialized type now causes
+  implicit initialization of the type instead of a fail.
+
 - Issue #18287: PyType_Ready() now checks that tp_name is not NULL.
   Original patch by Niklas Koep.
 
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -2545,11 +2545,25 @@
     /* Look in tp_dict of types in MRO */
     mro = type->tp_mro;
 
-    /* If mro is NULL, the type is either not yet initialized
-       by PyType_Ready(), or already cleared by type_clear().
-       Either way the safest thing to do is to return NULL. */
-    if (mro == NULL)
-        return NULL;
+    if (mro == NULL) {
+        if ((type->tp_flags & Py_TPFLAGS_READYING) == 0 &&
+            PyType_Ready(type) < 0) {
+            /* It's not ideal to clear the error condition,
+               but this function is documented as not setting
+               an exception, and I don't want to change that.
+               When PyType_Ready() can't proceed, it won't
+               set the "ready" flag, so future attempts to ready
+               the same type will call it again -- hopefully
+               in a context that propagates the exception out.
+            */
+            PyErr_Clear();
+            return NULL;
+        }
+        mro = type->tp_mro;
+        if (mro == NULL) {
+            return NULL;
+        }
+    }
 
     res = NULL;
     assert(PyTuple_Check(mro));

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list