Skip to content
Closed
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
3 changes: 2 additions & 1 deletion Include/internal/pycore_symtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

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.

Why changing this comment? the naming of the field is also clearly wwrong if it's only for type params now.

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.
Expand Down
61 changes: 61 additions & 0 deletions Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
24 changes: 13 additions & 11 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));

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.

Why this change?

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);
Expand Down Expand Up @@ -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);
Expand Down
Loading