Skip to content

Commit 9a413fa

Browse files
bpo-35560: Remove assertion from format(float, "n") (GH-11288)
Fix an assertion error in format() in debug build for floating point formatting with "n" format, zero padding and small width. Release build is not impacted. Patch by Karthikeyan Singaravelan. (cherry picked from commit 3f7983a) Co-authored-by: Xtreak <tir.karthi@gmail.com>
1 parent 65ed9f3 commit 9a413fa

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Lib/test/test_float.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,25 @@ def test_issue5864(self):
701701
self.assertEqual(format(1234.56, '.4'), '1.235e+03')
702702
self.assertEqual(format(12345.6, '.4'), '1.235e+04')
703703

704+
def test_issue35560(self):
705+
self.assertEqual(format(123.0, '00'), '123.0')
706+
self.assertEqual(format(123.34, '00f'), '123.340000')
707+
self.assertEqual(format(123.34, '00e'), '1.233400e+02')
708+
self.assertEqual(format(123.34, '00g'), '123.34')
709+
self.assertEqual(format(123.34, '00.10f'), '123.3400000000')
710+
self.assertEqual(format(123.34, '00.10e'), '1.2334000000e+02')
711+
self.assertEqual(format(123.34, '00.10g'), '123.34')
712+
self.assertEqual(format(123.34, '01f'), '123.340000')
713+
714+
self.assertEqual(format(-123.0, '00'), '-123.0')
715+
self.assertEqual(format(-123.34, '00f'), '-123.340000')
716+
self.assertEqual(format(-123.34, '00e'), '-1.233400e+02')
717+
self.assertEqual(format(-123.34, '00g'), '-123.34')
718+
self.assertEqual(format(-123.34, '00.10f'), '-123.3400000000')
719+
self.assertEqual(format(-123.34, '00.10f'), '-123.3400000000')
720+
self.assertEqual(format(-123.34, '00.10e'), '-1.2334000000e+02')
721+
self.assertEqual(format(-123.34, '00.10g'), '-123.34')
722+
704723
class ReprTestCase(unittest.TestCase):
705724
def test_repr(self):
706725
floats_file = open(os.path.join(os.path.split(__file__)[0],
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an assertion error in :func:`format` in debug build for floating point
2+
formatting with "n" format, zero padding and small width. Release build is
3+
not impacted. Patch by Karthikeyan Singaravelan.

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9356,6 +9356,7 @@ _PyUnicode_InsertThousandsGrouping(
93569356
PyObject *thousands_sep,
93579357
Py_UCS4 *maxchar)
93589358
{
9359+
min_width = Py_MAX(0, min_width);
93599360
if (writer) {
93609361
assert(digits != NULL);
93619362
assert(maxchar == NULL);
@@ -9366,7 +9367,6 @@ _PyUnicode_InsertThousandsGrouping(
93669367
}
93679368
assert(0 <= d_pos);
93689369
assert(0 <= n_digits);
9369-
assert(0 <= min_width);
93709370
assert(grouping != NULL);
93719371

93729372
if (digits != NULL) {

0 commit comments

Comments
 (0)