summaryrefslogtreecommitdiffstats
path: root/Python/pythonrun.c
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2003-08-09 09:47:11 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2003-08-09 09:47:11 (GMT)
commita2c17c58206cb3fc75bb5331a805663d635c62e2 (patch)
tree87ba4e19026a8f385336068b301d27f0b3afb479 /Python/pythonrun.c
parentfc9b75fafb8b1fefc75b47f1f3135536167dfc07 (diff)
downloadcpython-a2c17c58206cb3fc75bb5331a805663d635c62e2.zip
cpython-a2c17c58206cb3fc75bb5331a805663d635c62e2.tar.gz
cpython-a2c17c58206cb3fc75bb5331a805663d635c62e2.tar.bz2
Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Verify that the encoding actually exists. Fixes #775985. Will backport to 2.3.
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r--Python/pythonrun.c66
1 files changed, 51 insertions, 15 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index de4a77f..f82de0a 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -77,7 +77,7 @@ static PyObject *warnings_module = NULL;
If the module is returned, it is guaranteed to have been obtained
without acquiring the import lock
*/
-PyObject *PyModule_GetWarningsModule()
+PyObject *PyModule_GetWarningsModule(void)
{
PyObject *typ, *val, *tb;
PyObject *all_modules;
@@ -142,6 +142,11 @@ Py_Initialize(void)
PyThreadState *tstate;
PyObject *bimod, *sysmod;
char *p;
+#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
+ char *codeset;
+ char *saved_locale;
+ PyObject *sys_stream, *sys_isatty;
+#endif
extern void _Py_ReadyTypes(void);
if (initialized)
@@ -227,21 +232,52 @@ Py_Initialize(void)
/* On Unix, set the file system encoding according to the
user's preference, if the CODESET names a well-known
Python codec, and Py_FileSystemDefaultEncoding isn't
- initialized by other means. */
- if (!Py_FileSystemDefaultEncoding) {
- char *saved_locale = setlocale(LC_CTYPE, NULL);
- char *codeset;
- setlocale(LC_CTYPE, "");
- codeset = nl_langinfo(CODESET);
- if (*codeset) {
- PyObject *enc = PyCodec_Encoder(codeset);
- if (enc) {
- Py_FileSystemDefaultEncoding = strdup(codeset);
- Py_DECREF(enc);
- } else
- PyErr_Clear();
+ initialized by other means. Also set the encoding of
+ stdin and stdout if these are terminals. */
+
+ saved_locale = setlocale(LC_CTYPE, NULL);
+ setlocale(LC_CTYPE, "");
+ codeset = nl_langinfo(CODESET);
+ if (codeset && *codeset) {
+ PyObject *enc = PyCodec_Encoder(codeset);
+ if (enc) {
+ codeset = strdup(codeset);
+ Py_DECREF(enc);
+ } else {
+ codeset = NULL;
+ PyErr_Clear();
+ }
+ } else
+ codeset = NULL;
+ setlocale(LC_CTYPE, saved_locale);
+
+ if (codeset) {
+ sys_stream = PySys_GetObject("stdout");
+ sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
+ if (!sys_isatty)
+ PyErr_Clear();
+ if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
+ if (!PyFile_SetEncoding(sys_stream, codeset))
+ Py_FatalError("Cannot set codeset of stdin");
}
- setlocale(LC_CTYPE, saved_locale);
+ Py_XDECREF(sys_stream);
+ Py_XDECREF(sys_isatty);
+
+ sys_stream = PySys_GetObject("stdout");
+ sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
+ if (!sys_isatty)
+ PyErr_Clear();
+ if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
+ if (!PyFile_SetEncoding(sys_stream, codeset))
+ Py_FatalError("Cannot set codeset of stdout");
+ }
+ Py_XDECREF(sys_stream);
+ Py_XDECREF(sys_isatty);
+
+ if (!Py_FileSystemDefaultEncoding)
+ Py_FileSystemDefaultEncoding = codeset;
+ else
+ free(codeset);
}
#endif
}