@@ -57,6 +57,7 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
57
57
58
58
/* Forward */
59
59
static void initmain (void );
60
+ static void initfsencoding (void );
60
61
static void initsite (void );
61
62
static int initstdio (void );
62
63
static void flush_io (void );
@@ -159,7 +160,6 @@ get_codeset(void)
159
160
160
161
error :
161
162
Py_XDECREF (codec );
162
- PyErr_Clear ();
163
163
return NULL ;
164
164
}
165
165
#endif
@@ -171,9 +171,6 @@ Py_InitializeEx(int install_sigs)
171
171
PyThreadState * tstate ;
172
172
PyObject * bimod , * sysmod , * pstderr ;
173
173
char * p ;
174
- #if defined(HAVE_LANGINFO_H ) && defined(CODESET )
175
- char * codeset ;
176
- #endif
177
174
extern void _Py_ReadyTypes (void );
178
175
179
176
if (initialized )
@@ -264,21 +261,7 @@ Py_InitializeEx(int install_sigs)
264
261
265
262
_PyImportHooks_Init ();
266
263
267
- #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 ();
282
265
283
266
if (install_sigs )
284
267
initsigs (); /* Signal handling stuff, including initintr() */
@@ -496,7 +479,7 @@ Py_Finalize(void)
496
479
_PyUnicode_Fini ();
497
480
498
481
/* reset file system default encoding */
499
- if (!Py_HasFileSystemDefaultEncoding ) {
482
+ if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding ) {
500
483
free ((char * )Py_FileSystemDefaultEncoding );
501
484
Py_FileSystemDefaultEncoding = NULL ;
502
485
}
@@ -707,6 +690,45 @@ initmain(void)
707
690
}
708
691
}
709
692
693
+ 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
+
710
732
/* Import the site module (not into __main__ though) */
711
733
712
734
static void
0 commit comments