Skip to content

Commit 9b975be

Browse files
author
amaury.forgeotdarc
committed
Issue2221: in Idle, exec('xx') raised a SystemError('error return without exception set')
instead of the expected NameError This happens when sys.stdout is redirected to something that cannot flush(). the flush_io() function must be exception-neutral: don't raise, and don't clear exceptions. Next step: exec() is not supposed to flush sys.stdout... git-svn-id: http://svn.python.org/projects/python/branches/py3k@62157 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent deb941d commit 9b975be

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Lib/test/test_builtin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,17 @@ def test_exec(self):
448448
del l['__builtins__']
449449
self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
450450

451+
def test_exec_redirected(self):
452+
savestdout = sys.stdout
453+
sys.stdout = None # Whatever that cannot flush()
454+
try:
455+
# Used to raise SystemError('error return without exception set')
456+
exec('a')
457+
except NameError:
458+
pass
459+
finally:
460+
sys.stdout = savestdout
461+
451462
def test_filter(self):
452463
self.assertEqual(list(filter(lambda c: 'a' <= c <= 'z', 'Hello World')), list('elloorld'))
453464
self.assertEqual(list(filter(None, [1, 'hello', [], [3], '', None, 9, 0])), [1, 'hello', [3], 9])

Python/pythonrun.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,11 @@ static void
14671467
flush_io(void)
14681468
{
14691469
PyObject *f, *r;
1470+
PyObject *type, *value, *traceback;
1471+
1472+
/* Save the current exception */
1473+
PyErr_Fetch(&type, &value, &traceback);
1474+
14701475
f = PySys_GetObject("stderr");
14711476
if (f != NULL) {
14721477
r = PyObject_CallMethod(f, "flush", "");
@@ -1483,6 +1488,8 @@ flush_io(void)
14831488
else
14841489
PyErr_Clear();
14851490
}
1491+
1492+
PyErr_Restore(type, value, traceback);
14861493
}
14871494

14881495
static PyObject *

0 commit comments

Comments
 (0)