mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Issue #28563: Make plural form selection more lenient and accepting
non-integer numbers. Django tests depend on this.
This commit is contained in:
parent
b626643734
commit
60ac989d6f
2 changed files with 20 additions and 8 deletions
|
@ -156,6 +156,14 @@ 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
|
||||
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.
|
||||
|
@ -179,11 +187,11 @@ def c2py(plural):
|
|||
elif c == ')':
|
||||
depth -= 1
|
||||
|
||||
ns = {}
|
||||
ns = {'_as_int': _as_int}
|
||||
exec('''if True:
|
||||
def func(n):
|
||||
if not isinstance(n, int):
|
||||
raise ValueError('Plural value must be an integer.')
|
||||
n = _as_int(n)
|
||||
return int(%s)
|
||||
''' % result, ns)
|
||||
return ns['func']
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue