Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Lib/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import sys
import encodings
import encodings.aliases
import re
import _collections_abc
from builtins import str as _builtin_str
import functools
Expand Down Expand Up @@ -177,8 +176,7 @@ def _strip_padding(s, amount):
amount -= 1
return s[lpos:rpos+1]

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

def _format(percent, value, grouping=False, monetary=False, *additional):
if additional:
Expand Down Expand Up @@ -217,6 +215,13 @@ def format_string(f, val, grouping=False, monetary=False):
Grouping is applied if the third parameter is true.
Conversion uses monetary thousands separator and grouping strings if
forth parameter monetary is true."""
global _percent_re
if _percent_re is None:
import re

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

percents = list(_percent_re.finditer(f))
new_f = _percent_re.sub('%s', f)

Expand Down
41 changes: 14 additions & 27 deletions Lib/test/test_locale.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from decimal import Decimal
from test.support import verbose, is_android, is_emscripten, is_wasi
from test.support import cpython_only, verbose, is_android, linked_to_musl, os_helper
from test.support.warnings_helper import check_warnings
from test.support.import_helper import import_fresh_module
from test.support.import_helper import ensure_lazy_imports, import_fresh_module
from unittest import mock
import unittest
import locale
import os
import sys
import codecs

class LazyImportTest(unittest.TestCase):
@cpython_only
def test_lazy_import(self):
ensure_lazy_imports("locale", {"re", "warnings"})


class BaseLocalizedTest(unittest.TestCase):
#
Expand Down Expand Up @@ -351,21 +356,15 @@ def setUp(self):

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

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

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

Expand All @@ -551,25 +549,14 @@ def test_defaults_UTF8(self):
else:
orig_getlocale = None

orig_env = {}
try:
for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
if key in os.environ:
orig_env[key] = os.environ[key]
del os.environ[key]

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

with check_warnings(('', DeprecationWarning)):
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
with os_helper.EnvironmentVarGuard() as env:
env.unset('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')
env.set('LC_CTYPE', 'UTF-8')

with check_warnings(('', DeprecationWarning)):
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
finally:
for k in orig_env:
os.environ[k] = orig_env[k]

if 'LC_CTYPE' not in orig_env:
del os.environ['LC_CTYPE']

if orig_getlocale is not None:
_locale._getdefaultlocale = orig_getlocale

Expand Down
Loading