comparison Python/pythonrun.c @ 49611:9fc573fbaf5e

Issue #4213: The file system encoding is now normalized by the codec subsystem, for example UTF-8 is turned into utf-8. Patch created by Victor and reviewed by me. The change is required for proper initialization of subinterpreters.
author Christian Heimes <christian@cheimes.de>
date Thu, 30 Oct 2008 21:40:04 +0000
parents dab7abb77e8f
children efef5fc45a9a
comparison
equal deleted inserted replaced
49610:d2ec6d5b2db9 49611:9fc573fbaf5e
124 if (flag < 1) 124 if (flag < 1)
125 flag = 1; 125 flag = 1;
126 return flag; 126 return flag;
127 } 127 }
128 128
129 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
130 static char*
131 get_codeset(void)
132 {
133 char* codeset;
134 PyObject *codec, *name;
135
136 codeset = nl_langinfo(CODESET);
137 if (!codeset || codeset[0] == '\0')
138 return NULL;
139
140 codec = _PyCodec_Lookup(codeset);
141 if (!codec)
142 goto error;
143
144 name = PyObject_GetAttrString(codec, "name");
145 Py_CLEAR(codec);
146 if (!name)
147 goto error;
148
149 codeset = strdup(_PyUnicode_AsString(name));
150 Py_DECREF(name);
151 return codeset;
152
153 error:
154 Py_XDECREF(codec);
155 PyErr_Clear();
156 return NULL;
157 }
158 #endif
159
129 void 160 void
130 Py_InitializeEx(int install_sigs) 161 Py_InitializeEx(int install_sigs)
131 { 162 {
132 PyInterpreterState *interp; 163 PyInterpreterState *interp;
133 PyThreadState *tstate; 164 PyThreadState *tstate;
255 user's preference, if the CODESET names a well-known 286 user's preference, if the CODESET names a well-known
256 Python codec, and Py_FileSystemDefaultEncoding isn't 287 Python codec, and Py_FileSystemDefaultEncoding isn't
257 initialized by other means. Also set the encoding of 288 initialized by other means. Also set the encoding of
258 stdin and stdout if these are terminals. */ 289 stdin and stdout if these are terminals. */
259 290
260 codeset = nl_langinfo(CODESET); 291 codeset = get_codeset();
261 if (codeset && *codeset) {
262 if (PyCodec_KnownEncoding(codeset))
263 codeset = strdup(codeset);
264 else
265 codeset = NULL;
266 } else
267 codeset = NULL;
268
269 if (codeset) { 292 if (codeset) {
270 if (!Py_FileSystemDefaultEncoding) 293 if (!Py_FileSystemDefaultEncoding)
271 Py_FileSystemDefaultEncoding = codeset; 294 Py_FileSystemDefaultEncoding = codeset;
272 else 295 else
273 free(codeset); 296 free(codeset);