8000 gh-118761: Always lazy import `re` in `locale` (#129860) · python/cpython@cf5e438 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf5e438

Browse files
authored
gh-118761: Always lazy import re in locale (#129860)
1 parent 3796884 commit cf5e438

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve import time of :mod:`locale` using lazy import ``re``. Patch by
2+
Semyon Moroz.

0 commit comments

Comments
 (0)
0