gh-124022: Fix bug where class docstring is removed in interactive mode (#124023)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Irit Katriel 2024-09-13 15:06:06 +01:00 committed by GitHub
parent cfe6074d1f
commit a9594a34c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 37 additions and 12 deletions

View file

@ -746,7 +746,7 @@ _PyCodegen_Expression(compiler *c, expr_ty e)
and for annotations. */
int
_PyCodegen_Body(compiler *c, location loc, asdl_stmt_seq *stmts)
_PyCodegen_Body(compiler *c, location loc, asdl_stmt_seq *stmts, bool is_interactive)
{
/* If from __future__ import annotations is active,
* every annotated class and module should have __annotations__.
@ -758,7 +758,7 @@ _PyCodegen_Body(compiler *c, location loc, asdl_stmt_seq *stmts)
return SUCCESS;
}
Py_ssize_t first_instr = 0;
if (!IS_INTERACTIVE(c)) {
if (!is_interactive) { /* A string literal on REPL prompt is not a docstring */
PyObject *docstring = _PyAST_GetDocString(stmts);
if (docstring) {
first_instr = 1;
@ -1432,7 +1432,7 @@ codegen_class_body(compiler *c, stmt_ty s, int firstlineno)
ADDOP_N_IN_SCOPE(c, loc, STORE_DEREF, &_Py_ID(__classdict__), cellvars);
}
/* compile the body proper */
RETURN_IF_ERROR_IN_SCOPE(c, _PyCodegen_Body(c, loc, s->v.ClassDef.body));
RETURN_IF_ERROR_IN_SCOPE(c, _PyCodegen_Body(c, loc, s->v.ClassDef.body, false));
PyObject *static_attributes = _PyCompile_StaticAttributesAsTuple(c);
if (static_attributes == NULL) {
_PyCompile_ExitScope(c);