[3.13] gh-150146: Fix NULL dereference in _Py_subs_parameters (GH-150147) (#150155)

* [3.13] gh-150146: Fix NULL dereference in `_Py_subs_parameters` (GH-150147)
(cherry picked from commit f621ba16b7)

Co-authored-by: sobolevn <mail@sobolevn.me>
This commit is contained in:
sobolevn 2026-05-20 17:04:17 +03:00 committed by GitHub
parent e87baa8d24
commit 32feeff72d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 2 deletions

View file

@ -56,9 +56,8 @@
from queue import Queue, SimpleQueue
from weakref import WeakSet, ReferenceType, ref
import typing
from typing import Unpack
from typing import TypeVar, Unpack
from typing import TypeVar
T = TypeVar('T')
K = TypeVar('K')
V = TypeVar('V')
@ -537,6 +536,13 @@ def test_del_iter(self):
iter_x = iter(t)
del iter_x
def test_gh150146(self):
# It used to crash:
for container in [list, tuple]:
with self.subTest(container=container):
x = container[TypeVar("")]
with self.assertRaises(TypeError):
x[*typing.Mapping[..., ...]]
class TypeIterationTests(unittest.TestCase):
_UNITERABLE_TYPES = (list, tuple)

View file

@ -0,0 +1,5 @@
Fix a crash on a complex type variable substitution.
``from typing import TypeVar; memoryview[TypeVar("")][*typing.Mapping[...,
...]]`` used to fail due to missing ``NULL`` check on ``_unpack_args`` C
function call.

View file

@ -443,6 +443,9 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
self);
}
item = _unpack_args(item);
if (item == NULL) {
return NULL;
}
for (Py_ssize_t i = 0; i < nparams; i++) {
PyObject *param = PyTuple_GET_ITEM(parameters, i);
PyObject *prepare, *tmp;