comparison Python/pythonrun.c @ 42539:43e32b2b4004

Set sys.stdout.encoding properly. Always set LC_CTYPE on interpreter startup. Add device_encoding function.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 11 Aug 2007 14:02:14 +0000
parents 9f99ea4f81e9
children 20477cbfd996
comparison
equal deleted inserted replaced
42538:9c3e6440f54c 42539:43e32b2b4004
152 PyThreadState *tstate; 152 PyThreadState *tstate;
153 PyObject *bimod, *sysmod; 153 PyObject *bimod, *sysmod;
154 char *p; 154 char *p;
155 #if defined(HAVE_LANGINFO_H) && defined(CODESET) 155 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
156 char *codeset; 156 char *codeset;
157 char *saved_locale;
158 #endif 157 #endif
159 extern void _Py_ReadyTypes(void); 158 extern void _Py_ReadyTypes(void);
160 159
161 if (initialized) 160 if (initialized)
162 return; 161 return;
163 initialized = 1; 162 initialized = 1;
163
164 #ifdef HAVE_SETLOCALE
165 /* Set up the LC_CTYPE locale, so we can obtain
166 the locale's charset without having to switch
167 locales. */
168 setlocale(LC_CTYPE, "");
169 #endif
164 170
165 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0') 171 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
166 Py_DebugFlag = add_flag(Py_DebugFlag, p); 172 Py_DebugFlag = add_flag(Py_DebugFlag, p);
167 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0') 173 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
168 Py_VerboseFlag = add_flag(Py_VerboseFlag, p); 174 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
252 user's preference, if the CODESET names a well-known 258 user's preference, if the CODESET names a well-known
253 Python codec, and Py_FileSystemDefaultEncoding isn't 259 Python codec, and Py_FileSystemDefaultEncoding isn't
254 initialized by other means. Also set the encoding of 260 initialized by other means. Also set the encoding of
255 stdin and stdout if these are terminals. */ 261 stdin and stdout if these are terminals. */
256 262
257 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
258 setlocale(LC_CTYPE, "");
259 codeset = nl_langinfo(CODESET); 263 codeset = nl_langinfo(CODESET);
260 if (codeset && *codeset) { 264 if (codeset && *codeset) {
261 PyObject *enc = PyCodec_Encoder(codeset); 265 PyObject *enc = PyCodec_Encoder(codeset);
262 if (enc) { 266 if (enc) {
263 codeset = strdup(codeset); 267 codeset = strdup(codeset);
266 codeset = NULL; 270 codeset = NULL;
267 PyErr_Clear(); 271 PyErr_Clear();
268 } 272 }
269 } else 273 } else
270 codeset = NULL; 274 codeset = NULL;
271 setlocale(LC_CTYPE, saved_locale);
272 free(saved_locale);
273 275
274 if (codeset) { 276 if (codeset) {
275 if (!Py_FileSystemDefaultEncoding) 277 if (!Py_FileSystemDefaultEncoding)
276 Py_FileSystemDefaultEncoding = codeset; 278 Py_FileSystemDefaultEncoding = codeset;
277 else 279 else