https://github.com/python/cpython/commit/429ed984c7c0cd444fb38f66cc0258b686186cb3
commit: 429ed984c7c0cd444fb38f66cc0258b686186cb3
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-20T18:43:59Z
summary:

[3.15] gh-159739: [typing] Handle generic alias construction errors (GH-159772) 
(#159909)



(cherry picked from commit 07df106f7efa7e5e3f5fba4b4927eaba5bcd98d0)

Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.b62fc9.rst
M Objects/genericaliasobject.c
M Objects/typevarobject.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.b62fc9.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.b62fc9.rst
new file mode 100644
index 00000000000000..64d84ae260bb3b
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.b62fc9.rst
@@ -0,0 +1,2 @@
+Propagate errors during generic class construction and avoid a crash after
+failed generic alias substitution.
diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c
index 8bb7cc8c74a592..18321d9948428f 100644
--- a/Objects/genericaliasobject.c
+++ b/Objects/genericaliasobject.c
@@ -303,7 +303,7 @@ subs_tvars(PyObject *obj, PyObject *params,
                                     PyTuple_GET_SIZE(arg));
                     if (j < 0) {
                         Py_DECREF(subparams);
-                        Py_DECREF(subargs);
+                        assert(subargs == NULL);
                         return NULL;
                     }
                     continue;
diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c
index eed1457cbb426d..398a63257beb4a 100644
--- a/Objects/typevarobject.c
+++ b/Objects/typevarobject.c
@@ -2359,6 +2359,9 @@ _Py_subscript_generic(PyThreadState* unused, PyObject 
*params)
         return NULL;
     }
     params = unpack_typevartuples(params);
+    if (params == NULL) {
+        return NULL;
+    }
     PyObject *args[2] = {(PyObject *)interp->cached_objects.generic_type, 
params};
     PyObject *result = call_typing_func_object("_GenericAlias", args, 2);
     Py_DECREF(params);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to