summaryrefslogtreecommitdiff
path: root/django/utils/translation
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-06-09 12:48:20 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-06-11 20:34:59 +0200
commit9e38ed0536c7dc598a6c2c1bb774d0a8db3cdddc (patch)
tree735fc06110b76c86b0090a8dd53e2a6a1fcfae4e /django/utils/translation
parent175656e166712db019a4704c4031510b9fd6b00a (diff)
Fixed #27486 -- Fixed Python 3.7 DeprecationWarning in intword and filesizeformat filters.
intword and filesizeformat passed floats to ngettext() which is deprecated in Python 3.7. The rationale for this warning is documented in BPO-28692: https://bugs.python.org/issue28692. For filesizeformat, the filesize value is expected to be an int -- it fills %d string formatting placeholders. It was likely coerced to a float to ensure floating point division on Python 2. Python 3 always does floating point division, so coerce to an int instead of a float to fix the warning. For intword, the number may contain a decimal component. In English, a decimal component makes the noun plural. A helper function, round_away_from_one(), was added to convert the float to an integer that is appropriate for ngettext().
Diffstat (limited to 'django/utils/translation')
-rw-r--r--django/utils/translation/__init__.py5
1 files changed, 5 insertions, 0 deletions
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
index 8f3e3396a4..e48c7d245d 100644
--- a/django/utils/translation/__init__.py
+++ b/django/utils/translation/__init__.py
@@ -4,6 +4,7 @@ Internationalization support.
import re
import warnings
from contextlib import ContextDecorator
+from decimal import ROUND_UP, Decimal
from django.utils.autoreload import autoreload_started, file_changed
from django.utils.deprecation import RemovedInDjango40Warning
@@ -332,3 +333,7 @@ trim_whitespace_re = re.compile(r'\s*\n\s*')
def trim_whitespace(s):
return trim_whitespace_re.sub(' ', s.strip())
+
+
+def round_away_from_one(value):
+ return int(Decimal(value - 1).quantize(Decimal('0'), rounding=ROUND_UP)) + 1