Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#17414 closed Bug (fixed)

intcomma raises ZeroDivisionError for locales whose NUMBER_GROUPING is unspecified

Reported by: jittat Owned by: nobody
Component: Internationalization Version: master
Severity: Normal Keywords: ZeroDivisionError, intcomma
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When using locales that do not specify NUMBER_GROUPING (like 'fa','ja','th',) template filter intcomma (from humanize) raises ZeroDivisionError.

Version: Django 1.4 pre-alpha SVN-17202

How to reproduce:

  1. Make sure that USE_L10N = True in settings.py
  2. Try this code in shell
from django.utils import translation
translation.activate('th')
t = "{% load humanize %}}{{ 100|intcomma }}"
from django.template import Template, Context
Template(t).render(Context())
  1. You can change 'th' to 'ja', 'mn', 'ro' and many others.

Possible cause:

This is not the issue directly related to contrib.humanize's intcomma.

The exception occurs in function format in django.utils.numberformat when Django tries to divide something with parameter grouping. This parameter is assigned in function number_format in django.utils.formats from a call to get_format('NUMBER_GROUPING'), which returns 0 for many locales (e.g., th, ja, km, zh) with no definition of NUMBER_GROUPING in the format files. Therefore, you get ZeroDivisionError.

How to fix:

I am not sure where to put the fixing code. These are the options:

  • Use something like 3 as a default for NUMBER_GROUPING.
  • Change function format in django.utils.numberformat to make sure that grouping is non-zero (and if it is zero, use something like 3 as a default).

Since I am not sure where to put the fix, I have not tried to produce the patch. I can try to do that for the 2nd option if it makes sense.

Attachments (1)

17414-1.diff (1.6 KB) - added by claudep 3 years ago.
Fix possible ZeroDivisionError in numberformat

Download all attachments as: .zip

Change History (4)

comment:1 Changed 3 years ago by claudep

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 3 years ago by aaugustin

  • Resolution set to fixed
  • Status changed from new to closed

In [17267]:

Fixed #17414 -- Prevented numberformat from trying to group digits when the number of digits per group is zero.

Changed 3 years ago by claudep

Fix possible ZeroDivisionError in numberformat

comment:3 Changed 3 years ago by aaugustin

In [17268]:

Added a lower level test for numberformat when grouping is 0 and force_grouping is True. Thanks Claude Paroz. Refs #17414.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.