Skip to content

gh-119949: refactor test_exc() helper in test_format.py #135452

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
140 changes: 64 additions & 76 deletions Lib/test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,18 @@ def testcommon(formatstr, args, output=None, limit=None, overflowok=False):
testformat(b_format, b_args, b_output, limit, overflowok)
testformat(ba_format, b_args, ba_output, limit, overflowok)

def test_exc(formatstr, args, exception, excmsg):
try:
testformat(formatstr, args)
except exception as exc:
if str(exc) == excmsg:
if verbose:
print("yes")
else:
if verbose: print('no')
print('Unexpected ', exception, ':', repr(str(exc)))
except:
if verbose: print('no')
print('Unexpected exception')
raise
else:
raise TestFailed('did not get expected exception: %s' % excmsg)

def test_exc_common(formatstr, args, exception, excmsg):
# test str and bytes
test_exc(formatstr, args, exception, excmsg)
test_exc(formatstr.encode('ascii'), args, exception, excmsg)

class FormatTest(unittest.TestCase):

def check_exc(self, formatstr, args, exception, excmsg):
with self.assertRaisesRegex(exception, excmsg):
testformat(formatstr, args)

def check_exc_common(self, formatstr, args, exception, excmsg):
# test str and bytes
self.check_exc(formatstr, args, exception, excmsg)
self.check_exc(formatstr.encode('ascii'), args, exception, excmsg)

def test_common_format(self):
# test the format identifiers that work the same across
# str, bytes, and bytearrays (integer, float, oct, hex)
Expand Down Expand Up @@ -272,21 +260,21 @@ def test_common_format(self):

if verbose:
print('Testing exceptions')
test_exc_common('%', (), ValueError, "incomplete format")
test_exc_common('% %s', 1, ValueError,
"unsupported format character '%' (0x25) at index 2")
test_exc_common('%d', '1', TypeError,
"%d format: a real number is required, not str")
test_exc_common('%d', b'1', TypeError,
"%d format: a real number is required, not bytes")
test_exc_common('%x', '1', TypeError,
"%x format: an integer is required, not str")
test_exc_common('%x', 3.14, TypeError,
"%x format: an integer is required, not float")
test_exc_common('%i', '1', TypeError,
"%i format: a real number is required, not str")
test_exc_common('%i', b'1', TypeError,
"%i format: a real number is required, not bytes")
self.check_exc_common('%', (), ValueError, "incomplete format")
self.check_exc_common('% %s', 1, ValueError,
r"unsupported format character '%' \(0x25\) at index 2")
self.check_exc_common('%d', '1', TypeError,
"%d format: a real number is required, not str")
self.check_exc_common('%d', b'1', TypeError,
"%d format: a real number is required, not bytes")
self.check_exc_common('%x', '1', TypeError,
"%x format: an integer is required, not str")
self.check_exc_common('%x', 3.14, TypeError,
"%x format: an integer is required, not float")
self.check_exc_common('%i', '1', TypeError,
"%i format: a real number is required, not str")
self.check_exc_common('%i', b'1', TypeError,
"%i format: a real number is required, not bytes")

def test_str_format(self):
testformat("%r", "\u0378", "'\\u0378'") # non printable
Expand All @@ -297,20 +285,20 @@ def test_str_format(self):
# Test exception for unknown format characters, etc.
if verbose:
print('Testing exceptions')
test_exc('abc %b', 1, ValueError,
"unsupported format character 'b' (0x62) at index 5")
#test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
# "unsupported format character '?' (0x3000) at index 5")
test_exc('%g', '1', TypeError, "must be real number, not str")
test_exc('no format', '1', TypeError,
"not all arguments converted during string formatting")
test_exc('%c', -1, OverflowError, "%c arg not in range(0x110000)")
test_exc('%c', sys.maxunicode+1, OverflowError,
"%c arg not in range(0x110000)")
#test_exc('%c', 2**128, OverflowError, "%c arg not in range(0x110000)")
test_exc('%c', 3.14, TypeError, "%c requires an int or a unicode character, not float")
test_exc('%c', 'ab', TypeError, "%c requires an int or a unicode character, not a string of length 2")
test_exc('%c', b'x', TypeError, "%c requires an int or a unicode character, not bytes")
self.check_exc('abc %b', 1, ValueError,
r"unsupported format character 'b' \(0x62\) at index 5")
#self.check_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
# r"unsupported format character '?' \(0x3000\) at index 5")
self.check_exc('%g', '1', TypeError, "must be real number, not str")
self.check_exc('no format', '1', TypeError,
"not all arguments converted during string formatting")
self.check_exc('%c', -1, OverflowError, r"%c arg not in range\(0x110000\)")
self.check_exc('%c', sys.maxunicode+1, OverflowError,
r"%c arg not in range\(0x110000\)")
#self.check_exc('%c', 2**128, OverflowError, r"%c arg not in range\(0x110000\)")
self.check_exc('%c', 3.14, TypeError, "%c requires an int or a unicode character, not float")
self.check_exc('%c', 'ab', TypeError, "%c requires an int or a unicode character, not a string of length 2")
self.check_exc('%c', b'x', TypeError, "%c requires an int or a unicode character, not bytes")

