Skip to content

Commit d3efc12

Browse files
committed
gh-94808: Improve coverage of _PyBytes_FormatEx
There were two specific areas not covered: - %(name) syntax - %*s syntax
1 parent 1b46d11 commit d3efc12

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Lib/test/test_bytes.py

+18
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,24 @@ def test_mod(self):
715715
self.assertEqual(b, b'hello,\x00world!')
716716
self.assertIs(type(b), self.type2test)
717717

718+
def check(fmt, vals, result):
719+
b = self.type2test(fmt)
720+
b = b % vals
721+
self.assertEqual(b, result)
722+
self.assertIs(type(b), self.type2test)
723+
724+
# A set of tests adapted from test_unicode:UnicodeTest.test_formatting
725+
check(b'...%(foo)s...', {b'foo':b"abc"}, b'...abc...')
726+
check(b'...%(f(o)o)s...', {b'f(o)o':b"abc", b'foo':b'bar'}, b'...abc...')
727+
check(b'...%(foo)s...', {b'foo':b"abc",b'def':123}, b'...abc...')
728+
check(b'%*s', (5, b'abc',), b' abc')
729+
check(b'%*s', (-5, b'abc',), b'abc ')
730+
check(b'%*.*s', (5, 2, b'abc',), b' ab')
731+
check(b'%*.*s', (5, 3, b'abc',), b' abc')
732+
check(b'%i %*.*s', (10, 5, 3, b'abc',), b'10 abc')
733+
check(b'%i%s %*.*s', (10, b'3', 5, 3, b'abc',), b'103 abc')
734+
check(b'%c', b'a', b'a')
735+
718736
def test_imod(self):
719737
b = self.type2test(b'hello, %b!')
720738
orig = b

0 commit comments

Comments
 (0)