8000 bpo-35560: Remove assertion from format(float, "n") (GH-11288) · python/cpython@3f7983a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f7983a

Browse files
tirkarthivstinner
authored andcommitted
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.
1 parent a0bb51e commit 3f7983a

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
@@ -9381,6 +9381,7 @@ _PyUnicode_InsertThousandsGrouping(
93819381
PyObject *thousands_sep,
93829382
Py_UCS4 *maxchar)
93839383
{
9384+
min_width = Py_MAX(0, min_width);
93849385
if (writer) {
93859386
assert(digits != NULL);
93869387
assert(maxchar == NULL);
@@ -9391,7 +9392,6 @@ _PyUnicode_InsertThousandsGrouping(
93919392
}
93929393
assert(0 <= d_pos);
93939394
assert(0 <= n_digits);
9394-
assert(0 <= min_width);
93959395
assert(grouping != NULL);
93969396

93979397
if (digits != NULL) {

0 commit comments

Comments
 (0)
0