]> granicus.if.org Git - python/commitdiff
Issue #3798: sys.exit(message) writes the message to sys.stderr file, instead
authorVictor Stinner <victor.stinner@haypocalc.com>
Fri, 21 May 2010 23:45:42 +0000 (23:45 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Fri, 21 May 2010 23:45:42 +0000 (23:45 +0000)
of the C file stderr, to use stderr encoding and error handler

Lib/test/test_sys.py
Misc/NEWS
Python/pythonrun.c

index 4fb1d366a571fb0a338522a56446e91ba319483f..2caf09fc784e792b7ea792807d8f726dd188aea0 100644 (file)
@@ -146,9 +146,9 @@ class SysModuleTest(unittest.TestCase):
                               "raise SystemExit(47)"])
         self.assertEqual(rc, 47)
 
-        def check_exit_message(code, expected):
+        def check_exit_message(code, expected, env=None):
             process = subprocess.Popen([sys.executable, "-c", code],
-                                       stderr=subprocess.PIPE)
+                                       stderr=subprocess.PIPE, env=env)
             stdout, stderr = process.communicate()
             self.assertEqual(process.returncode, 1)
             self.assertTrue(stderr.startswith(expected),
@@ -166,6 +166,14 @@ class SysModuleTest(unittest.TestCase):
             r'import sys; sys.exit("surrogates:\uDCFF")',
             b"surrogates:\\udcff")
 
+        # test that the unicode message is encoded to the stderr encoding
+        # instead of the default encoding (utf8)
+        env = os.environ.copy()
+        env['PYTHONIOENCODING'] = 'latin-1'
+        check_exit_message(
+            r'import sys; sys.exit("h\xe9")',
+            b"h\xe9", env=env)
+
     def test_getdefaultencoding(self):
         self.assertRaises(TypeError, sys.getdefaultencoding, 42)
         # can't check more than the type, as the user might have changed it
index 0c9a8a997e3ebd477cb3b8826f148902dc65a996..7c2cc5de93802c07573b8edc7eff0f33db888aa4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -393,6 +393,9 @@ C-API
 Library
 -------
 
+- Issue #3798: sys.exit(message) writes the message to sys.stderr file, instead
+  of the C file stderr, to use stderr encoding and error handler
+
 - Issue #8782: Add a trailing newline in linecache.updatecache to the last line
   of files without one.
 
index b469c4a625f1f35a436a4624233bce15cf922ec3..1581c90fb318382dc5290a7c5512d55198a37d41 100644 (file)
@@ -1386,10 +1386,12 @@ handle_system_exit(void)
         exitcode = (int)PyLong_AsLong(value);
     else {
         PyObject *sys_stderr = PySys_GetObject("stderr");
-        if (sys_stderr != NULL)
-            PyObject_CallMethod(sys_stderr, "flush", NULL);
-        PyObject_Print(value, stderr, Py_PRINT_RAW);
-        fflush(stderr);
+        if (sys_stderr != NULL && sys_stderr != Py_None) {
+            PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
+        } else {
+            PyObject_Print(value, stderr, Py_PRINT_RAW);
+            fflush(stderr);
+        }
         PySys_WriteStderr("\n");
         exitcode = 1;
     }