summaryrefslogtreecommitdiffstats
path: root/Lib/gettext.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-11-14 17:25:56 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-11-14 17:25:56 (GMT)
commit5ca92ab3d233f8c51d48bc6b70ee93c919b789bf (patch)
treefcda7e37b5ffc163ec11ef9bc703e16833a1b459 /Lib/gettext.py
parent84293aff9fec20cb903bf1242c28404c671c56d1 (diff)
parent60ac989d6f9884f551327cc7e9a4b71ef67ab533 (diff)
downloadcpython-5ca92ab3d233f8c51d48bc6b70ee93c919b789bf.zip
cpython-5ca92ab3d233f8c51d48bc6b70ee93c919b789bf.tar.gz
cpython-5ca92ab3d233f8c51d48bc6b70ee93c919b789bf.tar.bz2
Issue #28563: Make plural form selection more lenient and accepting
non-integer numbers. Django tests depend on this.
Diffstat (limited to 'Lib/gettext.py')
-rw-r--r--Lib/gettext.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/Lib/gettext.py b/Lib/gettext.py
index 1591e7e..4e6e15e 100644
--- a/Lib/gettext.py
+++ b/Lib/gettext.py
@@ -158,6 +158,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.
@@ -181,11 +189,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']