Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ecd49a

Browse files
gpsheadvstinner
authored andcommitted
gh-104783: Remove locale.getdefaultlocale()
locale.getencoding() now uses sys.getfilesystemencoding() if _locale.getencoding() is missing.
1 parent 2e5d8a9 commit 1ecd49a

File tree

5 files changed

+17
-180
lines changed

5 files changed

+17
-180
lines changed

Doc/library/locale.rst

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -281,31 +281,6 @@ The :mod:`locale` module defines the following exception and functions:
281281
0 to 99.
282282

283283

284-
.. function:: getdefaultlocale([envvars])
285-
286-
Tries to determine the default locale settings and returns them as a tuple of
287-
the form ``(language code, encoding)``.
288-
289-
According to POSIX, a program which has not called ``setlocale(LC_ALL, '')``
290-
runs using the portable ``'C'`` locale. Calling ``setlocale(LC_ALL, '')`` lets
291-
it use the default locale as defined by the :envvar:`LANG` variable. Since we
292-
do not want to interfere with the current locale setting we thus emulate the
293-
behavior in the way described above.
294-
295-
To maintain compatibility with other platforms, not only the :envvar:`LANG`
296-
variable is tested, but a list of variables given as envvars parameter. The
297-
first found to be defined will be used. *envvars* defaults to the search
298-
path used in GNU gettext; it must always contain the variable name
299-
``'LANG'``. The GNU gettext search path contains ``'LC_ALL'``,
300-
``'LC_CTYPE'``, ``'LANG'`` and ``'LANGUAGE'``, in that order.
301-
302-
Except for the code ``'C'``, the language code corresponds to :rfc:`1766`.
303-
*language code* and *encoding* may be ``None`` if their values cannot be
304-
determined.
305-
306-
.. deprecated-removed:: 3.11 3.13
307-
308-
309284
.. function:: getlocale(category=LC_CTYPE)
310285

311286
Returns the current setting for the given locale category as sequence containing
@@ -370,16 +345,6 @@ The :mod:`locale` module defines the following exception and functions:
370345
encoding for the locale code just like :func:`setlocale`.
371346

372347

373-
.. function:: resetlocale(category=LC_ALL)
374-
375-
Sets the locale for *category* to the default setting.
376-
377-
The default setting is determined by calling :func:`getdefaultlocale`.
378-
*category* defaults to :const:`LC_ALL`.
379-
380-
.. deprecated-removed:: 3.11 3.13
381-
382-
383348
.. function:: strcoll(string1, string2)
384349

385350
Compares two strings according to the current :const:`LC_COLLATE` setting. As

Doc/whatsnew/3.13.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ Deprecated
102102
Removed
103103
=======
104104

