Skip to content

Commit ea62d25

Browse files
committed
Bug #1421664: Set sys.stderr.encoding
1 parent cf0a2a8 commit ea62d25

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.5 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Bug #1421664: sys.stderr.encoding is now set to the same value as
16+
sys.stdout.encoding.
17+
1518
- __import__ accepts keyword arguments.
1619

1720
- Patch #1460496: round() now accepts keyword arguments.

‎Python/pythonrun.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ Py_InitializeEx(int install_sigs)
281281
}
282282
Py_XDECREF(sys_isatty);
283283

284+
sys_stream = PySys_GetObject("stderr");
285+
sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
286+
if (!sys_isatty)
287+
PyErr_Clear();
288+
if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
289+
if (!PyFile_SetEncoding(sys_stream, codeset))
290+
Py_FatalError("Cannot set codeset of stderr");
291+
}
292+
Py_XDECREF(sys_isatty);
293+
284294
if (!Py_FileSystemDefaultEncoding)
285295
Py_FileSystemDefaultEncoding = codeset;
286296
else

‎Python/sysmodule.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,11 @@ _PySys_Init(void)
10691069
if (!PyFile_SetEncoding(sysout, buf))
10701070
return NULL;
10711071
}
1072+
if(isatty(_fileno(stderr))) {
1073+
sprintf(buf, "cp%d", GetConsoleOutputCP());
1074+
if (!PyFile_SetEncoding(syserr, buf))
1075+
return NULL;
1076+
}
10721077
#endif
10731078

10741079
PyDict_SetItemString(sysdict, "stdin", sysin);

0 commit comments

Comments
 (0)