Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-94194: gettext plural values must now be integers #94195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ Changes in the Python API
to :term:`filesystem encoding and error handler`.
Argument files should be encoded in UTF-8 instead of ANSI Codepage on Windows.

* :mod:`gettext`: Using non-integer value for selecting a plural form is no
longer supported. It never correctly worked and was deprecated since Python
3.7.
(Contributed by Victor Stinner in :gh:`94194`.)


Build Changes
=============
Expand Down
26 changes: 8 additions & 18 deletions Lib/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,26 +164,13 @@ def _parse(tokens, priority=-1):
return result, nexttok


def _as_int(n):
try:
i = round(n)
except TypeError:
raise TypeError('Plural value must be an integer, got %s' %
(n.__class__.__name__,)) from None
import warnings
warnings.warn('Plural value must be an integer, got %s' %
(n.__class__.__name__,),
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.
"""

if len(plural) > 1000:
raise ValueError('plural form expression is too long')

try:
result, nexttok = _parse(_tokenize(plural))
if nexttok:
Expand All @@ -200,13 +187,16 @@ def c2py(plural):
elif c == ')':
depth -= 1

ns = {'_as_int': _as_int}
exec('''if True:
ns = {}
code = '''if True:
def func(n):
if not isinstance(n, int):
n = _as_int(n)
raise TypeError(f'Plural value must be an integer, '
f'got {n.__class__.__name__}')
return int(%s)
''' % result, ns)
'''
code = code % result
exec(code, ns)
return ns['func']
except RecursionError:
# Recursion error can be raised in _parse() or exec().
Expand Down
8 changes: 2 additions & 6 deletions Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,8 @@ def test_plural_number(self):
f = gettext.c2py('n != 1')
self.assertEqual(f(1), 0)
self.assertEqual(f(2), 1)
with self.assertWarns(DeprecationWarning):
self.assertEqual(f(1.0), 0)
with self.assertWarns(DeprecationWarning):
self.assertEqual(f(2.0), 1)
with self.assertWarns(DeprecationWarning):
self.assertEqual(f(1.1), 1)

self.assertRaises(TypeError, f, 1.0)
self.assertRaises(TypeError, f, '2')
self.assertRaises(TypeError, f, b'2')
self.assertRaises(TypeError, f, [])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`gettext`: Using non-integer value for selecting a plural form is no
longer supported. It never correctly worked and was deprecated since Python
3.7. Patch by Victor Stinner.