[Python-checkins] bpo-46659: Deprecate locale.getdefaultlocale() (GH-31206)

vstinner webhook-mailer at python.org
Tue Feb 22 16:06:50 EST 2022


https://github.com/python/cpython/commit/b899126094731bc49fecb61f2c1b7557d74ca839
commit: b899126094731bc49fecb61f2c1b7557d74ca839
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-02-22T22:06:43+01:00
summary:

bpo-46659: Deprecate locale.getdefaultlocale() (GH-31206)

The locale.getdefaultlocale() function is deprecated and will be
removed in Python 3.13. Use locale.setlocale(),
locale.getpreferredencoding(False) and locale.getlocale() functions
instead.

files:
A Misc/NEWS.d/next/Library/2022-02-06-17-57-45.bpo-46659.zTmkoQ.rst
M Doc/library/locale.rst
M Doc/whatsnew/3.11.rst
M Lib/locale.py
M Lib/test/test_locale.py

diff --git a/Doc/library/locale.rst b/Doc/library/locale.rst
index 60d0c59d017c7..1b147342cef14 100644
--- a/Doc/library/locale.rst
+++ b/Doc/library/locale.rst
@@ -301,6 +301,8 @@ The :mod:`locale` module defines the following exception and functions:
    *language code* and *encoding* may be ``None`` if their values cannot be
    determined.
 
+   .. deprecated:: 3.11 3.13
+
 
 .. function:: getlocale(category=LC_CTYPE)
 
diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index 85f12fe8b4fc7..32f021f03ccce 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -486,6 +486,12 @@ Deprecated
 
   (Contributed by Hugo van Kemenade in :issue:`45173`.)
 
+* The :func:`locale.getdefaultlocale` function is deprecated and will be
+  removed in Python 3.13. Use :func:`locale.setlocale`,
+  :func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>` and
+  :func:`locale.getlocale` functions instead.
+  (Contributed by Victor Stinner in :issue:`46659`.)
+
 
 Removed
 =======
diff --git a/Lib/locale.py b/Lib/locale.py
index 4bd31c9fa2cdf..a710f27a807b0 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -555,6 +555,12 @@ def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
 
     """
 
+    import warnings
+    warnings.warn(
+        "Use setlocale(), getpreferredencoding(False) and getlocale() instead",
+        DeprecationWarning, stacklevel=2
+    )
+
     try:
         # check if it's supported by the _locale module
         import _locale
diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py
index f844e62ca2e72..2a3b0acc6bd60 100644
--- a/Lib/test/test_locale.py
+++ b/Lib/test/test_locale.py
@@ -518,7 +518,8 @@ def test_defaults_UTF8(self):
 
             os.environ['LC_CTYPE'] = 'UTF-8'
 
-            self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
+            with check_warnings(('', DeprecationWarning)):
+                self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
 
         finally:
             for k in orig_env:
diff --git a/Misc/NEWS.d/next/Library/2022-02-06-17-57-45.bpo-46659.zTmkoQ.rst b/Misc/NEWS.d/next/Library/2022-02-06-17-57-45.bpo-46659.zTmkoQ.rst
new file mode 100644
index 0000000000000..6fd9a53c260f4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-02-06-17-57-45.bpo-46659.zTmkoQ.rst
@@ -0,0 +1,4 @@
+The :func:`locale.getdefaultlocale` function is deprecated and will be removed
+in Python 3.13. Use :func:`locale.setlocale`,
+:func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>` and
+:func:`locale.getlocale` functions instead.  Patch by Victor Stinner.



More information about the Python-checkins mailing list