8000 correctly make it a cellvar · python/cpython@aa8ea68 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa8ea68

Browse files
committed
correctly make it a cellvar
1 parent 9c7a153 commit aa8ea68

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

Include/internal/pycore_symtable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ typedef struct _symtable_entry {
5151
PyObject *ste_directives;/* locations of global and nonlocal statements */
5252
int ste_active_typeparam_scope; /* 0 if no active typeparam scope */
5353
int ste_num_typeparam_scopes; /* number of typeparam scopes encountered */
54+
PyObject *ste_typeparam_names; /* set of typeparam names */
5455
_Py_block_ty ste_type; /* module, class, function or annotation */
5556
int ste_nested; /* true if block is nested */
5657
unsigned ste_free : 1; /* true if block has free variables */
@@ -106,6 +107,7 @@ extern PyObject* _Py_Mangle(PyObject *p, PyObject *name);
106107
#define DEF_IMPORT 2<<6 /* assignment occurred via import */
107108
#define DEF_ANNOT 2<<7 /* this name is annotated */
108109
#define DEF_COMP_ITER 2<<8 /* this name is a comprehension iteration variable */
110+
#define DEF_TYPE_PARAM 2<<9 /* this name is a type parameter */
109111

110112
#define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
111113

Python/compile.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,26 +1274,14 @@ compiler_enter_scope(struct compiler *c, identifier name,
12741274
compiler_unit_free(u);
12751275
return ERROR;
12761276
}
1277+
u->u_ste->ste_num_typeparam_scopes = 0;
12771278
u->u_name = Py_Ne 8000 wRef(name);
12781279
u->u_varnames = list2dict(u->u_ste->ste_varnames);
12791280
u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
12801281
if (!u->u_varnames || !u->u_cellvars) {
12811282
compiler_unit_free(u);
12821283
return ERROR;
12831284
}
1284-
// if (u->u_ste->ste_parent_typeparam_overlay) {
1285-
// PySTEntryObject *ste = u->u_ste->ste_parent_typeparam_overlay;
1286-
// Py_ssize_t size = PyDict_Size(u->u_cellvars);
1287-
// Py_ssize_t pos = 0;
1288-
// while (PyDict_Next(ste->ste_symbols, &pos, &name, NULL)) {
1289-
// PyObject *val = PyLong_FromLong(size);
1290-
// size++;
1291-
// if (PyDict_SetItem(u->u_cellvars, name, val) < 0) {
1292-
// compiler_unit_free(u);
1293-
// return ERROR;
1294-
// }
1295-
// }
1296-
// }
12971285
if (u->u_ste->ste_needs_class_closure) {
12981286
/* Cook up an implicit __class__ cell. */
12991287
int res;

Python/symtable.c

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block,
8080
ste->ste_children = NULL;
8181
ste->ste_num_typeparam_scopes = 0;
8282
ste->ste_active_typeparam_scope = 0;
83+
ste->ste_typeparam_names = NULL;
8384

8485
ste->ste_directives = NULL;
8586

@@ -143,6 +144,7 @@ ste_dealloc(PySTEntryObject *ste)
143144
Py_XDECREF(ste->ste_varnames);
144145
Py_XDECREF(ste->ste_children);
145146
Py_XDECREF(ste->ste_directives);
147+
Py_XDECREF(ste->ste_typeparam_names);
146148
PyObject_Free(ste);
147149
}
148150

@@ -405,6 +407,10 @@ _PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
405407
return 0;
406408
}
407409
v = PyDict_GetItemWithError(ste->ste_symbols, mangled);
410+
if (v) {
411+
printf("Look for mangled %s in %p: %p %d %d\n", PyUnicode_AsUTF8(mangled), ste->ste_symbols, v,
412+
PyLong_AS_LONG(v), (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK);
413+
}
408414
Py_DECREF(mangled);
409415
assert(!PyErr_Occurred());
410416
}
@@ -653,6 +659,9 @@ update_symbols(PyObject *symbols, PyObject *scopes,
653659
long scope, flags;
654660
assert(PyLong_Check(v));
655661
flags = PyLong_AS_LONG(v);
662+
if (flags & DEF_TYPE_PARAM) {
663+
continue;
664+
}
656665
v_scope = PyDict_GetItemWithError(scopes, name);
657666
assert(v_scope && PyLong_Check(v_scope));
658667
scope = PyLong_AS_LONG(v_scope);
@@ -983,17 +992,23 @@ symtable_exit_block(struct symtable *st)
983992
return 1;
984993
}
985994

