Skip to content

Commit a27d812

Browse files
CPython Developersyouknowone
authored andcommitted
Update locale from v3.14.3
1 parent 04cf5da commit a27d812

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

Lib/locale.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import sys
1414
import encodings
1515
import encodings.aliases
16-
import re
1716
import _collections_abc
1817
from builtins import str as _builtin_str
1918
import functools
@@ -177,8 +176,7 @@ def _strip_padding(s, amount):
177176
amount -= 1
178177
return s[lpos:rpos+1]
179178

180-
_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?'
181-
r'(?P<modifiers>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')
179+
_percent_re = None
182180

183181
def _format(percent, value, grouping=False, monetary=False, *additional):
184182
if additional:
@@ -217,6 +215,13 @@ def format_string(f, val, grouping=False, monetary=False):
217215
Grouping is applied if the third parameter is true.
218216
Conversion uses monetary thousands separator and grouping strings if
219217
forth parameter monetary is true."""
218+
global _percent_re
219+
if _percent_re is None:
220+
import re
221+
222+
_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?(?P<modifiers'
223+
r'>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')
224+
220225
percents = list(_percent_re.finditer(f))
221226
new_f = _percent_re.sub('%s', f)
222227

Lib/test/test_locale.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
from decimal import Decimal
2-
from test.support import verbose, is_android, is_emscripten, is_wasi
2+
from test.support import cpython_only, verbose, is_android, linked_to_musl, os_helper
33
from test.support.warnings_helper import check_warnings
4-
from test.support.import_helper import import_fresh_module
4+
from test.support.import_helper import ensure_lazy_imports, import_fresh_module
55
from unittest import mock
66
import unittest
77
import locale
88
import os
99
import sys
1010
import codecs
1111

12+
class LazyImportTest(unittest.TestCase):
13+
@cpython_only
14+
def test_lazy_import(self):
15+
ensure_lazy_imports("locale", {"re", "warnings"})
16+
1217

1318
class BaseLocalizedTest(unittest.TestCase):
1419
#
@@ -351,21 +356,15 @@ def setUp(self):
351356

352357
@unittest.skipIf(sys.platform.startswith('aix'),
353358
'bpo-29972: broken test on AIX')
354-
@unittest.skipIf(
355-
is_emscripten or is_wasi,
356-
"musl libc issue on Emscripten/WASI, bpo-46390"
357-
)
359+
@unittest.skipIf(linked_to_musl(), "musl libc issue, bpo-46390")
358360
@unittest.skipIf(sys.platform.startswith("netbsd"),
359361
"gh-124108: NetBSD doesn't support UTF-8 for LC_COLLATE")
360362
def test_strcoll_with_diacritic(self):
361363
self.assertLess(locale.strcoll('à', 'b'), 0)
362364

363365
@unittest.skipIf(sys.platform.startswith('aix'),
364366
'bpo-29972: broken test on AIX')
365-
@unittest.skipIf(
366-
is_emscripten or is_wasi,
367-
"musl libc issue on Emscripten/WASI, bpo-46390"
368-
)
367+
@unittest.skipIf(linked_to_musl(), "musl libc issue, bpo-46390")
369368
@unittest.skipIf(sys.platform.startswith("netbsd"),
370369
"gh-124108: NetBSD doesn't support UTF-8 for LC_COLLATE")
371370
def test_strxfrm_with_diacritic(self):
@@ -541,7 +540,6 @@ def test_defaults_UTF8(self):
541540
# valid. Furthermore LC_CTYPE=UTF is used by the UTF-8 locale coercing
542541
# during interpreter startup (on macOS).
543542
import _locale
544-
import os
545543

546544
self.assertEqual(locale._parse_localename('UTF-8'), (None, 'UTF-8'))
547545

@@ -551,25 +549,14 @@ def test_defaults_UTF8(self):
551549
else:
552550
orig_getlocale = None
553551

554-
orig_env = {}
555552
try:
556-
for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
557-
if key in os.environ:
558-
orig_env[key] = os.environ[key]
559-
del os.environ[key]
560-
561-
os.environ['LC_CTYPE'] = 'UTF-8'
562-
563-
with check_warnings(('', DeprecationWarning)):
564-
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
553+
with os_helper.EnvironmentVarGuard() as env:
554+
env.unset('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')
555+
env.set('LC_CTYPE', 'UTF-8')
565556

557+
with check_warnings(('', DeprecationWarning)):
558+
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
566559
finally:
567-
for k in orig_env:
568-
os.environ[k] = orig_env[k]
569-
570-
if 'LC_CTYPE' not in orig_env:
571-
del os.environ['LC_CTYPE']
572-
573560
if orig_getlocale is not None:
574561
_locale._getdefaultlocale = orig_getlocale
575562

0 commit comments

Comments
 (0)