Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.c @ 47413:1062b7dec95b legacy-trunk
New environment variable PYTHONIOENCODING.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Sun, 01 Jun 2008 07:20:46 +0000 |
| parents | 4fe192d48960 |
| children | 622cabbaf2bc |
comparison
equal
deleted
inserted
replaced
| 47409:0156c336e6b3 | 47413:1062b7dec95b |
|---|---|
| 130 { | 130 { |
| 131 PyInterpreterState *interp; | 131 PyInterpreterState *interp; |
| 132 PyThreadState *tstate; | 132 PyThreadState *tstate; |
| 133 PyObject *bimod, *sysmod; | 133 PyObject *bimod, *sysmod; |
| 134 char *p; | 134 char *p; |
| 135 char *icodeset; /* On Windows, input codeset may theoretically | |
| 136 differ from output codeset. */ | |
| 137 char *codeset = NULL; | |
| 138 char *errors = NULL; | |
| 139 int free_codeset = 0; | |
| 140 int overridden = 0; | |
| 135 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) | 141 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 136 char *codeset; | 142 char *saved_locale, *loc_codeset; |
| 137 char *saved_locale; | |
| 138 PyObject *sys_stream, *sys_isatty; | 143 PyObject *sys_stream, *sys_isatty; |
| 144 #endif | |
| 145 #ifdef MS_WINDOWS | |
| 146 char ibuf[128]; | |
| 147 char buf[128]; | |
| 139 #endif | 148 #endif |
| 140 extern void _Py_ReadyTypes(void); | 149 extern void _Py_ReadyTypes(void); |
| 141 | 150 |
| 142 if (initialized) | 151 if (initialized) |
| 143 return; | 152 return; |
| 236 /* auto-thread-state API, if available */ | 245 /* auto-thread-state API, if available */ |
| 237 #ifdef WITH_THREAD | 246 #ifdef WITH_THREAD |
| 238 _PyGILState_Init(interp, tstate); | 247 _PyGILState_Init(interp, tstate); |
| 239 #endif /* WITH_THREAD */ | 248 #endif /* WITH_THREAD */ |
| 240 | 249 |
| 250 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') { | |
| 251 p = icodeset = codeset = strdup(p); | |
| 252 free_codeset = 1; | |
| 253 errors = strchr(p, ':'); | |
| 254 if (errors) { | |
| 255 *errors = '\0'; | |
| 256 errors++; | |
| 257 } | |
| 258 overridden = 1; | |
| 259 } | |
| 260 | |
| 241 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) | 261 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 242 /* On Unix, set the file system encoding according to the | 262 /* On Unix, set the file system encoding according to the |
| 243 user's preference, if the CODESET names a well-known | 263 user's preference, if the CODESET names a well-known |
| 244 Python codec, and Py_FileSystemDefaultEncoding isn't | 264 Python codec, and Py_FileSystemDefaultEncoding isn't |
| 245 initialized by other means. Also set the encoding of | 265 initialized by other means. Also set the encoding of |
| 246 stdin and stdout if these are terminals. */ | 266 stdin and stdout if these are terminals, unless overridden. */ |
| 247 | 267 |
| 248 saved_locale = strdup(setlocale(LC_CTYPE, NULL)); | 268 if (!overridden || !Py_FileSystemDefaultEncoding) { |
| 249 setlocale(LC_CTYPE, ""); | 269 saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
| 250 codeset = nl_langinfo(CODESET); | 270 setlocale(LC_CTYPE, ""); |
| 251 if (codeset && *codeset) { | 271 loc_codeset = nl_langinfo(CODESET); |
| 252 PyObject *enc = PyCodec_Encoder(codeset); | 272 if (loc_codeset && *loc_codeset) { |
| 253 if (enc) { | 273 PyObject *enc = PyCodec_Encoder(loc_codeset); |
| 254 codeset = strdup(codeset); | 274 if (enc) { |
| 255 Py_DECREF(enc); | 275 loc_codeset = strdup(loc_codeset); |
| 256 } else { | 276 Py_DECREF(enc); |
| 257 codeset = NULL; | 277 } else { |
| 258 PyErr_Clear(); | 278 loc_codeset = NULL; |
| 259 } | 279 PyErr_Clear(); |
| 260 } else | 280 } |
| 261 codeset = NULL; | 281 } else |
| 262 setlocale(LC_CTYPE, saved_locale); | 282 loc_codeset = NULL; |
| 263 free(saved_locale); | 283 setlocale(LC_CTYPE, saved_locale); |
| 284 free(saved_locale); | |
| 285 | |
| 286 if (!overridden) { | |
| 287 codeset = icodeset = loc_codeset; | |
| 288 free_codeset = 1; | |
| 289 } | |
| 290 | |
| 291 /* Initialize Py_FileSystemDefaultEncoding from | |
| 292 locale even if PYTHONIOENCODING is set. */ | |
| 293 if (!Py_FileSystemDefaultEncoding) { | |
| 294 Py_FileSystemDefaultEncoding = loc_codeset; | |
| 295 if (!overridden) | |
| 296 free_codeset = 0; | |
| 297 } | |
| 298 } | |
| 299 #endif | |
| 300 | |
| 301 #ifdef MS_WINDOWS | |
| 302 if (!overridden) { | |
| 303 icodeset = ibuf; | |
| 304 encoding = buf; | |
| 305 sprintf(ibuf, "cp%d", GetConsoleCP()); | |
| 306 sprintf(buf, "cp%d", GetConsoleOutputCP()); | |
| 307 } | |
| 308 #endif | |
| 264 | 309 |
| 265 if (codeset) { | 310 if (codeset) { |
| 266 sys_stream = PySys_GetObject("stdin"); | 311 sys_stream = PySys_GetObject("stdin"); |
| 267 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); | 312 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); |
| 268 if (!sys_isatty) | 313 if (!sys_isatty) |
| 269 PyErr_Clear(); | 314 PyErr_Clear(); |
| 270 if(sys_isatty && PyObject_IsTrue(sys_isatty) && | 315 if ((overridden || |
| 316 (sys_isatty && PyObject_IsTrue(sys_isatty))) && | |
| 271 PyFile_Check(sys_stream)) { | 317 PyFile_Check(sys_stream)) { |
| 272 if (!PyFile_SetEncoding(sys_stream, codeset)) | 318 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors)) |
| 273 Py_FatalError("Cannot set codeset of stdin"); | 319 Py_FatalError("Cannot set codeset of stdin"); |
| 274 } | 320 } |
| 275 Py_XDECREF(sys_isatty); | 321 Py_XDECREF(sys_isatty); |
| 276 | 322 |
| 277 sys_stream = PySys_GetObject("stdout"); | 323 sys_stream = PySys_GetObject("stdout"); |
| 278 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); | 324 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); |
| 279 if (!sys_isatty) | 325 if (!sys_isatty) |
| 280 PyErr_Clear(); | 326 PyErr_Clear(); |
| 281 if(sys_isatty && PyObject_IsTrue(sys_isatty) && | 327 if ((overridden || |
| 328 (sys_isatty && PyObject_IsTrue(sys_isatty))) && | |
| 282 PyFile_Check(sys_stream)) { | 329 PyFile_Check(sys_stream)) { |
| 283 if (!PyFile_SetEncoding(sys_stream, codeset)) | 330 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors)) |
| 284 Py_FatalError("Cannot set codeset of stdout"); | 331 Py_FatalError("Cannot set codeset of stdout"); |
| 285 } | 332 } |
| 286 Py_XDECREF(sys_isatty); | 333 Py_XDECREF(sys_isatty); |
| 287 | 334 |
| 288 sys_stream = PySys_GetObject("stderr"); | 335 sys_stream = PySys_GetObject("stderr"); |
| 289 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); | 336 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); |
| 290 if (!sys_isatty) | 337 if (!sys_isatty) |
| 291 PyErr_Clear(); | 338 PyErr_Clear(); |
| 292 if(sys_isatty && PyObject_IsTrue(sys_isatty) && | 339 if((overridden || |
| 340 (sys_isatty && PyObject_IsTrue(sys_isatty))) && | |
| 293 PyFile_Check(sys_stream)) { | 341 PyFile_Check(sys_stream)) { |
| 294 if (!PyFile_SetEncoding(sys_stream, codeset)) | 342 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors)) |
| 295 Py_FatalError("Cannot set codeset of stderr"); | 343 Py_FatalError("Cannot set codeset of stderr"); |
| 296 } | 344 } |
| 297 Py_XDECREF(sys_isatty); | 345 Py_XDECREF(sys_isatty); |
| 298 | 346 |
| 299 if (!Py_FileSystemDefaultEncoding) | 347 if (free_codeset) |
| 300 Py_FileSystemDefaultEncoding = codeset; | |
| 301 else | |
| 302 free(codeset); | 348 free(codeset); |
| 303 } | 349 } |
| 304 #endif | |
| 305 } | 350 } |
| 306 | 351 |
| 307 void | 352 void |
| 308 Py_Initialize(void) | 353 Py_Initialize(void) |
| 309 { | 354 { |
