[Django] #34363: floatformat() crashes on "0.0000"

35 views
Skip to first unread message

Django

unread,
Feb 22, 2023, 6:25:50 AM2/22/23
to django-...@googlegroups.com
#34363: floatformat() crashes on "0.0000"
-------------------------------------------+-------------------------
Reporter: Takis Issaris | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 4.2
Severity: Release blocker | Keywords: filters
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+-------------------------
Similar to #34272 the current Django code (both 4.2b1 and latest git)
crash on using the `floatformat` template filter with
0 values.

{{{#!python
from decimal import Decimal
from django.template.defaultfilters import floatformat

floatformat('0.0000', 2)
floatformat(Decimal('0.0000'), 2)
}}}
Both throw {{{ValueError: valid range for prec is [1, MAX_PREC]}}}

Using `git bisect` I tracked the bug to commit
08c5a787262c1ae57f6517d4574b54a5fcaad124.

When one uses `"0.0000": floatformat:2` the current code results in a
precision of 0 which is
not allowed:

{{{
tupl = d.as_tuple() # with d being "0.0000" returns
(sign=0,digits=(0,),exponent=-4)
units = len(tupl[1]) # equals 1
units += -tupl[2] if m else tupl[2] # 1 + (-4)
prec = abs(p) + units + 1 # 2 (number of requested decimals) +
(-3) + 1 equals 0

rounded_d = d.quantize(exp, ROUND_HALF_UP, Context(prec=prec))
}}}

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

Django

unread,
Feb 22, 2023, 6:26:49 AM2/22/23
to django-...@googlegroups.com
#34363: floatformat() crashes on "0.0000"
---------------------------------+--------------------------------------

Reporter: Takis Issaris | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 4.2
Severity: Release blocker | Resolution:

Keywords: filters | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Takis Issaris):

* Attachment "ic-20230222T1222-django-floatformat.diff" added.

Fix floatformat for zero values

Django

unread,
Feb 22, 2023, 6:36:54 AM2/22/23
to django-...@googlegroups.com
#34363: floatformat() crashes on "0.0000"
---------------------------------+--------------------------------------
Reporter: Takis Issaris | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 4.2
Severity: Release blocker | Resolution:
Keywords: filters | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by Takis Issaris):

A minimal patch is available on this branch on GitHub:
https://github.com/takis/django/tree/ticket_34363

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

Django

unread,
Feb 22, 2023, 6:43:37 AM2/22/23
to django-...@googlegroups.com
#34363: floatformat() crashes on "0.0000"
---------------------------------+-----------------------------------------
Reporter: Takis Issaris | Owner: Takis Issaris
Type: Bug | Status: assigned

Component: Template system | Version: 4.2
Severity: Release blocker | Resolution:
Keywords: filters | 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):

* cc: David Wobrock (added)
* owner: nobody => Takis Issaris
* status: new => assigned
* stage: Unreviewed => Accepted


Comment:

Thanks for the report! Please submit PR via GitHub.

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

Django

unread,
Feb 22, 2023, 6:49:08 AM2/22/23
to django-...@googlegroups.com
#34363: floatformat() crashes on "0.0000"
---------------------------------+-----------------------------------------
Reporter: Takis Issaris | Owner: Takis Issaris
Type: Bug | Status: assigned
Component: Template system | Version: 4.2
Severity: Release blocker | Resolution:
Keywords: filters | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+-----------------------------------------

Comment (by Takis Issaris):

Thanks for the quick response! I've created a pull request on GitHub.

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

Django

unread,
Feb 22, 2023, 6:58:57 AM2/22/23
to django-...@googlegroups.com
#34363: floatformat() crashes on "0.0000"
---------------------------------+-----------------------------------------
Reporter: Takis Issaris | Owner: Takis Issaris
Type: Bug | Status: assigned
Component: Template system | Version: 4.2
Severity: Release blocker | Resolution:
Keywords: filters | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+-----------------------------------------

Comment (by Takis Issaris):

My actual code was different, but I had tried to keep the patch-size
minimal.

The code I was using in a separate test-file in which I factored out the
get_precision function for easier
testing. But I figured this might not be OK for performance reasons for
Django.

{{{
def get_precision(d: Decimal, p: int) -> int:
"""Return the number of significant digits for a decimal number.

d: the decimal number
p: the number of decimals requested
"""
if d == 0:
# If the number is zero, the precision is the number of decimals
requested
return p
_, digits, exponent = d.as_tuple()
units = len(digits) # total number of digits
before_comma = units + exponent

# adding 1 for possible rounding up e.g. (99.99, 1) -> 100.0 needs 4
digits
prec = abs(p) + before_comma + 1
return max(1, prec)
}}}

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

Django

unread,
Feb 22, 2023, 2:43:34 PM2/22/23
to django-...@googlegroups.com
#34363: floatformat() crashes on "0.0000"
-------------------------------------+-------------------------------------

Reporter: Takis Issaris | Owner: Takis
| Issaris
Type: Bug | Status: assigned
Component: Template system | Version: 4.2
Severity: Release blocker | Resolution:
Keywords: filters | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


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

Django

unread,
Feb 22, 2023, 2:46:31 PM2/22/23
to django-...@googlegroups.com
#34363: floatformat() crashes on "0.0000"
-------------------------------------+-------------------------------------
Reporter: Takis Issaris | Owner: Takis
| Issaris
Type: Bug | Status: closed

Component: Template system | Version: 4.2
Severity: Release blocker | Resolution: fixed

Keywords: filters | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"dcd974698301a38081c141ccba6dcafa5ed2c80e" dcd97469]:
{{{
#!CommitTicketReference repository=""
revision="dcd974698301a38081c141ccba6dcafa5ed2c80e"
Fixed #34363 -- Fixed floatformat crash on zero with trailing zeros.

Regression in 08c5a787262c1ae57f6517d4574b54a5fcaad124.
Follow up to 4b066bde692078b194709d517b27e55defae787c.
}}}

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

Django

unread,
Feb 22, 2023, 2:50:38 PM2/22/23
to django-...@googlegroups.com
#34363: floatformat() crashes on "0.0000"
-------------------------------------+-------------------------------------
Reporter: Takis Issaris | Owner: Takis
| Issaris
Type: Bug | Status: closed
Component: Template system | Version: 4.2
Severity: Release blocker | Resolution: fixed
Keywords: filters | Triage Stage: Ready for
| checkin
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:"ce69dba0000be7686eabdffa9a8ff60296229109" ce69dba0]:
{{{
#!CommitTicketReference repository=""
revision="ce69dba0000be7686eabdffa9a8ff60296229109"
[4.2.x] Fixed #34363 -- Fixed floatformat crash on zero with trailing
zeros.

Regression in 08c5a787262c1ae57f6517d4574b54a5fcaad124.
Follow up to 4b066bde692078b194709d517b27e55defae787c.

Backport of dcd974698301a38081c141ccba6dcafa5ed2c80e from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34363#comment:7>

Reply all
Reply to author
Forward
0 new messages