Skip to content

Commit 2d3dec0

Browse files
authored
gh-140924: In locale module, add missing names to __all__ (GH-140925)
1 parent 56171da commit 2d3dec0

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

Lib/locale.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@
1717
from builtins import str as _builtin_str
1818
import functools
1919

20-
# Try importing the _locale module.
21-
#
22-
# If this fails, fall back on a basic 'C' locale emulation.
23-
2420
# Yuck: LC_MESSAGES is non-standard: can't tell whether it exists before
2521
# trying the import. So __all__ is also fiddled at the end of the file.
2622
__all__ = ["getlocale", "getdefaultlocale", "getpreferredencoding", "Error",
2723
"setlocale", "localeconv", "strcoll", "strxfrm",
2824
"str", "atof", "atoi", "format_string", "currency",
2925
"normalize", "LC_CTYPE", "LC_COLLATE", "LC_TIME", "LC_MONETARY",
30-
"LC_NUMERIC", "LC_ALL", "CHAR_MAX", "getencoding"]
26+
"LC_NUMERIC", "LC_ALL", "CHAR_MAX", "getencoding", "delocalize",
27+
"localize"]
3128

3229
def _strcoll(a,b):
3330
""" strcoll(string,string) -> int.
@@ -41,6 +38,9 @@ def _strxfrm(s):
4138
"""
4239
return s
4340

41+
# Try importing the _locale module.
42+
#
43+
# If this fails, fall back on a basic 'C' locale emulation.
4444
try:
4545

4646
from _locale import *
@@ -91,6 +91,29 @@ def setlocale(category, value=None):
9191
raise Error('_locale emulation only supports "C" locale')
9292
return 'C'
9393

94+
else:
95+
_conditional_constants_names = ["ABDAY_1", "ABDAY_2", "ABDAY_3",
96+
"ABDAY_4", "ABDAY_5", "ABDAY_6",
97+
"ABDAY_7", "ABMON_1", "ABMON_10",
98+
"ABMON_11", "ABMON_12", "ABMON_2",
99+
"ABMON_3", "ABMON_4", "ABMON_5",
100+
"ABMON_6", "ABMON_7", "ABMON_8",
101+
"ABMON_9", "ALT_DIGITS", "CODESET",
102+
"CRNCYSTR", "DAY_1", "DAY_2", "DAY_3",
103+
"DAY_4", "DAY_5", "DAY_6",
104+
"DAY_7", "D_FMT", "D_T_FMT",
105+
"ERA", "ERA_D_FMT", "ERA_D_T_FMT",
106+
"ERA_T_FMT", "MON_1", "MON_10",
107+
"MON_11", "MON_12", "MON_2", "MON_3",
108+
"MON_4", "MON_5", "MON_6", "MON_7",
109+
"MON_8", "MON_9", "NOEXPR",
110+
"RADIXCHAR", "THOUSEP", "T_FMT",
111+
"T_FMT_AMPM", "YESEXPR", "AM_STR",
112+
"PM_STR"]
113+
# The constants defined in _locale are platform-dependent,
114+
# so we only include those that are available on the current platform.
115+
__all__.extend(vars().keys() & _conditional_constants_names)
116+
94117
# These may or may not exist in _locale, so be sure to set them.
95118
if 'strxfrm' not in globals():
96119
strxfrm = _strxfrm

Lib/test/test_locale.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
import sys
1010
import codecs
1111

12+
13+
class MiscTestCase(unittest.TestCase):
14+
maxDiff = None
15+
def test__all__(self):
16+
extra = ["localeconv", "strcoll", "strxfrm", "getencoding",
17+
"Error"]
18+
not_exported = ["locale_encoding_alias", "locale_alias", "windows_locale"]
19+
support.check__all__(self, locale, extra=extra, not_exported=not_exported)
20+
1221
class LazyImportTest(unittest.TestCase):
1322
@cpython_only
1423
def test_lazy_import(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :func:`locale.localize`, :func:`locale.delocalize`
2+
and platform-specific locale constants
3+
from the :mod:`!_locale` module to ``locale.__all__``.

0 commit comments

Comments
 (0)