Skip to content

Commit f6e323c

Browse files
authored
bpo-34523: Fix C locale coercion on FreeBSD CURRENT (GH-10672) (GH-10673)
bpo-34523, bpo-35290: C locale coercion now resets the Python internal "force ASCII" mode. This change fix the filesystem encoding on FreeBSD CURRENT, which has a new "C.UTF-8" locale, when the UTF-8 mode is disabled. Add _Py_ResetForceASCII(): _Py_SetLocaleFromEnv() now calls it. (cherry picked from commit 353933e)
1 parent 95036ea commit f6e323c

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

Include/fileutils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ PyAPI_FUNC(int) _Py_GetLocaleconvNumeric(
185185

186186
#ifdef Py_BUILD_CORE
187187
PyAPI_FUNC(int) _Py_GetForceASCII(void);
188+
189+
/* Reset "force ASCII" mode (if it was initialized).
190+
191+
This function should be called when Python changes the LC_CTYPE locale,
192+
so the "force ASCII" mode can be detected again on the new locale
193+
encoding. */
194+
PyAPI_FUNC(void) _Py_ResetForceASCII(void);
188195
#endif
189196

190197
#ifdef __cplusplus

Python/fileutils.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ _Py_GetForceASCII(void)
191191
}
192192

193193

194+
void
195+
_Py_ResetForceASCII(void)
196+
{
197+
force_ascii = -1;
198+
}
199+
194200

195201
static int
196202
encode_ascii(const wchar_t *text, char **str,
@@ -252,6 +258,12 @@ _Py_GetForceASCII(void)
252258
{
253259
return 0;
254260
}
261+
262+
void
263+
_Py_ResetForceASCII(void)
264+
{
265+
/* nothing to do */
266+
}
255267
#endif /* !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS) */
256268

257269

Python/pylifecycle.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ defined(HAVE_LANGINFO_H) && defined(CODESET)
523523
char *
524524
_Py_SetLocaleFromEnv(int category)
525525
{
526+
char *res;
526527
#ifdef __ANDROID__
527528
const char *locale;
528529
const char **pvar;
@@ -569,10 +570,12 @@ _Py_SetLocaleFromEnv(int category)
569570
}
570571
}
571572
#endif
572-
return setlocale(category, utf8_locale);
573-
#else /* __ANDROID__ */
574-
return setlocale(category, "");
575-
#endif /* __ANDROID__ */
573+
res = setlocale(category, utf8_locale);
574+
#else /* !defined(__ANDROID__) */
575+
res = setlocale(category, "");
576+
#endif
577+
_Py_ResetForceASCII();
578+
return res;
576579
}
577580

578581

0 commit comments

Comments
 (0)