Skip to content

Commit e8cf9bb

Browse files
ApostolFetpicnixz
andcommitted
pythongh-131015: Add test for bytes formatting errors (python#131881)
Co-authored-by: Bénédikt Tran <[email protected]> (cherry picked from commit 0555778)
1 parent 240c200 commit e8cf9bb

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Lib/test/test_bytes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,36 @@ def check(fmt, vals, result):
737737
check(b'%i%b %*.*b', (10, b'3', 5, 3, b'abc',), b'103 abc')
738738
check(b'%c', b'a', b'a')
739739

740+
class PseudoFloat:
741+
def __init__(self, value):
742+
self.value = float(value)
743+
def __int__(self):
744+
return int(self.value)
745+
746+
pi = PseudoFloat(3.1415)
747+
748+
exceptions_params = [
749+
('%x format: an integer is required, not float', b'%x', 3.14),
750+
('%X format: an integer is required, not float', b'%X', 2.11),
751+
('%o format: an integer is required, not float', b'%o', 1.79),
752+
('%x format: an integer is required, not PseudoFloat', b'%x', pi),
753+
('%x format: an integer is required, not complex', b'%x', 3j),
754+
('%X format: an integer is required, not complex', b'%X', 2j),
755+
('%o format: an integer is required, not complex', b'%o', 1j),
756+
('%u format: a real number is required, not complex', b'%u', 3j),
757+
('%i format: a real number is required, not complex', b'%i', 2j),
758+
('%d format: a real number is required, not complex', b'%d', 2j),
759+
(
760+
r'%c requires an integer in range\(256\)'
761+
r' or a single byte, not .*\.PseudoFloat',
762+
b'%c', pi
763+
),
764+
]
765+
766+
for msg, format_bytes, value in exceptions_params:
767+
with self.assertRaisesRegex(TypeError, msg):
768+
operator.mod(format_bytes, value)
769+
740770
def test_imod(self):
741771
b = self.type2test(b'hello, %b!')
742772
orig = b

0 commit comments

Comments
 (0)