[Python-checkins] bpo-44232: Fix type_new() error reporting (GH-26359) (GH-26365)

vstinner webhook-mailer at python.org
Wed May 26 05:31:08 EDT 2021


https://github.com/python/cpython/commit/7b3b6982a5683f5146ede58a448d3edb777e501b
commit: 7b3b6982a5683f5146ede58a448d3edb777e501b
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: vstinner <vstinner at python.org>
date: 2021-05-26T11:30:55+02:00
summary:

bpo-44232: Fix type_new() error reporting (GH-26359) (GH-26365)

Fix a regression in type() when a metaclass raises an exception. The
C function type_new() must properly report the exception when a
metaclass constructor raises an exception and the winner class is not
the metaclass.
(cherry picked from commit bd199e72fb60a8ff001a023f23925092a290be91)

Co-authored-by: Victor Stinner <vstinner at python.org>

files:
A Misc/NEWS.d/next/Core and Builtins/2021-05-25-18-20-10.bpo-44232.DMcCCf.rst
M Lib/test/test_types.py
M Objects/typeobject.c

diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 25ebec5fa5509..767c3d06a4f58 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -1334,6 +1334,24 @@ class N(type, metaclass=M):
             N(5)
         self.assertEqual(str(cm.exception), expected_message)
 
+    def test_metaclass_new_error(self):
+        # bpo-44232: The C function type_new() must properly report the
+        # exception when a metaclass constructor raises an exception and the
+        # winner class is not the metaclass.
+        class ModelBase(type):
+            def __new__(cls, name, bases, attrs):
+                super_new = super().__new__
+                new_class = super_new(cls, name, bases, {})
+                if name != "Model":
+                    raise RuntimeWarning(f"{name=}")
+                return new_class
+
+        class Model(metaclass=ModelBase):
+            pass
+
+        with self.assertRaises(RuntimeWarning):
+            type("SouthPonies", (Model,), {})
+
 
 class SimpleNamespaceTests(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-05-25-18-20-10.bpo-44232.DMcCCf.rst b/Misc/NEWS.d/next/Core and Builtins/2021-05-25-18-20-10.bpo-44232.DMcCCf.rst
new file mode 100644
index 0000000000000..fcd124d51442a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-05-25-18-20-10.bpo-44232.DMcCCf.rst	
@@ -0,0 +1,4 @@
+Fix a regression in :func:`type` when a metaclass raises an exception. The C
+function :c:func:`type_new` must properly report the exception when a metaclass
+constructor raises an exception and the winner class is not the metaclass.
+Patch by Victor Stinner.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 8f6764879c2ae..a551402b5ce53 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -3256,6 +3256,9 @@ type_new_get_bases(type_new_ctx *ctx, PyObject **type)
         if (winner->tp_new != type_new) {
             /* Pass it to the winner */
             *type = winner->tp_new(winner, ctx->args, ctx->kwds);
+            if (*type == NULL) {
+                return -1;
+            }
             return 1;
         }
 
@@ -3307,6 +3310,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
     PyObject *type = NULL;
     int res = type_new_get_bases(&ctx, &type);
     if (res < 0) {
+        assert(PyErr_Occurred());
         return NULL;
     }
     if (res == 1) {



More information about the Python-checkins mailing list