Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6edafd

Browse files
committed
do not add None to co_consts if no docstring
1 parent 257d58e commit b6edafd

4 files changed

Lines changed: 13 additions & 3 deletions

File tree

Include/internal/pycore_symtable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ typedef struct _symtable_entry {
123123
unsigned ste_comp_iter_target : 1; /* true if visiting comprehension target */
124124
unsigned ste_can_see_class_scope : 1; /* true if this block can see names bound in an
125125
enclosing class scope */
126+
unsigned ste_has_docstring : 1; /* true if docstring present */
126127
int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
127128
_Py_SourceLocation ste_loc; /* source location of block */
128129
struct _symtable_entry *ste_annotation_block; /* symbol table entry for this entry's annotations */

Python/codegen.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,10 @@ _PyCodegen_Body(compiler *c, location loc, asdl_stmt_seq *stmts, bool is_interac
772772
assert(st->kind == Expr_kind);
773773
location loc = LOC(st->v.Expr.value);
774774
ADDOP_LOAD_CONST(c, loc, cleandoc);
775+
776+
PySTEntryObject *ste = SYMTABLE_ENTRY(c);
777+
ste->ste_has_docstring = 1;
778+
775779
Py_DECREF(cleandoc);
776780
RETURN_IF_ERROR(codegen_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store));
777781
}
@@ -1225,21 +1229,22 @@ codegen_function_body(compiler *c, stmt_ty s, int is_async, Py_ssize_t funcflags
12251229
Py_ssize_t first_instr = 0;
12261230
PyObject *docstring = _PyAST_GetDocString(body);
12271231
assert(OPTIMIZATION_LEVEL(c) < 2 || docstring == NULL);
1232+
PySTEntryObject *ste = SYMTABLE_ENTRY(c);
12281233
if (docstring) {
12291234
first_instr = 1;
12301235
docstring = _PyCompile_CleanDoc(docstring);
12311236
if (docstring == NULL) {
12321237
_PyCompile_ExitScope(c);
12331238
return ERROR;
12341239
}
1240+
Py_ssize_t idx = _PyCompile_AddConst(c, docstring);
1241+
ste->ste_has_docstring = 1;
1242+
RETURN_IF_ERROR_IN_SCOPE(c, idx < 0 ? ERROR : SUCCESS);
12351243
}
1236-
Py_ssize_t idx = _PyCompile_AddConst(c, docstring ? docstring : Py_None);
12371244
Py_XDECREF(docstring);
1238-
RETURN_IF_ERROR_IN_SCOPE(c, idx < 0 ? ERROR : SUCCESS);
12391245

12401246
NEW_JUMP_TARGET_LABEL(c, start);
12411247
USE_LABEL(c, start);
1242-
PySTEntryObject *ste = SYMTABLE_ENTRY(c);
12431248
bool add_stopiteration_handler = ste->ste_coroutine || ste->ste_generator;
12441249
if (add_stopiteration_handler) {
12451250
/* codegen_wrap_in_stopiteration_handler will push a block, so we need to account for that */

Python/compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,8 @@ compute_code_flags(compiler *c)
12851285
flags |= CO_VARARGS;
12861286
if (ste->ste_varkeywords)
12871287
flags |= CO_VARKEYWORDS;
1288+
if (ste->ste_has_docstring)
1289+
flags |= CO_HAS_DOCSTRING;
12881290
}
12891291

12901292
if (ste->ste_coroutine && !ste->ste_generator) {

Python/symtable.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block,
136136
ste->ste_needs_classdict = 0;
137137
ste->ste_annotation_block = NULL;
138138

139+
ste->ste_has_docstring = 0;
140+
139141
ste->ste_symbols = PyDict_New();
140142
ste->ste_varnames = PyList_New(0);
141143
ste->ste_children = PyList_New(0);

0 commit comments

Comments
 (0)