]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-91156: Use `locale.getencoding()` instead of getpreferredencoding (GH-91732)
authorInada Naoki <songofacandy@gmail.com>
Fri, 22 Apr 2022 01:39:24 +0000 (10:39 +0900)
committerGitHub <noreply@github.com>
Fri, 22 Apr 2022 01:39:24 +0000 (10:39 +0900)
Co-authored-by: Victor Stinner <vstinner@python.org>
14 files changed:
Doc/howto/curses.rst
Doc/library/csv.rst
Doc/library/curses.rst
Doc/library/functions.rst
Doc/library/os.rst
Lib/test/libregrtest/main.py
Lib/test/pythoninfo.py
Lib/test/support/__init__.py
Lib/test/test__locale.py
Lib/test/test_builtin.py
Lib/test/test_cmd_line.py
Lib/test/test_io.py
Lib/test/test_locale.py
Lib/test/test_mimetypes.py

index c0149ffff37716bc6c8876261b0ec10b4a678a23..26c4ece5ae6df411c23e7aa236b6d93232d9c8ab 100644 (file)
@@ -299,8 +299,7 @@ The :meth:`~curses.window.addstr` method takes a Python string or
 bytestring as the value to be displayed.  The contents of bytestrings
 are sent to the terminal as-is.  Strings are encoded to bytes using
 the value of the window's :attr:`encoding` attribute; this defaults to
-the default system encoding as returned by
-:func:`locale.getpreferredencoding`.
+the default system encoding as returned by :func:`locale.getencoding`.
 
 The :meth:`~curses.window.addch` methods take a character, which can be
 either a string of length 1, a bytestring of length 1, or an integer.
index 3a7817cfdfad87bf71b9d16980a9c3b2cd40ab32..9dec7240d9c50fdecd4ad9ac7738b029f178f2c2 100644 (file)
@@ -542,7 +542,7 @@ The corresponding simplest possible writing example is::
 
 Since :func:`open` is used to open a CSV file for reading, the file
 will by default be decoded into unicode using the system default
-encoding (see :func:`locale.getpreferredencoding`).  To decode a file
+encoding (see :func:`locale.getencoding`).  To decode a file
 using a different encoding, use the ``encoding`` argument of open::
 
    import csv
index 37e822c0e2b2079effc91746d5dbde94b1054203..a7cc4952778011afd7bffaed91b1eca8f43c5f1c 100644 (file)
@@ -27,20 +27,6 @@ Linux and the BSD variants of Unix.
    Whenever the documentation mentions a *character string* it can be specified
    as a Unicode string or a byte string.
 
-.. note::
-
-   Since version 5.4, the ncurses library decides how to interpret non-ASCII data
-   using the ``nl_langinfo`` function.  That means that you have to call
-   :func:`locale.setlocale` in the application and encode Unicode strings
-   using one of the system's available encodings.  This example uses the
-   system's default encoding::
-
-      import locale
-      locale.setlocale(locale.LC_ALL, '')
-      code = locale.getpreferredencoding()
-
-   Then use *code* as the encoding for :meth:`str.encode` calls.
-
 .. seealso::
 
    Module :mod:`curses.ascii`
@@ -923,8 +909,8 @@ the following methods and attributes:
 
    Encoding used to encode method arguments (Unicode strings and characters).
    The encoding attribute is inherited from the parent window when a subwindow
-   is created, for example with :meth:`window.subwin`. By default, the locale
-   encoding is used (see :func:`locale.getpreferredencoding`).
+   is created, for example with :meth:`window.subwin`.
+   By default, current locale encoding is used (see :func:`locale.getencoding`).
 
    .. versionadded:: 3.3
 
index e6fd0bb5eeef9960a76f8b8b7a8b4699911dd4b2..f3b8e40babbd88a934f6ac63a13fc75f57d8ed9e 100644 (file)
@@ -1123,8 +1123,8 @@ are always available.  They are listed here in alphabetical order.
    (which on *some* Unix systems, means that *all* writes append to the end of
    the file regardless of the current seek position).  In text mode, if
    *encoding* is not specified the encoding used is platform-dependent:
