From d301fc3f3199745c6c06085fb5453f0814df2195 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 11 Aug 2022 13:29:03 -0400 Subject: [PATCH] gh-94808: Improve coverage of _PyBytes_FormatEx There were two specific areas not covered: - %(name) syntax - %*s syntax --- Lib/test/test_bytes.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 521e391b8d5f9d..53ba1ad6f5911f 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -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)b...', {b'foo':b"abc"}, b'...abc...') + check(b'...%(f(o)o)b...', {b'f(o)o':b"abc", b'foo':b'bar'}, b'...abc...') + check(b'...%(foo)b...', {b'foo':b"abc",b'def':123}, b'...abc...') + check(b'%*b', (5, b'abc',), b' abc') + check(b'%*b', (-5, b'abc',), b'abc ') + check(b'%*.*b', (5, 2, b'abc',), b' ab') + check(b'%*.*b', (5, 3, b'abc',), b' abc') + check(b'%i %*.*b', (10, 5, 3, b'abc',), b'10 abc') + check(b'%i%b %*.*b', (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