summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorXuanteng Huang <44627253+xuantengh@users.noreply.github.com>2024-10-30 09:01:09 (GMT)
committerGitHub <noreply@github.com>2024-10-30 09:01:09 (GMT)
commit35df4eb959b3923c08aaaeff728c5ed1706f31cf (patch)
tree0066d2a27b47aaa3fc3dde9debcfd9b553579565 /Python
parent2ab377a47c8290f8bf52c8ffb5d7fc4c45452611 (diff)
downloadcpython-35df4eb959b3923c08aaaeff728c5ed1706f31cf.zip
cpython-35df4eb959b3923c08aaaeff728c5ed1706f31cf.tar.gz
cpython-35df4eb959b3923c08aaaeff728c5ed1706f31cf.tar.bz2
gh-126072: do not add `None` to `co_consts` if there is no docstring (GH-126101)
Diffstat (limited to 'Python')
-rw-r--r--Python/codegen.c6
-rw-r--r--Python/compile.c2
-rw-r--r--Python/symtable.c10
3 files changed, 15 insertions, 3 deletions
diff --git a/Python/codegen.c b/Python/codegen.c
index 976c942..d79aee4 100644
--- a/Python/codegen.c
+++ b/Python/codegen.c
@@ -1243,10 +1243,10 @@ codegen_function_body(compiler *c, stmt_ty s, int is_async, Py_ssize_t funcflags
_PyCompile_ExitScope(c);
return ERROR;
}
+ Py_ssize_t idx = _PyCompile_AddConst(c, docstring);
+ Py_DECREF(docstring);
+ RETURN_IF_ERROR_IN_SCOPE(c, idx < 0 ? ERROR : SUCCESS);
}
- Py_ssize_t idx = _PyCompile_AddConst(c, docstring ? docstring : Py_None);
- Py_XDECREF(docstring);
- RETURN_IF_ERROR_IN_SCOPE(c, idx < 0 ? ERROR : SUCCESS);
NEW_JUMP_TARGET_LABEL(c, start);
USE_LABEL(c, start);
diff --git a/Python/compile.c b/Python/compile.c
index d463fcd..4dcb9a1 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1285,6 +1285,8 @@ compute_code_flags(compiler *c)
flags |= CO_VARARGS;
if (ste->ste_varkeywords)
flags |= CO_VARKEYWORDS;
+ if (ste->ste_has_docstring)
+ flags |= CO_HAS_DOCSTRING;
}
if (ste->ste_coroutine && !ste->ste_generator) {
diff --git a/Python/symtable.c b/Python/symtable.c
index 709918b..32d7151 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -136,6 +136,8 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block,
ste->ste_needs_classdict = 0;
ste->ste_annotation_block = NULL;
+ ste->ste_has_docstring = 0;
+
ste->ste_symbols = PyDict_New();
ste->ste_varnames = PyList_New(0);
ste->ste_children = PyList_New(0);
@@ -1841,6 +1843,10 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
return 0;
}
+ if (_PyAST_GetDocString(s->v.FunctionDef.body)) {
+ new_ste->ste_has_docstring = 1;
+ }
+
if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
s->v.FunctionDef.returns, new_ste)) {
Py_DECREF(new_ste);
@@ -2168,6 +2174,10 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
return 0;
}
+ if (_PyAST_GetDocString(s->v.AsyncFunctionDef.body)) {
+ new_ste->ste_has_docstring = 1;
+ }
+
if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
s->v.AsyncFunctionDef.returns, new_ste)) {
Py_DECREF(new_ste);