Skip to content

[3.12] gh-131015: Add test for bytes formatting errors (#131881) #132114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
import copy
import functools
import operator
import pickle
import tempfile
import textwrap
Expand Down Expand Up @@ -737,6 +738,37 @@ def check(fmt, vals, result):
check(b'%i%b %*.*b', (10, b'3', 5, 3, b'abc',), b'103 abc')
check(b'%c', b'a', b'a')

class PseudoFloat:
def __init__(self, value):
self.value = float(value)
def __int__(self):
return int(self.value)

pi = PseudoFloat(3.1415)

exceptions_params = [
('%x format: an integer is required, not float', b'%x', 3.14),
('%X format: an integer is required, not float', b'%X', 2.11),
('%o format: an integer is required, not float', b'%o', 1.79),
('%x format: an integer is required, not PseudoFloat', b'%x', pi),
('%x format: an integer is required, not complex', b'%x', 3j),
('%X format: an integer is required, not complex', b'%X', 2j),
('%o format: an integer is required, not complex', b'%o', 1j),
('%u format: a real number is required, not complex', b'%u', 3j),
# See https://github.com/python/cpython/issues/130928 as for why
# the exception message contains '%d' instead of '%i'.
('%d format: a real number is required, not complex', b'%i', 2j),
('%d format: a real number is required, not complex', b'%d', 2j),
(
r'%c requires an integer in range\(256\) or a single byte',
b'%c', pi
),
]

for msg, format_bytes, value in exceptions_params:
with self.assertRaisesRegex(TypeError, msg):
operator.mod(format_bytes, value)

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