986-
static void
995+
static int
987996
symtable_enter_typeparam_overlay(struct symtable *st)
988997
{
989998
struct _symtable_entry *cur = st->st_cur;
990999
cur->ste_num_typeparam_scopes += 1;
9911000
cur->ste_active_typeparam_scope = cur->ste_num_typeparam_scopes;
1001+
cur->ste_typeparam_names = PySet_New(NULL);
1002+
if (cur->ste_typeparam_names == NULL) {
1003+
return 0;
1004+
}
1005+
return 1;
9921006
}
9931007

9941008
static void
9951009
symtable_leave_typeparam_overlay(struct symtable *st) {
9961010
st->st_cur->ste_active_typeparam_scope = 0;
1011+
Py_CLEAR(st->st_cur->ste_typeparam_names);
9971012
}
9981013

9991014
static int
@@ -1063,24 +1078,34 @@ symtable_add_typeparam(struct symtable *st, PyObject *name,
10631078
}
10641079
PyObject *dict = ste->ste_symbols;
10651080
PyObject *current;
1066-
if ((current = PyDict_GetItemWithError(dict, name))) {
1081+
if ((current = PyDict_GetItemWithError(dict, mangled))) {
10671082
PyErr_Format(PyExc_SyntaxError, "duplicate type parameter '%U'", name);
10681083
PyErr_RangedSyntaxLocationObject(st->st_filename,
10691084
lineno, col_offset + 1,
10701085
end_lineno, end_col_offset + 1);
1086+
Py_DECREF(mangled);
10711087
return 0;
10721088
}
10731089
else if (PyErr_Occurred()) {
1090+
Py_DECREF(mangled);
10741091
return 0;
10751092
}
1076-
PyObject *flags = PyLong_FromLong(CELL << SCOPE_OFFSET);
1093+
PyObject *flags = PyLong_FromLong((CELL << SCOPE_OFFSET) | DEF_TYPE_PARAM);
10771094
if (flags == NULL) {
1095+
Py_DECREF(mangled);
10781096
return 0;
10791097
}
1080-
if (PyDict_SetItem(dict, name, flags) < 0) {
1098+
if (PyDict_SetItem(dict, mangled, flags) < 0) {
1099+
Py_DECREF(mangled);
10811100
Py_DECREF(flags);
10821101
return 0;
10831102
}
1103+
if (PySet_Add(ste->ste_typeparam_names, name) < 0) {
1104+
Py_DECREF(mangled);
1105+
Py_DECREF(flags);
1106+
return 0;
1107+
}
1108+
Py_DECREF(mangled);
10841109
Py_DECREF(flags);
10851110
return 1;
10861111
}
@@ -1094,9 +1119,23 @@ symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _s
10941119
long val;
10951120
PyObject *mangled = _Py_Mangle(st->st_private, name);
10961121

1097-
10981122
if (!mangled)
10991123
return 0;
1124+
if (ste->ste_active_typeparam_scope != 0) {
1125+
int result = PySet_Contains(ste->ste_typeparam_names, name);
1126+
if (result == -1) {
1127+
goto error;
1128+
}
1129+
if (result) {
1130+
PyObject *new_mangled = _PyST_MangleTypeParam(ste, name);
1131+
if (new_mangled == NULL) {
1132+
goto error;
1133+
}
1134+
printf("Mangled %s to %s\n", PyUnicode_AsUTF8(name), PyUnicode_AsUTF8(new_mangled));
1135+
flag = CELL << SCOPE_OFFSET;
1136+
Py_SETREF(mangled, new_mangled);
1137+
}
1138+
}
11001139
dict = ste->ste_symbols;
11011140
if ((o = PyDict_GetItemWithError(dict, mangled))) {
11021141
val = PyLong_AS_LONG(o);
@@ -1270,7 +1309,9 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
12701309
if (s->v.FunctionDef.decorator_list)
12711310
VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
12721311
if (s->v.FunctionDef.typeparams) {
1273-
symtable_enter_typeparam_overlay(st);
1312+
if (!symtable_enter_typeparam_overlay(st)) {
1313+
VISIT_QUIT(st, 0);
1314+
}
12741315
VISIT_SEQ(st, typeparam, s->v.FunctionDef.typeparams);
12751316
}
12761317
if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,

0 commit comments

Comments
 (0)
0