-   ``locale.getpreferredencoding(False)`` is called to get the current locale
-   encoding. (For reading and writing raw bytes use binary mode and leave
+   :func:`locale.getencoding()` is called to get the current locale encoding.
+   (For reading and writing raw bytes use binary mode and leave
    *encoding* unspecified.)  The available modes are:
 
    .. _filemodes:
@@ -1183,10 +1183,9 @@ are always available.  They are listed here in alphabetical order.
 
    *encoding* is the name of the encoding used to decode or encode the file.
    This should only be used in text mode.  The default encoding is platform
-   dependent (whatever :func:`locale.getpreferredencoding` returns), but any
-   :term:`text encoding` supported by Python
-   can be used.  See the :mod:`codecs` module for
-   the list of supported encodings.
+   dependent (whatever :func:`locale.getencoding` returns), but any
+   :term:`text encoding` supported by Python can be used.
+   See the :mod:`codecs` module for the list of supported encodings.
 
    *errors* is an optional string that specifies how encoding and decoding
    errors are to be handled—this cannot be used in binary mode.
index c22bf56a9f2cdec20a1258fd5d2b7bd5c452c8be..471890e74c8e5812f8742664bac27d78b499382b 100644 (file)
@@ -105,15 +105,15 @@ of the UTF-8 encoding:
 
 * Use UTF-8 as the :term:`filesystem encoding <filesystem encoding and error
   handler>`.
-* :func:`sys.getfilesystemencoding()` returns ``'UTF-8'``.
-* :func:`locale.getpreferredencoding()` returns ``'UTF-8'`` (the *do_setlocale*
+* :func:`sys.getfilesystemencoding()` returns ``'utf-8'``.
+* :func:`locale.getpreferredencoding()` returns ``'utf-8'`` (the *do_setlocale*
   argument has no effect).
 * :data:`sys.stdin`, :data:`sys.stdout`, and :data:`sys.stderr` all use
   UTF-8 as their text encoding, with the ``surrogateescape``
   :ref:`error handler <error-handlers>` being enabled for :data:`sys.stdin`
   and :data:`sys.stdout` (:data:`sys.stderr` continues to use
   ``backslashreplace`` as it does in the default locale-aware mode)
-* On Unix, :func:`os.device_encoding` returns ``'UTF-8'`` rather than the
+* On Unix, :func:`os.device_encoding` returns ``'utf-8'`` rather than the
   device encoding.
 
 Note that the standard stream settings in UTF-8 mode can be overridden by
index e7e3dde0b0a66dc9a621823e611807e5fb2e4620..0cacccfc0b5e3917d132d73245b8b5ec7c6a91ad 100644 (file)
@@ -482,8 +482,7 @@ class Regrtest:
         if cpu_count:
             print("== CPU count:", cpu_count)
         print("== encodings: locale=%s, FS=%s"
-              % (locale.getpreferredencoding(False),
-                 sys.getfilesystemencoding()))
+              % (locale.getencoding(), sys.getfilesystemencoding()))
 
     def get_tests_result(self):
         result = []
index b00830c279e8763af53dc6289bdacfc0083e75c2..39301e6397aab310da8c09944b7b30f3187fe402 100644 (file)
@@ -155,7 +155,7 @@ def collect_platform(info_add):
 def collect_locale(info_add):
     import locale
 
-    info_add('locale.encoding', locale.getpreferredencoding(False))
+    info_add('locale.getencoding', locale.getencoding())
 
 
 def collect_builtins(info_add):
index c5666d66f4782520ebdce5a09bae99856fb1c209..3b2f33979db9a8ede633858a49234a77290008a6 100644 (file)
@@ -1445,7 +1445,7 @@ def skip_if_buggy_ucrt_strfptime(test):
     global _buggy_ucrt
     if _buggy_ucrt is None:
         if(sys.platform == 'win32' and
-                locale.getpreferredencoding(False)  == 'cp65001' and
+                locale.getencoding() == 'cp65001' and
                 time.localtime().tm_zone == ''):
             _buggy_ucrt = True
         else:
index e25c92c2c82c57d742a3fd8e8beb6a6b25e1a9f3..b3bc54cd55104847e3a3ffea171267c93b595154 100644 (file)
@@ -43,7 +43,7 @@ def setUpModule():
                     locale.setlocale(locale.LC_ALL, loc)
                 except Error:
                     continue
-                encoding = locale.getpreferredencoding(False)
+                encoding = locale.getencoding()
                 try:
                     localeconv()
                 except Exception as err:
index a601a524d6eb7259f9b4078d3234e0fbdad962bf..29039230201aca1a8c63935af364d9e63ecab6d6 100644 (file)
@@ -1204,7 +1204,7 @@ class BuiltinTest(unittest.TestCase):
                     del os.environ[key]
 
             self.write_testfile()
-            current_locale_encoding = locale.getpreferredencoding(False)
+            current_locale_encoding = locale.getencoding()
             with warnings.catch_warnings():
                 warnings.simplefilter("ignore", EncodingWarning)
                 fp = open(TESTFN, 'w')
index 84eab71f9770149355a1be5161330a09abce3a1b..e8f1964c2a40d5be18961e117e0b4de70d00b060 100644 (file)
@@ -216,7 +216,7 @@ class CmdLineTest(unittest.TestCase):
         code = (
             b'import locale; '
             b'print(ascii("' + undecodable + b'"), '
-                b'locale.getpreferredencoding())')
+                b'locale.getencoding())')
         p = subprocess.Popen(
             [sys.executable, "-c", code],
             stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
index 45bf81b61f4163e5b42c6d890974413602cc9810..5528c461e58ae6277056e29743dc33780d0e3975 100644 (file)
@@ -2726,7 +2726,7 @@ class TextIOWrapperTest(unittest.TestCase):
                 if key in os.environ:
                     del os.environ[key]
 
-            current_locale_encoding = locale.getpreferredencoding(False)
+            current_locale_encoding = locale.getencoding()
             b = self.BytesIO()
             with warnings.catch_warnings():
                 warnings.simplefilter("ignore", EncodingWarning)
index 774b0fcd333449921b23dcd389b85949a46ee2f8..5cb6edc52d777cfd5eb36d93866fb4976e9b7a89 100644 (file)
@@ -363,7 +363,7 @@ class TestEnUSCollation(BaseLocalizedTest, TestCollation):
     locale_type = locale.LC_ALL
 
     def setUp(self):
-        enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name
+        enc = codecs.lookup(locale.getencoding() or 'ascii').name
         if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
             raise unittest.SkipTest('encoding not suitable')
         if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or
@@ -533,6 +533,14 @@ class TestMiscellaneous(unittest.TestCase):
             if orig_getlocale is not None:
                 _locale._getdefaultlocale = orig_getlocale
 
+    def test_getencoding(self):
+        # Invoke getencoding to make sure it does not cause exceptions.
+        enc = locale.getencoding()
+        self.assertIsInstance(enc, str)
+        self.assertNotEqual(enc, "")
+        # make sure it is valid
+        codecs.lookup(enc)
+
     def test_getpreferredencoding(self):
         # Invoke getpreferredencoding to make sure it does not cause exceptions.
         enc = locale.getpreferredencoding()
index 3477b18376a4f3211e8d5425d099ed8728c9054d..f2b103693a9b2c78d81f8a43a8df60c575adf5f9 100644 (file)
@@ -1,5 +1,4 @@
 import io
-import locale
 import mimetypes
 import pathlib
 import sys
@@ -33,7 +32,7 @@ def tearDownModule():
 class MimeTypesTestCase(unittest.TestCase):
     def setUp(self):
         self.db = mimetypes.MimeTypes()
-        
+
     def test_case_sensitivity(self):
         eq = self.assertEqual
         eq(self.db.guess_type("foobar.HTML"), self.db.guess_type("foobar.html"))
@@ -145,11 +144,6 @@ class MimeTypesTestCase(unittest.TestCase):
         self.assertNotIn('.no-such-ext', all)
 
     def test_encoding(self):
-        getpreferredencoding = locale.getpreferredencoding
-        self.addCleanup(setattr, locale, 'getpreferredencoding',
-                                 getpreferredencoding)
-        locale.getpreferredencoding = lambda: 'ascii'
-
         filename = support.findfile("mime.types")
         mimes = mimetypes.MimeTypes([filename])
         exts = mimes.guess_all_extensions('application/vnd.geocube+xml',