Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,22 @@ def with_const_expression():
self.assertIsNone(ns['with_fstring'].__doc__)
self.assertIsNone(ns['with_const_expression'].__doc__)

@support.cpython_only
def test_docstring_interactive_mode(self):
src = textwrap.dedent("""
def with_docstring():
"docstring"
""")
for opt in [0, 1, 2]:
with self.subTest(opt=opt):
code = compile(src, "<test>", "single", optimize=opt)
ns = {}
exec(code, ns)
if opt < 2:
self.assertEqual(ns['with_docstring'].__doc__, "docstring")
else:
self.assertIsNone(ns['with_docstring'].__doc__)

@support.cpython_only
def test_docstring_omitted(self):
# See gh-115347
Expand All @@ -919,12 +935,13 @@ class C:
return h
""")
for opt in [-1, 0, 1, 2]:
with self.subTest(opt=opt):
code = compile(src, "<test>", "exec", optimize=opt)
output = io.StringIO()
with contextlib.redirect_stdout(output):
dis.dis(code)
self.assertNotIn('NOP' , output.getvalue())
for mode in ["exec", "single"]:
with self.subTest(opt=opt, mode=mode):
code = compile(src, "<test>", mode, optimize=opt)
output = io.StringIO()
with contextlib.redirect_stdout(output):
dis.dis(code)
self.assertNotIn('NOP' , output.getvalue())

def test_dont_merge_constants(self):
# Issue #25843: compile() must not merge constants which are equal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug where docstring it removed from classes in interactive mode.
30 changes: 14 additions & 16 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,23 +758,21 @@ _PyCodegen_Body(compiler *c, location loc, asdl_stmt_seq *stmts)
return SUCCESS;
}
Py_ssize_t first_instr = 0;
if (!IS_INTERACTIVE(c)) {
PyObject *docstring = _PyAST_GetDocString(stmts);
if (docstring) {
first_instr = 1;
/* set docstring */
assert(OPTIMIZATION_LEVEL(c) < 2);
PyObject *cleandoc = _PyCompile_CleanDoc(docstring);
if (cleandoc == NULL) {
return ERROR;
}
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
assert(st->kind == Expr_kind);
location loc = LOC(st->v.Expr.value);
ADDOP_LOAD_CONST(c, loc, cleandoc);
Py_DECREF(cleandoc);
RETURN_IF_ERROR(codegen_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store));
PyObject *docstring = _PyAST_GetDocString(stmts);
if (docstring) {
first_instr = 1;
/* set docstring */
assert(OPTIMIZATION_LEVEL(c) < 2);
PyObject *cleandoc = _PyCompile_CleanDoc(docstring);
if (cleandoc == NULL) {
return ERROR;
}
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
assert(st->kind == Expr_kind);
location loc = LOC(st->v.Expr.value);
ADDOP_LOAD_CONST(c, loc, cleandoc);
Py_DECREF(cleandoc);
RETURN_IF_ERROR(codegen_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store));
}
for (Py_ssize_t i = first_instr; i < asdl_seq_LEN(stmts); i++) {
VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Expand Down
Loading