]> granicus.if.org Git - python/commitdiff
Plug the most annoying recursive printing problem -- reset '_' to None
authorGuido van Rossum <guido@python.org>
Fri, 26 Dec 1997 22:15:57 +0000 (22:15 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 26 Dec 1997 22:15:57 +0000 (22:15 +0000)
before printing and set it to the printed variable *after* printing
(and only when printing is successful).

Python/ceval.c

index fad8c2bf58d6724e2a57e648f82c171c1582d8a4..8b7447cc2448c7472f7da6e9cf6cea933ec74da3 100644 (file)
@@ -1008,11 +1008,12 @@ eval_code2(co, globals, locals,
                
                case PRINT_EXPR:
                        v = POP();
-                       /* Print value except if procedure result */
-                       /* Before printing, also assign to '_' */
+                       /* Print value except if None */
+                       /* After printing, also assign to '_' */
+                       /* Before, set '_' to None to avoid recursion */
                        if (v != Py_None &&
                            (err = PyDict_SetItemString(
-                                   f->f_builtins, "_", v)) == 0) {
+                                   f->f_builtins, "_", Py_None)) == 0) {
                                err = Py_FlushLine();
                                if (err == 0) {
                                        x = PySys_GetObject("stdout");
@@ -1025,6 +1026,10 @@ eval_code2(co, globals, locals,
                                        PyFile_SoftSpace(x, 1);
                                        err = Py_FlushLine();
                                }
+                               if (err == 0) {
+                                       err = PyDict_SetItemString(
+                                               f->f_builtins, "_", v);
+                               }
                        }
                        Py_DECREF(v);
                        break;