Skip to content

Commit e4d8bc9

Browse files
committed
Pass current tests
1 parent 80673fb commit e4d8bc9

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

Lib/test/test_format.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -661,26 +661,38 @@ def test_g_format_has_no_trailing_zeros(self):
661661
self.assertEqual(format(12300050.0, "#.6g"), "1.23000e+07")
662662

663663
def test_with_two_commas_in_format_specifier(self):
664-
error_msg = re.escape("Cannot specify ',' with ','.")
664+
error_msg = re.escape("Cannot specify grouping ',' more than once")
665665
with self.assertRaisesRegex(ValueError, error_msg):
666666
'{:,,}'.format(1)
667+
with self.assertRaisesRegex(ValueError, error_msg):
668+
'{:.,,}'.format(1.1)
669+
with self.assertRaisesRegex(ValueError, error_msg):
670+
'{:.,,f}'.format(1.1)
667671

668672
def test_with_two_underscore_in_format_specifier(self):
669-
error_msg = re.escape("Cannot specify '_' with '_'.")
673+
error_msg = re.escape("Cannot specify grouping '_' more than once")
670674
with self.assertRaisesRegex(ValueError, error_msg):
671675
'{:__}'.format(1)
676+
with self.assertRaisesRegex(ValueError, error_msg):
677+
'{:.__}'.format(1.1)
678+
with self.assertRaisesRegex(ValueError, error_msg):
679+
'{:.__f}'.format(1.1)
672680

673-
def test_with_a_commas_and_an_underscore_in_format_specifier(self):
674-
error_msg = re.escape("Cannot specify both ',' and '_'.")
681+
def test_with_a_comma_and_an_underscore_in_format_specifier(self):
682+
error_msg = re.escape("Cannot specify both ',' and '_'")
675683
with self.assertRaisesRegex(ValueError, error_msg):
676684
'{:,_}'.format(1)
685+
with self.assertRaisesRegex(ValueError, error_msg):
686+
'{:.,_}'.format(1.1)
677687
with self.assertRaisesRegex(ValueError, error_msg):
678688
'{:.,_f}'.format(1.1)
679689

680690
def test_with_an_underscore_and_a_comma_in_format_specifier(self):
681-
error_msg = re.escape("Cannot specify both ',' and '_'.")
691+
error_msg = re.escape("Cannot specify both ',' and '_'")
682692
with self.assertRaisesRegex(ValueError, error_msg):
683693
'{:_,}'.format(1)
694+
with self.assertRaisesRegex(ValueError, error_msg):
695+
'{:._,}'.format(1.1)
684696
with self.assertRaisesRegex(ValueError, error_msg):
685697
'{:._,f}'.format(1.1)
686698

Lib/test/test_fstring.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,24 +1697,32 @@ def test_invalid_syntax_error_message(self):
16971697
compile("f'{a $ b}'", "?", "exec")
16981698

16991699
def test_with_two_commas_in_format_specifier(self):
1700-
error_msg = re.escape("Cannot specify ',' with ','.")
1700+
error_msg = re.escape("Cannot specify grouping ',' more than once")
17011701
with self.assertRaisesRegex(ValueError, error_msg):
17021702
f'{1:,,}'
1703+
with self.assertRaisesRegex(ValueError, error_msg):
1704+
f'{1.1:.,,}'
17031705

17041706
def test_with_two_underscore_in_format_specifier(self):
1705-
error_msg = re.escape("Cannot specify '_' with '_'.")
1707+
error_msg = re.escape("Cannot specify grouping '_' more than once")
17061708
with self.assertRaisesRegex(ValueError, error_msg):
17071709
f'{1:__}'
1710+
with self.assertRaisesRegex(ValueError, error_msg):
1711+
f'{1.1:.__}'
17081712

1709-
def test_with_a_commas_and_an_underscore_in_format_specifier(self):
1710-
error_msg = re.escape("Cannot specify both ',' and '_'.")
1713+
def test_with_a_comma_and_an_underscore_in_format_specifier(self):
1714+
error_msg = re.escape("Cannot specify both ',' and '_'")
17111715
with self.assertRaisesRegex(ValueError, error_msg):
17121716
f'{1:,_}'
1717+
with self.assertRaisesRegex(ValueError, error_msg):
1718+
f'{1.1:.,_}'
17131719

17141720
def test_with_an_underscore_and_a_comma_in_format_specifier(self):
1715-
error_msg = re.escape("Cannot specify both ',' and '_'.")
1721+
error_msg = re.escape("Cannot specify both ',' and '_'")
17161722
with self.assertRaisesRegex(ValueError, error_msg):
17171723
f'{1:_,}'
1724+
with self.assertRaisesRegex(ValueError, error_msg):
1725+
f'{1.1:._,}'
17181726

17191727
def test_syntax_error_for_starred_expressions(self):
17201728
with self.assertRaisesRegex(SyntaxError, "can't use starred expression here"):

Objects/unicode_formatter.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,18 @@ invalid_thousands_separator_type(char separator, Py_UCS4 presentation_type)
283283
/* presentation_type has been checked before thousands separator. */
284284
assert(presentation_type >= 32 && presentation_type < 127);
285285
PyErr_Format(PyExc_ValueError,
286-
"Cannot specify '%c' with type code '%c'",
286+
"Cannot specify '%c' with '%c'",
287287
separator, (int)presentation_type);
288288
}
289289

290290
static void
291291
invalid_fraction_separator_type(char separator, Py_UCS4 presentation_type)
292292
{
293293
assert(separator == ',' || separator == '_');
294-
/* presentation_type has been checked before thousands separator. */
294+
/* presentation_type has been checked before fraction separator. */
295295
assert(presentation_type >= 32 && presentation_type < 127);
296296
PyErr_Format(PyExc_ValueError,
297-
"Cannot specify '%c' in fractional part with type code '%c'",
297+
"Cannot specify '%c' in fractional part with '%c'",
298298
separator, (int)presentation_type);
299299
}
300300

@@ -551,7 +551,17 @@ parse_internal_render_format_spec(PyObject *obj,
551551
"Format specifier missing precision");
552552
return 0;
553553
}
554+
}
554555

556+
if (end-pos) {
557+
Py_UCS4 next = READ_spec(pos);
558+
if (next == ',' || next == '_') {
559+
/* Expect type, got another grouping character */
560+
PyErr_Format(PyExc_ValueError,
561+
"Cannot specify grouping '%c' more than once",
562+
next);
563+
return 0;
564+
}
555565
}
556566

557567
/* Finally, parse the type field. */

0 commit comments

Comments
 (0)