Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b31ded5c
Commit
b31ded5c
authored
Nov 14, 2016
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #28563: Make plural form selection more lenient and accepting
non-integer numbers. Django tests depend on this.
parent
742dd004
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
8 deletions
+20
-8
Lib/gettext.py
Lib/gettext.py
+10
-2
Lib/test/test_gettext.py
Lib/test/test_gettext.py
+10
-6
No files found.
Lib/gettext.py
View file @
b31ded5c
...
...
@@ -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'
]
...
...
Lib/test/test_gettext.py
View file @
b31ded5c
...
...
@@ -365,12 +365,16 @@ class PluralFormsTestCase(GettextBaseTest):
self
.
assertRaises
(
ZeroDivisionError
,
f
,
0
)
def
test_plural_number
(
self
):
f
=
gettext
.
c2py
(
'1'
)
self
.
assertEqual
(
f
(
1
),
1
)
self
.
assertRaises
(
ValueError
,
f
,
1.0
)
self
.
assertRaises
(
ValueError
,
f
,
'1'
)
self
.
assertRaises
(
ValueError
,
f
,
[])
self
.
assertRaises
(
ValueError
,
f
,
object
())
f
=
gettext
.
c2py
(
'n != 1'
)
self
.
assertEqual
(
f
(
1
),
0
)
self
.
assertEqual
(
f
(
2
),
1
)
self
.
assertEqual
(
f
(
1.0
),
0
)
self
.
assertEqual
(
f
(
2.0
),
1
)
self
.
assertEqual
(
f
(
1.1
),
1
)
self
.
assertRaises
(
TypeError
,
f
,
'2'
)
self
.
assertRaises
(
TypeError
,
f
,
b'2'
)
self
.
assertRaises
(
TypeError
,
f
,
[])
self
.
assertRaises
(
TypeError
,
f
,
object
())
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment