summaryrefslogtreecommitdiffstats
path: root/Python/compile.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-05-29 09:04:55 (GMT)
committerGitHub <noreply@github.com>2018-05-29 09:04:55 (GMT)
commit73cbe7a01a22d02dbe1ec841e8779c775cad3d08 (patch)
tree0c39a8adb1cebdfc95cc991223c6c278fb13100a /Python/compile.c
parent2179022d94937d7b0600b0dc192ca6fa5f53d830 (diff)
downloadcpython-73cbe7a01a22d02dbe1ec841e8779c775cad3d08.zip
cpython-73cbe7a01a22d02dbe1ec841e8779c775cad3d08.tar.gz
cpython-73cbe7a01a22d02dbe1ec841e8779c775cad3d08.tar.bz2
bpo-32911: Revert bpo-29463. (GH-7121) (GH-7197)
Remove the docstring attribute of AST types and restore docstring expression as a first stmt in their body. Co-authored-by: INADA Naoki <methane@users.noreply.github.com>
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c55
1 files changed, 42 insertions, 13 deletions
diff --git a/Python/compile.c b/Python/compile.c
index c3397f0..42ae508 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1393,6 +1393,18 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
}
static int
+compiler_isdocstring(stmt_ty s)
+{
+ if (s->kind != Expr_kind)
+ return 0;
+ if (s->v.Expr.value->kind == Str_kind)
+ return 1;
+ if (s->v.Expr.value->kind == Constant_kind)
+ return PyUnicode_CheckExact(s->v.Expr.value->v.Constant.value);
+ return 0;
+}
+
+static int
is_const(expr_ty e)
{
switch (e->kind) {
@@ -1587,27 +1599,37 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
and for annotations. */
static int
-compiler_body(struct compiler *c, asdl_seq *stmts, string docstring)
+compiler_body(struct compiler *c, asdl_seq *stmts)
{
+ int i = 0;
+ stmt_ty st;
+
/* Set current line number to the line number of first statement.
This way line number for SETUP_ANNOTATIONS will always
coincide with the line number of first "real" statement in module.
If body is empy, then lineno will be set later in assemble. */
if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&
!c->u->u_lineno && asdl_seq_LEN(stmts)) {
- stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
+ st = (stmt_ty)asdl_seq_GET(stmts, 0);
c->u->u_lineno = st->lineno;
}
/* Every annotated class and module should have __annotations__. */
if (find_ann(stmts)) {
ADDOP(c, SETUP_ANNOTATIONS);
}
+ if (!asdl_seq_LEN(stmts))
+ return 1;
+ st = (stmt_ty)asdl_seq_GET(stmts, 0);
/* if not -OO mode, set docstring */
- if (c->c_optimize < 2 && docstring) {
- ADDOP_LOAD_CONST(c, docstring);
- ADDOP_NAME(c, STORE_NAME, __doc__, names);
+ if (compiler_isdocstring(st) && c->c_optimize < 2) {
+ /* don't generate docstrings if -OO */
+ i = 1;
+ VISIT(c, expr, st->v.Expr.value);
+ if (!compiler_nameop(c, __doc__, Store))
+ return 0;
}
- VISIT_SEQ(c, stmt, stmts);
+ for (; i < asdl_seq_LEN(stmts); i++)
+ VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
return 1;
}
@@ -1627,7 +1649,7 @@ compiler_mod(struct compiler *c, mod_ty mod)
return NULL;
switch (mod->kind) {
case Module_kind:
- if (!compiler_body(c, mod->v.Module.body, mod->v.Module.docstring)) {
+ if (!compiler_body(c, mod->v.Module.body)) {
compiler_exit_scope(c);
return 0;
}
@@ -1957,13 +1979,15 @@ static int
compiler_function(struct compiler *c, stmt_ty s, int is_async)
{
PyCodeObject *co;
- PyObject *qualname, *docstring = Py_None;
+ PyObject *qualname, *first_const = Py_None;
arguments_ty args;
expr_ty returns;
identifier name;
asdl_seq* decos;
asdl_seq *body;
+ stmt_ty st;
Py_ssize_t i, funcflags;
+ int docstring;
int annotations;
int scope_type;
@@ -2010,16 +2034,21 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
}
/* if not -OO mode, add docstring */
- if (c->c_optimize < 2 && s->v.FunctionDef.docstring)
- docstring = s->v.FunctionDef.docstring;
- if (compiler_add_const(c, docstring) < 0) {
+ st = (stmt_ty)asdl_seq_GET(body, 0);
+ docstring = compiler_isdocstring(st);
+ if (docstring && c->c_optimize < 2) {
+ if (st->v.Expr.value->kind == Constant_kind)
+ first_const = st->v.Expr.value->v.Constant.value;
+ else
+ first_const = st->v.Expr.value->v.Str.s;
+ }
+ if (compiler_add_const(c, first_const) < 0) {
compiler_exit_scope(c);
return 0;
}
c->u->u_argcount = asdl_seq_LEN(args->args);
c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
- /* if there was a docstring, we need to skip the first statement */
VISIT_SEQ_IN_SCOPE(c, stmt, body);
co = assemble(c, 1);
qualname = c->u->u_qualname;
@@ -2101,7 +2130,7 @@ compiler_class(struct compiler *c, stmt_ty s)
}
Py_DECREF(str);
/* compile the body proper */
- if (!compiler_body(c, s->v.ClassDef.body, s->v.ClassDef.docstring)) {
+ if (!compiler_body(c, s->v.ClassDef.body)) {
compiler_exit_scope(c);
return 0;
}