Skip to content

Commit b899126

Browse files
authored
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.
1 parent ccbe804 commit b899126

File tree

5 files changed

+20
-1
lines changed

5 files changed

+20
-1
lines changed

Diff for: Doc/library/locale.rst

+2
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ The :mod:`locale` module defines the following exception and functions:
301301
*language code* and *encoding* may be ``None`` if their values cannot be
302302
determined.
303303

304+
.. deprecated:: 3.11 3.13
305+
304306

305307
.. function:: getlocale(category=LC_CTYPE)
306308

Diff for: Doc/whatsnew/3.11.rst

+6
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ Deprecated
486486

487487
(Contributed by Hugo van Kemenade in :issue:`45173`.)
488488

489+
* The :func:`locale.getdefaultlocale` function is deprecated and will be
490+
removed in Python 3.13. Use :func:`locale.setlocale`,
491+
:func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>` and
492+
:func:`locale.getlocale` functions instead.
493+
(Contributed by Victor Stinner in :issue:`46659`.)
494+
489495

490496
Removed
491497
=======

Diff for: Lib/locale.py

+6
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,12 @@ def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
555555
556556
"""
557557

558+
import warnings
559+
warnings.warn(
560+
"Use setlocale(), getpreferredencoding(False) and getlocale() instead",
561+
DeprecationWarning, stacklevel=2
562+
)
563+
558564
try:
559565
# check if it's supported by the _locale module
560566
import _locale

Diff for: Lib/test/test_locale.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,8 @@ def test_defaults_UTF8(self):
518518

519519
os.environ['LC_CTYPE'] = 'UTF-8'
520520

521-
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
521+
with check_warnings(('', DeprecationWarning)):
522+
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
522523

523524
finally:
524525
for k in orig_env:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The :func:`locale.getdefaultlocale` function is deprecated and will be removed
2+
in Python 3.13. Use :func:`locale.setlocale`,
3+
:func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>` and
4+
:func:`locale.getlocale` functions instead. Patch by Victor Stinner.

0 commit comments

Comments
 (0)