Issue #8610: Load file system codec at startup, and display a fatal e… · python/cpython@b744ba1
@@ -57,6 +57,7 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
57575858/* Forward */
5959static void initmain(void);
60+static void initfsencoding(void);
6061static void initsite(void);
6162static int initstdio(void);
6263static void flush_io(void);
@@ -159,7 +160,6 @@ get_codeset(void)
159160160161error:
161162Py_XDECREF(codec);
162-PyErr_Clear();
163163return NULL;
164164}
165165#endif
@@ -171,9 +171,6 @@ Py_InitializeEx(int install_sigs)
171171PyThreadState *tstate;
172172PyObject *bimod, *sysmod, *pstderr;
173173char *p;
174-#if defined(HAVE_LANGINFO_H) && defined(CODESET)
175-char *codeset;
176-#endif
177174extern void _Py_ReadyTypes(void);
178175179176if (initialized)
@@ -264,21 +261,7 @@ Py_InitializeEx(int install_sigs)
264261265262_PyImportHooks_Init();
266263267-#if defined(HAVE_LANGINFO_H) && defined(CODESET)
268-/* On Unix, set the file system encoding according to the
269- user's preference, if the CODESET names a well-known
270- Python codec, and Py_FileSystemDefaultEncoding isn't
271- initialized by other means. Also set the encoding of
272- stdin and stdout if these are terminals. */
273-274-codeset = get_codeset();
275-if (codeset) {
276-if (!Py_FileSystemDefaultEncoding)
277-Py_FileSystemDefaultEncoding = codeset;
278-else
279-free(codeset);
280- }
281-#endif
264+initfsencoding();
282265283266if (install_sigs)
284267initsigs(); /* Signal handling stuff, including initintr() */
@@ -496,7 +479,7 @@ Py_Finalize(void)
496479_PyUnicode_Fini();
497480498481/* reset file system default encoding */
499-if (!Py_HasFileSystemDefaultEncoding) {
482+if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
500483free((char*)Py_FileSystemDefaultEncoding);
501484Py_FileSystemDefaultEncoding = NULL;
502485 }
@@ -707,6 +690,45 @@ initmain(void)
707690 }
708691}
709692693+static void
694+initfsencoding(void)
695+{
696+PyObject *codec;
697+#if defined(HAVE_LANGINFO_H) && defined(CODESET)
698+char *codeset;
699+700+/* On Unix, set the file system encoding according to the
701+ user's preference, if the CODESET names a well-known
702+ Python codec, and Py_FileSystemDefaultEncoding isn't
703+ initialized by other means. Also set the encoding of
704+ stdin and stdout if these are terminals. */
705+codeset = get_codeset();
706+if (codeset != NULL) {
707+Py_FileSystemDefaultEncoding = codeset;
708+Py_HasFileSystemDefaultEncoding = 0;
709+return;
710+ }
711+712+PyErr_Clear();
713+fprintf(stderr,
714+"Unable to get the locale encoding: "
715+"fallback to utf-8\n");
716+Py_FileSystemDefaultEncoding = "utf-8";
717+Py_HasFileSystemDefaultEncoding = 1;
718+#endif
719+720+/* the encoding is mbcs, utf-8 or ascii */
721+codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
722+if (!codec) {
723+/* Such error can only occurs in critical situations: no more
724+ * memory, import a module of the standard library failed,
725+ * etc. */
726+Py_FatalError("Py_Initialize: unable to load the file system codec");
727+ } else {
728+Py_DECREF(codec);
729+ }
730+}
731+710732/* Import the site module (not into __main__ though) */
711733712734static void