Skip to content

Commit bca1380

Browse files
committed
Back performance to re
1 parent f2366f8 commit bca1380

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Lib/locale.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ def _strip_padding(s, amount):
176176
amount -= 1
177177
return s[lpos:rpos+1]
178178

179+
_percent_re = None
180+
179181
def _format(percent, value, grouping=False, monetary=False, *additional):
180182
if additional:
181183
formatted = percent % ((value,) + additional)
@@ -213,10 +215,12 @@ def format_string(f, val, grouping=False, monetary=False):
213215
Grouping is applied if the third parameter is true.
214216
Conversion uses monetary thousands separator and grouping strings if
215217
forth parameter monetary is true."""
216-
import re
218+
if _percent_re is None:
219+
import re
217220

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

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

0 commit comments

Comments
 (0)