summaryrefslogtreecommitdiffstats
path: root/Python/ast_opt.c
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2024-09-11 17:02:28 (GMT)
committerGitHub <noreply@github.com>2024-09-11 17:02:28 (GMT)
commite07154fd1e3152a758cf9b476257a4ffdc48dfc6 (patch)
tree8b87945e8a066bf948e8dea25ba78a6df8591bab /Python/ast_opt.c
parent2938c3dec99390087490124c2ef50e1592671e72 (diff)
downloadcpython-e07154fd1e3152a758cf9b476257a4ffdc48dfc6.zip
cpython-e07154fd1e3152a758cf9b476257a4ffdc48dfc6.tar.gz
cpython-e07154fd1e3152a758cf9b476257a4ffdc48dfc6.tar.bz2
gh-123958: apply docstring removal optimization in ast_opt instead of codegen (#123959)
Diffstat (limited to 'Python/ast_opt.c')
-rw-r--r--Python/ast_opt.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/Python/ast_opt.c b/Python/ast_opt.c
index 5a51305..f5b0475 100644
--- a/Python/ast_opt.c
+++ b/Python/ast_opt.c
@@ -674,9 +674,30 @@ static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimize
static int
+stmt_seq_remove_item(asdl_stmt_seq *stmts, Py_ssize_t idx)
+{
+ if (idx >= asdl_seq_LEN(stmts)) {
+ return 0;
+ }
+ for (Py_ssize_t i = idx; i < asdl_seq_LEN(stmts) - 1; i++) {
+ stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, i+1);
+ asdl_seq_SET(stmts, i, st);
+ }
+ stmts->size--;
+ return 1;
+}
+
+static int
astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state)
{
int docstring = _PyAST_GetDocString(stmts) != NULL;
+ if (docstring && (state->optimize >= 2)) {
+ /* remove the docstring */
+ if (!stmt_seq_remove_item(stmts, 0)) {
+ return 0;
+ }
+ docstring = 0;
+ }
CALL_SEQ(astfold_stmt, stmt, stmts);
if (!docstring && _PyAST_GetDocString(stmts) != NULL) {
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);