74
74
typedef _Py_SourceLocation location ;
75
75
typedef struct _PyCfgBuilder cfg_builder ;
76
76
77
+ struct compiler ;
78
+
79
+ static PyObject * compiler_maybe_mangle (struct compiler * c , PyObject * name );
80
+
77
81
#define LOCATION (LNO , END_LNO , COL , END_COL ) \
78
82
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
79
83
@@ -887,10 +891,10 @@ compiler_addop_o(struct compiler_unit *u, location loc,
887
891
#define LOAD_ZERO_SUPER_METHOD -4
888
892
889
893
static int
890
- compiler_addop_name (struct compiler_unit * u , location loc ,
894
+ compiler_addop_name (struct compiler * c , location loc ,
891
895
int opcode , PyObject * dict , PyObject * o )
892
896
{
893
- PyObject * mangled = _Py_MaybeMangle ( u -> u_private , u -> u_ste , o );
897
+ PyObject * mangled = compiler_maybe_mangle ( c , o );
894
898
if (!mangled ) {
895
899
return ERROR ;
896
900
}
@@ -925,7 +929,7 @@ compiler_addop_name(struct compiler_unit *u, location loc,
925
929
arg <<= 2 ;
926
930
arg |= 1 ;
927
931
}
928
- return codegen_addop_i (u -> u_instr_sequence , opcode , arg , loc );
932
+ return codegen_addop_i (INSTR_SEQUENCE ( c ) , opcode , arg , loc );
929
933
}
930
934
931
935
/* Add an opcode with an integer argument */
@@ -993,7 +997,7 @@ codegen_addop_j(instr_sequence *seq, location loc,
993
997
}
994
998
995
999
#define ADDOP_NAME (C , LOC , OP , O , TYPE ) \
996
- RETURN_IF_ERROR(compiler_addop_name((C)->u , (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)))
1000
+ RETURN_IF_ERROR(compiler_addop_name((C), (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)))
997
1001
998
1002
#define ADDOP_I (C , LOC , OP , O ) \
999
1003
RETURN_IF_ERROR(codegen_addop_i(INSTR_SEQUENCE(C), (OP), (O), (LOC)))
@@ -1052,8 +1056,8 @@ codegen_addop_j(instr_sequence *seq, location loc,
1052
1056
1053
1057
1054
1058
static int
1055
- compiler_enter_scope (struct compiler * c , identifier name ,
1056
- int scope_type , void * key , int lineno )
1059
+ compiler_enter_scope (struct compiler * c , identifier name , int scope_type ,
1060
+ void * key , int lineno , PyObject * private )
1057
1061
{
1058
1062
location loc = LOCATION (lineno , lineno , 0 , 0 );
1059
1063
@@ -1132,7 +1136,6 @@ compiler_enter_scope(struct compiler *c, identifier name,
1132
1136
return ERROR ;
1133
1137
}
1134
1138
1135
- u -> u_private = NULL ;
1136
1139
u -> u_deferred_annotations = NULL ;
1137
1140
if (scope_type == COMPILER_SCOPE_CLASS ) {
1138
1141
u -> u_static_attributes = PySet_New (0 );
@@ -1146,6 +1149,10 @@ compiler_enter_scope(struct compiler *c, identifier name,
1146
1149
}
1147
1150
1148
1151
u -> u_instr_sequence = (instr_sequence * )_PyInstructionSequence_New ();
1152
+ if (!u -> u_instr_sequence ) {
1153
+ compiler_unit_free (u );
1154
+ return ERROR ;
1155
+ }
1149
1156
1150
1157
/* Push the old compiler_unit on the stack. */
1151
1158
if (c -> u ) {
@@ -1156,8 +1163,13 @@ compiler_enter_scope(struct compiler *c, identifier name,
1156
1163
return ERROR ;
1157
1164
}
1158
1165
Py_DECREF (capsule );
1159
- u -> u_private = Py_XNewRef (c -> u -> u_private );
1166
+ if (private == NULL ) {
1167
+ private = c -> u -> u_private ;
1168
+ }
1160
1169
}
1170
+
1171
+ u -> u_private = Py_XNewRef (private );
1172
+
1161
1173
c -> u = u ;
1162
1174
1163
1175
c -> c_nestlevel ++ ;
@@ -1436,7 +1448,7 @@ compiler_setup_annotations_scope(struct compiler *c, location loc,
1436
1448
void * key , PyObject * name )
1437
1449
{
1438
1450
if (compiler_enter_scope (c , name , COMPILER_SCOPE_ANNOTATIONS ,
1439
- key , loc .lineno ) == -1 ) {
1451
+ key , loc .lineno , NULL ) == -1 ) {
1440
1452
return ERROR ;
1441
1453
}
1442
1454
c -> u -> u_metadata .u_posonlyargcount = 1 ;
@@ -1597,7 +1609,7 @@ compiler_enter_anonymous_scope(struct compiler* c, mod_ty mod)
1597
1609
_Py_DECLARE_STR (anon_module , "<module>" );
1598
1610
RETURN_IF_ERROR (
1599
1611
compiler_enter_scope (c , & _Py_STR (anon_module ), COMPILER_SCOPE_MODULE ,
1600
- mod , 1 ));
1612
+ mod , 1 , NULL ));
1601
1613
return SUCCESS ;
1602
1614
}
1603
1615
@@ -1770,7 +1782,7 @@ compiler_kwonlydefaults(struct compiler *c, location loc,
1770
1782
arg_ty arg = asdl_seq_GET (kwonlyargs , i );
1771
1783
expr_ty default_ = asdl_seq_GET (kw_defaults , i );
1772
1784
if (default_ ) {
1773
- PyObject * mangled = _Py_MaybeMangle ( c -> u -> u_private , c -> u -> u_ste , arg -> arg );
1785
+ PyObject * mangled = compiler_maybe_mangle ( c , arg -> arg );
1774
1786
if (!mangled ) {
1775
1787
goto error ;
1776
1788
}
@@ -1827,7 +1839,7 @@ compiler_argannotation(struct compiler *c, identifier id,
1827
1839
if (!annotation ) {
1828
1840
return SUCCESS ;
1829
1841
}
1830
- PyObject * mangled = _Py_MaybeMangle ( c -> u -> u_private , c -> u -> u_ste , id );
1842
+ PyObject * mangled = compiler_maybe_mangle ( c , id );
1831
1843
if (!mangled ) {
1832
1844
return ERROR ;
1833
1845
}
@@ -2052,7 +2064,7 @@ compiler_type_param_bound_or_default(struct compiler *c, expr_ty e,
2052
2064
bool allow_starred )
2053
2065
{
2054
2066
if (compiler_enter_scope (c , name , COMPILER_SCOPE_ANNOTATIONS ,
2055
- key , e -> lineno ) == -1 ) {
2067
+ key , e -> lineno , NULL ) == -1 ) {
2056
2068
return ERROR ;
2057
2069
}
2058
2070
if (allow_starred && e -> kind == Starred_kind ) {
@@ -2197,7 +2209,7 @@ compiler_function_body(struct compiler *c, stmt_ty s, int is_async, Py_ssize_t f
2197
2209
}
2198
2210
2199
2211
RETURN_IF_ERROR (
2200
- compiler_enter_scope (c , name , scope_type , (void * )s , firstlineno ));
2212
+ compiler_enter_scope (c , name , scope_type , (void * )s , firstlineno , NULL ));
2201
2213
2202
2214
Py_ssize_t first_instr = 0 ;
2203
2215
PyObject * docstring = _PyAST_GetDocString (body );
@@ -2324,7 +2336,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
2324
2336
return ERROR ;
2325
2337
}
2326
2338
if (compiler_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
2327
- (void * )type_params , firstlineno ) == -1 ) {
2339
+ (void * )type_params , firstlineno , NULL ) == -1 ) {
2328
2340
Py_DECREF (type_params_name );
2329
2341
return ERROR ;
2330
2342
}
@@ -2407,12 +2419,10 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2407
2419
2408
2420
/* 1. compile the class body into a code object */
2409
2421
RETURN_IF_ERROR (
2410
- compiler_enter_scope (c , s -> v .ClassDef .name ,
2411
- COMPILER_SCOPE_CLASS , (void * )s , firstlineno ));
2422
+ compiler_enter_scope (c , s -> v .ClassDef .name , COMPILER_SCOPE_CLASS ,
2423
+ (void * )s , firstlineno , s -> v . ClassDef . name ));
2412
2424
2413
2425
location loc = LOCATION (firstlineno , firstlineno , 0 , 0 );
2414
- /* use the class name for name mangling */
2415
- Py_XSETREF (c -> u -> u_private , Py_NewRef (s -> v .ClassDef .name ));
2416
2426
/* load (global) __name__ ... */
2417
2427
if (compiler_nameop (c , loc , & _Py_ID (__name__ ), Load ) < 0 ) {
2418
2428
compiler_exit_scope (c );
@@ -2558,12 +2568,11 @@ compiler_class(struct compiler *c, stmt_ty s)
2558
2568
return ERROR ;
2559
2569
}
2560
2570
if (compiler_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
2561
- (void * )type_params , firstlineno ) == -1 ) {
2571
+ (void * )type_params , firstlineno , s -> v . ClassDef . name ) == -1 ) {
2562
2572
Py_DECREF (type_params_name );
2563
2573
return ERROR ;
2564
2574
}
2565
2575
Py_DECREF (type_params_name );
2566
- Py_XSETREF (c -> u -> u_private , Py_NewRef (s -> v .ClassDef .name ));
2567
2576
RETURN_IF_ERROR_IN_SCOPE (c , compiler_type_params (c , type_params ));
2568
2577
_Py_DECLARE_STR (type_params , ".type_params" );
2569
2578
RETURN_IF_ERROR_IN_SCOPE (c , compiler_nameop (c , loc , & _Py_STR (type_params ), Store ));
@@ -2643,7 +2652,7 @@ compiler_typealias_body(struct compiler *c, stmt_ty s)
2643
2652
location loc = LOC (s );
2644
2653
PyObject * name = s -> v .TypeAlias .name -> v .Name .id ;
2645
2654
RETURN_IF_ERROR (
2646
- compiler_enter_scope (c , name , COMPILER_SCOPE_FUNCTION , s , loc .lineno ));
2655
+ compiler_enter_scope (c , name , COMPILER_SCOPE_FUNCTION , s , loc .lineno , NULL ));
2647
2656
/* Make None the first constant, so the evaluate function can't have a
2648
2657
docstring. */
2649
2658
RETURN_IF_ERROR (compiler_add_const (c -> c_const_cache , c -> u , Py_None ));
@@ -2678,7 +2687,7 @@ compiler_typealias(struct compiler *c, stmt_ty s)
2678
2687
return ERROR ;
2679
2688
}
2680
2689
if (compiler_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
2681
- (void * )type_params , loc .lineno ) == -1 ) {
2690
+ (void * )type_params , loc .lineno , NULL ) == -1 ) {
2682
2691
Py_DECREF (type_params_name );
2683
2692
return ERROR ;
2684
2693
}
@@ -2947,7 +2956,7 @@ compiler_lambda(struct compiler *c, expr_ty e)
2947
2956
_Py_DECLARE_STR (anon_lambda , "<lambda>" );
2948
2957
RETURN_IF_ERROR (
2949
2958
compiler_enter_scope (c , & _Py_STR (anon_lambda ), COMPILER_SCOPE_LAMBDA ,
2950
- (void * )e , e -> lineno ));
2959
+ (void * )e , e -> lineno , NULL ));
2951
2960
2952
2961
/* Make None the first constant, so the lambda can't have a
2953
2962
docstring. */
@@ -4115,7 +4124,7 @@ compiler_nameop(struct compiler *c, location loc,
4115
4124
return ERROR ;
4116
4125
}
4117
4126
4118
- mangled = _Py_MaybeMangle ( c -> u -> u_private , c -> u -> u_ste , name );
4127
+ mangled = compiler_maybe_mangle ( c , name );
4119
4128
if (!mangled ) {
4120
4129
return ERROR ;
4121
4130
}
@@ -5712,7 +5721,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
5712
5721
}
5713
5722
else {
5714
5723
if (compiler_enter_scope (c , name , COMPILER_SCOPE_COMPREHENSION ,
5715
- (void * )e , e -> lineno ) < 0 )
5724
+ (void * )e , e -> lineno , NULL ) < 0 )
5716
5725
{
5717
5726
goto error ;
5718
5727
}
@@ -6416,7 +6425,7 @@ compiler_annassign(struct compiler *c, stmt_ty s)
6416
6425
if (future_annotations ) {
6417
6426
VISIT (c , annexpr , s -> v .AnnAssign .annotation );
6418
6427
ADDOP_NAME (c , loc , LOAD_NAME , & _Py_ID (__annotations__ ), names );
6419
- mangled = _Py_MaybeMangle ( c -> u -> u_private , c -> u -> u_ste , targ -> v .Name .id );
6428
+ mangled = compiler_maybe_mangle ( c , targ -> v .Name .id );
6420
6429
ADDOP_LOAD_CONST_NEW (c , loc , mangled );
6421
6430
ADDOP (c , loc , STORE_SUBSCR );
6422
6431
}
@@ -7458,6 +7467,12 @@ consts_dict_keys_inorder(PyObject *dict)
7458
7467
return consts ;
7459
7468
}
7460
7469
7470
+ static PyObject *
7471
+ compiler_maybe_mangle (struct compiler * c , PyObject * name )
7472
+ {
7473
+ return _Py_MaybeMangle (c -> u -> u_private , c -> u -> u_ste , name );
7474
+ }
7475
+
7461
7476
static int
7462
7477
compute_code_flags (struct compiler * c )
7463
7478
{
0 commit comments