Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 `*`
Expand Down Expand Up @@ -97,6 +100,7 @@
tuple[*tuple[Unpack[tuple[int, ...]]]],
]

_testcapi = import_helper.import_module("_testcapi")

class BaseTest(unittest.TestCase):
"""Test basics."""
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in :class:`types.GenericAlias` when ``tuple_extend()`` fails
during substitution of a ``TypeVarTuple`` under allocation failure.
1 change: 0 additions & 1 deletion Objects/genericaliasobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading