diff options
author | Dong-hee Na <donghee.na@python.org> | 2021-05-30 01:29:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-30 01:29:45 (GMT) |
commit | 14ba761078b5ae83519e34d66ab883743912c45b (patch) | |
tree | b7bb5b3db8655c783730cc4f7d07612b9cc50527 /Lib | |
parent | 5ffa58cb97ddc6d36c8c2c94d86d58aac95406b7 (diff) | |
download | cpython-14ba761078b5ae83519e34d66ab883743912c45b.zip cpython-14ba761078b5ae83519e34d66ab883743912c45b.tar.gz cpython-14ba761078b5ae83519e34d66ab883743912c45b.tar.bz2 |
bpo-44235: Remove deprecated functions in the gettext module. (GH-26378)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/gettext.py | 184 | ||||
-rw-r--r-- | Lib/test/test_gettext.py | 193 |
2 files changed, 20 insertions, 357 deletions
diff --git a/Lib/gettext.py b/Lib/gettext.py index 77b67ae..6c5ec4e 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -52,11 +52,10 @@ import sys __all__ = ['NullTranslations', 'GNUTranslations', 'Catalog', - 'find', 'translation', 'install', 'textdomain', 'bindtextdomain', - 'bind_textdomain_codeset', - 'dgettext', 'dngettext', 'gettext', 'lgettext', 'ldgettext', - 'ldngettext', 'lngettext', 'ngettext', - 'pgettext', 'dpgettext', 'npgettext', 'dnpgettext', + 'bindtextdomain', 'find', 'translation', 'install', + 'textdomain', 'dgettext', 'dngettext', 'gettext', + 'ngettext', 'pgettext', 'dpgettext', 'npgettext', + 'dnpgettext' ] _default_localedir = os.path.join(sys.base_prefix, 'share', 'locale') @@ -83,6 +82,7 @@ _token_pattern = re.compile(r""" (?P<INVALID>\w+|.) # invalid token """, re.VERBOSE|re.DOTALL) + def _tokenize(plural): for mo in re.finditer(_token_pattern, plural): kind = mo.lastgroup @@ -94,12 +94,14 @@ def _tokenize(plural): yield value yield '' + def _error(value): if value: return ValueError('unexpected token in plural form: %s' % value) else: return ValueError('unexpected end of plural form') + _binary_ops = ( ('||',), ('&&',), @@ -111,6 +113,7 @@ _binary_ops = ( _binary_ops = {op: i for i, ops in enumerate(_binary_ops, 1) for op in ops} _c2py_ops = {'||': 'or', '&&': 'and', '/': '//'} + def _parse(tokens, priority=-1): result = '' nexttok = next(tokens) @@ -160,6 +163,7 @@ def _parse(tokens, priority=-1): return result, nexttok + def _as_int(n): try: i = round(n) @@ -172,6 +176,7 @@ def _as_int(n): DeprecationWarning, 4) return n + def c2py(plural): """Gets a C expression as used in PO files for plural forms and returns a Python function that implements an equivalent expression. @@ -250,12 +255,10 @@ def _expand_lang(loc): return ret - class NullTranslations: def __init__(self, fp=None): self._info = {} self._charset = None - self._output_charset = None self._fallback = None if fp is not None: self._parse(fp) @@ -274,20 +277,6 @@ class NullTranslations: return self._fallback.gettext(message) return message - def lgettext(self, message): - 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.*', - DeprecationWarning) - return self._fallback.lgettext(message) - if self._output_charset: - return message.encode(self._output_charset) - return message.encode(locale.getpreferredencoding()) - def ngettext(self, msgid1, msgid2, n): if self._fallback: return self._fallback.ngettext(msgid1, msgid2, n) @@ -296,24 +285,6 @@ class NullTranslations: else: return msgid2 - def lngettext(self, msgid1, msgid2, n): - 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.*', - DeprecationWarning) - return self._fallback.lngettext(msgid1, msgid2, n) - if n == 1: - tmsg = msgid1 - else: - tmsg = msgid2 - if self._output_charset: - return tmsg.encode(self._output_charset) - return tmsg.encode(locale.getpreferredencoding()) - def pgettext(self, context, message): if self._fallback: return self._fallback.pgettext(context, message) @@ -333,24 +304,11 @@ class NullTranslations: def charset(self): return self._charset - def output_charset(self): - import warnings - warnings.warn('output_charset() is deprecated', - DeprecationWarning, 2) - return self._output_charset - - def set_output_charset(self, charset): - import warnings - warnings.warn('set_output_charset() is deprecated', - DeprecationWarning, 2) - self._output_charset = charset - def install(self, names=None): import builtins builtins.__dict__['_'] = self.gettext if names is not None: - allowed = {'gettext', 'lgettext', 'lngettext', - 'ngettext', 'npgettext', 'pgettext'} + allowed = {'gettext', 'ngettext', 'npgettext', 'pgettext'} for name in allowed & set(names): builtins.__dict__[name] = getattr(self, name) @@ -460,39 +418,6 @@ class GNUTranslations(NullTranslations): masteridx += 8 transidx += 8 - def lgettext(self, message): - 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: - if self._fallback: - return self._fallback.lgettext(message) - tmsg = message - if self._output_charset: - return tmsg.encode(self._output_charset) - return tmsg.encode(locale.getpreferredencoding()) - - def lngettext(self, msgid1, msgid2, n): - import warnings - warnings.warn('lngettext() is deprecated, use ngettext() instead', - DeprecationWarning, 2) - import locale - try: - tmsg = self._catalog[(msgid1, self.plural(n))] - except KeyError: - if self._fallback: - return self._fallback.lngettext(msgid1, msgid2, n) - if n == 1: - tmsg = msgid1 - else: - tmsg = msgid2 - if self._output_charset: - return tmsg.encode(self._output_charset) - return tmsg.encode(locale.getpreferredencoding()) - def gettext(self, message): missing = object() tmsg = self._catalog.get(message, missing) @@ -575,13 +500,12 @@ def find(domain, localedir=None, languages=None, all=False): return result - # a mapping between absolute .mo file path and Translation object _translations = {} -_unspecified = ['unspecified'] + def translation(domain, localedir=None, languages=None, - class_=None, fallback=False, codeset=_unspecified): + class_=None, fallback=False): if class_ is None: class_ = GNUTranslations mofiles = find(domain, localedir, languages, all=True) @@ -607,15 +531,6 @@ def translation(domain, localedir=None, languages=None, # are not used. import copy t = copy.copy(t) - if codeset is not _unspecified: - import warnings - warnings.warn('parameter codeset is deprecated', - DeprecationWarning, 2) - if codeset: - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', r'.*\bset_output_charset\b.*', - DeprecationWarning) - t.set_output_charset(codeset) if result is None: result = t else: @@ -623,16 +538,13 @@ def translation(domain, localedir=None, languages=None, return result -def install(domain, localedir=None, codeset=_unspecified, names=None): - t = translation(domain, localedir, fallback=True, codeset=codeset) +def install(domain, localedir=None, *, names=None): + t = translation(domain, localedir, fallback=True) t.install(names) - # a mapping b/w domains and locale directories _localedirs = {} -# a mapping b/w domains and codesets -_localecodesets = {} # current global domain, `messages' used for compatibility w/ GNU gettext _current_domain = 'messages' @@ -651,16 +563,6 @@ def bindtextdomain(domain, localedir=None): return _localedirs.get(domain, _default_localedir) -def bind_textdomain_codeset(domain, codeset=None): - import warnings - warnings.warn('bind_textdomain_codeset() is deprecated', - DeprecationWarning, 2) - global _localecodesets - if codeset is not None: - _localecodesets[domain] = codeset - return _localecodesets.get(domain) - - def dgettext(domain, message): try: t = translation(domain, _localedirs.get(domain, None)) @@ -668,23 +570,6 @@ def dgettext(domain, message): return message return t.gettext(message) -def ldgettext(domain, message): - import warnings - warnings.warn('ldgettext() is deprecated, use dgettext() instead', - DeprecationWarning, 2) - import locale - codeset = _localecodesets.get(domain) - try: - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', r'.*\bparameter codeset\b.*', - DeprecationWarning) - t = translation(domain, _localedirs.get(domain, None), codeset=codeset) - except OSError: - return message.encode(codeset or locale.getpreferredencoding()) - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', r'.*\blgettext\b.*', - DeprecationWarning) - return t.lgettext(message) def dngettext(domain, msgid1, msgid2, n): try: @@ -696,28 +581,6 @@ def dngettext(domain, msgid1, msgid2, n): return msgid2 return t.ngettext(msgid1, msgid2, n) -def ldngettext(domain, msgid1, msgid2, n): - import warnings - warnings.warn('ldngettext() is deprecated, use dngettext() instead', - DeprecationWarning, 2) - import locale - codeset = _localecodesets.get(domain) - try: - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', r'.*\bparameter codeset\b.*', - DeprecationWarning) - t = translation(domain, _localedirs.get(domain, None), codeset=codeset) - except OSError: - if n == 1: - tmsg = msgid1 - else: - tmsg = msgid2 - return tmsg.encode(codeset or locale.getpreferredencoding()) - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', r'.*\blngettext\b.*', - DeprecationWarning) - return t.lngettext(msgid1, msgid2, n) - def dpgettext(domain, context, message): try: @@ -741,27 +604,10 @@ def dnpgettext(domain, context, msgid1, msgid2, n): def gettext(message): return dgettext(_current_domain, message) -def lgettext(message): - import warnings - warnings.warn('lgettext() is deprecated, use gettext() instead', - DeprecationWarning, 2) - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', r'.*\bldgettext\b.*', - DeprecationWarning) - return ldgettext(_current_domain, message) def ngettext(msgid1, msgid2, n): return dngettext(_current_domain, msgid1, msgid2, n) -def lngettext(msgid1, msgid2, n): - import warnings - warnings.warn('lngettext() is deprecated, use ngettext() instead', - DeprecationWarning, 2) - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', r'.*\bldngettext\b.*', - DeprecationWarning) - return ldngettext(_current_domain, msgid1, msgid2, n) - def pgettext(context, message): return dpgettext(_current_domain, context, message) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index 575914d..467652a 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -1,6 +1,5 @@ import os import base64 -import contextlib import gettext import unittest @@ -204,6 +203,7 @@ trggrkg zrffntr pngnybt yvoenel.''') def test_the_alternative_interface(self): eq = self.assertEqual + neq = self.assertNotEqual # test the alternative interface with open(self.mofile, 'rb') as fp: t = gettext.GNUTranslations(fp) @@ -215,12 +215,13 @@ trggrkg zrffntr pngnybt yvoenel.''') eq(_('mullusk'), 'bacon') # Test installation of other methods import builtins - t.install(names=["gettext", "lgettext"]) + t.install(names=["gettext", "ngettext"]) eq(_, t.gettext) eq(builtins.gettext, t.gettext) - eq(lgettext, t.lgettext) + eq(ngettext, t.ngettext) + neq(pgettext, t.pgettext) del builtins.gettext - del builtins.lgettext + del builtins.ngettext class GettextTestCase2(GettextBaseTest): @@ -502,180 +503,6 @@ class PluralFormsTestCase(GettextBaseTest): self.assertRaises(TypeError, f, object()) -class LGettextTestCase(GettextBaseTest): - def setUp(self): - GettextBaseTest.setUp(self) - self.mofile = MOFILE - - @contextlib.contextmanager - def assertDeprecated(self, name): - with self.assertWarnsRegex(DeprecationWarning, - fr'^{name}\(\) is deprecated'): - yield - - def test_lgettext(self): - lgettext = gettext.lgettext - ldgettext = gettext.ldgettext - with self.assertDeprecated('lgettext'): - self.assertEqual(lgettext('mullusk'), b'bacon') - with self.assertDeprecated('lgettext'): - self.assertEqual(lgettext('spam'), b'spam') - with self.assertDeprecated('ldgettext'): - self.assertEqual(ldgettext('gettext', 'mullusk'), b'bacon') - with self.assertDeprecated('ldgettext'): - self.assertEqual(ldgettext('gettext', 'spam'), b'spam') - - def test_lgettext_2(self): - with open(self.mofile, 'rb') as fp: - t = gettext.GNUTranslations(fp) - lgettext = t.lgettext - with self.assertDeprecated('lgettext'): - self.assertEqual(lgettext('mullusk'), b'bacon') - with self.assertDeprecated('lgettext'): - self.assertEqual(lgettext('spam'), b'spam') - - def test_lgettext_bind_textdomain_codeset(self): - lgettext = gettext.lgettext - ldgettext = gettext.ldgettext - with self.assertDeprecated('bind_textdomain_codeset'): - saved_codeset = gettext.bind_textdomain_codeset('gettext') - try: - with self.assertDeprecated('bind_textdomain_codeset'): - gettext.bind_textdomain_codeset('gettext', 'utf-16') - with self.assertDeprecated('lgettext'): - self.assertEqual(lgettext('mullusk'), 'bacon'.encode('utf-16')) - with self.assertDeprecated('lgettext'): - self.assertEqual(lgettext('spam'), 'spam'.encode('utf-16')) - with self.assertDeprecated('ldgettext'): - self.assertEqual(ldgettext('gettext', 'mullusk'), 'bacon'.encode('utf-16')) - with self.assertDeprecated('ldgettext'): - self.assertEqual(ldgettext('gettext', 'spam'), 'spam'.encode('utf-16')) - finally: - del gettext._localecodesets['gettext'] - with self.assertDeprecated('bind_textdomain_codeset'): - gettext.bind_textdomain_codeset('gettext', saved_codeset) - - def test_lgettext_output_encoding(self): - with open(self.mofile, 'rb') as fp: - t = gettext.GNUTranslations(fp) - lgettext = t.lgettext - with self.assertDeprecated('set_output_charset'): - t.set_output_charset('utf-16') - with self.assertDeprecated('lgettext'): - self.assertEqual(lgettext('mullusk'), 'bacon'.encode('utf-16')) - with self.assertDeprecated('lgettext'): - self.assertEqual(lgettext('spam'), 'spam'.encode('utf-16')) - - def test_lngettext(self): - lngettext = gettext.lngettext - ldngettext = gettext.ldngettext - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s file', 'There are %s files', 1) - self.assertEqual(x, b'Hay %s fichero') - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s file', 'There are %s files', 2) - self.assertEqual(x, b'Hay %s ficheros') - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s directory', 'There are %s directories', 1) - self.assertEqual(x, b'There is %s directory') - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s directory', 'There are %s directories', 2) - self.assertEqual(x, b'There are %s directories') - with self.assertDeprecated('ldngettext'): - x = ldngettext('gettext', 'There is %s file', 'There are %s files', 1) - self.assertEqual(x, b'Hay %s fichero') - with self.assertDeprecated('ldngettext'): - x = ldngettext('gettext', 'There is %s file', 'There are %s files', 2) - self.assertEqual(x, b'Hay %s ficheros') - with self.assertDeprecated('ldngettext'): - x = ldngettext('gettext', 'There is %s directory', 'There are %s directories', 1) - self.assertEqual(x, b'There is %s directory') - with self.assertDeprecated('ldngettext'): - x = ldngettext('gettext', 'There is %s directory', 'There are %s directories', 2) - self.assertEqual(x, b'There are %s directories') - - def test_lngettext_2(self): - with open(self.mofile, 'rb') as fp: - t = gettext.GNUTranslations(fp) - lngettext = t.lngettext - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s file', 'There are %s files', 1) - self.assertEqual(x, b'Hay %s fichero') - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s file', 'There are %s files', 2) - self.assertEqual(x, b'Hay %s ficheros') - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s directory', 'There are %s directories', 1) - self.assertEqual(x, b'There is %s directory') - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s directory', 'There are %s directories', 2) - self.assertEqual(x, b'There are %s directories') - - def test_lngettext_bind_textdomain_codeset(self): - lngettext = gettext.lngettext - ldngettext = gettext.ldngettext - with self.assertDeprecated('bind_textdomain_codeset'): - saved_codeset = gettext.bind_textdomain_codeset('gettext') - try: - with self.assertDeprecated('bind_textdomain_codeset'): - gettext.bind_textdomain_codeset('gettext', 'utf-16') - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s file', 'There are %s files', 1) - self.assertEqual(x, 'Hay %s fichero'.encode('utf-16')) - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s file', 'There are %s files', 2) - self.assertEqual(x, 'Hay %s ficheros'.encode('utf-16')) - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s directory', 'There are %s directories', 1) - self.assertEqual(x, 'There is %s directory'.encode('utf-16')) - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s directory', 'There are %s directories', 2) - self.assertEqual(x, 'There are %s directories'.encode('utf-16')) - with self.assertDeprecated('ldngettext'): - x = ldngettext('gettext', 'There is %s file', 'There are %s files', 1) - self.assertEqual(x, 'Hay %s fichero'.encode('utf-16')) - with self.assertDeprecated('ldngettext'): - x = ldngettext('gettext', 'There is %s file', 'There are %s files', 2) - self.assertEqual(x, 'Hay %s ficheros'.encode('utf-16')) - with self.assertDeprecated('ldngettext'): - x = ldngettext('gettext', 'There is %s directory', 'There are %s directories', 1) - self.assertEqual(x, 'There is %s directory'.encode('utf-16')) - with self.assertDeprecated('ldngettext'): - x = ldngettext('gettext', 'There is %s directory', 'There are %s directories', 2) - self.assertEqual(x, 'There are %s directories'.encode('utf-16')) - finally: - del gettext._localecodesets['gettext'] - with self.assertDeprecated('bind_textdomain_codeset'): - gettext.bind_textdomain_codeset('gettext', saved_codeset) - - def test_lngettext_output_encoding(self): - with open(self.mofile, 'rb') as fp: - t = gettext.GNUTranslations(fp) - lngettext = t.lngettext - with self.assertDeprecated('set_output_charset'): - t.set_output_charset('utf-16') - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s file', 'There are %s files', 1) - self.assertEqual(x, 'Hay %s fichero'.encode('utf-16')) - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s file', 'There are %s files', 2) - self.assertEqual(x, 'Hay %s ficheros'.encode('utf-16')) - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s directory', 'There are %s directories', 1) - self.assertEqual(x, 'There is %s directory'.encode('utf-16')) - with self.assertDeprecated('lngettext'): - x = lngettext('There is %s directory', 'There are %s directories', 2) - self.assertEqual(x, 'There are %s directories'.encode('utf-16')) - - def test_output_encoding(self): - with open(self.mofile, 'rb') as fp: - t = gettext.GNUTranslations(fp) - with self.assertDeprecated('set_output_charset'): - t.set_output_charset('utf-16') - with self.assertDeprecated('output_charset'): - self.assertEqual(t.output_charset(), 'utf-16') - - class GNUTranslationParsingTest(GettextBaseTest): def test_plural_form_error_issue17898(self): with open(MOFILE, 'wb') as fp: @@ -807,16 +634,6 @@ class GettextCacheTestCase(GettextBaseTest): self.assertEqual(len(gettext._translations), 2) self.assertEqual(t.__class__, DummyGNUTranslations) - # Test deprecated parameter codeset - with self.assertWarnsRegex(DeprecationWarning, 'parameter codeset'): - t = gettext.translation('gettext', self.localedir, - class_=DummyGNUTranslations, - codeset='utf-16') - self.assertEqual(len(gettext._translations), 2) - self.assertEqual(t.__class__, DummyGNUTranslations) - with self.assertWarns(DeprecationWarning): - self.assertEqual(t.output_charset(), 'utf-16') - class MiscTestCase(unittest.TestCase): def test__all__(self): |