105+
* Remove two :mod:`locale` functions deprecated in Python 3.11:
106+
107+
* ``getdefaultlocale()``: use :func:`locale.setlocale`,
108+
:func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`
109+
and :func:`locale.getlocale` functions instead.
110+
* ``resetlocale()``: use ``locale.setlocale(locale.LC_ALL, "")`` instead.
111+
112+
(Contributed by Victor Stinner in :gh:`104783`.)
105113

106114

107115
Porting to Python 3.13

Lib/locale.py

Lines changed: 6 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
# Yuck: LC_MESSAGES is non-standard: can't tell whether it exists before
2626
# trying the import. So __all__ is also fiddled at the end of the file.
27-
__all__ = ["getlocale", "getdefaultlocale", "getpreferredencoding", "Error",
28-
"setlocale", "resetlocale", "localeconv", "strcoll", "strxfrm",
27+
__all__ = ["getlocale", "getpreferredencoding", "Error",
28+
"setlocale", "localeconv", "strcoll", "strxfrm",
2929
"str", "atof", "atoi", "format_string", "currency",
3030
"normalize", "LC_CTYPE", "LC_COLLATE", "LC_TIME", "LC_MONETARY",
3131
"LC_NUMERIC", "LC_ALL", "CHAR_MAX", "getencoding"]
@@ -516,67 +516,6 @@ def _build_localename(localetuple):
516516
raise TypeError('Locale must be None, a string, or an iterable of '
517517
'two strings -- language code, encoding.') from None
518518

519-
def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
520-
521-
""" Tries to determine the default locale settings and returns
522-
them as tuple (language code, encoding).
523-
524-
According to POSIX, a program which has not called
525-
setlocale(LC_ALL, "") runs using the portable 'C' locale.
526-
Calling setlocale(LC_ALL, "") lets it use the default locale as
527-
defined by the LANG variable. Since we don't want to interfere
528-
with the current locale setting we thus emulate the behavior
529-
in the way described above.
530-
531-
To maintain compatibility with other platforms, not only the
532-
LANG variable is tested, but a list of variables given as
533-
envvars parameter. The first found to be defined will be
534-
used. envvars defaults to the search path used in GNU gettext;
535-
it must always contain the variable name 'LANG'.
536-
537-
Except for the code 'C', the language code corresponds to RFC
538-
1766. code and encoding can be None in case the values cannot
539-
be determined.
540-
541-
"""
542-
543-
import warnings
544-
warnings.warn(
545-
"Use setlocale(), getencoding() and getlocale() instead",
546-
DeprecationWarning, stacklevel=2
547-
)
548-
return _getdefaultlocale(envvars)
549-
550-
def _getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
551-
try:
552-
# check if it's supported by the _locale module
553-
import _locale
554-
code, encoding = _locale._getdefaultlocale()
555-
except (ImportError, AttributeError):
556-
pass
557-
else:
558-
# make sure the code/encoding values are valid
559-
if sys.platform == "win32" and code and code[:2] == "0x":
560-
# map windows language identifier to language name
561-
code = windows_locale.get(int(code, 0))
562-
# ...add other platform-specific processing here, if
563-
# necessary...
564-
return code, encoding
565-
566-
# fall back on POSIX behaviour
567-
import os
568-
lookup = os.environ.get
569-
for variable in envvars:
570-
localename = lookup(variable,None)
571-
if localename:
572-
if variable == 'LANGUAGE':
573-
localename = localename.split(':')[0]
574-
break
575-
else:
576-
localename = 'C'
577-
return _parse_localename(localename)
578-
579-
580519
def getlocale(category=LC_CTYPE):
581520

582521
""" Returns the current setting for the given locale category as
@@ -612,40 +551,15 @@ def setlocale(category, locale=None):
612551
locale = normalize(_build_localename(locale))
613552
return _setlocale(category, locale)
614553

615-
def resetlocale(category=LC_ALL):
616-
617-
""" Sets the locale for category to the default setting.
618-
619-
The default setting is determined by calling
620-
getdefaultlocale(). category defaults to LC_ALL.
621-
622-
"""
623-
import warnings
624-
warnings.warn(
625-
'Use locale.setlocale(locale.LC_ALL, "") instead',
626-
DeprecationWarning, stacklevel=2
627-
)
628-
629-
with warnings.catch_warnings():
630-
warnings.simplefilter('ignore', category=DeprecationWarning)
631-
loc = getdefaultlocale()
632-
633-
_setlocale(category, _build_localename(loc))
634-
635554

636555
try:
637556
from _locale import getencoding
638557
except ImportError:
558+
# When _locale.getencoding() is missing, use the Python filesystem
559+
# encoding.
560+
_encoding = sys.getfilesystemencoding()
639561
def getencoding():
640-
if hasattr(sys, 'getandroidapilevel'):
641-
# On Android langinfo.h and CODESET are missing, and UTF-8 is
642-
# always used in mbstowcs() and wcstombs().
643-
return 'utf-8'
644-
encoding = _getdefaultlocale()[1]
645-
if encoding is None:
646-
# LANG not set, default to UTF-8
647-
encoding = 'utf-8'
648-
return encoding
562+
return _encoding
649563

650564
try:
651565
CODESET
@@ -1713,13 +1627,6 @@ def _init_categories(categories=categories):
17131627
_init_categories()
17141628
del categories['LC_ALL']
17151629

1716-
print('Locale defaults as determined by getdefaultlocale():')
1717-
print('-'*72)
1718-
lang, enc = getdefaultlocale()
1719-
print('Language: ', lang or '(undefined)')
1720-
print('Encoding: ', enc or '(undefined)')
1721-
print()
1722-
17231630
print('Locale settings on startup:')
17241631
print('-'*72)
17251632
for name,category in categories.items():
@@ -1729,17 +1636,6 @@ def _init_categories(categories=categories):
17291636
print(' Encoding: ', enc or '(undefined)')
17301637
print()
17311638

1732-
print()
1733-
print('Locale settings after calling resetlocale():')
1734-
print('-'*72)
1735-
resetlocale()
1736-
for name,category in categories.items():
1737-
print(name, '...')
1738-
lang, enc = getlocale(category)
1739-
print(' Language: ', lang or '(undefined)')
1740-
print(' Encoding: ', enc or '(undefined)')
1741-
print()
1742-
17431639
try:
17441640
setlocale(LC_ALL, "")
17451641
except:

Lib/test/test_locale.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -478,43 +478,9 @@ def test_japanese(self):
478478

479479

480480
class TestMiscellaneous(unittest.TestCase):
481-
def test_defaults_UTF8(self):
482-
# Issue #18378: on (at least) macOS setting LC_CTYPE to "UTF-8" is
483-
# valid. Furthermore LC_CTYPE=UTF is used by the UTF-8 locale coercing
484-
# during interpreter startup (on macOS).
485-
import _locale
486-
import os
487-
481+
def test_parse_localename(self):
488482
self.assertEqual(locale._parse_localename('UTF-8'), (None, 'UTF-8'))
489483

490-
if hasattr(_locale, '_getdefaultlocale'):
491-
orig_getlocale = _locale._getdefaultlocale
492-
del _locale._getdefaultlocale
493-
else:
494-
orig_getlocale = None
495-
496-
orig_env = {}
497-
try:
498-
for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
499-
if key in os.environ:
500-
orig_env[key] = os.environ[key]
501-
del os.environ[key]
502-
503-
os.environ['LC_CTYPE'] = 'UTF-8'
504-
505-
with check_warnings(('', DeprecationWarning)):
506-
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
507-
508-
finally:
509-
for k in orig_env:
510-
os.environ[k] = orig_env[k]
511-
512-
if 'LC_CTYPE' not in orig_env:
513-
del os.environ['LC_CTYPE']
514-
515-
if orig_getlocale is not None:
516-
_locale._getdefaultlocale = orig_getlocale
517-
518484
def test_getencoding(self):
519485
# Invoke getencoding to make sure it does not cause exceptions.
520486
enc = locale.getencoding()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove two :mod:`locale` functions deprecated in Python 3.11:
2+
``getdefaultlocale()`` and ``resetlocale()``. Patch by Victor Stinner.

0 commit comments

Comments
 (0)