From f2366f8cbfc0d0d90b45bd2815f2503c644ff536 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Sat, 8 Feb 2025 22:25:51 +0400 Subject: [PATCH 1/3] Always lazy import re in locale --- Lib/locale.py | 9 +++++---- .../2025-02-08-21-37-05.gh-issue-118761.EtqxeB.rst | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-02-08-21-37-05.gh-issue-118761.EtqxeB.rst diff --git a/Lib/locale.py b/Lib/locale.py index 213d5e93418cfb..13ec9c8829d186 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -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 @@ -177,9 +176,6 @@ def _strip_padding(s, amount): amount -= 1 return s[lpos:rpos+1] -_percent_re = re.compile(r'%(?:\((?P.*?)\))?' - r'(?P[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]') - def _format(percent, value, grouping=False, monetary=False, *additional): if additional: formatted = percent % ((value,) + additional) @@ -217,6 +213,11 @@ 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.""" + import re + + _percent_re = re.compile(r'%(?:\((?P.*?)\))?(?P[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]') + percents = list(_percent_re.finditer(f)) new_f = _percent_re.sub('%s', f) diff --git a/Misc/NEWS.d/next/Library/2025-02-08-21-37-05.gh-issue-118761.EtqxeB.rst b/Misc/NEWS.d/next/Library/2025-02-08-21-37-05.gh-issue-118761.EtqxeB.rst new file mode 100644 index 00000000000000..b35275b321b4b2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-08-21-37-05.gh-issue-118761.EtqxeB.rst @@ -0,0 +1,2 @@ +Improve import time of :mod:`locale` using lazy import ``re``. Patch by +Semyon Moroz. From bca13804703451b715cfb5c5b0710ee8d0822f53 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Sun, 9 Feb 2025 04:50:56 +0400 Subject: [PATCH 2/3] Back performance to re --- Lib/locale.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/locale.py b/Lib/locale.py index 13ec9c8829d186..92efb576ec7676 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -176,6 +176,8 @@ def _strip_padding(s, amount): amount -= 1 return s[lpos:rpos+1] +_percent_re = None + def _format(percent, value, grouping=False, monetary=False, *additional): if additional: formatted = percent % ((value,) + additional) @@ -213,10 +215,12 @@ 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.""" - import re + if _percent_re is None: + import re - _percent_re = re.compile(r'%(?:\((?P.*?)\))?(?P[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]') + global _percent_re + _percent_re = re.compile(r'%(?:\((?P.*?)\))?(?P[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]') percents = list(_percent_re.finditer(f)) new_f = _percent_re.sub('%s', f) From 5e08b48d22cca84f4d7b30d5d90a3237b1ba2400 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Sun, 9 Feb 2025 04:56:13 +0400 Subject: [PATCH 3/3] Fix bug --- Lib/locale.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/locale.py b/Lib/locale.py index 92efb576ec7676..2feb10e59c96a3 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -215,10 +215,10 @@ 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 - global _percent_re _percent_re = re.compile(r'%(?:\((?P.*?)\))?(?P[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')