Skip to content

Commit de42755

Browse files
authored
bpo-34523: Py_FileSystemDefaultEncoding NULL by default (GH-9003)
* Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors default value is now NULL: initfsencoding() set them during Python initialization. * Document how Python chooses the filesystem encoding and error handler. * Add an assertion to _PyCoreConfig_Read().
1 parent cf21504 commit de42755

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

Include/coreconfig.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,34 @@ typedef struct {
6666
int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
6767
int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
6868

69-
/* Python filesystem encoding and error handler: see
69+
/* Python filesystem encoding and error handler:
7070
sys.getfilesystemencoding() and sys.getfilesystemencodeerrors().
7171
72-
Updated later by initfsencoding(). On Windows, can be updated by
73-
sys._enablelegacywindowsfsencoding() at runtime.
72+
Default encoding and error handler:
73+
74+
* if Py_SetStandardStreamEncoding() has been called: they have the
75+
highest priority;
76+
* PYTHONIOENCODING environment variable;
77+
* The UTF-8 Mode uses UTF-8/surrogateescape;
78+
* locale encoding: ANSI code page on Windows, UTF-8 on Android,
79+
LC_CTYPE locale encoding on other platforms;
80+
* On Windows, "surrogateescape" error handler;
81+
* "surrogateescape" error handler if the LC_CTYPE locale is "C" or "POSIX";
82+
* "surrogateescape" error handler if the LC_CTYPE locale has been coerced
83+
(PEP 538);
84+
* "strict" error handler.
85+
86+
Supported error handlers: "strict", "surrogateescape" and
87+
"surrogatepass". The surrogatepass error handler is only supported
88+
if Py_DecodeLocale() and Py_EncodeLocale() use directly the UTF-8 codec;
89+
it's only used on Windows.
90+
91+
initfsencoding() updates the encoding to the Python codec name.
92+
For example, "ANSI_X3.4-1968" is replaced with "ascii".
93+
94+
On Windows, sys._enablelegacywindowsfsencoding() sets the
95+
encoding/errors to mbcs/replace at runtime.
96+
7497
7598
See Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors.
7699
*/

Python/coreconfig.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,12 @@
1919

2020
/* Global configuration variables */
2121

22-
/* The default encoding used by the platform file system APIs
23-
Can remain NULL for all platforms that don't have such a concept
24-
25-
Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the
26-
values for Py_FileSystemDefaultEncoding!
27-
*/
28-
#if defined(__APPLE__)
29-
const char *Py_FileSystemDefaultEncoding = "utf-8";
30-
int Py_HasFileSystemDefaultEncoding = 1;
31-
#elif defined(MS_WINDOWS)
32-
/* may be changed by initfsencoding(), but should never be free()d */
33-
const char *Py_FileSystemDefaultEncoding = "utf-8";
34-
int Py_HasFileSystemDefaultEncoding = 1;
35-
#else
36-
const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */
22+
/* The filesystem encoding is chosen by config_init_fs_encoding(),
23+
see also initfsencoding(). */
24+
const char *Py_FileSystemDefaultEncoding = NULL;
3725
int Py_HasFileSystemDefaultEncoding = 0;
38-
#endif
39-
const char *Py_FileSystemDefaultEncodeErrors = "surrogateescape";
40-
static int _Py_HasFileSystemDefaultEncodeErrors = 1;
26+
const char *Py_FileSystemDefaultEncodeErrors = NULL;
27+
static int _Py_HasFileSystemDefaultEncodeErrors = 0;
4128

4229
/* UTF-8 mode (PEP 540): if equals to 1, use the UTF-8 encoding, and change
4330
stdin and stdout error handler to "surrogateescape". It is equal to
@@ -1362,6 +1349,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
13621349
assert(config->filesystem_errors != NULL);
13631350
assert(config->stdio_encoding != NULL);
13641351
assert(config->stdio_errors != NULL);
1352+
assert(config->_check_hash_pycs_mode != NULL);
13651353

13661354
return _Py_INIT_OK();
13671355
}

0 commit comments

Comments
 (0)