summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_sys.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-05-17 08:58:51 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-05-17 08:58:51 (GMT)
commite9fb319e688d2d76ab4dbf8b3165ce9d1e24e8fc (patch)
treee4922f7cec794522aa025616e1d160fcfc79b792 /Lib/test/test_sys.py
parent372ac5e73260d6e8c8aefe31fd979a7706841868 (diff)
downloadcpython-e9fb319e688d2d76ab4dbf8b3165ce9d1e24e8fc.zip
cpython-e9fb319e688d2d76ab4dbf8b3165ce9d1e24e8fc.tar.gz
cpython-e9fb319e688d2d76ab4dbf8b3165ce9d1e24e8fc.tar.bz2
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).
Diffstat (limited to 'Lib/test/test_sys.py')
-rw-r--r--Lib/test/test_sys.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index ecbc9db..2dd2746 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -86,6 +86,8 @@ class SysModuleTest(unittest.TestCase):
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
def test_exit(self):
+ import subprocess
+
self.assertRaises(TypeError, sys.exit, 42, 42)
# call without argument
@@ -140,20 +142,28 @@ class SysModuleTest(unittest.TestCase):
self.fail("no exception")
# test that the exit machinery handles SystemExits properly
- import subprocess
rc = subprocess.call([sys.executable, "-c",
"raise SystemExit(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
# handler to stderr
- import subprocess
- code = r'import sys; sys.exit("surrogates:\uDCFF")'
- process = subprocess.Popen([sys.executable, "-c", code],
- stderr=subprocess.PIPE)
- stdout, stderr = process.communicate()
- self.assertEqual(process.returncode, 1)
- self.assertTrue(stderr.startswith(b"surrogates:\\udcff"), stderr)
+ check_exit_message(
+ r'import sys; sys.exit("surrogates:\uDCFF")',
+ b"surrogates:\\udcff")
def test_getdefaultencoding(self):
self.assertRaises(TypeError, sys.getdefaultencoding, 42)