Skip to content

Commit 21220bb

Browse files
authored
bpo-34403: Fix initfsencoding() for ASCII (GH-10233)
* Add _Py_GetForceASCII(): check if Python forces the usage of ASCII in Py_DecodeLocale() and Py_EncodeLocale(). * initfsencoding() now uses ASCII if _Py_GetForceASCII() is true.
1 parent 58f7bf3 commit 21220bb

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Include/fileutils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ PyAPI_FUNC(int) _Py_GetLocaleconvNumeric(
183183

184184
#endif /* Py_LIMITED_API */
185185

186+
#ifdef Py_BUILD_CORE
187+
PyAPI_FUNC(int) _Py_GetForceASCII(void);
188+
#endif
189+
186190
#ifdef __cplusplus
187191
}
188192
#endif

Python/fileutils.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ check_force_ascii(void)
180180
return 1;
181181
}
182182

183+
184+
int
185+
_Py_GetForceASCII(void)
186+
{
187+
if (force_ascii == -1) {
188+
force_ascii = check_force_ascii();
189+
}
190+
return force_ascii;
191+
}
192+
193+
194+
183195
static int
184196
encode_ascii(const wchar_t *text, char **str,
185197
size_t *error_pos, const char **reason,

Python/pylifecycle.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,10 @@ initfsencoding(PyInterpreterState *interp)
16151615
Py_FileSystemDefaultEncoding = "utf-8";
16161616
Py_HasFileSystemDefaultEncoding = 1;
16171617
}
1618+
else if (_Py_GetForceASCII()) {
1619+
Py_FileSystemDefaultEncoding = "ascii";
1620+
Py_HasFileSystemDefaultEncoding = 1;
1621+
}
16181622
else if (Py_FileSystemDefaultEncoding == NULL) {
16191623
Py_FileSystemDefaultEncoding = get_locale_encoding();
16201624
if (Py_FileSystemDefaultEncoding == NULL) {

0 commit comments

Comments
 (0)