From 3304449e0b8238d88b4a2c2ccf0eb1c08c9ea0a0 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 13:41:26 -0700 Subject: [PATCH] gh-159739: Handle generic alias construction errors Backport to 3.15 and 3.14 (Objects/typevarobject.c only on 3.14). --- .../2026-10-19-20-38-17.gh-issue-159739.b62fc9.rst | 2 ++ Objects/genericaliasobject.c | 2 +- Objects/typevarobject.c | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.b62fc9.rst 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 000000000000000..64d84ae260bb3b9 --- /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 8bb7cc8c74a5920..18321d9948428ff 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 eed1457cbb426d9..398a63257beb4a2 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);