Skip to content

Commit 3b04203

Browse files
committed
gh-162597: Mangle a private type parameter used before its definition
The symbol table added a generic class's type parameters to the set of names to mangle one at a time, as it visited them. A bound, constraint or default that referred to a later private type parameter was therefore recorded unmangled, while the compiler mangled it with the complete set, found no symbol and evaluated the bound with LOAD_NAME in a scope without locals, raising SystemError. Add all the type parameter names to the set before visiting any of them.
1 parent 0423fc6 commit 3b04203

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎Lib/test/test_type_params.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,23 @@ class Foo[__T, __U: __T](make_base(__T), make_base(lambda: __T)):
930930
self.assertIs(base1.__arg__, T)
931931
self.assertIs(base2.__arg__(), T)
932932

933+
def test_later_type_params_are_mangled(self):
934+
# gh-162597: a bound, constraint or default that refers to a later
935+
# private type parameter.
936+
ns = run_code("""
937+
__U = "global"
938+
939+
class Foo[T: __U, __U, V: (__W, int) = __W, __W = __U, **__P = [__U]]:
940+
pass
941+
""")
942+
Foo = ns["Foo"]
943+
T, U, V, W, P = Foo.__type_params__
944+
self.assertIs(T.__bound__, U)
945+
self.assertEqual(V.__constraints__, (W, int))
946+
self.assertIs(V.__default__, W)
947+
self.assertIs(W.__default__, U)
948+
self.assertEqual(P.__default__, [U])
949+
933950

934951
class TypeParamsComplexCallsTest(unittest.TestCase):
935952
def test_defaults(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a :exc:`SystemError` when a bound, constraint or default of a type
2+
parameter of a generic class refers to a later type parameter with a private
3+
name.

‎Python/symtable.c‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,12 +1664,37 @@ symtable_add_def_ctx(struct symtable *st, PyObject *name, int flag,
16641664
if ((flag & write_mask) && !check_name(st, name, loc, ctx)) {
16651665
return 0;
16661666
}
1667-
if ((flag & DEF_TYPE_PARAM) && st->st_cur->ste_mangled_names != NULL) {
1668-
if(PySet_Add(st->st_cur->ste_mangled_names, name) < 0) {
1667+
return symtable_add_def_helper(st, name, flag, st->st_cur, loc);
1668+
}
1669+
1670+
/* Add the names of a generic class's type parameters to the set of names
1671+
* to mangle before visiting any of them, so that a bound or default that
1672+
* refers to a later type parameter is mangled too. */
1673+
static int
1674+
symtable_add_mangled_type_params(struct symtable *st,
1675+
asdl_type_param_seq *type_params)
1676+
{
1677+
for (Py_ssize_t i = 0; i < asdl_seq_LEN(type_params); i++) {
1678+
type_param_ty tp = (type_param_ty)asdl_seq_GET(type_params, i);
1679+
PyObject *name;
1680+
switch (tp->kind) {
1681+
case TypeVar_kind:
1682+
name = tp->v.TypeVar.name;
1683+
break;
1684+
case TypeVarTuple_kind:
1685+
name = tp->v.TypeVarTuple.name;
1686+
break;
1687+
case ParamSpec_kind:
1688+
name = tp->v.ParamSpec.name;
1689+
break;
1690+
default:
1691+
Py_UNREACHABLE();
1692+
}
1693+
if (PySet_Add(st->st_cur->ste_mangled_names, name) < 0) {
16691694
return 0;
16701695
}
16711696
}
1672-
return symtable_add_def_helper(st, name, flag, st->st_cur, loc);
1697+
return 1;
16731698
}
16741699

16751700
static int
@@ -1986,6 +2011,9 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
19862011
if (!st->st_cur->ste_mangled_names) {
19872012
return 0;
19882013
}
2014+
if (!symtable_add_mangled_type_params(st, s->v.ClassDef.type_params)) {
2015+
return 0;
2016+
}
19892017
VISIT_SEQ(st, type_param, s->v.ClassDef.type_params);
19902018
}
19912019
VISIT_SEQ(st, expr, s->v.ClassDef.bases);

0 commit comments

Comments
 (0)