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);