﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36715	intcomma filter crashes on non-finite numbers	Tim Graham	Varun Kasyap Pentamaraju	"When looking at humanize filters, an anonymous reporter discovered that the intcomma filter does not robustly handle values that are not a finite number, like Inf, -Inf, Infinity, -Infinity, NaN or sNaN, and raises a TypeError that is not caught in the calling code.

{{{#!python
diff --git a/tests/humanize_tests/tests.py b/tests/humanize_tests/tests.py
index ab967e2874..8b90245311 100644
--- a/tests/humanize_tests/tests.py
+++ b/tests/humanize_tests/tests.py
@@ -153,6 +153,7 @@ class HumanizeTests(SimpleTestCase):
             ""-1234567.1234567"",
             Decimal(""1234567.1234567""),
             Decimal(""-1234567.1234567""),
+            Decimal(""Infinity""),
             None,
             ""１２３４５６７"",
             ""-１２３４５６７"",
}}}
Observe a crash:
{{{
            # Format values with more than 200 digits (an arbitrary cutoff) using
            # scientific notation to avoid high memory usage in {:f}'.format().
            _, digits, exponent = number.as_tuple()
>           if abs(exponent) + len(digits) > 200:
               ^^^^^^^^^^^^^
E           TypeError: bad operand type for abs(): 'str'
}}}
The code could be fortified with something like:
{{{#!python
diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py
index cf8b2d219c..1f9ae840a5 100644
--- a/django/utils/numberformat.py
+++ b/django/utils/numberformat.py
@@ -48,6 +48,10 @@ def format(
             if abs(number) < cutoff:
                 number = Decimal(""0"")
 
+        if not number.is_finite():
+            # like NaN or Infinity
+            return str(number)
+
         # Format values with more than 200 digits (an arbitrary cutoff) using
         # scientific notation to avoid high memory usage in {:f}'.format().
         _, digits, exponent = number.as_tuple()
}}}"	Bug	closed	contrib.humanize	5.2	Normal	fixed			Ready for checkin	1	0	0	0	1	0
