Skip to content

Commit 1317b70

Browse files
methanevstinner
andauthored
gh-91156: Use locale.getencoding() instead of getpreferredencoding (GH-91732)
Co-authored-by: Victor Stinner <[email protected]>
1 parent efe7fd4 commit 1317b70

14 files changed

+29
-44
lines changed

Doc/howto/curses.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ The :meth:`~curses.window.addstr` method takes a Python string or
299299
bytestring as the value to be displayed. The contents of bytestrings
300300
are sent to the terminal as-is. Strings are encoded to bytes using
301301
the value of the window's :attr:`encoding` attribute; this defaults to
302-
the default system encoding as returned by
303-
:func:`locale.getpreferredencoding`.
302+
the default system encoding as returned by :func:`locale.getencoding`.
304303

305304
The :meth:`~curses.window.addch` methods take a character, which can be
306305
either a string of length 1, a bytestring of length 1, or an integer.

Doc/library/csv.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ The corresponding simplest possible writing example is::
542542

543543
Since :func:`open` is used to open a CSV file for reading, the file
544544
will by default be decoded into unicode using the system default
545-
encoding (see :func:`locale.getpreferredencoding`). To decode a file
545+
encoding (see :func:`locale.getencoding`). To decode a file
546546
using a different encoding, use the ``encoding`` argument of open::
547547

548548
import csv

Doc/library/curses.rst

+2-16
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ Linux and the BSD variants of Unix.
2727
Whenever the documentation mentions a *character string* it can be specified
2828
as a Unicode string or a byte string.
2929

30-
.. note::
31-
32-
Since version 5.4, the ncurses library decides how to interpret non-ASCII data
33-
using the ``nl_langinfo`` function. That means that you have to call
34-
:func:`locale.setlocale` in the application and encode Unicode strings
35-
using one of the system's available encodings. This example uses the
36-
system's default encoding::
37-
38-
import locale
39-
locale.setlocale(locale.LC_ALL, '')
40-
code = locale.getpreferredencoding()
41-
42-
Then use *code* as the encoding for :meth:`str.encode` calls.
43-
4430
.. seealso::
4531

4632
Module :mod:`curses.ascii`
@@ -923,8 +909,8 @@ the following methods and attributes:
923909

924910
Encoding used to encode method arguments (Unicode strings and characters).
925911
The encoding attribute is inherited from the parent window when a subwindow
926-
is created, for example with :meth:`window.subwin`. By default, the locale
927-
encoding is used (see :func:`locale.getpreferredencoding`).
912+
is created, for example with :meth:`window.subwin`.
913+
By default, current locale encoding is used (see :func:`locale.getencoding`).
928914

929915
.. versionadded:: 3.3
930916

Doc/library/functions.rst

