From c2f4f3155d00869ec705249a0470199e57dc9ce2 Mon Sep 17 00:00:00 2001 From: pqminh27 <76590223+pqminh27@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:02:52 +0700 Subject: [PATCH] gh-162574: Mangle private names in nested generic headers --- Include/internal/pycore_symtable.h | 3 +- Lib/test/test_type_params.py | 61 +++++++++++++++++++ ...-11-26-01-49-38.gh-issue-162574.rge0FX.rst | 3 + Python/codegen.c | 2 +- Python/symtable.c | 24 ++++---- 5 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-26-01-49-38.gh-issue-162574.rge0FX.rst diff --git a/Include/internal/pycore_symtable.h b/Include/internal/pycore_symtable.h index 01afe5bb935b996..e1ace4912667744 100644 --- a/Include/internal/pycore_symtable.h +++ b/Include/internal/pycore_symtable.h @@ -98,7 +98,8 @@ typedef struct _symtable_entry { PyObject *ste_varnames; /* list of function parameters */ PyObject *ste_children; /* list of child blocks */ PyObject *ste_directives;/* locations of global and nonlocal statements */ - PyObject *ste_mangled_names; /* set of names for which mangling should be applied */ + PyObject *ste_mangled_names; /* type parameter names in a generic class header */ + PyObject *ste_type_param_mangling_class; /* class name for mangling those names */ _Py_block_ty ste_type; // Optional string set by symtable.c and used when reporting errors. diff --git a/Lib/test/test_type_params.py b/Lib/test/test_type_params.py index b6588269e1d0c65..c0ef2e6ed3b474d 100644 --- a/Lib/test/test_type_params.py +++ b/Lib/test/test_type_params.py @@ -882,6 +882,67 @@ class Derived[T](__Base, __kwarg=1): self.assertEqual(Derived.__bases__, (ns["__Base"], Generic)) self.assertEqual(Derived.kwargs, {"__kwarg": 1}) + def test_nested_generic_private_base(self): + for globals_ in ({}, {"__Base": list}): + with self.subTest(globals=globals_): + ns = run_code(""" + class Outer: + __Base = dict + class Plain(__Base): pass + class Inner[T](__Base): pass + """, globals_) + self.assertEqual(ns["Outer"].Plain.__bases__, (dict,)) + self.assertEqual(ns["Outer"].Inner.__bases__, (dict, Generic)) + + class Outer: + def make_class(self): + __Base = dict + class Inner[T](__Base): pass + return Inner + + self.assertEqual(Outer().make_class().__bases__, (dict, Generic)) + + def test_nested_generic_private_keyword_values(self): + class Meta(type): pass + + class Base: + def __init_subclass__(cls, **kwargs): + cls.kwargs = kwargs + + class Outer: + __Meta = Meta + __Value = object() + class Inner[T](Base, metaclass=__Meta, __tag=__Value): pass + + self.assertIs(type(Outer.Inner), Meta) + self.assertEqual(Outer.Inner.kwargs, {"__tag": Outer._Outer__Value}) + + def test_nested_generic_private_bound_and_default(self): + class Outer: + __Bound = int + __Default = str + class Inner[T: __Bound = __Default]: pass + class Constrained[T: (__Bound, __Default)]: pass + + T, = Outer.Inner.__type_params__ + self.assertIs(T.__bound__, int) + self.assertIs(T.__default__, str) + T, = Outer.Constrained.__type_params__ + self.assertEqual(T.__constraints__, (int, str)) + + def test_nested_generic_private_parameter_precedence(self): + class Outer: + __T = int + class Inner[__T, U: __T](list[__T]): + param = __T + + Inner = Outer.Inner + T, U = Inner.__type_params__ + self.assertEqual(T.__name__, "__T") + self.assertIs(U.__bound__, T) + self.assertEqual(Inner.__orig_bases__[0].__args__, (T,)) + self.assertIs(Inner.param, T) + def test_no_mangling_in_nested_scopes(self): ns = run_code(""" from test.test_type_params import make_base diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-26-01-49-38.gh-issue-162574.rge0FX.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-26-01-49-38.gh-issue-162574.rge0FX.rst new file mode 100644 index 000000000000000..515968cc08331a7 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-26-01-49-38.gh-issue-162574.rge0FX.rst @@ -0,0 +1,3 @@ +Private names in the header of a nested generic class are now mangled using +the enclosing class's name. Private type parameters still use the generic +class's own name. diff --git a/Python/codegen.c b/Python/codegen.c index ae60caf195f23ca..9b1f3e2ebd1de0f 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -1728,7 +1728,7 @@ codegen_class(compiler *c, stmt_ty s) return ERROR; } int ret = codegen_enter_scope(c, type_params_name, COMPILE_SCOPE_ANNOTATIONS, - (void *)type_params, firstlineno, s->v.ClassDef.name, NULL); + (void *)type_params, firstlineno, NULL, NULL); Py_DECREF(type_params_name); RETURN_IF_ERROR(ret); RETURN_IF_ERROR_IN_SCOPE(c, codegen_type_params(c, type_params)); diff --git a/Python/symtable.c b/Python/symtable.c index 429c204dba29b94..c56fb834853a4e7 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -124,6 +124,7 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block, ste->ste_directives = NULL; ste->ste_mangled_names = NULL; + ste->ste_type_param_mangling_class = NULL; ste->ste_type = block; ste->ste_scope_info = NULL; @@ -202,6 +203,7 @@ ste_dealloc(PyObject *op) Py_XDECREF(ste->ste_directives); Py_XDECREF(ste->ste_annotation_block); Py_XDECREF(ste->ste_mangled_names); + Py_XDECREF(ste->ste_type_param_mangling_class); PyObject_Free(ste); } @@ -1468,10 +1470,11 @@ symtable_enter_existing_block(struct symtable *st, PySTEntryObject* ste, bool ad if (prev) { ste->ste_comp_iter_expr = prev->ste_comp_iter_expr; } - /* No need to inherit ste_mangled_names in classes, where all names - * are mangled. */ + /* A class body starts its own private name mangling context. */ if (prev && prev->ste_mangled_names != NULL && ste->ste_type != ClassBlock) { - ste->ste_mangled_names = Py_NewRef(prev->ste_mangled_names); + Py_XSETREF(ste->ste_mangled_names, Py_NewRef(prev->ste_mangled_names)); + Py_XSETREF(ste->ste_type_param_mangling_class, + Py_NewRef(prev->ste_type_param_mangling_class)); } /* The entry is owned by the stack. Borrow it for st_cur. */ st->st_cur = ste; @@ -1981,11 +1984,12 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s) LOCATION(s))) { return 0; } - st->st_private = s->v.ClassDef.name; - st->st_cur->ste_mangled_names = PySet_New(NULL); + Py_XSETREF(st->st_cur->ste_mangled_names, PySet_New(NULL)); if (!st->st_cur->ste_mangled_names) { return 0; } + Py_XSETREF(st->st_cur->ste_type_param_mangling_class, + Py_NewRef(s->v.ClassDef.name)); VISIT_SEQ(st, type_param, s->v.ClassDef.type_params); } VISIT_SEQ(st, expr, s->v.ClassDef.bases); @@ -3292,18 +3296,16 @@ _Py_SymtableStringObjectFlags(const char *str, PyObject *filename, PyObject * _Py_MaybeMangle(PyObject *privateobj, PySTEntryObject *ste, PyObject *name) { - /* Special case for type parameter blocks around generic classes: - * we want to mangle type parameter names (so a type param with a private - * name can be used inside the class body), but we don't want to mangle - * any other names that appear within the type parameter scope. + /* A generic class header uses the enclosing class for mangling, except + * for its type parameters, which must match their uses in the class body. */ if (ste->ste_mangled_names != NULL) { int result = PySet_Contains(ste->ste_mangled_names, name); if (result < 0) { return NULL; } - if (result == 0) { - return Py_NewRef(name); + if (result) { + privateobj = ste->ste_type_param_mangling_class; } } return _Py_Mangle(privateobj, name);