@@ -1759,11 +1759,17 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
1759
1759
{
1760
1760
1761
1761
/* Set current line number to the line number of first statement.
1762
+ This way line number for SETUP_ANNOTATIONS will always
1763
+ coincide with the line number of first "real" statement in module.
1762
1764
If body is empty, then lineno will be set later in optimize_and_assemble. */
1763
1765
if (c -> u -> u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN (stmts )) {
1764
1766
stmt_ty st = (stmt_ty )asdl_seq_GET (stmts , 0 );
1765
1767
loc = LOC (st );
1766
1768
}
1769
+ /* Every annotated class and module should have __annotations__. */
1770
+ if ((c -> c_future .ff_features & CO_FUTURE_ANNOTATIONS ) && find_ann (stmts )) {
1771
+ ADDOP (c , loc , SETUP_ANNOTATIONS );
1772
+ }
1767
1773
if (!asdl_seq_LEN (stmts )) {
1768
1774
return SUCCESS ;
1769
1775
}
@@ -1788,7 +1794,8 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
1788
1794
for (Py_ssize_t i = first_instr ; i < asdl_seq_LEN (stmts ); i ++ ) {
1789
1795
VISIT (c , stmt , (stmt_ty )asdl_seq_GET (stmts , i ));
1790
1796
}
1791
- if (c -> u -> u_ste -> ste_annotation_block != NULL ) {
1797
+ if (!(c -> c_future .ff_features & CO_FUTURE_ANNOTATIONS ) &&
1798
+ c -> u -> u_ste -> ste_annotation_block != NULL ) {
1792
1799
NEW_JUMP_TARGET_LABEL (c , raise_notimp );
1793
1800
void * key = (void * )((uintptr_t )c -> u -> u_ste -> ste_id + 1 );
1794
1801
RETURN_IF_ERROR (compiler_setup_annotations_scope (c , loc , key , raise_notimp ));
@@ -2167,8 +2174,10 @@ compiler_visit_annotations(struct compiler *c, location loc,
2167
2174
compiler_visit_argannotation (c , & _Py_ID (return ), returns , & annotations_len , loc ));
2168
2175
2169
2176
if (future_annotations ) {
2170
- ADDOP_I (c , loc , BUILD_TUPLE , annotations_len * 2 );
2171
- return MAKE_FUNCTION_ANNOTATIONS ;
2177
+ if (annotations_len ) {
2178
+ ADDOP_I (c , loc , BUILD_TUPLE , annotations_len * 2 );
2179
+ return MAKE_FUNCTION_ANNOTATIONS ;
2180
+ }
2172
2181
}
2173
2182
else {
2174
2183
assert (ste != NULL );
@@ -4403,7 +4412,6 @@ compiler_nameop(struct compiler *c, location loc,
4403
4412
}
4404
4413
4405
4414
/* XXX Leave assert here, but handle __doc__ and the like better */
4406
- printf ("name %s %s\n" , PyUnicode_AsUTF8 (name ), PyUnicode_AsUTF8 (c -> u -> u_ste -> ste_name ));
4407
4415
assert (scope || PyUnicode_READ_CHAR (name , 0 ) == '_' );
4408
4416
4409
4417
switch (optype ) {
@@ -6625,7 +6633,7 @@ compiler_annassign(struct compiler *c, stmt_ty s)
6625
6633
{
6626
6634
location loc = LOC (s );
6627
6635
expr_ty targ = s -> v .AnnAssign .target ;
6628
- PyObject * mangled ;
6636
+ PyObject * mangled ;
6629
6637
6630
6638
assert (s -> kind == AnnAssign_kind );
6631
6639
@@ -6648,7 +6656,7 @@ compiler_annassign(struct compiler *c, stmt_ty s)
6648
6656
ADDOP_NAME (c , loc , LOAD_NAME , & _Py_ID (__annotations__ ), names );
6649
6657
mangled = _Py_Mangle (c -> u -> u_private , targ -> v .Name .id );
6650
6658
ADDOP_LOAD_CONST_NEW (c , loc , mangled );
6651
- ADDOP (c , loc , STORE_SUBSCR );
6659
+ ADDOP (c , loc , STORE_SUBSCR );
6652
6660
}
6653
6661
break ;
6654
6662
case Attribute_kind :
0 commit comments