@@ -1692,16 +1692,13 @@ compiler_unwind_fblock_stack(struct compiler *c, location *ploc,
1692
1692
static int
1693
1693
compiler_body (struct compiler * c , location loc , asdl_stmt_seq * stmts )
1694
1694
{
1695
- int i = 0 ;
1696
- stmt_ty st ;
1697
- PyObject * docstring ;
1698
1695
1699
1696
/* Set current line number to the line number of first statement.
1700
1697
This way line number for SETUP_ANNOTATIONS will always
1701
1698
coincide with the line number of first "real" statement in module.
1702
1699
If body is empty, then lineno will be set later in optimize_and_assemble. */
1703
1700
if (c -> u -> u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN (stmts )) {
1704
- st = (stmt_ty )asdl_seq_GET (stmts , 0 );
1701
+ stmt_ty st = (stmt_ty )asdl_seq_GET (stmts , 0 );
1705
1702
loc = LOC (st );
1706
1703
}
1707
1704
/* Every annotated class and module should have __annotations__. */
@@ -1711,24 +1708,25 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
1711
1708
if (!asdl_seq_LEN (stmts )) {
1712
1709
return SUCCESS ;
1713
1710
}
1714
- /* if not -OO mode, set docstring */
1715
- if (c -> c_optimize < 2 ) {
1716
- docstring = _PyAST_GetDocString (stmts );
1717
- if (docstring ) {
1711
+ Py_ssize_t first_instr = 0 ;
1712
+ PyObject * docstring = _PyAST_GetDocString (stmts );
1713
+ if (docstring ) {
1714
+ first_instr = 1 ;
1715
+ /* if not -OO mode, set docstring */
1716
+ if (c -> c_optimize < 2 ) {
1718
1717
PyObject * cleandoc = _PyCompile_CleanDoc (docstring );
1719
1718
if (cleandoc == NULL ) {
1720
1719
return ERROR ;
1721
1720
}
1722
- i = 1 ;
1723
- st = (stmt_ty )asdl_seq_GET (stmts , 0 );
1721
+ stmt_ty st = (stmt_ty )asdl_seq_GET (stmts , 0 );
1724
1722
assert (st -> kind == Expr_kind );
1725
1723
location loc = LOC (st -> v .Expr .value );
1726
1724
ADDOP_LOAD_CONST (c , loc , cleandoc );
1727
1725
Py_DECREF (cleandoc );
1728
1726
RETURN_IF_ERROR (compiler_nameop (c , NO_LOCATION , & _Py_ID (__doc__ ), Store ));
1729
1727
}
1730
1728
}
1731
- for (; i < asdl_seq_LEN (stmts ); i ++ ) {
1729
+ for (Py_ssize_t i = first_instr ; i < asdl_seq_LEN (stmts ); i ++ ) {
1732
1730
VISIT (c , stmt , (stmt_ty )asdl_seq_GET (stmts , i ));
1733
1731
}
1734
1732
return SUCCESS ;
@@ -2239,7 +2237,6 @@ static int
2239
2237
compiler_function_body (struct compiler * c , stmt_ty s , int is_async , Py_ssize_t funcflags ,
2240
2238
int firstlineno )
2241
2239
{
2242
- PyObject * docstring = NULL ;
2243
2240
arguments_ty args ;
2244
2241
identifier name ;
2245
2242
asdl_stmt_seq * body ;
@@ -2266,28 +2263,33 @@ compiler_function_body(struct compiler *c, stmt_ty s, int is_async, Py_ssize_t f
2266
2263
RETURN_IF_ERROR (
2267
2264
compiler_enter_scope (c , name , scope_type , (void * )s , firstlineno ));
2268
2265
2269
- /* if not -OO mode, add docstring */
2270
- if (c -> c_optimize < 2 ) {
2271
- docstring = _PyAST_GetDocString (body );
2272
- if (docstring ) {
2266
+ Py_ssize_t first_instr = 0 ;
2267
+ PyObject * docstring = _PyAST_GetDocString (body );
2268
+ if (docstring ) {
2269
+ first_instr = 1 ;
2270
+ /* if not -OO mode, add docstring */
2271
+ if (c -> c_optimize < 2 ) {
2273
2272
docstring = _PyCompile_CleanDoc (docstring );
2274
2273
if (docstring == NULL ) {
2275
2274
compiler_exit_scope (c );
2276
2275
return ERROR ;
2277
2276
}
2278
2277
}
2278
+ else {
2279
+ docstring = NULL ;
2280
+ }
2279
2281
}
2280
2282
if (compiler_add_const (c -> c_const_cache , c -> u , docstring ? docstring : Py_None ) < 0 ) {
2281
2283
Py_XDECREF (docstring );
2282
2284
compiler_exit_scope (c );
2283
2285
return ERROR ;
2284
2286
}
2285
- Py_XDECREF (docstring );
2287
+ Py_CLEAR (docstring );
2286
2288
2287
2289
c -> u -> u_metadata .u_argcount = asdl_seq_LEN (args -> args );
2288
2290
c -> u -> u_metadata .u_posonlyargcount = asdl_seq_LEN (args -> posonlyargs );
2289
2291
c -> u -> u_metadata .u_kwonlyargcount = asdl_seq_LEN (args -> kwonlyargs );
2290
- for (Py_ssize_t i = docstring ? 1 : 0 ; i < asdl_seq_LEN (body ); i ++ ) {
2292
+ for (Py_ssize_t i = first_instr ; i < asdl_seq_LEN (body ); i ++ ) {
2291
2293
VISIT_IN_SCOPE (c , stmt , (stmt_ty )asdl_seq_GET (body , i ));
2292
2294
}
2293
2295
if (c -> u -> u_ste -> ste_coroutine || c -> u -> u_ste -> ste_generator ) {
0 commit comments