Skip to content

Commit

Permalink
bpo-46659: Deprecate locale.getdefaultlocale() (GH-31206)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vstinner authored Feb 22, 2022
1 parent ccbe804 commit b899126
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Doc/library/locale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======
Expand Down
6 changes: 6 additions & 0 deletions Lib/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit b899126

Please sign in to comment.