mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-44235: Remove deprecated functions in the gettext module. (GH-26378)
This commit is contained in:
parent
5ffa58cb97
commit
14ba761078
5 changed files with 40 additions and 447 deletions
|
@ -46,16 +46,6 @@ class-based API instead.
|
||||||
returned. [#]_
|
returned. [#]_
|
||||||
|
|
||||||
|
|
||||||
.. function:: bind_textdomain_codeset(domain, codeset=None)
|
|
||||||
|
|
||||||
Bind the *domain* to *codeset*, changing the encoding of byte strings
|
|
||||||
returned by the :func:`lgettext`, :func:`ldgettext`, :func:`lngettext`
|
|
||||||
and :func:`ldngettext` functions.
|
|
||||||
If *codeset* is omitted, then the current binding is returned.
|
|
||||||
|
|
||||||
.. deprecated-removed:: 3.8 3.10
|
|
||||||
|
|
||||||
|
|
||||||
.. function:: textdomain(domain=None)
|
.. function:: textdomain(domain=None)
|
||||||
|
|
||||||
Change or query the current global domain. If *domain* is ``None``, then the
|
Change or query the current global domain. If *domain* is ``None``, then the
|
||||||
|
@ -108,29 +98,6 @@ class-based API instead.
|
||||||
.. versionadded:: 3.8
|
.. versionadded:: 3.8
|
||||||
|
|
||||||
|
|
||||||
.. function:: lgettext(message)
|
|
||||||
.. function:: ldgettext(domain, message)
|
|
||||||
.. function:: lngettext(singular, plural, n)
|
|
||||||
.. function:: ldngettext(domain, singular, plural, n)
|
|
||||||
|
|
||||||
Equivalent to the corresponding functions without the ``l`` prefix
|
|
||||||
(:func:`.gettext`, :func:`dgettext`, :func:`ngettext` and :func:`dngettext`),
|
|
||||||
but the translation is returned as a byte string encoded in the preferred
|
|
||||||
system encoding if no other encoding was explicitly set with
|
|
||||||
:func:`bind_textdomain_codeset`.
|
|
||||||
|
|
||||||
.. warning::
|
|
||||||
|
|
||||||
These functions should be avoided in Python 3, because they return
|
|
||||||
encoded bytes. It's much better to use alternatives which return
|
|
||||||
Unicode strings instead, since most Python applications will want to
|
|
||||||
manipulate human readable text as strings instead of bytes. Further,
|
|
||||||
it's possible that you may get unexpected Unicode-related exceptions
|
|
||||||
if there are encoding problems with the translated strings.
|
|
||||||
|
|
||||||
.. deprecated-removed:: 3.8 3.10
|
|
||||||
|
|
||||||
|
|
||||||
Note that GNU :program:`gettext` also defines a :func:`dcgettext` method, but
|
Note that GNU :program:`gettext` also defines a :func:`dcgettext` method, but
|
||||||
this was deemed not useful and so it is currently unimplemented.
|
this was deemed not useful and so it is currently unimplemented.
|
||||||
|
|
||||||
|
@ -181,7 +148,7 @@ install themselves in the built-in namespace as the function :func:`_`.
|
||||||
the environment variables.
|
the environment variables.
|
||||||
|
|
||||||
|
|
||||||
.. function:: translation(domain, localedir=None, languages=None, class_=None, fallback=False, codeset=None)
|
.. function:: translation(domain, localedir=None, languages=None, class_=None, fallback=False)
|
||||||
|
|
||||||
Return a :class:`*Translations` instance based on the *domain*, *localedir*,
|
Return a :class:`*Translations` instance based on the *domain*, *localedir*,
|
||||||
and *languages*, which are first passed to :func:`find` to get a list of the
|
and *languages*, which are first passed to :func:`find` to get a list of the
|
||||||
|
@ -205,15 +172,13 @@ install themselves in the built-in namespace as the function :func:`_`.
|
||||||
.. versionchanged:: 3.3
|
.. versionchanged:: 3.3
|
||||||
:exc:`IOError` used to be raised instead of :exc:`OSError`.
|
:exc:`IOError` used to be raised instead of :exc:`OSError`.
|
||||||
|
|
||||||
.. deprecated-removed:: 3.8 3.10
|
.. versionchanged:: 3.11
|
||||||
The *codeset* parameter.
|
*codeset* parameter is removed.
|
||||||
|
|
||||||
|
.. function:: install(domain, localedir=None, *, names=None)
|
||||||
.. function:: install(domain, localedir=None, codeset=None, names=None)
|
|
||||||
|
|
||||||
This installs the function :func:`_` in Python's builtins namespace, based on
|
This installs the function :func:`_` in Python's builtins namespace, based on
|
||||||
*domain*, *localedir*, and *codeset* which are passed to the function
|
*domain* and *localedir* which are passed to the function :func:`translation`.
|
||||||
:func:`translation`.
|
|
||||||
|
|
||||||
For the *names* parameter, please see the description of the translation
|
For the *names* parameter, please see the description of the translation
|
||||||
object's :meth:`~NullTranslations.install` method.
|
object's :meth:`~NullTranslations.install` method.
|
||||||
|
@ -228,9 +193,8 @@ install themselves in the built-in namespace as the function :func:`_`.
|
||||||
builtins namespace, so it is easily accessible in all modules of your
|
builtins namespace, so it is easily accessible in all modules of your
|
||||||
application.
|
application.
|
||||||
|
|
||||||
.. deprecated-removed:: 3.8 3.10
|
.. versionchanged:: 3.11
|
||||||
The *codeset* parameter.
|
*names* is now a keyword-only parameter.
|
||||||
|
|
||||||
|
|
||||||
The :class:`NullTranslations` class
|
The :class:`NullTranslations` class
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -294,22 +258,6 @@ are the methods of :class:`!NullTranslations`:
|
||||||
.. versionadded:: 3.8
|
.. versionadded:: 3.8
|
||||||
|
|
||||||
|
|
||||||
.. method:: lgettext(message)
|
|
||||||
.. method:: lngettext(singular, plural, n)
|
|
||||||
|
|
||||||
Equivalent to :meth:`.gettext` and :meth:`.ngettext`, but the translation
|
|
||||||
is returned as a byte string encoded in the preferred system encoding
|
|
||||||
if no encoding was explicitly set with :meth:`set_output_charset`.
|
|
||||||
Overridden in derived classes.
|
|
||||||
|
|
||||||
.. warning::
|
|
||||||
|
|
||||||
These methods should be avoided in Python 3. See the warning for the
|
|
||||||
:func:`lgettext` function.
|
|
||||||
|
|
||||||
.. deprecated-removed:: 3.8 3.10
|
|
||||||
|
|
||||||
|
|
||||||
.. method:: info()
|
.. method:: info()
|
||||||
|
|
||||||
Return the "protected" :attr:`_info` variable, a dictionary containing
|
Return the "protected" :attr:`_info` variable, a dictionary containing
|
||||||
|
@ -321,21 +269,6 @@ are the methods of :class:`!NullTranslations`:
|
||||||
Return the encoding of the message catalog file.
|
Return the encoding of the message catalog file.
|
||||||
|
|
||||||
|
|
||||||
.. method:: output_charset()
|
|
||||||
|
|
||||||
Return the encoding used to return translated messages in :meth:`.lgettext`
|
|
||||||
and :meth:`.lngettext`.
|
|
||||||
|
|
||||||
.. deprecated-removed:: 3.8 3.10
|
|
||||||
|
|
||||||
|
|
||||||
.. method:: set_output_charset(charset)
|
|
||||||
|
|
||||||
Change the encoding used to return translated messages.
|
|
||||||
|
|
||||||
.. deprecated-removed:: 3.8 3.10
|
|
||||||
|
|
||||||
|
|
||||||
.. method:: install(names=None)
|
.. method:: install(names=None)
|
||||||
|
|
||||||
This method installs :meth:`.gettext` into the built-in namespace,
|
This method installs :meth:`.gettext` into the built-in namespace,
|
||||||
|
@ -450,22 +383,6 @@ unexpected, or if other problems occur while reading the file, instantiating a
|
||||||
.. versionadded:: 3.8
|
.. versionadded:: 3.8
|
||||||
|
|
||||||
|
|
||||||
.. method:: lgettext(message)
|
|
||||||
.. method:: lngettext(singular, plural, n)
|
|
||||||
|
|
||||||
Equivalent to :meth:`.gettext` and :meth:`.ngettext`, but the translation
|
|
||||||
is returned as a byte string encoded in the preferred system encoding
|
|
||||||
if no encoding was explicitly set with
|
|
||||||
:meth:`~NullTranslations.set_output_charset`.
|
|
||||||
|
|
||||||
.. warning::
|
|
||||||
|
|
||||||
These methods should be avoided in Python 3. See the warning for the
|
|
||||||
:func:`lgettext` function.
|
|
||||||
|
|
||||||
.. deprecated-removed:: 3.8 3.10
|
|
||||||
|
|
||||||
|
|
||||||
Solaris message catalog support
|
Solaris message catalog support
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
|
|
@ -169,3 +169,15 @@ Removed
|
||||||
Use the new :c:type:`PyConfig` API of the :ref:`Python Initialization Configuration
|
Use the new :c:type:`PyConfig` API of the :ref:`Python Initialization Configuration
|
||||||
<init-config>` instead (:pep:`587`).
|
<init-config>` instead (:pep:`587`).
|
||||||
(Contributed by Victor Stinner in :issue:`44113`.)
|
(Contributed by Victor Stinner in :issue:`44113`.)
|
||||||
|
|
||||||
|
* The following deprecated functions and methods are removed in the :mod:`gettext`
|
||||||
|
module: :func:`~gettext.lgettext`, :func:`~gettext.ldgettext`,
|
||||||
|
:func:`~gettext.lngettext` and :func:`~gettext.ldngettext`.
|
||||||
|
|
||||||
|
Function :func:`~gettext.bind_textdomain_codeset`, methods
|
||||||
|
:meth:`~gettext.NullTranslations.output_charset` and
|
||||||
|
:meth:`~gettext.NullTranslations.set_output_charset`, and the *codeset*
|
||||||
|
parameter of functions :func:`~gettext.translation` and
|
||||||
|
:func:`~gettext.install` are also removed, since they are only used for
|
||||||
|
the ``l*gettext()`` functions.
|
||||||
|
(Contributed by Dong-hee Na and Serhiy Storchaka in :issue:`44235`.)
|
184
Lib/gettext.py
184
Lib/gettext.py
|
@ -52,11 +52,10 @@ import sys
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['NullTranslations', 'GNUTranslations', 'Catalog',
|
__all__ = ['NullTranslations', 'GNUTranslations', 'Catalog',
|
||||||
'find', 'translation', 'install', 'textdomain', 'bindtextdomain',
|
'bindtextdomain', 'find', 'translation', 'install',
|
||||||
'bind_textdomain_codeset',
|
'textdomain', 'dgettext', 'dngettext', 'gettext',
|
||||||
'dgettext', 'dngettext', 'gettext', 'lgettext', 'ldgettext',
|
'ngettext', 'pgettext', 'dpgettext', 'npgettext',
|
||||||
'ldngettext', 'lngettext', 'ngettext',
|
'dnpgettext'
|
||||||
'pgettext', 'dpgettext', 'npgettext', 'dnpgettext',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
_default_localedir = os.path.join(sys.base_prefix, 'share', 'locale')
|
_default_localedir = os.path.join(sys.base_prefix, 'share', 'locale')
|
||||||
|
@ -83,6 +82,7 @@ _token_pattern = re.compile(r"""
|
||||||
(?P<INVALID>\w+|.) # invalid token
|
(?P<INVALID>\w+|.) # invalid token
|
||||||
""", re.VERBOSE|re.DOTALL)
|
""", re.VERBOSE|re.DOTALL)
|
||||||
|
|
||||||
|
|
||||||
def _tokenize(plural):
|
def _tokenize(plural):
|
||||||
for mo in re.finditer(_token_pattern, plural):
|
for mo in re.finditer(_token_pattern, plural):
|
||||||
kind = mo.lastgroup
|
kind = mo.lastgroup
|
||||||
|
@ -94,12 +94,14 @@ def _tokenize(plural):
|
||||||
yield value
|
yield value
|
||||||
yield ''
|
yield ''
|
||||||
|
|
||||||
|
|
||||||
def _error(value):
|
def _error(value):
|
||||||
if value:
|
if value:
|
||||||
return ValueError('unexpected token in plural form: %s' % value)
|
return ValueError('unexpected token in plural form: %s' % value)
|
||||||
else:
|
else:
|
||||||
return ValueError('unexpected end of plural form')
|
return ValueError('unexpected end of plural form')
|
||||||
|
|
||||||
|
|
||||||
_binary_ops = (
|
_binary_ops = (
|
||||||
('||',),
|
('||',),
|
||||||
('&&',),
|
('&&',),
|
||||||
|
@ -111,6 +113,7 @@ _binary_ops = (
|
||||||
_binary_ops = {op: i for i, ops in enumerate(_binary_ops, 1) for op in ops}
|
_binary_ops = {op: i for i, ops in enumerate(_binary_ops, 1) for op in ops}
|
||||||
_c2py_ops = {'||': 'or', '&&': 'and', '/': '//'}
|
_c2py_ops = {'||': 'or', '&&': 'and', '/': '//'}
|
||||||
|
|
||||||
|
|
||||||
def _parse(tokens, priority=-1):
|
def _parse(tokens, priority=-1):
|
||||||
result = ''
|
result = ''
|
||||||
nexttok = next(tokens)
|
nexttok = next(tokens)
|
||||||
|
@ -160,6 +163,7 @@ def _parse(tokens, priority=-1):
|
||||||
|
|
||||||
return result, nexttok
|
return result, nexttok
|
||||||
|
|
||||||
|
|
||||||
def _as_int(n):
|
def _as_int(n):
|
||||||
try:
|
try:
|
||||||
i = round(n)
|
i = round(n)
|
||||||
|
@ -172,6 +176,7 @@ def _as_int(n):
|
||||||
DeprecationWarning, 4)
|
DeprecationWarning, 4)
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
|
||||||
def c2py(plural):
|
def c2py(plural):
|
||||||
"""Gets a C expression as used in PO files for plural forms and returns a
|
"""Gets a C expression as used in PO files for plural forms and returns a
|
||||||
Python function that implements an equivalent expression.
|
Python function that implements an equivalent expression.
|
||||||
|
@ -250,12 +255,10 @@ def _expand_lang(loc):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class NullTranslations:
|
class NullTranslations:
|
||||||
def __init__(self, fp=None):
|
def __init__(self, fp=None):
|
||||||
self._info = {}
|
self._info = {}
|
||||||
self._charset = None
|
self._charset = None
|
||||||
self._output_charset = None
|
|
||||||
self._fallback = None
|
self._fallback = None
|
||||||
if fp is not None:
|
if fp is not None:
|
||||||
self._parse(fp)
|
self._parse(fp)
|
||||||
|
@ -274,20 +277,6 @@ class NullTranslations:
|
||||||
return self._fallback.gettext(message)
|
return self._fallback.gettext(message)
|
||||||
return 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):
|
def ngettext(self, msgid1, msgid2, n):
|
||||||
if self._fallback:
|
if self._fallback:
|
||||||
return self._fallback.ngettext(msgid1, msgid2, n)
|
return self._fallback.ngettext(msgid1, msgid2, n)
|
||||||
|
@ -296,24 +285,6 @@ class NullTranslations:
|
||||||
else:
|
else:
|
||||||
return msgid2
|
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):
|
def pgettext(self, context, message):
|
||||||
if self._fallback:
|
if self._fallback:
|
||||||
return self._fallback.pgettext(context, message)
|
return self._fallback.pgettext(context, message)
|
||||||
|
@ -333,24 +304,11 @@ class NullTranslations:
|
||||||
def charset(self):
|
def charset(self):
|
||||||
return self._charset
|
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):
|
def install(self, names=None):
|
||||||
import builtins
|
import builtins
|
||||||
builtins.__dict__['_'] = self.gettext
|
builtins.__dict__['_'] = self.gettext
|
||||||
if names is not None:
|
if names is not None:
|
||||||
allowed = {'gettext', 'lgettext', 'lngettext',
|
allowed = {'gettext', 'ngettext', 'npgettext', 'pgettext'}
|
||||||
'ngettext', 'npgettext', 'pgettext'}
|
|
||||||
for name in allowed & set(names):
|
for name in allowed & set(names):
|
||||||
builtins.__dict__[name] = getattr(self, name)
|
builtins.__dict__[name] = getattr(self, name)
|
||||||
|
|
||||||
|
@ -460,39 +418,6 @@ class GNUTranslations(NullTranslations):
|
||||||
masteridx += 8
|
masteridx += 8
|
||||||
transidx += 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):
|
def gettext(self, message):
|
||||||
missing = object()
|
missing = object()
|
||||||
tmsg = self._catalog.get(message, missing)
|
tmsg = self._catalog.get(message, missing)
|
||||||
|
@ -575,13 +500,12 @@ def find(domain, localedir=None, languages=None, all=False):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# a mapping between absolute .mo file path and Translation object
|
# a mapping between absolute .mo file path and Translation object
|
||||||
_translations = {}
|
_translations = {}
|
||||||
_unspecified = ['unspecified']
|
|
||||||
|
|
||||||
def translation(domain, localedir=None, languages=None,
|
def translation(domain, localedir=None, languages=None,
|
||||||
class_=None, fallback=False, codeset=_unspecified):
|
class_=None, fallback=False):
|
||||||
if class_ is None:
|
if class_ is None:
|
||||||
class_ = GNUTranslations
|
class_ = GNUTranslations
|
||||||
mofiles = find(domain, localedir, languages, all=True)
|
mofiles = find(domain, localedir, languages, all=True)
|
||||||
|
@ -607,15 +531,6 @@ def translation(domain, localedir=None, languages=None,
|
||||||
# are not used.
|
# are not used.
|
||||||
import copy
|
import copy
|
||||||
t = copy.copy(t)
|
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:
|
if result is None:
|
||||||
result = t
|
result = t
|
||||||
else:
|
else:
|
||||||
|
@ -623,16 +538,13 @@ def translation(domain, localedir=None, languages=None,
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def install(domain, localedir=None, codeset=_unspecified, names=None):
|
def install(domain, localedir=None, *, names=None):
|
||||||
t = translation(domain, localedir, fallback=True, codeset=codeset)
|
t = translation(domain, localedir, fallback=True)
|
||||||
t.install(names)
|
t.install(names)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# a mapping b/w domains and locale directories
|
# a mapping b/w domains and locale directories
|
||||||
_localedirs = {}
|
_localedirs = {}
|
||||||
# a mapping b/w domains and codesets
|
|
||||||
_localecodesets = {}
|
|
||||||
# current global domain, `messages' used for compatibility w/ GNU gettext
|
# current global domain, `messages' used for compatibility w/ GNU gettext
|
||||||
_current_domain = 'messages'
|
_current_domain = 'messages'
|
||||||
|
|
||||||
|
@ -651,16 +563,6 @@ def bindtextdomain(domain, localedir=None):
|
||||||
return _localedirs.get(domain, _default_localedir)
|
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):
|
def dgettext(domain, message):
|
||||||
try:
|
try:
|
||||||
t = translation(domain, _localedirs.get(domain, None))
|
t = translation(domain, _localedirs.get(domain, None))
|
||||||
|
@ -668,23 +570,6 @@ def dgettext(domain, message):
|
||||||
return message
|
return message
|
||||||
return t.gettext(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):
|
def dngettext(domain, msgid1, msgid2, n):
|
||||||
try:
|
try:
|
||||||
|
@ -696,28 +581,6 @@ def dngettext(domain, msgid1, msgid2, n):
|
||||||
return msgid2
|
return msgid2
|
||||||
return t.ngettext(msgid1, msgid2, n)
|
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):
|
def dpgettext(domain, context, message):
|
||||||
try:
|
try:
|
||||||
|
@ -741,27 +604,10 @@ def dnpgettext(domain, context, msgid1, msgid2, n):
|
||||||
def gettext(message):
|
def gettext(message):
|
||||||
return dgettext(_current_domain, 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):
|
def ngettext(msgid1, msgid2, n):
|
||||||
return dngettext(_current_domain, 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):
|
def pgettext(context, message):
|
||||||
return dpgettext(_current_domain, context, message)
|
return dpgettext(_current_domain, context, message)
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import os
|
import os
|
||||||
import base64
|
import base64
|
||||||
import contextlib
|
|
||||||
import gettext
|
import gettext
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
@ -204,6 +203,7 @@ trggrkg zrffntr pngnybt yvoenel.''')
|
||||||
|
|
||||||
def test_the_alternative_interface(self):
|
def test_the_alternative_interface(self):
|
||||||
eq = self.assertEqual
|
eq = self.assertEqual
|
||||||
|
neq = self.assertNotEqual
|
||||||
# test the alternative interface
|
# test the alternative interface
|
||||||
with open(self.mofile, 'rb') as fp:
|
with open(self.mofile, 'rb') as fp:
|
||||||
t = gettext.GNUTranslations(fp)
|
t = gettext.GNUTranslations(fp)
|
||||||
|
@ -215,12 +215,13 @@ trggrkg zrffntr pngnybt yvoenel.''')
|
||||||
eq(_('mullusk'), 'bacon')
|
eq(_('mullusk'), 'bacon')
|
||||||
# Test installation of other methods
|
# Test installation of other methods
|
||||||
import builtins
|
import builtins
|
||||||
t.install(names=["gettext", "lgettext"])
|
t.install(names=["gettext", "ngettext"])
|
||||||
eq(_, t.gettext)
|
eq(_, t.gettext)
|
||||||
eq(builtins.gettext, t.gettext)
|
eq(builtins.gettext, t.gettext)
|
||||||
eq(lgettext, t.lgettext)
|
eq(ngettext, t.ngettext)
|
||||||
|
neq(pgettext, t.pgettext)
|
||||||
del builtins.gettext
|
del builtins.gettext
|
||||||
del builtins.lgettext
|
del builtins.ngettext
|
||||||
|
|
||||||
|
|
||||||
class GettextTestCase2(GettextBaseTest):
|
class GettextTestCase2(GettextBaseTest):
|
||||||
|
@ -502,180 +503,6 @@ class PluralFormsTestCase(GettextBaseTest):
|
||||||
self.assertRaises(TypeError, f, object())
|
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):
|
class GNUTranslationParsingTest(GettextBaseTest):
|
||||||
def test_plural_form_error_issue17898(self):
|
def test_plural_form_error_issue17898(self):
|
||||||
with open(MOFILE, 'wb') as fp:
|
with open(MOFILE, 'wb') as fp:
|
||||||
|
@ -807,16 +634,6 @@ class GettextCacheTestCase(GettextBaseTest):
|
||||||
self.assertEqual(len(gettext._translations), 2)
|
self.assertEqual(len(gettext._translations), 2)
|
||||||
self.assertEqual(t.__class__, DummyGNUTranslations)
|
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):
|
class MiscTestCase(unittest.TestCase):
|
||||||
def test__all__(self):
|
def test__all__(self):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Remove deprecated functions in the :mod:`gettext`. Patch by Dong-hee Na.
|
Loading…
Add table
Add a link
Reference in a new issue