@@ -95,6 +95,7 @@ static PySTEntryObject *compiler_symtable_entry(struct compiler *c);
95
95
#define OPTIMIZATION_LEVEL (C ) compiler_optimization_level(C)
96
96
#define IS_INTERACTIVE (C ) compiler_is_interactive(C)
97
97
#define IS_NESTED_SCOPE (C ) compiler_is_nested_scope(C)
98
+ #define SCOPE_TYPE (C ) compiler_scope_type(C)
98
99
99
100
typedef _Py_SourceLocation location ;
100
101
typedef struct _PyCfgBuilder cfg_builder ;
@@ -104,6 +105,7 @@ static PyObject *compiler_maybe_mangle(struct compiler *c, PyObject *name);
104
105
static int compiler_optimization_level (struct compiler * c );
105
106
static int compiler_is_interactive (struct compiler * c );
106
107
static int compiler_is_nested_scope (struct compiler * c );
108
+ static int compiler_scope_type (struct compiler * c );
107
109
108
110
#define LOCATION (LNO , END_LNO , COL , END_COL ) \
109
111
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
@@ -314,7 +316,7 @@ static int compiler_visit_stmt(struct compiler *, stmt_ty);
314
316
static int compiler_visit_keyword (struct compiler * , keyword_ty );
315
317
static int compiler_visit_expr (struct compiler * , expr_ty );
316
318
static int codegen_augassign (struct compiler * , stmt_ty );
317
- static int compiler_annassign (struct compiler * , stmt_ty );
319
+ static int codegen_annassign (struct compiler * , stmt_ty );
318
320
static int codegen_subscript (struct compiler * , expr_ty );
319
321
static int codegen_slice (struct compiler * , expr_ty );
320
322
@@ -1481,7 +1483,7 @@ codegen_setup_annotations_scope(struct compiler *c, location loc,
1481
1483
ADDOP_I (c , loc , LOAD_COMMON_CONSTANT , CONSTANT_NOTIMPLEMENTEDERROR );
1482
1484
ADDOP_I (c , loc , RAISE_VARARGS , 1 );
1483
1485
USE_LABEL (c , body );
1484
- return 0 ;
1486
+ return SUCCESS ;
1485
1487
}
1486
1488
1487
1489
static int
@@ -1501,11 +1503,69 @@ codegen_leave_annotations_scope(struct compiler *c, location loc,
1501
1503
return SUCCESS ;
1502
1504
}
1503
1505
1506
+ static PyObject *
1507
+ compiler_deferred_annotations (struct compiler * c )
1508
+ {
1509
+ return c -> u -> u_deferred_annotations ;
1510
+ }
1511
+
1512
+ static int
1513
+ codegen_process_deferred_annotations (struct compiler * c , location loc )
1514
+ {
1515
+ PyObject * deferred_anno = compiler_deferred_annotations (c );
1516
+ if (deferred_anno == NULL ) {
1517
+ return SUCCESS ;
1518
+ }
1519
+ Py_INCREF (deferred_anno );
1520
+
1521
+ // It's possible that ste_annotations_block is set but
1522
+ // u_deferred_annotations is not, because the former is still
1523
+ // set if there are only non-simple annotations (i.e., annotations
1524
+ // for attributes, subscripts, or parenthesized names). However, the
1525
+ // reverse should not be possible.
1526
+ PySTEntryObject * ste = SYMTABLE_ENTRY (c );
1527
+ assert (ste -> ste_annotation_block != NULL );
1528
+ void * key = (void * )((uintptr_t )ste -> ste_id + 1 );
1529
+ if (codegen_setup_annotations_scope (c , loc , key ,
1530
+ ste -> ste_annotation_block -> ste_name ) < 0 ) {
1531
+ Py_DECREF (deferred_anno );
1532
+ return ERROR ;
1533
+ }
1534
+ Py_ssize_t annotations_len = PyList_Size (deferred_anno );
1535
+ for (Py_ssize_t i = 0 ; i < annotations_len ; i ++ ) {
1536
+ PyObject * ptr = PyList_GET_ITEM (deferred_anno , i );
1537
+ stmt_ty st = (stmt_ty )PyLong_AsVoidPtr (ptr );
1538
+ if (st == NULL ) {
1539
+ compiler_exit_scope (c );
1540
+ Py_DECREF (deferred_anno );
1541
+ return ERROR ;
1542
+ }
1543
+ PyObject * mangled = compiler_mangle (c , st -> v .AnnAssign .target -> v .Name .id );
1544
+ if (!mangled ) {
1545
+ compiler_exit_scope (c );
1546
+ Py_DECREF (deferred_anno );
1547
+ return ERROR ;
1548
+ }
1549
+ ADDOP_LOAD_CONST_NEW (c , LOC (st ), mangled );
1550
+ VISIT (c , expr , st -> v .AnnAssign .annotation );
1551
+ }
1552
+ Py_DECREF (deferred_anno );
1553
+
1554
+ RETURN_IF_ERROR (
1555
+ codegen_leave_annotations_scope (c , loc , annotations_len )
1556
+ );
1557
+ RETURN_IF_ERROR (
1558
+ compiler_nameop (c , loc , & _Py_ID (__annotate__ ), Store )
1559
+ );
1560
+
1561
+ return SUCCESS ;
1562
+ }
1563
+
1504
1564
/* Compile a sequence of statements, checking for a docstring
1505
1565
and for annotations. */
1506
1566
1507
1567
static int
1508
- compiler_body (struct compiler * c , location loc , asdl_stmt_seq * stmts )
1568
+ codegen_body (struct compiler * c , location loc , asdl_stmt_seq * stmts )
1509
1569
{
1510
1570
/* If from __future__ import annotations is active,
1511
1571
* every annotated class and module should have __annotations__.
@@ -1542,44 +1602,8 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
1542
1602
// If there are annotations and the future import is not on, we
1543
1603
// collect the annotations in a separate pass and generate an
1544
1604
// __annotate__ function. See PEP 649.
1545
- if (!(FUTURE_FEATURES (c ) & CO_FUTURE_ANNOTATIONS ) &&
1546
- c -> u -> u_deferred_annotations != NULL ) {
1547
-
1548
- // It's possible that ste_annotations_block is set but
1549
- // u_deferred_annotations is not, because the former is still
1550
- // set if there are only non-simple annotations (i.e., annotations
1551
- // for attributes, subscripts, or parenthesized names). However, the
1552
- // reverse should not be possible.
1553
- PySTEntryObject * ste = SYMTABLE_ENTRY (c );
1554
- assert (ste -> ste_annotation_block != NULL );
1555
- PyObject * deferred_anno = Py_NewRef (c -> u -> u_deferred_annotations );
1556
- void * key = (void * )((uintptr_t )ste -> ste_id + 1 );
1557
- if (codegen_setup_annotations_scope (c , loc , key ,
1558
- ste -> ste_annotation_block -> ste_name ) == -1 ) {
1559
- Py_DECREF (deferred_anno );
1560
- return ERROR ;
1561
- }
1562
- Py_ssize_t annotations_len = PyList_Size (deferred_anno );
1563
- for (Py_ssize_t i = 0 ; i < annotations_len ; i ++ ) {
1564
- PyObject * ptr = PyList_GET_ITEM (deferred_anno , i );
1565
- stmt_ty st = (stmt_ty )PyLong_AsVoidPtr (ptr );
1566
- if (st == NULL ) {
1567
- compiler_exit_scope (c );
1568
- Py_DECREF (deferred_anno );
1569
- return ERROR ;
1570
- }
1571
- PyObject * mangled = compiler_mangle (c , st -> v .AnnAssign .target -> v .Name .id );
1572
- ADDOP_LOAD_CONST_NEW (c , LOC (st ), mangled );
1573
- VISIT (c , expr , st -> v .AnnAssign .annotation );
1574
- }
1575
- Py_DECREF (deferred_anno );
1576
-
1577
- RETURN_IF_ERROR (
1578
- codegen_leave_annotations_scope (c , loc , annotations_len )
1579
- );
1580
- RETURN_IF_ERROR (
1581
- compiler_nameop (c , loc , & _Py_ID (__annotate__ ), Store )
1582
- );
1605
+ if (!(FUTURE_FEATURES (c ) & CO_FUTURE_ANNOTATIONS )) {
1606
+ RETURN_IF_ERROR (codegen_process_deferred_annotations (c , loc ));
1583
1607
}
1584
1608
return SUCCESS ;
1585
1609
}
@@ -1606,13 +1630,13 @@ compiler_codegen(struct compiler *c, mod_ty mod)
1606
1630
switch (mod -> kind ) {
1607
1631
case Module_kind : {
1608
1632
asdl_stmt_seq * stmts = mod -> v .Module .body ;
1609
- RETURN_IF_ERROR (compiler_body (c , start_location (stmts ), stmts ));
1633
+ RETURN_IF_ERROR (codegen_body (c , start_location (stmts ), stmts ));
1610
1634
break ;
1611
1635
}
1612
1636
case Interactive_kind : {
1613
1637
c -> c_interactive = 1 ;
1614
1638
asdl_stmt_seq * stmts = mod -> v .Interactive .body ;
1615
- RETURN_IF_ERROR (compiler_body (c , start_location (stmts ), stmts ));
1639
+ RETURN_IF_ERROR (codegen_body (c , start_location (stmts ), stmts ));
1616
1640
break ;
1617
1641
}
1618
1642
case Expression_kind : {
@@ -2385,7 +2409,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2385
2409
ADDOP_N_IN_SCOPE (c , loc , STORE_DEREF , & _Py_ID (__classdict__ ), cellvars );
2386
2410
}
2387
2411
/* compile the body proper */
2388
- RETURN_IF_ERROR_IN_SCOPE (c , compiler_body (c , loc , s -> v .ClassDef .body ));
2412
+ RETURN_IF_ERROR_IN_SCOPE (c , codegen_body (c , loc , s -> v .ClassDef .body ));
2389
2413
assert (c -> u -> u_static_attributes );
2390
2414
PyObject * static_attributes = PySequence_Tuple (c -> u -> u_static_attributes );
2391
2415
if (static_attributes == NULL ) {
@@ -3847,7 +3871,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
3847
3871
case AugAssign_kind :
3848
3872
return codegen_augassign (c , s );
3849
3873
case AnnAssign_kind :
3850
- return compiler_annassign (c , s );
3874
+ return codegen_annassign (c , s );
3851
3875
case For_kind :
3852
3876
return codegen_for (c , s );
3853
3877
case While_kind :
@@ -6287,7 +6311,28 @@ codegen_check_ann_subscr(struct compiler *c, expr_ty e)
6287
6311
}
6288
6312
6289
6313
static int
6290
- compiler_annassign (struct compiler * c , stmt_ty s )
6314
+ compiler_add_deferred_annotation (struct compiler * c , stmt_ty s )
6315
+ {
6316
+ if (c -> u -> u_deferred_annotations == NULL ) {
6317
+ c -> u -> u_deferred_annotations = PyList_New (0 );
6318
+ if (c -> u -> u_deferred_annotations == NULL ) {
6319
+ return ERROR ;
6320
+ }
6321
+ }
6322
+ PyObject * ptr = PyLong_FromVoidPtr ((void * )s );
6323
+ if (ptr == NULL ) {
6324
+ return ERROR ;
6325
+ }
6326
+ if (PyList_Append (c -> u -> u_deferred_annotations , ptr ) < 0 ) {
6327
+ Py_DECREF (ptr );
6328
+ return ERROR ;
6329
+ }
6330
+ Py_DECREF (ptr );
6331
+ return SUCCESS ;
6332
+ }
6333
+
6334
+ static int
6335
+ codegen_annassign (struct compiler * c , stmt_ty s )
6291
6336
{
6292
6337
location loc = LOC (s );
6293
6338
expr_ty targ = s -> v .AnnAssign .target ;
@@ -6305,8 +6350,8 @@ compiler_annassign(struct compiler *c, stmt_ty s)
6305
6350
case Name_kind :
6306
6351
/* If we have a simple name in a module or class, store annotation. */
6307
6352
if (s -> v .AnnAssign .simple &&
6308
- (c -> u -> u_scope_type == COMPILER_SCOPE_MODULE ||
6309
- c -> u -> u_scope_type == COMPILER_SCOPE_CLASS )) {
6353
+ (SCOPE_TYPE ( c ) == COMPILER_SCOPE_MODULE ||
6354
+ SCOPE_TYPE ( c ) == COMPILER_SCOPE_CLASS )) {
6310
6355
if (future_annotations ) {
6311
6356
VISIT (c , annexpr , s -> v .AnnAssign .annotation );
6312
6357
ADDOP_NAME (c , loc , LOAD_NAME , & _Py_ID (__annotations__ ), names );
@@ -6315,21 +6360,7 @@ compiler_annassign(struct compiler *c, stmt_ty s)
6315
6360
ADDOP (c , loc , STORE_SUBSCR );
6316
6361
}
6317
6362
else {
6318
- if (c -> u -> u_deferred_annotations == NULL ) {
6319
- c -> u -> u_deferred_annotations = PyList_New (0 );
6320
- if (c -> u -> u_deferred_annotations == NULL ) {
6321
- return ERROR ;
6322
- }
6323
- }
6324
- PyObject * ptr = PyLong_FromVoidPtr ((void * )s );
6325
- if (ptr == NULL ) {
6326
- return ERROR ;
6327
- }
6328
- if (PyList_Append (c -> u -> u_deferred_annotations , ptr ) < 0 ) {
6329
- Py_DECREF (ptr );
6330
- return ERROR ;
6331
- }
6332
- Py_DECREF (ptr );
6363
+ RETURN_IF_ERROR (compiler_add_deferred_annotation (c , s ));
6333
6364
}
6334
6365
}
6335
6366
break ;
@@ -7399,6 +7430,12 @@ compiler_is_nested_scope(struct compiler *c)
7399
7430
return PyList_GET_SIZE (c -> c_stack ) > 0 ;
7400
7431
}
7401
7432
7433
+ static int
7434
+ compiler_scope_type (struct compiler * c )
7435
+ {
7436
+ return c -> u -> u_scope_type ;
7437
+ }
7438
+
7402
7439
static int
7403
7440
compute_code_flags (struct compiler * c )
7404
7441
{
0 commit comments