handle_system_exit() flushs files to warranty the output order
PyObject_Print() writes into the C object stderr, whereas PySys_WriteStderr() writes into the Python object sys.stderr. Each object has its own buffer, so call sys.stderr.flush() and fflush(stderr).
This commit is contained in:
parent
372ac5e732
commit
e9fb319e68
@ -86,6 +86,8 @@ class SysModuleTest(unittest.TestCase):
|
|||||||
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
|
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
|
||||||
|
|
||||||
def test_exit(self):
|
def test_exit(self):
|
||||||
|
import subprocess
|
||||||
|
|
||||||
self.assertRaises(TypeError, sys.exit, 42, 42)
|
self.assertRaises(TypeError, sys.exit, 42, 42)
|
||||||
|
|
||||||
# call without argument
|
# call without argument
|
||||||
@ -140,20 +142,28 @@ class SysModuleTest(unittest.TestCase):
|
|||||||
self.fail("no exception")
|
self.fail("no exception")
|
||||||
|
|
||||||
# test that the exit machinery handles SystemExits properly
|
# test that the exit machinery handles SystemExits properly
|
||||||
import subprocess
|
|
||||||
rc = subprocess.call([sys.executable, "-c",
|
rc = subprocess.call([sys.executable, "-c",
|
||||||
"raise SystemExit(47)"])
|
"raise SystemExit(47)"])
|
||||||
self.assertEqual(rc, 47)
|
self.assertEqual(rc, 47)
|
||||||
|
|
||||||
|
def check_exit_message(code, expected):
|
||||||
|
process = subprocess.Popen([sys.executable, "-c", code],
|
||||||
|
stderr=subprocess.PIPE)
|
||||||
|
stdout, stderr = process.communicate()
|
||||||
|
self.assertEqual(process.returncode, 1)
|
||||||
|
self.assertTrue(stderr.startswith(expected), stderr)
|
||||||
|
|
||||||
|
# test that stderr buffer if flushed before the exit message is written
|
||||||
|
# into stderr
|
||||||
|
check_exit_message(
|
||||||
|
r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
|
||||||
|
b"unflushed,message")
|
||||||
|
|
||||||
# test that the exit message is written with backslashreplace error
|
# test that the exit message is written with backslashreplace error
|
||||||
# handler to stderr
|
# handler to stderr
|
||||||
import subprocess
|
check_exit_message(
|
||||||
code = r'import sys; sys.exit("surrogates:\uDCFF")'
|
r'import sys; sys.exit("surrogates:\uDCFF")',
|
||||||
process = subprocess.Popen([sys.executable, "-c", code],
|
b"surrogates:\\udcff")
|
||||||
stderr=subprocess.PIPE)
|
|
||||||
stdout, stderr = process.communicate()
|
|
||||||
self.assertEqual(process.returncode, 1)
|
|
||||||
self.assertTrue(stderr.startswith(b"surrogates:\\udcff"), stderr)
|
|
||||||
|
|
||||||
def test_getdefaultencoding(self):
|
def test_getdefaultencoding(self):
|
||||||
self.assertRaises(TypeError, sys.getdefaultencoding, 42)
|
self.assertRaises(TypeError, sys.getdefaultencoding, 42)
|
||||||
|
@ -1367,7 +1367,11 @@ handle_system_exit(void)
|
|||||||
if (PyLong_Check(value))
|
if (PyLong_Check(value))
|
||||||
exitcode = (int)PyLong_AsLong(value);
|
exitcode = (int)PyLong_AsLong(value);
|
||||||
else {
|
else {
|
||||||
|
PyObject *sys_stderr = PySys_GetObject("stderr");
|
||||||
|
if (sys_stderr != NULL)
|
||||||
|
PyObject_CallMethod(sys_stderr, "flush", NULL);
|
||||||
PyObject_Print(value, stderr, Py_PRINT_RAW);
|
PyObject_Print(value, stderr, Py_PRINT_RAW);
|
||||||
|
fflush(stderr);
|
||||||
PySys_WriteStderr("\n");
|
PySys_WriteStderr("\n");
|
||||||
exitcode = 1;
|
exitcode = 1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user