if maxsize == 2**31-1:
# crashes 2.2.1 and earlier:
Expand Down Expand Up @@ -359,32 +347,32 @@ def __bytes__(self):
# Test exception for unknown format characters, etc.
if verbose:
print('Testing exceptions')
test_exc(b'%g', '1', TypeError, "float argument required, not str")
test_exc(b'%g', b'1', TypeError, "float argument required, not bytes")
test_exc(b'no format', 7, TypeError,
"not all arguments converted during bytes formatting")
test_exc(b'no format', b'1', TypeError,
"not all arguments converted during bytes formatting")
test_exc(b'no format', bytearray(b'1'), TypeError,
"not all arguments converted during bytes formatting")
test_exc(b"%c", -1, OverflowError,
"%c arg not in range(256)")
test_exc(b"%c", 256, OverflowError,
"%c arg not in range(256)")
test_exc(b"%c", 2**128, OverflowError,
"%c arg not in range(256)")
test_exc(b"%c", b"Za", TypeError,
"%c requires an integer in range(256) or a single byte, not a bytes object of length 2")
test_exc(b"%c", "Y", TypeError,
"%c requires an integer in range(256) or a single byte, not str")
test_exc(b"%c", 3.14, TypeError,
"%c requires an integer in range(256) or a single byte, not float")
test_exc(b"%b", "Xc", TypeError,
"%b requires a bytes-like object, "
"or an object that implements __bytes__, not 'str'")
test_exc(b"%s", "Wd", TypeError,
"%b requires a bytes-like object, "
"or an object that implements __bytes__, not 'str'")
self.check_exc(b'%g', '1', TypeError, "float argument required, not str")
self.check_exc(b'%g', b'1', TypeError, "float argument required, not bytes")
self.check_exc(b'no format', 7, TypeError,
"not all arguments converted during bytes formatting")
self.check_exc(b'no format', b'1', TypeError,
"not all arguments converted during bytes formatting")
self.check_exc(b'no format', bytearray(b'1'), TypeError,
"not all arguments converted during bytes formatting")
self.check_exc(b"%c", -1, OverflowError,
r"%c arg not in range\(256\)")
self.check_exc(b"%c", 256, OverflowError,
r"%c arg not in range\(256\)")
self.check_exc(b"%c", 2**128, OverflowError,
r"%c arg not in range\(256\)")
self.check_exc(b"%c", b"Za", TypeError,
r"%c requires an integer in range\(256\) or a single byte, not a bytes object of length 2")
self.check_exc(b"%c", "Y", TypeError,
r"%c requires an integer in range\(256\) or a single byte, not str")
self.check_exc(b"%c", 3.14, TypeError,
r"%c requires an integer in range\(256\) or a single byte, not float")
self.check_exc(b"%b", "Xc", TypeError,
"%b requires a bytes-like object, "
"or an object that implements __bytes__, not 'str'")
self.check_exc(b"%s", "Wd", TypeError,
"%b requires a bytes-like object, "
"or an object that implements __bytes__, not 'str'")

if maxsize == 2**31-1:
# crashes 2.2.1 and earlier:
Expand Down
Loading