Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_c_locale_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,25 @@ def test_PYTHONCOERCECLOCALE_set_to_one(self):
text=True)
self.assertEqual(cmd.stdout.rstrip(), loc)

def test_unsupported_locale_fallback_to_utf8(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure where to put this test.

locales = [
"zh_TW.euctw",
"hy_AM.armscii8",
"ka_GE.georgianps",
]

for locale in locales:
with self.subTest(locale=locale):
env = dict(os.environ, LC_ALL=locale, PYTHONUTF8="0")

result = subprocess.run(
[sys.executable, "-c", ""],
env=env,
capture_output=True,
text=True,
timeout=10)

self.assertEqual(result.returncode, 0)

def tearDownModule():
support.reap_children()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fallback to UTF-8 if an unsupported locale is provided.
15 changes: 13 additions & 2 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -16457,8 +16457,19 @@ config_get_codec_name(wchar_t **config_encoding)
PyObject *codec = _PyCodec_Lookup(encoding);
PyMem_RawFree(encoding);

if (!codec)
goto error;
if (!codec) { // Fallback to UTF-8 if the codec is not found
PyErr_Clear();

wchar_t *utf8_encoding = _PyMem_RawWcsdup(L"utf-8");
if (utf8_encoding == NULL) {
PyErr_NoMemory();
return -1;
}

PyMem_RawFree(*config_encoding);
*config_encoding = utf8_encoding;
return 0;
}

name_obj = PyObject_GetAttrString(codec, "name");
Py_CLEAR(codec);
Expand Down
Loading