From 16b98b1d1cce4580fb62a59e797f4e225a014e58 Mon Sep 17 00:00:00 2001 From: Bhuvansh Kataria Date: Sat, 1 Aug 2026 19:32:49 +0000 Subject: [PATCH] gh-155053: Fix GenericAlias crash on OOM --- Lib/test/test_genericalias.py | 29 ++++++++++++++++++- ...-08-01-19-17-56.gh-issue-155053.sxmlTO.rst | 2 ++ Objects/genericaliasobject.c | 1 - 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-08-01-19-17-56.gh-issue-155053.sxmlTO.rst diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 7816775620bc01..32cfbf6cfe5fbc 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -1,6 +1,8 @@ """Tests for C-implemented GenericAlias.""" import unittest +from test import support +from test.support import import_helper import pickle from array import array import copy @@ -62,10 +64,11 @@ from string.templatelib import Template, Interpolation import typing -from typing import TypeVar, Unpack +from typing import TypeVar, TypeVarTuple, Unpack T = TypeVar('T') K = TypeVar('K') V = TypeVar('V') +Ts = TypeVarTuple("Ts") _UNPACKED_TUPLES = [ # Unpacked tuple using `*` @@ -97,6 +100,7 @@ tuple[*tuple[Unpack[tuple[int, ...]]]], ] +_testcapi = import_helper.import_module("_testcapi") class BaseTest(unittest.TestCase): """Test basics.""" @@ -628,6 +632,29 @@ def test_gh150146(self): with self.assertRaises(TypeError): x[*typing.Mapping[..., ...]] + @support.nomemtest + def test_subs_tvars_nomemory(self): + alias = dict[str, tuple[*Ts]] + key = (int, str) + + # Warm the code path to stabilize the allocation window. + # This injection point was determined experimentally for this build. + try: + dict[str, tuple[int, str]] + except Exception: + pass + + _testcapi.set_nomemory(24, 25) + raised = False + try: + alias[key] + except MemoryError: + raised = True + finally: + _testcapi.remove_mem_hooks() + + self.assertTrue(raised, "MemoryError not raised") + class TypeIterationTests(unittest.TestCase): _UNITERABLE_TYPES = (list, tuple) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-01-19-17-56.gh-issue-155053.sxmlTO.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-01-19-17-56.gh-issue-155053.sxmlTO.rst new file mode 100644 index 00000000000000..a0e0d787794e09 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-01-19-17-56.gh-issue-155053.sxmlTO.rst @@ -0,0 +1,2 @@ +Fix a crash in :class:`types.GenericAlias` when ``tuple_extend()`` fails +during substitution of a ``TypeVarTuple`` under allocation failure. diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c index 348c7dd6967a39..5cb1686fdbf8e2 100644 --- a/Objects/genericaliasobject.c +++ b/Objects/genericaliasobject.c @@ -302,7 +302,6 @@ subs_tvars(PyObject *obj, PyObject *params, PyTuple_GET_SIZE(arg)); if (j < 0) { Py_DECREF(subparams); - Py_DECREF(subargs); return NULL; } continue;