[Django] #27486: intword and filesizeformat filters shouldn't pass non-integers to gettext

13 views
Skip to first unread message

Django

unread,
Nov 15, 2016, 9:34:02 AM11/15/16
to django-...@googlegroups.com
#27486: intword and filesizeformat filters shouldn't pass non-integers to gettext
-------------------------------------+-------------------------------------
Reporter: Tim | Owner: Tim Graham
Graham |
Type: | Status: assigned
Cleanup/optimization |
Component: Template | Version: master
system |
Severity: Normal | Keywords:
Triage Stage: Accepted | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Python plans to [http://bugs.python.org/issue28692 deprecate selecting
plural form by fractional numbers]. The `intword` and `filesizeformat`
template filters are currently doing this:

{{{
$ python -Wall tests/runtests.py
humanize_tests.tests.HumanizeTests.test_i18n_intword
Testing against Django installed in '/home/tim/code/django/django' with up
to 3 processes
Creating test database for alias 'default'...
Creating test database for alias 'other'...
/home/tim/code/django/django/utils/translation/trans_real.py:373:
DeprecationWarning: Plural value must be an integer, got float
return getattr(t, translation_function)(singular, plural, number)
.
}}}

{{{
python -Wall tests/runtests.py
template_tests.filter_tests.test_filesizeformat.FunctionTests.test_localized_formats
Testing against Django installed in '/home/tim/code/django/django' with up
to 3 processes
Creating test database for alias 'default'...
Creating test database for alias 'other'...
/home/tim/code/django/django/utils/translation/trans_real.py:373:
DeprecationWarning: Plural value must be an integer, got float
return getattr(t, translation_function)(singular, plural, number)
.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27486>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 15, 2016, 2:40:49 PM11/15/16
to django-...@googlegroups.com
#27486: intword and filesizeformat filters shouldn't pass non-integers to gettext
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Tim
Type: | Graham
Cleanup/optimization | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz):

The exact rule about pluralizing floats/decimals depends on the language,
so whether we floor or ceil the number before passing it to `ngettext`
we'll do the right thing for some languages and wrong for others. This is
a known limitation of the `gettext` spec (in C, `ngettext` is only
accepting integers). We should make a choice and then document the
limitation.

It would not be impossible to try providing a language-specific hook in
the LANG_INFO structure and call that hook when converting floats to ints.

--
Ticket URL: <https://code.djangoproject.com/ticket/27486#comment:1>

Django

unread,
Nov 16, 2016, 12:15:54 PM11/16/16
to django-...@googlegroups.com
#27486: intword and filesizeformat filters shouldn't pass non-integers to gettext
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Tim
Type: | Graham
Cleanup/optimization | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

My [https://github.com/django/django/pull/7561 PR] does naive `int()`
casting. I closed it since it seems a more involved solution would be
better. Perhaps we could look to see how other projects handle the issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/27486#comment:2>

Django

unread,
Feb 17, 2017, 1:36:59 PM2/17/17
to django-...@googlegroups.com
#27486: intword and filesizeformat filters shouldn't pass non-integers to gettext
--------------------------------------+------------------------------------
Reporter: Tim Graham | Owner: (none)
Type: Cleanup/optimization | Status: new

Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* status: assigned => new
* owner: Tim Graham => (none)


--
Ticket URL: <https://code.djangoproject.com/ticket/27486#comment:3>

Django

unread,
Jun 9, 2019, 7:59:12 PM6/9/19
to django-...@googlegroups.com
#27486: intword and filesizeformat filters shouldn't pass non-integers to gettext
--------------------------------------+------------------------------------
Reporter: Tim Graham | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Jon Dufresne):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/11459 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/27486#comment:4>

Django

unread,
Jun 11, 2019, 4:13:08 PM6/11/19
to django-...@googlegroups.com
#27486: intword and filesizeformat filters shouldn't pass non-integers to gettext
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Mariusz
Type: | Felisiak <felisiak.mariusz@…>
Cleanup/optimization | Status: closed

Component: Template system | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: new => closed
* owner: (none) => Mariusz Felisiak <felisiak.mariusz@…>
* resolution: => fixed


Comment:

In [changeset:"9e38ed0536c7dc598a6c2c1bb774d0a8db3cdddc" 9e38ed05]:
{{{
#!CommitTicketReference repository=""
revision="9e38ed0536c7dc598a6c2c1bb774d0a8db3cdddc"
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().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27486#comment:6>

Django

unread,
Jun 11, 2019, 4:13:09 PM6/11/19
to django-...@googlegroups.com
#27486: intword and filesizeformat filters shouldn't pass non-integers to gettext
--------------------------------------+------------------------------------
Reporter: Tim Graham | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"53ea535f13e988b13499a62bab3831eb44e19e06" 53ea535f]:
{{{
#!CommitTicketReference repository=""
revision="53ea535f13e988b13499a62bab3831eb44e19e06"
Refs #27486 -- Added tests for filesizeformat filter.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27486#comment:5>

Reply all
Reply to author
Forward
0 new messages