@@ -672,9 +672,7 @@ codegen_setup_annotations_scope(compiler *c, location loc,
672672 codegen_enter_scope (c , name , COMPILE_SCOPE_ANNOTATIONS ,
673673 key , loc .lineno , NULL , & umd ));
674674
675- // Insert None into consts to prevent an annotation
676- // appearing to be a docstring
677- _PyCompile_AddConst (c , Py_None );
675+ assert (!SYMTABLE_ENTRY (c )-> ste_has_docstring );
678676 // if .format != 1: raise NotImplementedError
679677 _Py_DECLARE_STR (format , ".format" );
680678 ADDOP_I (c , loc , LOAD_FAST , 0 );
@@ -770,16 +768,18 @@ _PyCodegen_Body(compiler *c, location loc, asdl_stmt_seq *stmts, bool is_interac
770768 /* If from __future__ import annotations is active,
771769 * every annotated class and module should have __annotations__.
772770 * Else __annotate__ is created when necessary. */
773- if ((FUTURE_FEATURES (c ) & CO_FUTURE_ANNOTATIONS ) && SYMTABLE_ENTRY (c )-> ste_annotations_used ) {
771+ PySTEntryObject * ste = SYMTABLE_ENTRY (c );
772+ if ((FUTURE_FEATURES (c ) & CO_FUTURE_ANNOTATIONS ) && ste -> ste_annotations_used ) {
774773 ADDOP (c , loc , SETUP_ANNOTATIONS );
775774 }
776775 if (!asdl_seq_LEN (stmts )) {
777776 return SUCCESS ;
778777 }
779778 Py_ssize_t first_instr = 0 ;
780779 if (!is_interactive ) { /* A string literal on REPL prompt is not a docstring */
781- PyObject * docstring = _PyAST_GetDocString (stmts );
782- if (docstring ) {
780+ if (ste -> ste_has_docstring ) {
781+ PyObject * docstring = _PyAST_GetDocString (stmts );
782+ assert (docstring );
783783 first_instr = 1 ;
784784 /* set docstring */
785785 assert (OPTIMIZATION_LEVEL (c ) < 2 );
@@ -1241,10 +1241,11 @@ codegen_function_body(compiler *c, stmt_ty s, int is_async, Py_ssize_t funcflags
12411241 RETURN_IF_ERROR (
12421242 codegen_enter_scope (c , name , scope_type , (void * )s , firstlineno , NULL , & umd ));
12431243
1244+ PySTEntryObject * ste = SYMTABLE_ENTRY (c );
12441245 Py_ssize_t first_instr = 0 ;
1245- PyObject * docstring = _PyAST_GetDocString ( body );
1246- assert ( OPTIMIZATION_LEVEL ( c ) < 2 || docstring == NULL );
1247- if (docstring ) {
1246+ if ( ste -> ste_has_docstring ) {
1247+ PyObject * docstring = _PyAST_GetDocString ( body );
1248+ assert (docstring );
12481249 first_instr = 1 ;
12491250 docstring = _PyCompile_CleanDoc (docstring );
12501251 if (docstring == NULL ) {
@@ -1258,7 +1259,6 @@ codegen_function_body(compiler *c, stmt_ty s, int is_async, Py_ssize_t funcflags
12581259
12591260 NEW_JUMP_TARGET_LABEL (c , start );
12601261 USE_LABEL (c , start );
1261- PySTEntryObject * ste = SYMTABLE_ENTRY (c );
12621262 bool add_stopiteration_handler = ste -> ste_coroutine || ste -> ste_generator ;
12631263 if (add_stopiteration_handler ) {
12641264 /* codegen_wrap_in_stopiteration_handler will push a block, so we need to account for that */
@@ -1600,9 +1600,8 @@ codegen_typealias_body(compiler *c, stmt_ty s)
16001600 ADDOP_LOAD_CONST_NEW (c , loc , defaults );
16011601 RETURN_IF_ERROR (
16021602 codegen_setup_annotations_scope (c , LOC (s ), s , name ));
1603- /* Make None the first constant, so the evaluate function can't have a
1604- docstring. */
1605- RETURN_IF_ERROR (_PyCompile_AddConst (c , Py_None ));
1603+
1604+ assert (!SYMTABLE_ENTRY (c )-> ste_has_docstring );
16061605 VISIT_IN_SCOPE (c , expr , s -> v .TypeAlias .value );
16071606 ADDOP_IN_SCOPE (c , loc , RETURN_VALUE );
16081607 PyCodeObject * co = _PyCompile_OptimizeAndAssemble (c , 0 );
@@ -1898,9 +1897,7 @@ codegen_lambda(compiler *c, expr_ty e)
18981897 codegen_enter_scope (c , & _Py_STR (anon_lambda ), COMPILE_SCOPE_LAMBDA ,
18991898 (void * )e , e -> lineno , NULL , & umd ));
19001899
1901- /* Make None the first constant, so the lambda can't have a
1902- docstring. */
1903- RETURN_IF_ERROR (_PyCompile_AddConst (c , Py_None ));
1900+ assert (!SYMTABLE_ENTRY (c )-> ste_has_docstring );
19041901
19051902 VISIT_IN_SCOPE (c , expr , e -> v .Lambda .body );
19061903 if (SYMTABLE_ENTRY (c )-> ste_generator ) {
0 commit comments