comparison Python/pythonrun.c @ 71191:7ce685cda0ae

Issue #9642: Fix filesystem encoding initialization: use the ANSI code page on Windows if the mbcs codec is not available, and fail with a fatal error if we cannot get the locale encoding (if nl_langinfo(CODESET) is not available) instead of using UTF-8.
author Victor Stinner <victor.stinner@haypocalc.com>
date Mon, 04 Jul 2011 13:48:30 +0200
parents 2e72fcfb2527
children ad0804d3dbd0
comparison
equal deleted inserted replaced
71190:79d7bcbe88f6 71191:7ce685cda0ae
166 Py_XDECREF(codec); 166 Py_XDECREF(codec);
167 Py_XDECREF(name); 167 Py_XDECREF(name);
168 return NULL; 168 return NULL;
169 } 169 }
170 170
171 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
172 static char* 171 static char*
173 get_codeset(void) 172 get_locale_encoding(void)
174 { 173 {
174 #ifdef MS_WINDOWS
175 char codepage[100];
176 PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
177 return get_codec_name(codepage);
178 #elif defined(HAVE_LANGINFO_H) && defined(CODESET)
175 char* codeset = nl_langinfo(CODESET); 179 char* codeset = nl_langinfo(CODESET);
176 if (!codeset || codeset[0] == '\0') { 180 if (!codeset || codeset[0] == '\0') {
177 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty"); 181 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
178 return NULL; 182 return NULL;
179 } 183 }
180 return get_codec_name(codeset); 184 return get_codec_name(codeset);
181 } 185 #else
182 #endif 186 PyErr_SetNone(PyExc_NotImplementedError);
187 return NULL;
188 #endif
189 }
183 190
184 void 191 void
185 Py_InitializeEx(int install_sigs) 192 Py_InitializeEx(int install_sigs)
186 { 193 {
187 PyInterpreterState *interp; 194 PyInterpreterState *interp;
744 751
745 static int 752 static int
746 initfsencoding(PyInterpreterState *interp) 753 initfsencoding(PyInterpreterState *interp)
747 { 754 {
748 PyObject *codec; 755 PyObject *codec;
749 #if defined(HAVE_LANGINFO_H) && defined(CODESET) 756
750 char *codeset = NULL; 757 if (Py_FileSystemDefaultEncoding == NULL)
751 758 {
752 if (Py_FileSystemDefaultEncoding == NULL) { 759 Py_FileSystemDefaultEncoding = get_locale_encoding();
753 /* On Unix, set the file system encoding according to the 760 if (Py_FileSystemDefaultEncoding == NULL)
754 user's preference, if the CODESET names a well-known
755 Python codec, and Py_FileSystemDefaultEncoding isn't
756 initialized by other means. */
757 codeset = get_codeset();
758 if (codeset == NULL)
759 Py_FatalError("Py_Initialize: Unable to get the locale encoding"); 761 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
760 762
761 Py_FileSystemDefaultEncoding = codeset;
762 Py_HasFileSystemDefaultEncoding = 0; 763 Py_HasFileSystemDefaultEncoding = 0;
763 interp->fscodec_initialized = 1; 764 interp->fscodec_initialized = 1;
764 return 0; 765 return 0;
765 } 766 }
766 #endif
767 767
768 /* the encoding is mbcs, utf-8 or ascii */ 768 /* the encoding is mbcs, utf-8 or ascii */
769 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); 769 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
770 if (!codec) { 770 if (!codec) {
771 /* Such error can only occurs in critical situations: no more 771 /* Such error can only occurs in critical situations: no more