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
28 changes: 28 additions & 0 deletions Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,34 @@ class Foo[__T, __U: __T](make_base(__T), make_base(lambda: __T)):
self.assertIs(base1.__arg__, T)
self.assertIs(base2.__arg__(), T)

def test_later_type_params_are_mangled(self):
# gh-162597: a bound, constraint or default that refers to a later
# private type parameter.
ns = run_code("""
class C[T: __U, __U]:
pass
class D[__U, T: __U]:
pass
""")
T, U = ns["C"].__type_params__
self.assertIs(T.__bound__, U)
U, T = ns["D"].__type_params__
self.assertIs(T.__bound__, U)

ns = run_code("""
__U = "global"

class Foo[T: __U, __U, V: (__W, int) = __W, __W = __U, **__P = [__U]]:
pass
""")
Foo = ns["Foo"]
T, U, V, W, P = Foo.__type_params__
self.assertIs(T.__bound__, U)
self.assertEqual(V.__constraints__, (W, int))
self.assertIs(V.__default__, W)
self.assertIs(W.__default__, U)
self.assertEqual(P.__default__, [U])


class TypeParamsComplexCallsTest(unittest.TestCase):
def test_defaults(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a :exc:`SystemError` when a bound, constraint or default of a type
parameter of a generic class refers to a later type parameter with a private
name.
34 changes: 31 additions & 3 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1664,12 +1664,37 @@ symtable_add_def_ctx(struct symtable *st, PyObject *name, int flag,
if ((flag & write_mask) && !check_name(st, name, loc, ctx)) {
return 0;
}
if ((flag & DEF_TYPE_PARAM) && st->st_cur->ste_mangled_names != NULL) {
if(PySet_Add(st->st_cur->ste_mangled_names, name) < 0) {
return symtable_add_def_helper(st, name, flag, st->st_cur, loc);
}

/* Add the names of a generic class's type parameters to the set of names
* to mangle before visiting any of them, so that a bound or default that
* refers to a later type parameter is mangled too. */
static int
symtable_add_mangled_type_params(struct symtable *st,
asdl_type_param_seq *type_params)
{
for (Py_ssize_t i = 0; i < asdl_seq_LEN(type_params); i++) {
type_param_ty tp = (type_param_ty)asdl_seq_GET(type_params, i);
PyObject *name;
switch (tp->kind) {
case TypeVar_kind:
name = tp->v.TypeVar.name;
break;
case TypeVarTuple_kind:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not tested.

name = tp->v.TypeVarTuple.name;
break;
case ParamSpec_kind:
name = tp->v.ParamSpec.name;
break;
default:
Py_UNREACHABLE();
}
if (PySet_Add(st->st_cur->ste_mangled_names, name) < 0) {
return 0;
}
}
return symtable_add_def_helper(st, name, flag, st->st_cur, loc);
return 1;
}

static int
Expand Down Expand Up @@ -1986,6 +2011,9 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
if (!st->st_cur->ste_mangled_names) {
return 0;
}
if (!symtable_add_mangled_type_params(st, s->v.ClassDef.type_params)) {
return 0;
}
VISIT_SEQ(st, type_param, s->v.ClassDef.type_params);
}
VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Expand Down
Loading