]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue 4597: Fix several cases in EvalFrameEx where an exception could be
authorJeffrey Yasskin <jyasskin@gmail.com>
Mon, 8 Dec 2008 18:55:24 +0000 (18:55 +0000)
committerJeffrey Yasskin <jyasskin@gmail.com>
Mon, 8 Dec 2008 18:55:24 +0000 (18:55 +0000)
"raised" without setting x, err, or why to let the eval loop know.

Lib/test/test_file.py
Python/ceval.c

index b93bdbd9d0a5bd28c4b8e7ad64e945890d840c29..2d791a55c07f0df91f545cede9fff7c71440ca77 100644 (file)
@@ -531,6 +531,20 @@ class StdoutTests(unittest.TestCase):
         finally:
             sys.stdout = save_stdout
 
+    def test_del_stdout_before_print(self):
+        # Issue 4597: 'print' with no argument wasn't reporting when
+        # sys.stdout was deleted.
+        save_stdout = sys.stdout
+        del sys.stdout
+        try:
+            print
+        except RuntimeError as e:
+            self.assertEquals(str(e), "lost sys.stdout")
+        else:
+            self.fail("Expected RuntimeError")
+        finally:
+            sys.stdout = save_stdout
+
 
 def test_main():
     # Historically, these tests have been sloppy about removing TESTFN.
index cdcf9f6f32abffd3b7b5a07e5e5e7ee98c116a0a..0a1d86a9d702f3f7ba0cb7520e94ac9d2a02d4e7 100644 (file)
@@ -1049,6 +1049,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
                        }
                        Py_FatalError("invalid argument to DUP_TOPX"
                                      " (bytecode corruption?)");
+                       /* Never returns, so don't bother to set why. */
                        break;
 
                case UNARY_POSITIVE:
@@ -1642,9 +1643,11 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
                case PRINT_NEWLINE:
                        if (stream == NULL || stream == Py_None) {
                                w = PySys_GetObject("stdout");
-                               if (w == NULL)
+                               if (w == NULL) {
                                        PyErr_SetString(PyExc_RuntimeError,
                                                        "lost sys.stdout");
+                                       why = WHY_EXCEPTION;
+                               }
                        }
                        if (w != NULL) {
                                /* w.write() may replace sys.stdout, so we
@@ -1870,6 +1873,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
                                PyErr_Format(PyExc_SystemError,
                                             "no locals when loading %s",
                                             PyObject_REPR(w));
+                               why = WHY_EXCEPTION;
                                break;
                        }
                        if (PyDict_CheckExact(v)) {
@@ -2459,7 +2463,10 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
                        Py_DECREF(v);
                        if (x != NULL) {
                                v = POP();
-                               err = PyFunction_SetClosure(x, v);
+                               if (PyFunction_SetClosure(x, v) != 0) {
+                                       /* Can't happen unless bytecode is corrupt. */
+                                       why = WHY_EXCEPTION;
+                               }
                                Py_DECREF(v);
                        }
                        if (x != NULL && oparg > 0) {
@@ -2473,7 +2480,11 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
                                        w = POP();
                                        PyTuple_SET_ITEM(v, oparg, w);
                                }
-                               err = PyFunction_SetDefaults(x, v);
+                               if (PyFunction_SetDefaults(x, v) != 0) {
+                                       /* Can't happen unless
+                                           PyFunction_SetDefaults changes. */
+                                       why = WHY_EXCEPTION;
+                               }
                                Py_DECREF(v);
                        }
                        PUSH(x);