comparison Python/pythonrun.c @ 30108:a88e6dfbfd6d legacy-trunk

Move initialization of sys.std{in,out}.encoding to Py_Initialize. Verify that the encoding actually exists. Fixes #775985. Will backport to 2.3.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 09 Aug 2003 09:47:11 +0000
parents ed8709b1a3ef
children 210676ec9d5f
comparison
equal deleted inserted replaced
30106:265079883668 30108:a88e6dfbfd6d
75 75
76 /* Returns a borrowed reference to the 'warnings' module, or NULL. 76 /* Returns a borrowed reference to the 'warnings' module, or NULL.
77 If the module is returned, it is guaranteed to have been obtained 77 If the module is returned, it is guaranteed to have been obtained
78 without acquiring the import lock 78 without acquiring the import lock
79 */ 79 */
80 PyObject *PyModule_GetWarningsModule() 80 PyObject *PyModule_GetWarningsModule(void)
81 { 81 {
82 PyObject *typ, *val, *tb; 82 PyObject *typ, *val, *tb;
83 PyObject *all_modules; 83 PyObject *all_modules;
84 /* If we managed to get the module at init time, just use it */ 84 /* If we managed to get the module at init time, just use it */
85 if (warnings_module) 85 if (warnings_module)
140 { 140 {
141 PyInterpreterState *interp; 141 PyInterpreterState *interp;
142 PyThreadState *tstate; 142 PyThreadState *tstate;
143 PyObject *bimod, *sysmod; 143 PyObject *bimod, *sysmod;
144 char *p; 144 char *p;
145 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
146 char *codeset;
147 char *saved_locale;
148 PyObject *sys_stream, *sys_isatty;
149 #endif
145 extern void _Py_ReadyTypes(void); 150 extern void _Py_ReadyTypes(void);
146 151
147 if (initialized) 152 if (initialized)
148 return; 153 return;
149 initialized = 1; 154 initialized = 1;
225 230
226 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) 231 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
227 /* On Unix, set the file system encoding according to the 232 /* On Unix, set the file system encoding according to the
228 user's preference, if the CODESET names a well-known 233 user's preference, if the CODESET names a well-known
229 Python codec, and Py_FileSystemDefaultEncoding isn't 234 Python codec, and Py_FileSystemDefaultEncoding isn't
230 initialized by other means. */ 235 initialized by other means. Also set the encoding of
231 if (!Py_FileSystemDefaultEncoding) { 236 stdin and stdout if these are terminals. */
232 char *saved_locale = setlocale(LC_CTYPE, NULL); 237
233 char *codeset; 238 saved_locale = setlocale(LC_CTYPE, NULL);
234 setlocale(LC_CTYPE, ""); 239 setlocale(LC_CTYPE, "");
235 codeset = nl_langinfo(CODESET); 240 codeset = nl_langinfo(CODESET);
236 if (*codeset) { 241 if (codeset && *codeset) {
237 PyObject *enc = PyCodec_Encoder(codeset); 242 PyObject *enc = PyCodec_Encoder(codeset);
238 if (enc) { 243 if (enc) {
239 Py_FileSystemDefaultEncoding = strdup(codeset); 244 codeset = strdup(codeset);
240 Py_DECREF(enc); 245 Py_DECREF(enc);
241 } else 246 } else {
242 PyErr_Clear(); 247 codeset = NULL;
243 } 248 PyErr_Clear();
244 setlocale(LC_CTYPE, saved_locale); 249 }
250 } else
251 codeset = NULL;
252 setlocale(LC_CTYPE, saved_locale);
253
254 if (codeset) {
255 sys_stream = PySys_GetObject("stdout");
256 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
257 if (!sys_isatty)
258 PyErr_Clear();
259 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
260 if (!PyFile_SetEncoding(sys_stream, codeset))
261 Py_FatalError("Cannot set codeset of stdin");
262 }
263 Py_XDECREF(sys_stream);
264 Py_XDECREF(sys_isatty);
265
266 sys_stream = PySys_GetObject("stdout");
267 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
268 if (!sys_isatty)
269 PyErr_Clear();
270 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
271 if (!PyFile_SetEncoding(sys_stream, codeset))
272 Py_FatalError("Cannot set codeset of stdout");
273 }
274 Py_XDECREF(sys_stream);
275 Py_XDECREF(sys_isatty);
276
277 if (!Py_FileSystemDefaultEncoding)
278 Py_FileSystemDefaultEncoding = codeset;
279 else
280 free(codeset);
245 } 281 }
246 #endif 282 #endif
247 } 283 }
248 284
249 #ifdef COUNT_ALLOCS 285 #ifdef COUNT_ALLOCS