@@ -1626,7 +1626,17 @@ PyUnicode_EncodeFSDefault(PyObject *unicode)
16261626 PyUnicode_GET_SIZE (unicode ),
16271627 "surrogateescape" );
16281628#else
1629- if (Py_FileSystemDefaultEncoding ) {
1629+ PyInterpreterState * interp = PyThreadState_GET ()-> interp ;
1630+ /* Bootstrap check: if the filesystem codec is implemented in Python, we
1631+ cannot use it to encode and decode filenames before it is loaded. Load
1632+ the Python codec requires to encode at least its own filename. Use the C
1633+ version of the locale codec until the codec registry is initialized and
1634+ the Python codec is loaded.
1635+
1636+ Py_FileSystemDefaultEncoding is shared between all interpreters, we
1637+ cannot only rely on it: check also interp->fscodec_initialized for
1638+ subinterpreters. */
1639+ if (Py_FileSystemDefaultEncoding && interp -> fscodec_initialized ) {
16301640 return PyUnicode_AsEncodedString (unicode ,
16311641 Py_FileSystemDefaultEncoding ,
16321642 "surrogateescape" );
@@ -1818,12 +1828,17 @@ PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
18181828#elif defined(__APPLE__ )
18191829 return PyUnicode_DecodeUTF8 (s , size , "surrogateescape" );
18201830#else
1821- /* During the early bootstrapping process, Py_FileSystemDefaultEncoding
1822- can be undefined. If it is case, decode using UTF-8. The following assumes
1823- that Py_FileSystemDefaultEncoding is set to a built-in encoding during the
1824- bootstrapping process where the codecs aren't ready yet.
1825- */
1826- if (Py_FileSystemDefaultEncoding ) {
1831+ PyInterpreterState * interp = PyThreadState_GET ()-> interp ;
1832+ /* Bootstrap check: if the filesystem codec is implemented in Python, we
1833+ cannot use it to encode and decode filenames before it is loaded. Load
1834+ the Python codec requires to encode at least its own filename. Use the C
1835+ version of the locale codec until the codec registry is initialized and
1836+ the Python codec is loaded.
1837+
1838+ Py_FileSystemDefaultEncoding is shared between all interpreters, we
1839+ cannot only rely on it: check also interp->fscodec_initialized for
1840+ subinterpreters. */
1841+ if (Py_FileSystemDefaultEncoding && interp -> fscodec_initialized ) {
18271842 return PyUnicode_Decode (s , size ,
18281843 Py_FileSystemDefaultEncoding ,
18291844 "surrogateescape" );
0 commit comments