@@ -253,7 +253,7 @@ static int compiler_async_comprehension_generator(
253253 expr_ty elt , expr_ty val , int type );
254254
255255static PyCodeObject * assemble (struct compiler * , int addNone );
256- static PyObject * __doc__ , * __annotations__ ;
256+ static PyObject * __doc__ ;
257257
258258#define CAPSULE_NAME "compile.c compiler unit"
259259
@@ -358,11 +358,6 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
358358 if (!__doc__ )
359359 return NULL ;
360360 }
361- if (!__annotations__ ) {
362- __annotations__ = PyUnicode_InternFromString ("__annotations__" );
363- if (!__annotations__ )
364- return NULL ;
365- }
366361 if (!compiler_init (& c ))
367362 return NULL ;
368363 Py_INCREF (filename );
@@ -1786,9 +1781,9 @@ compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblock
17861781static int
17871782compiler_body (struct compiler * c , asdl_stmt_seq * stmts )
17881783{
1789- int i = 0 ;
1784+ int i = 0 , has_annotations = 0 ;
17901785 stmt_ty st ;
1791- PyObject * docstring ;
1786+ PyObject * docstring , * annotations ;
17921787
17931788 /* Set current line number to the line number of first statement.
17941789 This way line number for SETUP_ANNOTATIONS will always
@@ -1801,6 +1796,10 @@ compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
18011796 /* Every annotated class and module should have __annotations__. */
18021797 if (find_ann (stmts )) {
18031798 ADDOP (c , SETUP_ANNOTATIONS );
1799+ has_annotations = 1 ;
1800+ c -> u -> u_annotations = PyList_New (0 );
1801+ if (!c -> u -> u_annotations )
1802+ return 0 ;
18041803 }
18051804 if (!asdl_seq_LEN (stmts ))
18061805 return 1 ;
@@ -1818,6 +1817,16 @@ compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
18181817 }
18191818 for (; i < asdl_seq_LEN (stmts ); i ++ )
18201819 VISIT (c , stmt , (stmt_ty )asdl_seq_GET (stmts , i ));
1820+
1821+ if (has_annotations ) {
1822+ annotations = PyList_AsTuple (c -> u -> u_annotations );
1823+ Py_DECREF (c -> u -> u_annotations );
1824+ c -> u -> u_annotations = Py_None ;
1825+ if (annotations == NULL )
1826+ return 0 ;
1827+ c -> u -> u_annotations = annotations ;
1828+ }
1829+
18211830 return 1 ;
18221831}
18231832
@@ -2027,39 +2036,47 @@ compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
20272036 return 1 ;
20282037}
20292038
2039+ static int
2040+ compiler_add_annotation (struct compiler * c , PyObject * mangled ,
2041+ expr_ty annotation , PyObject * annotations ) {
2042+ PyObject * pair ;
2043+ PyObject * value ;
2044+
2045+ value = _PyAST_ExprAsUnicode (annotation );
2046+ if (value == NULL )
2047+ return 0 ;
2048+
2049+ pair = PyTuple_Pack (2 , mangled , value );
2050+ if (pair == NULL ) {
2051+ Py_DECREF (value );
2052+ return 0 ;
2053+ }
2054+
2055+ if (PyList_Append (annotations , pair ) < 0 ) {
2056+ Py_DECREF (value );
2057+ Py_DECREF (pair );
2058+ return 0 ;
2059+ }
2060+
2061+ Py_DECREF (value );
2062+ Py_DECREF (pair );
2063+ return 1 ;
2064+ }
2065+
20302066static int
20312067compiler_visit_argannotation (struct compiler * c , identifier id ,
20322068 expr_ty annotation , PyObject * annotations )
20332069{
20342070 if (annotation ) {
20352071 PyObject * mangled ;
2036- PyObject * pair ;
2037- PyObject * value ;
20382072 mangled = _Py_Mangle (c -> u -> u_private , id );
20392073 if (!mangled )
20402074 return 0 ;
2041-
2042- value = _PyAST_ExprAsUnicode (annotation );
2043- if (value == NULL )
2044- return 0 ;
2045-
2046- pair = PyTuple_Pack (2 , mangled , value );
2047- if (pair == NULL ) {
2048- Py_DECREF (value );
2049- Py_DECREF (mangled );
2050- return 0 ;
2051- }
2052-
2053- if (PyList_Append (annotations , pair ) < 0 ) {
2054- Py_DECREF (value );
2075+ if (!compiler_add_annotation (c , mangled , annotation , annotations )){
20552076 Py_DECREF (mangled );
2056- Py_DECREF (pair );
20572077 return 0 ;
20582078 }
2059-
2060- Py_DECREF (value );
20612079 Py_DECREF (mangled );
2062- Py_DECREF (pair );
20632080 }
20642081 return 1 ;
20652082}
@@ -5284,11 +5301,12 @@ compiler_annassign(struct compiler *c, stmt_ty s)
52845301 if (s -> v .AnnAssign .simple &&
52855302 (c -> u -> u_scope_type == COMPILER_SCOPE_MODULE ||
52865303 c -> u -> u_scope_type == COMPILER_SCOPE_CLASS )) {
5287- VISIT (c , annexpr , s -> v .AnnAssign .annotation );
5288- ADDOP_NAME (c , LOAD_NAME , __annotations__ , names );
52895304 mangled = _Py_Mangle (c -> u -> u_private , targ -> v .Name .id );
5290- ADDOP_LOAD_CONST_NEW (c , mangled );
5291- ADDOP (c , STORE_SUBSCR );
5305+ if (!compiler_add_annotation (c , mangled , s -> v .AnnAssign .annotation , c -> u -> u_annotations )){
5306+ Py_DECREF (mangled );
5307+ return 0 ;
5308+ }
5309+ Py_DECREF (mangled );
52925310 }
52935311 break ;
52945312 case Attribute_kind :
0 commit comments