Skip to content

Commit 4a386f6

Browse files
author
jeffrey.yasskin
committed
Issue 4597: Fix several cases in EvalFrameEx where an exception could be
"raised" without setting x, err, or why to let the eval loop know. git-svn-id: http://svn.python.org/projects/python/trunk@67666 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent d2c4765 commit 4a386f6

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

Lib/test/test_file.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,20 @@ def write(self, data):
531531
finally:
532532
sys.stdout = save_stdout
533533

534+
def test_del_stdout_before_print(self):
535+
# Issue 4597: 'print' with no argument wasn't reporting when
536+
# sys.stdout was deleted.
537+
save_stdout = sys.stdout
538+
del sys.stdout
539+
try:
540+
print
541+
except RuntimeError as e:
542+
self.assertEquals(str(e), "lost sys.stdout")
543+
else:
544+
self.fail("Expected RuntimeError")
545+
finally:
546+
sys.stdout = save_stdout
547+
534548

535549
def test_main():
536550
# Historically, these tests have been sloppy about removing TESTFN.

Python/ceval.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
10491049
}
10501050
Py_FatalError("invalid argument to DUP_TOPX"
10511051
" (bytecode corruption?)");
1052+
/* Never returns, so don't bother to set why. */
10521053
break;
10531054

10541055
case UNARY_POSITIVE:
@@ -1642,9 +1643,11 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
16421643
case PRINT_NEWLINE:
16431644
if (stream == NULL || stream == Py_None) {
16441645
w = PySys_GetObject("stdout");
1645-
if (w == NULL)
1646+
if (w == NULL) {
16461647
PyErr_SetString(PyExc_RuntimeError,
16471648
"lost sys.stdout");
1649+
why = WHY_EXCEPTION;
1650+
}
16481651
}
16491652
if (w != NULL) {
16501653
/* w.write() may replace sys.stdout, so we
@@ -1870,6 +1873,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
18701873
PyErr_Format(PyExc_SystemError,
18711874
"no locals when loading %s",
18721875
PyObject_REPR(w));
1876+
why = WHY_EXCEPTION;
18731877
break;
18741878
}
18751879
if (PyDict_CheckExact(v)) {
@@ -2459,7 +2463,10 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
24592463
Py_DECREF(v);
24602464
if (x != NULL) {
24612465
v = POP();
2462-
err = PyFunction_SetClosure(x, v);
2466+
if (PyFunction_SetClosure(x, v) != 0) {
2467+
/* Can't happen unless bytecode is corrupt. */
2468+
why = WHY_EXCEPTION;
2469+
}
24632470
Py_DECREF(v);
24642471
}
24652472
if (x != NULL && oparg > 0) {
@@ -2473,7 +2480,11 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
24732480
w = POP();
24742481
PyTuple_SET_ITEM(v, oparg, w);
24752482
}
2476-
err = PyFunction_SetDefaults(x, v);
2483+
if (PyFunction_SetDefaults(x, v) != 0) {
2484+
/* Can't happen unless
2485+
PyFunction_SetDefaults changes. */
2486+
why = WHY_EXCEPTION;
2487+
}
24772488
Py_DECREF(v);
24782489
}
24792490
PUSH(x);

0 commit comments

Comments
 (0)