bpo-42208: Add _locale._get_locale_encoding() (GH-23052) · python/cpython@b62bdf7 · GitHub
Skip to content

Commit b62bdf7

Browse files
authored
bpo-42208: Add _locale._get_locale_encoding() (GH-23052)
* Add a new _locale._get_locale_encoding() function to get the current locale encoding. * Modify locale.getpreferredencoding() to use it. * Remove the _bootlocale module.
1 parent 710e826 commit b62bdf7

6 files changed

Lines changed: 85 additions & 102 deletions

File tree

Lib/_bootlocale.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

Lib/locale.py

Lines changed: 39 additions & 43 deletions

Lib/test/test_mimetypes.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import mimetypes
44
import pathlib
55
import sys
6-
import unittest
6+
import unittest.mock
77

88
from test import support
99
from test.support import os_helper
@@ -71,14 +71,14 @@ def test_read_mime_types(self):
7171
# bpo-41048: read_mime_types should read the rule file with 'utf-8' encoding.
7272
# Not with locale encoding. _bootlocale has been imported because io.open(...)
7373
# uses it.
74-
with os_helper.temp_dir() as directory:
75-
data = "application/no-mans-land Fran\u00E7ais"
76-
file = pathlib.Path(directory, "sample.mimetype")
77-
file.write_text(data, encoding='utf-8')
78-
import _bootlocale
79-
with support.swap_attr(_bootlocale, 'getpreferredencoding', lambda do_setlocale=True: 'ASCII'):
80-
mime_dict = mimetypes.read_mime_types(file)
81-
eq(mime_dict[".Français"], "application/no-mans-land")
74+
data = "application/no-mans-land Fran\u00E7ais"
75+
filename = "filename"
76+
fp = io.StringIO(data)
77+
with unittest.mock.patch.object(mimetypes, 'open',
78+
return_value=fp) as mock_open:
79+
mime_dict = mimetypes.read_mime_types(filename)
80+
mock_open.assert_called_with(filename, encoding='utf-8')
81+
eq(mime_dict[".Français"], "application/no-mans-land")
8282

8383
def test_non_standard_types(self):
8484
eq = self.assertEqual

Modules/_localemodule.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,24 @@ _locale_bind_textdomain_codeset_impl(PyObject *module, const char *domain,
768768
}
769769
Py_RETURN_NONE;
770770
}
771-
#endif
771+
#endif // HAVE_BIND_TEXTDOMAIN_CODESET
772+
773+
#endif // HAVE_LIBINTL_H
774+
775+
776+
/*[clinic input]
777+
_locale._get_locale_encoding
778+
779+
Get the current locale encoding.
780+
[clinic start generated code]*/
781+
782+
static PyObject *
783+
_locale__get_locale_encoding_impl(PyObject *module)
784+
/*[clinic end generated code: output=e8e2f6f6f184591a input=513d9961d2f45c76]*/
785+
{
786+
return _Py_GetLocaleEncoding();
787+
}
772788

773-
#endif
774789

775790
static struct PyMethodDef PyLocale_Methods[] = {
776791
_LOCALE_SETLOCALE_METHODDEF
@@ -797,6 +812,7 @@ static struct PyMethodDef PyLocale_Methods[] = {
797812
_LOCALE_BIND_TEXTDOMAIN_CODESET_METHODDEF
798813
#endif
799814
#endif
815+
_LOCALE__GET_LOCALE_ENCODING_METHODDEF
800816
{NULL, NULL}
801817
};
802818

Modules/clinic/_localemodule.c.h

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PCbuild/lib.pyproj

Lines changed: 0 additions & 1 deletion

0 commit comments

Comments
 (0)