Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa06b0e

Browse files
committed
Plug the most annoying recursive printing problem -- reset '_' to None
before printing and set it to the printed variable *after* printing (and only when printing is successful).
1 parent 81e84c9 commit aa06b0e

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

Python/ceval.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,11 +1008,12 @@ eval_code2(co, globals, locals,
10081008

10091009
case PRINT_EXPR:
10101010
v = POP();
1011-
/* Print value except if procedure result */
1012-
/* Before printing, also assign to '_' */
1011+
/* Print value except if None */
1012+
/* After printing, also assign to '_' */
1013+
/* Before, set '_' to None to avoid recursion */
10131014
if (v != Py_None &&
10141015
(err = PyDict_SetItemString(
1015-
f->f_builtins, "_", v)) == 0) {
1016+
f->f_builtins, "_", Py_None)) == 0) {
10161017
err = Py_FlushLine();
10171018
if (err == 0) {
10181019
x = PySys_GetObject("stdout");
@@ -1025,6 +1026,10 @@ eval_code2(co, globals, locals,
10251026
PyFile_SoftSpace(x, 1);
10261027
err = Py_FlushLine();
10271028
}
1029+
if (err == 0) {
1030+
err = PyDict_SetItemString(
1031+
f->f_builtins, "_", v);
1032+
}
10281033
}
10291034
Py_DECREF(v);
10301035
break;

0 commit comments

Comments
 (0)