Skip to content

Commit

Permalink
gh-94808: Improve coverage of _PyBytes_FormatEx
Browse files Browse the repository at this point in the history
There were two specific areas not covered:

- %(name) syntax
- %*s syntax
  • Loading branch information
mdboom committed Aug 11, 2022
1 parent 1b46d11 commit d3efc12
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,24 @@ def test_mod(self):
self.assertEqual(b, b'hello,\x00world!')
self.assertIs(type(b), self.type2test)

def check(fmt, vals, result):
b = self.type2test(fmt)
b = b % vals
self.assertEqual(b, result)
self.assertIs(type(b), self.type2test)

# A set of tests adapted from test_unicode:UnicodeTest.test_formatting
check(b'...%(foo)s...', {b'foo':b"abc"}, b'...abc...')
check(b'...%(f(o)o)s...', {b'f(o)o':b"abc", b'foo':b'bar'}, b'...abc...')
check(b'...%(foo)s...', {b'foo':b"abc",b'def':123}, b'...abc...')
check(b'%*s', (5, b'abc',), b' abc')
check(b'%*s', (-5, b'abc',), b'abc ')
check(b'%*.*s', (5, 2, b'abc',), b' ab')
check(b'%*.*s', (5, 3, b'abc',), b' abc')
check(b'%i %*.*s', (10, 5, 3, b'abc',), b'10 abc')
check(b'%i%s %*.*s', (10, b'3', 5, 3, b'abc',), b'103 abc')
check(b'%c', b'a', b'a')

def test_imod(self):
b = self.type2test(b'hello, %b!')
orig = b
Expand Down

0 comments on commit d3efc12

Please sign in to comment.