From b16779e92e44a4cc09c2b869e1abf897210857df Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Tue, 5 May 2020 00:36:23 +0800 Subject: [PATCH 1/2] import locale lazily in gettext --- Lib/gettext.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/gettext.py b/Lib/gettext.py index b98f501884b75a..f0ffb1686fb89d 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -46,7 +46,6 @@ # find this format documented anywhere. -import locale import os import re import sys @@ -210,6 +209,7 @@ def func(n): def _expand_lang(loc): + import locale loc = locale.normalize(loc) COMPONENT_CODESET = 1 << 0 COMPONENT_TERRITORY = 1 << 1 @@ -275,6 +275,7 @@ def gettext(self, message): return message def lgettext(self, message): + import locale import warnings warnings.warn('lgettext() is deprecated, use gettext() instead', DeprecationWarning, 2) @@ -296,6 +297,7 @@ def ngettext(self, msgid1, msgid2, n): return msgid2 def lngettext(self, msgid1, msgid2, n): + import locale import warnings warnings.warn('lngettext() is deprecated, use ngettext() instead', DeprecationWarning, 2) @@ -459,6 +461,7 @@ def _parse(self, fp): transidx += 8 def lgettext(self, message): + import locale import warnings warnings.warn('lgettext() is deprecated, use gettext() instead', DeprecationWarning, 2) @@ -473,6 +476,7 @@ def lgettext(self, message): return tmsg.encode(locale.getpreferredencoding()) def lngettext(self, msgid1, msgid2, n): + import locale import warnings warnings.warn('lngettext() is deprecated, use ngettext() instead', DeprecationWarning, 2) @@ -665,6 +669,7 @@ def dgettext(domain, message): return t.gettext(message) def ldgettext(domain, message): + import locale import warnings warnings.warn('ldgettext() is deprecated, use dgettext() instead', DeprecationWarning, 2) @@ -692,6 +697,7 @@ def dngettext(domain, msgid1, msgid2, n): return t.ngettext(msgid1, msgid2, n) def ldngettext(domain, msgid1, msgid2, n): + import locale import warnings warnings.warn('ldngettext() is deprecated, use dngettext() instead', DeprecationWarning, 2) From 359f8f1ea6db235569a794d8495f8b2412716267 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Tue, 5 May 2020 08:45:09 +0800 Subject: [PATCH 2/2] import locale in gettext after calling warnings.warn() --- Lib/gettext.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/gettext.py b/Lib/gettext.py index f0ffb1686fb89d..77b67aef4204c9 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -275,10 +275,10 @@ def gettext(self, message): return message def lgettext(self, message): - import locale import warnings warnings.warn('lgettext() is deprecated, use gettext() instead', DeprecationWarning, 2) + import locale if self._fallback: with warnings.catch_warnings(): warnings.filterwarnings('ignore', r'.*\blgettext\b.*', @@ -297,10 +297,10 @@ def ngettext(self, msgid1, msgid2, n): return msgid2 def lngettext(self, msgid1, msgid2, n): - import locale import warnings warnings.warn('lngettext() is deprecated, use ngettext() instead', DeprecationWarning, 2) + import locale if self._fallback: with warnings.catch_warnings(): warnings.filterwarnings('ignore', r'.*\blngettext\b.*', @@ -461,10 +461,10 @@ def _parse(self, fp): transidx += 8 def lgettext(self, message): - import locale import warnings warnings.warn('lgettext() is deprecated, use gettext() instead', DeprecationWarning, 2) + import locale missing = object() tmsg = self._catalog.get(message, missing) if tmsg is missing: @@ -476,10 +476,10 @@ def lgettext(self, message): return tmsg.encode(locale.getpreferredencoding()) def lngettext(self, msgid1, msgid2, n): - import locale import warnings warnings.warn('lngettext() is deprecated, use ngettext() instead', DeprecationWarning, 2) + import locale try: tmsg = self._catalog[(msgid1, self.plural(n))] except KeyError: @@ -669,10 +669,10 @@ def dgettext(domain, message): return t.gettext(message) def ldgettext(domain, message): - import locale import warnings warnings.warn('ldgettext() is deprecated, use dgettext() instead', DeprecationWarning, 2) + import locale codeset = _localecodesets.get(domain) try: with warnings.catch_warnings(): @@ -697,10 +697,10 @@ def dngettext(domain, msgid1, msgid2, n): return t.ngettext(msgid1, msgid2, n) def ldngettext(domain, msgid1, msgid2, n): - import locale import warnings warnings.warn('ldngettext() is deprecated, use dngettext() instead', DeprecationWarning, 2) + import locale codeset = _localecodesets.get(domain) try: with warnings.catch_warnings():