@@ -1392,18 +1392,6 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
13921392 } \
13931393}
13941394
1395- static int
1396- compiler_isdocstring (stmt_ty s )
1397- {
1398- if (s -> kind != Expr_kind )
1399- return 0 ;
1400- if (s -> v .Expr .value -> kind == Str_kind )
1401- return 1 ;
1402- if (s -> v .Expr .value -> kind == Constant_kind )
1403- return PyUnicode_CheckExact (s -> v .Expr .value -> v .Constant .value );
1404- return 0 ;
1405- }
1406-
14071395static int
14081396is_const (expr_ty e )
14091397{
@@ -1603,6 +1591,7 @@ compiler_body(struct compiler *c, asdl_seq *stmts)
16031591{
16041592 int i = 0 ;
16051593 stmt_ty st ;
1594+ PyObject * docstring ;
16061595
16071596 /* Set current line number to the line number of first statement.
16081597 This way line number for SETUP_ANNOTATIONS will always
@@ -1619,14 +1608,17 @@ compiler_body(struct compiler *c, asdl_seq *stmts)
16191608 }
16201609 if (!asdl_seq_LEN (stmts ))
16211610 return 1 ;
1622- st = (stmt_ty )asdl_seq_GET (stmts , 0 );
16231611 /* if not -OO mode, set docstring */
1624- if (compiler_isdocstring (st ) && c -> c_optimize < 2 ) {
1625- /* don't generate docstrings if -OO */
1626- i = 1 ;
1627- VISIT (c , expr , st -> v .Expr .value );
1628- if (!compiler_nameop (c , __doc__ , Store ))
1629- return 0 ;
1612+ if (c -> c_optimize < 2 ) {
1613+ docstring = _PyAST_GetDocString (stmts );
1614+ if (docstring ) {
1615+ i = 1 ;
1616+ st = (stmt_ty )asdl_seq_GET (stmts , 0 );
1617+ assert (st -> kind == Expr_kind );
1618+ VISIT (c , expr , st -> v .Expr .value );
1619+ if (!compiler_nameop (c , __doc__ , Store ))
1620+ return 0 ;
1621+ }
16301622 }
16311623 for (; i < asdl_seq_LEN (stmts ); i ++ )
16321624 VISIT (c , stmt , (stmt_ty )asdl_seq_GET (stmts , i ));
@@ -1979,15 +1971,13 @@ static int
19791971compiler_function (struct compiler * c , stmt_ty s , int is_async )
19801972{
19811973 PyCodeObject * co ;
1982- PyObject * qualname , * first_const = Py_None ;
1974+ PyObject * qualname , * docstring = NULL ;
19831975 arguments_ty args ;
19841976 expr_ty returns ;
19851977 identifier name ;
19861978 asdl_seq * decos ;
19871979 asdl_seq * body ;
1988- stmt_ty st ;
19891980 Py_ssize_t i , funcflags ;
1990- int docstring ;
19911981 int annotations ;
19921982 int scope_type ;
19931983
@@ -2034,15 +2024,10 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
20342024 }
20352025
20362026 /* if not -OO mode, add docstring */
2037- st = (stmt_ty )asdl_seq_GET (body , 0 );
2038- docstring = compiler_isdocstring (st );
2039- if (docstring && c -> c_optimize < 2 ) {
2040- if (st -> v .Expr .value -> kind == Constant_kind )
2041- first_const = st -> v .Expr .value -> v .Constant .value ;
2042- else
2043- first_const = st -> v .Expr .value -> v .Str .s ;
2027+ if (c -> c_optimize < 2 ) {
2028+ docstring = _PyAST_GetDocString (body );
20442029 }
2045- if (compiler_add_const (c , first_const ) < 0 ) {
2030+ if (compiler_add_const (c , docstring ? docstring : Py_None ) < 0 ) {
20462031 compiler_exit_scope (c );
20472032 return 0 ;
20482033 }
0 commit comments