Skip to content

Commit 16b98b1

Browse files
committed
gh-155053: Fix GenericAlias crash on OOM
1 parent 7ce7f0b commit 16b98b1

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

Lib/test/test_genericalias.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Tests for C-implemented GenericAlias."""
22

33
import unittest
4+
from test import support
5+
from test.support import import_helper
46
import pickle
57
from array import array
68
import copy
@@ -62,10 +64,11 @@
6264
from string.templatelib import Template, Interpolation
6365

6466
import typing
65-
from typing import TypeVar, Unpack
67+
from typing import TypeVar, TypeVarTuple, Unpack
6668
T = TypeVar('T')
6769
K = TypeVar('K')
6870
V = TypeVar('V')
71+
Ts = TypeVarTuple("Ts")
6972

7073
_UNPACKED_TUPLES = [
7174
# Unpacked tuple using `*`
@@ -97,6 +100,7 @@
97100
tuple[*tuple[Unpack[tuple[int, ...]]]],
98101
]
99102

103+
_testcapi = import_helper.import_module("_testcapi")
100104

101105
class BaseTest(unittest.TestCase):
102106
"""Test basics."""
@@ -628,6 +632,29 @@ def test_gh150146(self):
628632
with self.assertRaises(TypeError):
629633
x[*typing.Mapping[..., ...]]
630634

635+
@support.nomemtest
636+
def test_subs_tvars_nomemory(self):
637+
alias = dict[str, tuple[*Ts]]
638+
key = (int, str)
639+
640+
# Warm the code path to stabilize the allocation window.
641+
# This injection point was determined experimentally for this build.
642+
try:
643+
dict[str, tuple[int, str]]
644+
except Exception:
645+
pass
646+
647+
_testcapi.set_nomemory(24, 25)
648+
raised = False
649+
try:
650+
alias[key]
651+
except MemoryError:
652+
raised = True
653+
finally:
654+
_testcapi.remove_mem_hooks()
655+
656+
self.assertTrue(raised, "MemoryError not raised")
657+
631658

632659
class TypeIterationTests(unittest.TestCase):
633660
_UNITERABLE_TYPES = (list, tuple)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash in :class:`types.GenericAlias` when ``tuple_extend()`` fails
2+
during substitution of a ``TypeVarTuple`` under allocation failure.

Objects/genericaliasobject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ subs_tvars(PyObject *obj, PyObject *params,
302302
PyTuple_GET_SIZE(arg));
303303
if (j < 0) {
304304
Py_DECREF(subparams);
305-
Py_DECREF(subargs);
306305
return NULL;
307306
}
308307
continue;

0 commit comments

Comments
 (0)