mirror of
https://github.com/python/cpython.git
synced 2025-09-16 21:56:14 +00:00
Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding and
error handler, instead of writing to the C stderr file in utf-8
This commit is contained in:
parent
7c4d7d3e17
commit
c49dfcc8dc
3 changed files with 30 additions and 1 deletions
|
@ -178,6 +178,26 @@ class SysModuleTest(unittest.TestCase):
|
||||||
"raise SystemExit(47)"])
|
"raise SystemExit(47)"])
|
||||||
self.assertEqual(rc, 47)
|
self.assertEqual(rc, 47)
|
||||||
|
|
||||||
|
def check_exit_message(code, expected, env=None):
|
||||||
|
process = subprocess.Popen([sys.executable, "-c", code],
|
||||||
|
stderr=subprocess.PIPE, env=env)
|
||||||
|
stdout, stderr = process.communicate()
|
||||||
|
self.assertEqual(process.returncode, 1)
|
||||||
|
self.assertTrue(stderr.startswith(expected),
|
||||||
|
"%s doesn't start with %s" % (repr(stderr), repr(expected)))
|
||||||
|
|
||||||
|
# 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 unicode message is encoded to the stderr encoding
|
||||||
|
env = os.environ.copy()
|
||||||
|
env['PYTHONIOENCODING'] = 'latin-1'
|
||||||
|
check_exit_message(
|
||||||
|
r'import sys; sys.exit(u"h\xe9")',
|
||||||
|
b"h\xe9", env=env)
|
||||||
|
|
||||||
def test_getdefaultencoding(self):
|
def test_getdefaultencoding(self):
|
||||||
if test.test_support.have_unicode:
|
if test.test_support.have_unicode:
|
||||||
|
|
|
@ -12,6 +12,9 @@ What's New in Python 2.7 Release Candidate 1?
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #3798: Write sys.exit() message to sys.stderr to use stderr encoding
|
||||||
|
and error handler, instead of writing to the C stderr file in utf-8
|
||||||
|
|
||||||
- Issue #7902: When using explicit relative import syntax, don't try implicit
|
- Issue #7902: When using explicit relative import syntax, don't try implicit
|
||||||
relative import semantics.
|
relative import semantics.
|
||||||
|
|
||||||
|
|
|
@ -1106,7 +1106,13 @@ handle_system_exit(void)
|
||||||
if (PyInt_Check(value))
|
if (PyInt_Check(value))
|
||||||
exitcode = (int)PyInt_AsLong(value);
|
exitcode = (int)PyInt_AsLong(value);
|
||||||
else {
|
else {
|
||||||
|
PyObject *sys_stderr = PySys_GetObject("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);
|
PyObject_Print(value, stderr, Py_PRINT_RAW);
|
||||||
|
fflush(stderr);
|
||||||
|
}
|
||||||
PySys_WriteStderr("\n");
|
PySys_WriteStderr("\n");
|
||||||
exitcode = 1;
|
exitcode = 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue