]> granicus.if.org Git - python/commitdiff
Issue2221: in Idle, exec('xx') raised a SystemError('error return without exception...
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 4 Apr 2008 23:25:27 +0000 (23:25 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 4 Apr 2008 23:25:27 +0000 (23:25 +0000)
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...

Lib/test/test_builtin.py
Python/pythonrun.c

index 111090c1ab09578f0df1cc5a89b031c731fae4a2..7244aff8eff857f85751bd655d5cfded231ac36c 100644 (file)
@@ -448,6 +448,17 @@ class BuiltinTest(unittest.TestCase):
             del l['__builtins__']
         self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
 
+    def test_exec_redirected(self):
+        savestdout = sys.stdout
+        sys.stdout = None # Whatever that cannot flush()
+        try:
+            # Used to raise SystemError('error return without exception set')
+            exec('a')
+        except NameError:
+            pass
+        finally:
+            sys.stdout = savestdout
+
     def test_filter(self):
         self.assertEqual(list(filter(lambda c: 'a' <= c <= 'z', 'Hello World')), list('elloorld'))
         self.assertEqual(list(filter(None, [1, 'hello', [], [3], '', None, 9, 0])), [1, 'hello', [3], 9])
index 74e3430e9722a88c16fa90973afb96959edfad34..3207fb8b8174465a02e5c482505690f3dd3ed42b 100644 (file)
@@ -1467,6 +1467,11 @@ static void
 flush_io(void)
 {
        PyObject *f, *r;
+       PyObject *type, *value, *traceback;
+
+       /* Save the current exception */
+       PyErr_Fetch(&type, &value, &traceback);
+
        f = PySys_GetObject("stderr");
        if (f != NULL) {
                r = PyObject_CallMethod(f, "flush", "");
@@ -1483,6 +1488,8 @@ flush_io(void)
                else
                        PyErr_Clear();
        }
+
+       PyErr_Restore(type, value, traceback);
 }
 
 static PyObject *