@@ -1164,44 +1164,46 @@ config_init_fs_encoding(_PyCoreConfig *config)
11641164 }
11651165 }
11661166
1167- /* Windows defaults to utf-8/surrogatepass (PEP 529) */
1167+ /* Windows defaults to utf-8/surrogatepass (PEP 529).
1168+
1169+ Note: UTF-8 Mode takes the same code path and the Legacy Windows FS
1170+ encoding has the priortiy over UTF-8 Mode. */
11681171 if (config -> filesystem_encoding == NULL ) {
11691172 config -> filesystem_encoding = _PyMem_RawStrdup ("utf-8" );
11701173 if (config -> filesystem_encoding == NULL ) {
11711174 return _Py_INIT_NO_MEMORY ();
11721175 }
11731176 }
1177+
11741178 if (config -> filesystem_errors == NULL ) {
11751179 config -> filesystem_errors = _PyMem_RawStrdup ("surrogatepass" );
11761180 if (config -> filesystem_errors == NULL ) {
11771181 return _Py_INIT_NO_MEMORY ();
11781182 }
11791183 }
11801184#else
1181- if (config -> utf8_mode ) {
1182- /* UTF-8 Mode use: utf-8/surrogateescape */
1183- if ( config -> filesystem_encoding == NULL ) {
1185+ if (config -> filesystem_encoding == NULL ) {
1186+ if ( config -> utf8_mode ) {
1187+ /* UTF-8 Mode use: utf-8/surrogateescape */
11841188 config -> filesystem_encoding = _PyMem_RawStrdup ("utf-8" );
1185- if (config -> filesystem_encoding == NULL ) {
1186- return _Py_INIT_NO_MEMORY ();
1187- }
1189+ /* errors defaults to surrogateescape above */
11881190 }
1189- /* errors defaults to surrogateescape above */
1190- }
1191-
1192- if (config -> filesystem_encoding == NULL ) {
1193- /* macOS and Android use UTF-8, other platforms use
1194- the locale encoding. */
1195- char * locale_encoding ;
1191+ else if (_Py_GetForceASCII ()) {
1192+ config -> filesystem_encoding = _PyMem_RawStrdup ("ascii" );
1193+ }
1194+ else {
1195+ /* macOS and Android use UTF-8,
1196+ other platforms use the locale encoding. */
11961197#if defined(__APPLE__ ) || defined(__ANDROID__ )
1197- locale_encoding = "UTF -8" ;
1198+ config -> filesystem_encoding = _PyMem_RawStrdup ( "utf -8") ;
11981199#else
1199- _PyInitError err = get_locale_encoding (& locale_encoding );
1200- if (_Py_INIT_FAILED (err )) {
1201- return err ;
1202- }
1200+ _PyInitError err = get_locale_encoding (& config -> filesystem_encoding );
1201+ if (_Py_INIT_FAILED (err )) {
1202+ return err ;
1203+ }
12031204#endif
1204- config -> filesystem_encoding = _PyMem_RawStrdup (locale_encoding );
1205+ }
1206+
12051207 if (config -> filesystem_encoding == NULL ) {
12061208 return _Py_INIT_NO_MEMORY ();
12071209 }
0 commit comments