+5-6
Original file line numberDiff line numberDiff line change
@@ -1123,8 +1123,8 @@ are always available. They are listed here in alphabetical order.
11231123
(which on *some* Unix systems, means that *all* writes append to the end of
11241124
the file regardless of the current seek position). In text mode, if
11251125
*encoding* is not specified the encoding used is platform-dependent:
1126-
``locale.getpreferredencoding(False)`` is called to get the current locale
1127-
encoding. (For reading and writing raw bytes use binary mode and leave
1126+
:func:`locale.getencoding()` is called to get the current locale encoding.
1127+
(For reading and writing raw bytes use binary mode and leave
11281128
*encoding* unspecified.) The available modes are:
11291129

11301130
.. _filemodes:
@@ -1183,10 +1183,9 @@ are always available. They are listed here in alphabetical order.
11831183

11841184
*encoding* is the name of the encoding used to decode or encode the file.
11851185
This should only be used in text mode. The default encoding is platform
1186-
dependent (whatever :func:`locale.getpreferredencoding` returns), but any
1187-
:term:`text encoding` supported by Python
1188-
can be used. See the :mod:`codecs` module for
1189-
the list of supported encodings.
1186+
dependent (whatever :func:`locale.getencoding` returns), but any
1187+
:term:`text encoding` supported by Python can be used.
1188+
See the :mod:`codecs` module for the list of supported encodings.
11901189

11911190
*errors* is an optional string that specifies how encoding and decoding
11921191
errors are to be handled—this cannot be used in binary mode.

Doc/library/os.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ of the UTF-8 encoding:
105105

106106
* Use UTF-8 as the :term:`filesystem encoding <filesystem encoding and error
107107
handler>`.
108-
* :func:`sys.getfilesystemencoding()` returns ``'UTF-8'``.
109-
* :func:`locale.getpreferredencoding()` returns ``'UTF-8'`` (the *do_setlocale*
108+
* :func:`sys.getfilesystemencoding()` returns ``'utf-8'``.
109+
* :func:`locale.getpreferredencoding()` returns ``'utf-8'`` (the *do_setlocale*
110110
argument has no effect).
111111
* :data:`sys.stdin`, :data:`sys.stdout`, and :data:`sys.stderr` all use
112112
UTF-8 as their text encoding, with the ``surrogateescape``
113113
:ref:`error handler <error-handlers>` being enabled for :data:`sys.stdin`
114114
and :data:`sys.stdout` (:data:`sys.stderr` continues to use
115115
``backslashreplace`` as it does in the default locale-aware mode)
116-
* On Unix, :func:`os.device_encoding` returns ``'UTF-8'`` rather than the
116+
* On Unix, :func:`os.device_encoding` returns ``'utf-8'`` rather than the
117117
device encoding.
118118

119119
Note that the standard stream settings in UTF-8 mode can be overridden by

Lib/test/libregrtest/main.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,7 @@ def display_header(self):
482482
if cpu_count:
483483
print("== CPU count:", cpu_count)
484484
print("== encodings: locale=%s, FS=%s"
485-
% (locale.getpreferredencoding(False),
486-
sys.getfilesystemencoding()))
485+
% (locale.getencoding(), sys.getfilesystemencoding()))
487486

488487
def get_tests_result(self):
489488
result = []

Lib/test/pythoninfo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def collect_platform(info_add):
155155
def collect_locale(info_add):
156156
import locale
157157

158-
info_add('locale.encoding', locale.getpreferredencoding(False))
158+
info_add('locale.getencoding', locale.getencoding())
159159

160160

161161
def collect_builtins(info_add):

Lib/test/support/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ def skip_if_buggy_ucrt_strfptime(test):
14451445
global _buggy_ucrt
14461446
if _buggy_ucrt is None:
14471447
if(sys.platform == 'win32' and
1448-
locale.getpreferredencoding(False) == 'cp65001' and
1448+
locale.getencoding() == 'cp65001' and
14491449
time.localtime().tm_zone == ''):
14501450
_buggy_ucrt = True
14511451
else:

Lib/test/test__locale.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def setUpModule():
4343
locale.setlocale(locale.LC_ALL, loc)
4444
except Error:
4545
continue
46-
encoding = locale.getpreferredencoding(False)
46+
encoding = locale.getencoding()
4747
try:
4848
localeconv()
4949
except Exception as err:

Lib/test/test_builtin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ def test_open_default_encoding(self):
12041204
del os.environ[key]
12051205

12061206
self.write_testfile()
1207-
current_locale_encoding = locale.getpreferredencoding(False)
1207+
current_locale_encoding = locale.getencoding()
12081208
with warnings.catch_warnings():
12091209
warnings.simplefilter("ignore", EncodingWarning)
12101210
fp = open(TESTFN, 'w')

Lib/test/test_cmd_line.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def test_undecodable_code(self):
216216
code = (
217217
b'import locale; '
218218
b'print(ascii("' + undecodable + b'"), '
219-
b'locale.getpreferredencoding())')
219+
b'locale.getencoding())')
220220
p = subprocess.Popen(
221221
[sys.executable, "-c", code],
222222
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,

Lib/test/test_io.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2726,7 +2726,7 @@ def test_default_encoding(self):
27262726
if key in os.environ:
27272727
del os.environ[key]
27282728

2729-
current_locale_encoding = locale.getpreferredencoding(False)
2729+
current_locale_encoding = locale.getencoding()
27302730
b = self.BytesIO()
27312731
with warnings.catch_warnings():
27322732
warnings.simplefilter("ignore", EncodingWarning)

Lib/test/test_locale.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class TestEnUSCollation(BaseLocalizedTest, TestCollation):
363363
locale_type = locale.LC_ALL
364364

365365
def setUp(self):
366-
enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name
366+
enc = codecs.lookup(locale.getencoding() or 'ascii').name
367367
if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
368368
raise unittest.SkipTest('encoding not suitable')
369369
if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or
@@ -533,6 +533,14 @@ def test_defaults_UTF8(self):
533533
if orig_getlocale is not None:
534534
_locale._getdefaultlocale = orig_getlocale
535535

536+
def test_getencoding(self):
537+
# Invoke getencoding to make sure it does not cause exceptions.
538+
enc = locale.getencoding()
539+
self.assertIsInstance(enc, str)
540+
self.assertNotEqual(enc, "")
541+
# make sure it is valid
542+
codecs.lookup(enc)
543+
536544
def test_getpreferredencoding(self):
537545
# Invoke getpreferredencoding to make sure it does not cause exceptions.
538546
enc = locale.getpreferredencoding()

Lib/test/test_mimetypes.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import io
2-
import locale
32
import mimetypes
43
import pathlib
54
import sys
@@ -33,7 +32,7 @@ def tearDownModule():
3332
class MimeTypesTestCase(unittest.TestCase):
3433
def setUp(self):
3534
self.db = mimetypes.MimeTypes()
36-
35+
3736
def test_case_sensitivity(self):
3837
eq = self.assertEqual
3938
eq(self.db.guess_type("foobar.HTML"), self.db.guess_type("foobar.html"))
@@ -145,11 +144,6 @@ def test_guess_all_types(self):
145144
self.assertNotIn('.no-such-ext', all)
146145

147146
def test_encoding(self):
148-
getpreferredencoding = locale.getpreferredencoding
149-
self.addCleanup(setattr, locale, 'getpreferredencoding',
150-
getpreferredencoding)
151-
locale.getpreferredencoding = lambda: 'ascii'
152-
153147
filename = support.findfile("mime.types")
154148
mimes = mimetypes.MimeTypes([filename])
155149
exts = mimes.guess_all_extensions('application/vnd.geocube+xml',

0 commit comments

Comments
 (0)