@@ -80,6 +80,7 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block,
80
80
ste -> ste_children = NULL ;
81
81
ste -> ste_num_typeparam_scopes = 0 ;
82
82
ste -> ste_active_typeparam_scope = 0 ;
83
+ ste -> ste_typeparam_names = NULL ;
83
84
84
85
ste -> ste_directives = NULL ;
85
86
@@ -143,6 +144,7 @@ ste_dealloc(PySTEntryObject *ste)
143
144
Py_XDECREF (ste -> ste_varnames );
144
145
Py_XDECREF (ste -> ste_children );
145
146
Py_XDECREF (ste -> ste_directives );
147
+ Py_XDECREF (ste -> ste_typeparam_names );
146
148
PyObject_Free (ste );
147
149
}
148
150
@@ -405,6 +407,10 @@ _PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
405
407
return 0 ;
406
408
}
407
409
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
+ }
408
414
Py_DECREF (mangled );
409
415
assert (!PyErr_Occurred ());
410
416
}
@@ -653,6 +659,9 @@ update_symbols(PyObject *symbols, PyObject *scopes,
653
659
long scope , flags ;
654
660
assert (PyLong_Check (v ));
655
661
flags = PyLong_AS_LONG (v );
662
+ if (flags & DEF_TYPE_PARAM ) {
663
+ continue ;
664
+ }
656
665
v_scope = PyDict_GetItemWithError (scopes , name );
657
666
assert (v_scope && PyLong_Check (v_scope ));
658
667
scope = PyLong_AS_LONG (v_scope );
@@ -983,17 +992,23 @@ symtable_exit_block(struct symtable *st)
983
992
return 1 ;
984
993
}
985
994
986
- static void
995
+ static int
987
996
symtable_enter_typeparam_overlay (struct symtable * st )
988
997
{
989
998
struct _symtable_entry * cur = st -> st_cur ;
990
999
cur -> ste_num_typeparam_scopes += 1 ;
991
1000
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 ;
992
1006
}
993
1007
994
1008
static void
995
1009
symtable_leave_typeparam_overlay (struct symtable * st ) {
996
1010
st -> st_cur -> ste_active_typeparam_scope = 0 ;
1011
+ Py_CLEAR (st -> st_cur -> ste_typeparam_names );
997
1012
}
998
1013
999
1014
static int
@@ -1063,24 +1078,34 @@ symtable_add_typeparam(struct symtable *st, PyObject *name,
1063
1078
}
1064
1079
PyObject * dict = ste -> ste_symbols ;
1065
1080
PyObject * current ;
1066
- if ((current = PyDict_GetItemWithError (dict , name ))) {
1081
+ if ((current = PyDict_GetItemWithError (dict , mangled ))) {
1067
1082
PyErr_Format (PyExc_SyntaxError , "duplicate type parameter '%U'" , name );
1068
1083
PyErr_RangedSyntaxLocationObject (st -> st_filename ,
1069
1084
lineno , col_offset + 1 ,
1070
1085
end_lineno , end_col_offset + 1 );
1086
+ Py_DECREF (mangled );
1071
1087
return 0 ;
1072
1088
}
1073
1089
else if (PyErr_Occurred ()) {
1090
+ Py_DECREF (mangled );
1074
1091
return 0 ;
1075
1092
}
1076
- PyObject * flags = PyLong_FromLong (CELL << SCOPE_OFFSET );
1093
+ PyObject * flags = PyLong_FromLong (( CELL << SCOPE_OFFSET ) | DEF_TYPE_PARAM );
1077
1094
if (flags == NULL ) {
1095
+ Py_DECREF (mangled );
1078
1096
return 0 ;
1079
1097
}
1080
- if (PyDict_SetItem (dict , name , flags ) < 0 ) {
1098
+ if (PyDict_SetItem (dict , mangled , flags ) < 0 ) {
1099
+ Py_DECREF (mangled );
1081
1100
Py_DECREF (flags );
1082
1101
return 0 ;
1083
1102
}
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 );
1084
1109
Py_DECREF (flags );
1085
1110
return 1 ;
1086
1111
}
@@ -1094,9 +1119,23 @@ symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _s
1094
1119
long val ;
1095
1120
PyObject * mangled = _Py_Mangle (st -> st_private , name );
1096
1121
1097
-
1098
1122
if (!mangled )
1099
1123
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
+ }
1100
1139
dict = ste -> ste_symbols ;
1101
1140
if ((o = PyDict_GetItemWithError (dict , mangled ))) {
1102
1141
val = PyLong_AS_LONG (o );
@@ -1270,7 +1309,9 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
1270
1309
if (s -> v .FunctionDef .decorator_list )
1271
1310
VISIT_SEQ (st , expr , s -> v .FunctionDef .decorator_list );
1272
1311
if (s -> v .FunctionDef .typeparams ) {
1273
- symtable_enter_typeparam_overlay (st );
1312
+ if (!symtable_enter_typeparam_overlay (st )) {
1313
+ VISIT_QUIT (st , 0 );
1314
+ }
1274
1315
VISIT_SEQ (st , typeparam , s -> v .FunctionDef .typeparams );
1275
1316
}
1276
1317
if (!symtable_visit_annotations (st , s , s -> v .FunctionDef .args ,
0 commit comments