diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2003-08-09 09:48:29 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2003-08-09 09:48:29 (GMT) |
commit | 144b440fc200df72b860c2b5d8932bb626a764f2 (patch) | |
tree | b73b2f92dfe6cc46594ca56f3302e27aaba34a9e | |
parent | 59f31be670d73573fbc6f4176302bdd4a6139d4b (diff) | |
download | cpython-144b440fc200df72b860c2b5d8932bb626a764f2.zip cpython-144b440fc200df72b860c2b5d8932bb626a764f2.tar.gz cpython-144b440fc200df72b860c2b5d8932bb626a764f2.tar.bz2 |
Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Verify that the encoding actually exists. Fixes #775985.
-rw-r--r-- | Misc/NEWS | 2 | ||||
-rw-r--r-- | Python/pythonrun.c | 66 | ||||
-rw-r--r-- | Python/sysmodule.c | 18 |
3 files changed, 53 insertions, 33 deletions
@@ -12,6 +12,8 @@ What's New in Python 2.3.1? Core and builtins ----------------- +- Bug #775985: Only set stdout.encoding if a codec is available. + Extension modules ----------------- 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 } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 8c77a88..c98e9f1 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -905,9 +905,6 @@ _PySys_Init(void) #ifdef MS_WINDOWS char buf[10]; #endif -#if defined(HAVE_LANGINFO_H) && defined(CODESET) - char *oldloc, *codeset; -#endif m = Py_InitModule3("sys", sys_methods, sys_doc); sysdict = PyModule_GetDict(m); @@ -930,21 +927,6 @@ _PySys_Init(void) } #endif -#if defined(HAVE_LANGINFO_H) && defined(CODESET) - oldloc = setlocale(LC_CTYPE, 0); - setlocale(LC_CTYPE, ""); - codeset = nl_langinfo(CODESET); - setlocale(LC_CTYPE, oldloc); - if(codeset && isatty(fileno(stdin))){ - if (!PyFile_SetEncoding(sysin, codeset)) - return NULL; - } - if(codeset && isatty(fileno(stdout))) { - if (!PyFile_SetEncoding(sysout, codeset)) - return NULL; - } -#endif - PyDict_SetItemString(sysdict, "stdin", sysin); PyDict_SetItemString(sysdict, "stdout", sysout); PyDict_SetItemString(sysdict, "stderr", syserr); |