@@ -96,6 +96,7 @@ static PySTEntryObject *compiler_symtable_entry(struct compiler *c);
96
96
#define IS_INTERACTIVE (C ) compiler_is_interactive(C)
97
97
#define IS_NESTED_SCOPE (C ) compiler_is_nested_scope(C)
98
98
#define SCOPE_TYPE (C ) compiler_scope_type(C)
99
+ #define QUALNAME (C ) compiler_qualname(C)
99
100
100
101
typedef _Py_SourceLocation location ;
101
102
typedef struct _PyCfgBuilder cfg_builder ;
@@ -106,6 +107,7 @@ static int compiler_optimization_level(struct compiler *c);
106
107
static int compiler_is_interactive (struct compiler * c );
107
108
static int compiler_is_nested_scope (struct compiler * c );
108
109
static int compiler_scope_type (struct compiler * c );
110
+ static PyObject * compiler_qualname (struct compiler * c );
109
111
110
112
#define LOCATION (LNO , END_LNO , COL , END_COL ) \
111
113
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
@@ -336,14 +338,14 @@ static int codegen_call_helper(struct compiler *c, location loc,
336
338
static int codegen_try_except (struct compiler * , stmt_ty );
337
339
static int codegen_try_star_except (struct compiler * , stmt_ty );
338
340
339
- static int compiler_sync_comprehension_generator (
341
+ static int codegen_sync_comprehension_generator (
340
342
struct compiler * c , location loc ,
341
343
asdl_comprehension_seq * generators , int gen_index ,
342
344
int depth ,
343
345
expr_ty elt , expr_ty val , int type ,
344
346
int iter_on_stack );
345
347
346
- static int compiler_async_comprehension_generator (
348
+ static int codegen_async_comprehension_generator (
347
349
struct compiler * c , location loc ,
348
350
asdl_comprehension_seq * generators , int gen_index ,
349
351
int depth ,
@@ -1714,6 +1716,13 @@ dict_lookup_arg(PyObject *dict, PyObject *name)
1714
1716
return PyLong_AsLong (v );
1715
1717
}
1716
1718
1719
+ static int
1720
+ compiler_lookup_cellvar (struct compiler * c , PyObject * name )
1721
+ {
1722
+ assert (c -> u -> u_metadata .u_cellvars );
1723
+ return dict_lookup_arg (c -> u -> u_metadata .u_cellvars , name );
1724
+ }
1725
+
1717
1726
static int
1718
1727
compiler_lookup_arg (struct compiler * c , PyCodeObject * co , PyObject * name )
1719
1728
{
@@ -2367,8 +2376,15 @@ codegen_set_type_params_in_class(struct compiler *c, location loc)
2367
2376
return SUCCESS ;
2368
2377
}
2369
2378
2379
+ static PyObject *
2380
+ compiler_static_attributes_tuple (struct compiler * c )
2381
+ {
2382
+ assert (c -> u -> u_static_attributes );
2383
+ return PySequence_Tuple (c -> u -> u_static_attributes );
2384
+ }
2385
+
2370
2386
static int
2371
- compiler_class_body (struct compiler * c , stmt_ty s , int firstlineno )
2387
+ codegen_class_body (struct compiler * c , stmt_ty s , int firstlineno )
2372
2388
{
2373
2389
/* ultimately generate code for:
2374
2390
<name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
@@ -2391,8 +2407,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2391
2407
RETURN_IF_ERROR_IN_SCOPE (c , compiler_nameop (c , loc , & _Py_ID (__name__ ), Load ));
2392
2408
/* ... and store it as __module__ */
2393
2409
RETURN_IF_ERROR_IN_SCOPE (c , compiler_nameop (c , loc , & _Py_ID (__module__ ), Store ));
2394
- assert (c -> u -> u_metadata .u_qualname );
2395
- ADDOP_LOAD_CONST (c , loc , c -> u -> u_metadata .u_qualname );
2410
+ ADDOP_LOAD_CONST (c , loc , QUALNAME (c ));
2396
2411
RETURN_IF_ERROR_IN_SCOPE (c , compiler_nameop (c , loc , & _Py_ID (__qualname__ ), Store ));
2397
2412
ADDOP_LOAD_CONST_NEW (c , loc , PyLong_FromLong (c -> u -> u_metadata .u_firstlineno ));
2398
2413
RETURN_IF_ERROR_IN_SCOPE (c , compiler_nameop (c , loc , & _Py_ID (__firstlineno__ ), Store ));
@@ -2410,8 +2425,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2410
2425
}
2411
2426
/* compile the body proper */
2412
2427
RETURN_IF_ERROR_IN_SCOPE (c , codegen_body (c , loc , s -> v .ClassDef .body ));
2413
- assert (c -> u -> u_static_attributes );
2414
- PyObject * static_attributes = PySequence_Tuple (c -> u -> u_static_attributes );
2428
+ PyObject * static_attributes = compiler_static_attributes_tuple (c );
2415
2429
if (static_attributes == NULL ) {
2416
2430
compiler_exit_scope (c );
2417
2431
return ERROR ;
@@ -2424,7 +2438,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2424
2438
/* Set __classdictcell__ if necessary */
2425
2439
if (SYMTABLE_ENTRY (c )-> ste_needs_classdict ) {
2426
2440
/* Store __classdictcell__ into class namespace */
2427
- int i = dict_lookup_arg ( c -> u -> u_metadata . u_cellvars , & _Py_ID (__classdict__ ));
2441
+ int i = compiler_lookup_cellvar ( c , & _Py_ID (__classdict__ ));
2428
2442
RETURN_IF_ERROR_IN_SCOPE (c , i );
2429
2443
ADDOP_I (c , NO_LOCATION , LOAD_CLOSURE , i );
2430
2444
RETURN_IF_ERROR_IN_SCOPE (
@@ -2433,7 +2447,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2433
2447
/* Return __classcell__ if it is referenced, otherwise return None */
2434
2448
if (SYMTABLE_ENTRY (c )-> ste_needs_class_closure ) {
2435
2449
/* Store __classcell__ into class namespace & return it */
2436
- int i = dict_lookup_arg ( c -> u -> u_metadata . u_cellvars , & _Py_ID (__class__ ));
2450
+ int i = compiler_lookup_cellvar ( c , & _Py_ID (__class__ ));
2437
2451
RETURN_IF_ERROR_IN_SCOPE (c , i );
2438
2452
ADDOP_I (c , NO_LOCATION , LOAD_CLOSURE , i );
2439
2453
ADDOP_I (c , NO_LOCATION , COPY , 1 );
@@ -2503,7 +2517,7 @@ codegen_class(struct compiler *c, stmt_ty s)
2503
2517
RETURN_IF_ERROR_IN_SCOPE (c , compiler_nameop (c , loc , & _Py_STR (type_params ), Store ));
2504
2518
}
2505
2519
2506
- int ret = compiler_class_body (c , s , firstlineno );
2520
+ int ret = codegen_class_body (c , s , firstlineno );
2507
2521
if (is_generic ) {
2508
2522
RETURN_IF_ERROR_IN_SCOPE (c , ret );
2509
2523
}
@@ -5107,31 +5121,31 @@ codegen_call_helper(struct compiler *c, location loc,
5107
5121
5108
5122
5109
5123
static int
5110
- compiler_comprehension_generator (struct compiler * c , location loc ,
5111
- asdl_comprehension_seq * generators , int gen_index ,
5112
- int depth ,
5113
- expr_ty elt , expr_ty val , int type ,
5114
- int iter_on_stack )
5124
+ codegen_comprehension_generator (struct compiler * c , location loc ,
5125
+ asdl_comprehension_seq * generators , int gen_index ,
5126
+ int depth ,
5127
+ expr_ty elt , expr_ty val , int type ,
5128
+ int iter_on_stack )
5115
5129
{
5116
5130
comprehension_ty gen ;
5117
5131
gen = (comprehension_ty )asdl_seq_GET (generators , gen_index );
5118
5132
if (gen -> is_async ) {
5119
- return compiler_async_comprehension_generator (
5133
+ return codegen_async_comprehension_generator (
5120
5134
c , loc , generators , gen_index , depth , elt , val , type ,
5121
5135
iter_on_stack );
5122
5136
} else {
5123
- return compiler_sync_comprehension_generator (
5137
+ return codegen_sync_comprehension_generator (
5124
5138
c , loc , generators , gen_index , depth , elt , val , type ,
5125
5139
iter_on_stack );
5126
5140
}
5127
5141
}
5128
5142
5129
5143
static int
5130
- compiler_sync_comprehension_generator (struct compiler * c , location loc ,
5131
- asdl_comprehension_seq * generators ,
5132
- int gen_index , int depth ,
5133
- expr_ty elt , expr_ty val , int type ,
5134
- int iter_on_stack )
5144
+ codegen_sync_comprehension_generator (struct compiler * c , location loc ,
5145
+ asdl_comprehension_seq * generators ,
5146
+ int gen_index , int depth ,
5147
+ expr_ty elt , expr_ty val , int type ,
5148
+ int iter_on_stack )
5135
5149
{
5136
5150
/* generate code for the iterator, then each of the ifs,
5137
5151
and then write to the element */
@@ -5145,8 +5159,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
5145
5159
5146
5160
if (!iter_on_stack ) {
5147
5161
if (gen_index == 0 ) {
5148
- /* Receive outermost iter as an implicit argument */
5149
- c -> u -> u_metadata .u_argcount = 1 ;
5162
+ assert (c -> u -> u_metadata .u_argcount == 1 );
5150
5163
ADDOP_I (c , loc , LOAD_FAST , 0 );
5151
5164
}
5152
5165
else {
@@ -5195,9 +5208,9 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
5195
5208
5196
5209
if (++ gen_index < asdl_seq_LEN (generators )) {
5197
5210
RETURN_IF_ERROR (
5198
- compiler_comprehension_generator (c , loc ,
5199
- generators , gen_index , depth ,
5200
- elt , val , type , 0 ));
5211
+ codegen_comprehension_generator (c , loc ,
5212
+ generators , gen_index , depth ,
5213
+ elt , val , type , 0 ));
5201
5214
}
5202
5215
5203
5216
location elt_loc = LOC (elt );
@@ -5252,7 +5265,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
5252
5265
}
5253
5266
5254
5267
static int
5255
- compiler_async_comprehension_generator (struct compiler * c , location loc ,
5268
+ codegen_async_comprehension_generator (struct compiler * c , location loc ,
5256
5269
asdl_comprehension_seq * generators ,
5257
5270
int gen_index , int depth ,
5258
5271
expr_ty elt , expr_ty val , int type ,
@@ -5267,8 +5280,7 @@ compiler_async_comprehension_generator(struct compiler *c, location loc,
5267
5280
5268
5281
if (!iter_on_stack ) {
5269
5282
if (gen_index == 0 ) {
5270
- /* Receive outermost iter as an implicit argument */
5271
- c -> u -> u_metadata .u_argcount = 1 ;
5283
+ assert (c -> u -> u_metadata .u_argcount == 1 );
5272
5284
ADDOP_I (c , loc , LOAD_FAST , 0 );
5273
5285
}
5274
5286
else {
@@ -5300,9 +5312,9 @@ compiler_async_comprehension_generator(struct compiler *c, location loc,
5300
5312
depth ++ ;
5301
5313
if (++ gen_index < asdl_seq_LEN (generators )) {
5302
5314
RETURN_IF_ERROR (
5303
- compiler_comprehension_generator (c , loc ,
5304
- generators , gen_index , depth ,
5305
- elt , val , type , 0 ));
5315
+ codegen_comprehension_generator (c , loc ,
5316
+ generators , gen_index , depth ,
5317
+ elt , val , type , 0 ));
5306
5318
}
5307
5319
5308
5320
location elt_loc = LOC (elt );
@@ -5622,15 +5634,15 @@ codegen_comprehension_iter(struct compiler *c, location loc,
5622
5634
}
5623
5635
5624
5636
static int
5625
- compiler_comprehension (struct compiler * c , expr_ty e , int type ,
5626
- identifier name , asdl_comprehension_seq * generators , expr_ty elt ,
5627
- expr_ty val )
5637
+ codegen_comprehension (struct compiler * c , expr_ty e , int type ,
5638
+ identifier name , asdl_comprehension_seq * generators , expr_ty elt ,
5639
+ expr_ty val )
5628
5640
{
5629
5641
PyCodeObject * co = NULL ;
5630
5642
inlined_comprehension_state inline_state = {NULL , NULL , NULL , NO_LABEL };
5631
5643
comprehension_ty outermost ;
5632
5644
#ifndef NDEBUG
5633
- int scope_type = c -> u -> u_scope_type ;
5645
+ int scope_type = SCOPE_TYPE ( c ) ;
5634
5646
int is_top_level_await = IS_TOP_LEVEL_AWAIT (c );
5635
5647
#endif
5636
5648
PySTEntryObject * entry = _PySymtable_Lookup (SYMTABLE (c ), (void * )e );
@@ -5652,8 +5664,12 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
5652
5664
}
5653
5665
}
5654
5666
else {
5667
+ /* Receive outermost iter as an implicit argument */
5668
+ _PyCompile_CodeUnitMetadata umd = {
5669
+ .u_argcount = 1 ,
5670
+ };
5655
5671
if (compiler_enter_scope (c , name , COMPILER_SCOPE_COMPREHENSION ,
5656
- (void * )e , e -> lineno , NULL , NULL ) < 0 ) {
5672
+ (void * )e , e -> lineno , NULL , & umd ) < 0 ) {
5657
5673
goto error ;
5658
5674
}
5659
5675
}
@@ -5689,8 +5705,8 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
5689
5705
}
5690
5706
}
5691
5707
5692
- if (compiler_comprehension_generator (c , loc , generators , 0 , 0 ,
5693
- elt , val , type , is_inlined ) < 0 ) {
5708
+ if (codegen_comprehension_generator (c , loc , generators , 0 , 0 ,
5709
+ elt , val , type , is_inlined ) < 0 ) {
5694
5710
goto error_in_scope ;
5695
5711
}
5696
5712
@@ -5753,29 +5769,29 @@ codegen_genexp(struct compiler *c, expr_ty e)
5753
5769
{
5754
5770
assert (e -> kind == GeneratorExp_kind );
5755
5771
_Py_DECLARE_STR (anon_genexpr , "<genexpr>" );
5756
- return compiler_comprehension (c , e , COMP_GENEXP , & _Py_STR (anon_genexpr ),
5757
- e -> v .GeneratorExp .generators ,
5758
- e -> v .GeneratorExp .elt , NULL );
5772
+ return codegen_comprehension (c , e , COMP_GENEXP , & _Py_STR (anon_genexpr ),
5773
+ e -> v .GeneratorExp .generators ,
5774
+ e -> v .GeneratorExp .elt , NULL );
5759
5775
}
5760
5776
5761
5777
static int
5762
5778
codegen_listcomp (struct compiler * c , expr_ty e )
5763
5779
{
5764
5780
assert (e -> kind == ListComp_kind );
5765
5781
_Py_DECLARE_STR (anon_listcomp , "<listcomp>" );
5766
- return compiler_comprehension (c , e , COMP_LISTCOMP , & _Py_STR (anon_listcomp ),
5767
- e -> v .ListComp .generators ,
5768
- e -> v .ListComp .elt , NULL );
5782
+ return codegen_comprehension (c , e , COMP_LISTCOMP , & _Py_STR (anon_listcomp ),
5783
+ e -> v .ListComp .generators ,
5784
+ e -> v .ListComp .elt , NULL );
5769
5785
}
5770
5786
5771
5787
static int
5772
5788
codegen_setcomp (struct compiler * c , expr_ty e )
5773
5789
{
5774
5790
assert (e -> kind == SetComp_kind );
5775
5791
_Py_DECLARE_STR (anon_setcomp , "<setcomp>" );
5776
- return compiler_comprehension (c , e , COMP_SETCOMP , & _Py_STR (anon_setcomp ),
5777
- e -> v .SetComp .generators ,
5778
- e -> v .SetComp .elt , NULL );
5792
+ return codegen_comprehension (c , e , COMP_SETCOMP , & _Py_STR (anon_setcomp ),
5793
+ e -> v .SetComp .generators ,
5794
+ e -> v .SetComp .elt , NULL );
5779
5795
}
5780
5796
5781
5797
@@ -5784,9 +5800,9 @@ codegen_dictcomp(struct compiler *c, expr_ty e)
5784
5800
{
5785
5801
assert (e -> kind == DictComp_kind );
5786
5802
_Py_DECLARE_STR (anon_dictcomp , "<dictcomp>" );
5787
- return compiler_comprehension (c , e , COMP_DICTCOMP , & _Py_STR (anon_dictcomp ),
5788
- e -> v .DictComp .generators ,
5789
- e -> v .DictComp .key , e -> v .DictComp .value );
5803
+ return codegen_comprehension (c , e , COMP_DICTCOMP , & _Py_STR (anon_dictcomp ),
5804
+ e -> v .DictComp .generators ,
5805
+ e -> v .DictComp .key , e -> v .DictComp .value );
5790
5806
}
5791
5807
5792
5808
@@ -6264,7 +6280,7 @@ codegen_check_ann_expr(struct compiler *c, expr_ty e)
6264
6280
}
6265
6281
6266
6282
static int
6267
- compiler_check_annotation (struct compiler * c , stmt_ty s )
6283
+ codegen_check_annotation (struct compiler * c , stmt_ty s )
6268
6284
{
6269
6285
/* Annotations of complex targets does not produce anything
6270
6286
under annotations future */
@@ -6273,8 +6289,8 @@ compiler_check_annotation(struct compiler *c, stmt_ty s)
6273
6289
}
6274
6290
6275
6291
/* Annotations are only evaluated in a module or class. */
6276
- if (c -> u -> u_scope_type == COMPILER_SCOPE_MODULE ||
6277
- c -> u -> u_scope_type == COMPILER_SCOPE_CLASS ) {
6292
+ if (SCOPE_TYPE ( c ) == COMPILER_SCOPE_MODULE ||
6293
+ SCOPE_TYPE ( c ) == COMPILER_SCOPE_CLASS ) {
6278
6294
return codegen_check_ann_expr (c , s -> v .AnnAssign .annotation );
6279
6295
}
6280
6296
return SUCCESS ;
@@ -6384,7 +6400,7 @@ codegen_annassign(struct compiler *c, stmt_ty s)
6384
6400
return ERROR ;
6385
6401
}
6386
6402
/* Annotation is evaluated last. */
6387
- if (future_annotations && !s -> v .AnnAssign .simple && compiler_check_annotation (c , s ) < 0 ) {
6403
+ if (future_annotations && !s -> v .AnnAssign .simple && codegen_check_annotation (c , s ) < 0 ) {
6388
6404
return ERROR ;
6389
6405
}
6390
6406
return SUCCESS ;
@@ -6604,7 +6620,7 @@ emit_and_reset_fail_pop(struct compiler *c, location loc,
6604
6620
}
6605
6621
6606
6622
static int
6607
- compiler_error_duplicate_store (struct compiler * c , location loc , identifier n )
6623
+ codegen_error_duplicate_store (struct compiler * c , location loc , identifier n )
6608
6624
{
6609
6625
return compiler_error (c , loc ,
6610
6626
"multiple assignments to name %R in pattern" , n );
@@ -6632,7 +6648,7 @@ codegen_pattern_helper_store_name(struct compiler *c, location loc,
6632
6648
int duplicate = PySequence_Contains (pc -> stores , n );
6633
6649
RETURN_IF_ERROR (duplicate );
6634
6650
if (duplicate ) {
6635
- return compiler_error_duplicate_store (c , loc , n );
6651
+ return codegen_error_duplicate_store (c , loc , n );
6636
6652
}
6637
6653
// Rotate this object underneath any items we need to preserve:
6638
6654
Py_ssize_t rotations = pc -> on_top + PyList_GET_SIZE (pc -> stores ) + 1 ;
@@ -7125,7 +7141,7 @@ codegen_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
7125
7141
goto error ;
7126
7142
}
7127
7143
if (dupe ) {
7128
- compiler_error_duplicate_store (c , LOC (p ), name );
7144
+ codegen_error_duplicate_store (c , LOC (p ), name );
7129
7145
goto error ;
7130
7146
}
7131
7147
if (PyList_Append (pc -> stores , name )) {
@@ -7436,6 +7452,13 @@ compiler_scope_type(struct compiler *c)
7436
7452
return c -> u -> u_scope_type ;
7437
7453
}
7438
7454
7455
+ static PyObject *
7456
+ compiler_qualname (struct compiler * c )
7457
+ {
7458
+ assert (c -> u -> u_metadata .u_qualname );
7459
+ return c -> u -> u_metadata .u_qualname ;
7460
+ }
7461
+
7439
7462
static int
7440
7463
compute_code_flags (struct compiler * c )
7441
7464
{
0 commit comments