Skip to content

Commit ab1af9d

Browse files
committed
pythongh-119949: refactor test_exc() helper in test_format.py
Use assertRaisesRegex() context and fix python#119781 (review)
1 parent 2b0c684 commit ab1af9d

File tree

1 file changed

+64
-76
lines changed

1 file changed

+64
-76
lines changed

Lib/test/test_format.py

Lines changed: 64 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,18 @@ def testcommon(formatstr, args, output=None, limit=None, overflowok=False):
7272
testformat(b_format, b_args, b_output, limit, overflowok)
7373
testformat(ba_format, b_args, ba_output, limit, overflowok)
7474

75-
def test_exc(formatstr, args, exception, excmsg):
76-
try:
77-
testformat(formatstr, args)
78-
except exception as exc:
79-
if str(exc) == excmsg:
80-
if verbose:
81-
print("yes")
82-
else:
83-
if verbose: print('no')
84-
print('Unexpected ', exception, ':', repr(str(exc)))
85-
except:
86-
if verbose: print('no')
87-
print('Unexpected exception')
88-
raise
89-
else:
90-
raise TestFailed('did not get expected exception: %s' % excmsg)
91-
92-
def test_exc_common(formatstr, args, exception, excmsg):
93-
# test str and bytes
94-
test_exc(formatstr, args, exception, excmsg)
95-
test_exc(formatstr.encode('ascii'), args, exception, excmsg)
9675

9776
class FormatTest(unittest.TestCase):
9877

78+
def check_exc(self, formatstr, args, exception, excmsg):
79+
with self.assertRaisesRegex(exception, excmsg):
80+
testformat(formatstr, args)
81+
82+
def check_exc_common(self, formatstr, args, exception, excmsg):
83+
# test str and bytes
84+
self.check_exc(formatstr, args, exception, excmsg)
85+
self.check_exc(formatstr.encode('ascii'), args, exception, excmsg)
86+
9987
def test_common_format(self):
10088
# test the format identifiers that work the same across
10189
# str, bytes, and bytearrays (integer, float, oct, hex)
@@ -272,21 +260,21 @@ def test_common_format(self):
272260

273261
if verbose:
274262
print('Testing exceptions')
275-
test_exc_common('%', (), ValueError, "incomplete format")
276-
test_exc_common('% %s', 1, ValueError,
277-
"unsupported format character '%' (0x25) at index 2")
278-
test_exc_common('%d', '1', TypeError,
279-
"%d format: a real number is required, not str")
280-
test_exc_common('%d', b'1', TypeError,
281-
"%d format: a real number is required, not bytes")
282-
test_exc_common('%x', '1', TypeError,
283-
"%x format: an integer is required, not str")
284-
test_exc_common('%x', 3.14, TypeError,
285-
"%x format: an integer is required, not float")
286-
test_exc_common('%i', '1', TypeError,
287-
"%i format: a real number is required, not str")
288-
test_exc_common('%i', b'1', TypeError,
289-
"%i format: a real number is required, not bytes")
263+
self.check_exc_common('%', (), ValueError, "incomplete format")
264+
self.check_exc_common('% %s', 1, ValueError,
265+
r"unsupported format character '%' \(0x25\) at index 2")
266+
self.check_exc_common('%d', '1', TypeError,
267+
"%d format: a real number is required, not str")
268+
self.check_exc_common('%d', b'1', TypeError,
269+
"%d format: a real number is required, not bytes")
270+
self.check_exc_common('%x', '1', TypeError,
271+
"%x format: an integer is required, not str")
272+
self.check_exc_common('%x', 3.14, TypeError,
273+
"%x format: an integer is required, not float")
274+
self.check_exc_common('%i', '1', TypeError,
275+
"%i format: a real number is required, not str")
276+
self.check_exc_common('%i', b'1', TypeError,
277+
"%i format: a real number is required, not bytes")
290278

291279
def test_str_format(self):
292280
testformat("%r", "\u0378", "'\\u0378'") # non printable
@@ -297,20 +285,20 @@ def test_str_format(self):
297285
# Test exception for unknown format characters, etc.
298286
if verbose:
299287
print('Testing exceptions')
300-
test_exc('abc %b', 1, ValueError,
301-
"unsupported format character 'b' (0x62) at index 5")
302-
#test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
303-
# "unsupported format character '?' (0x3000) at index 5")
304-
test_exc('%g', '1', TypeError, "must be real number, not str")
305-
test_exc('no format', '1', TypeError,
306-
"not all arguments converted during string formatting")
307-
test_exc('%c', -1, OverflowError, "%c arg not in range(0x110000)")
308-
test_exc('%c', sys.maxunicode+1, OverflowError,
309-
"%c arg not in range(0x110000)")
310-
#test_exc('%c', 2**128, OverflowError, "%c arg not in range(0x110000)")
311-
test_exc('%c', 3.14, TypeError, "%c requires an int or a unicode character, not float")
312-
test_exc('%c', 'ab', TypeError, "%c requires an int or a unicode character, not a string of length 2")
313-
test_exc('%c', b'x', TypeError, "%c requires an int or a unicode character, not bytes")
288+
self.check_exc('abc %b', 1, ValueError,
289+
r"unsupported format character 'b' \(0x62\) at index 5")
290+
#self.check_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
291+
# r"unsupported format character '?' \(0x3000\) at index 5")
292+
self.check_exc('%g', '1', TypeError, "must be real number, not str")
293+
self.check_exc('no format', '1', TypeError,
294+
"not all arguments converted during string formatting")
295+
self.check_exc('%c', -1, OverflowError, r"%c arg not in range\(0x110000\)")
296+
self.check_exc('%c', sys.maxunicode+1, OverflowError,
297+
r"%c arg not in range\(0x110000\)")
298+
#self.check_exc('%c', 2**128, OverflowError, r"%c arg not in range\(0x110000\)")
299+
self.check_exc('%c', 3.14, TypeError, "%c requires an int or a unicode character, not float")
300+
self.check_exc('%c', 'ab', TypeError, "%c requires an int or a unicode character, not a string of length 2")
301+
self.check_exc('%c', b'x', TypeError, "%c requires an int or a unicode character, not bytes")
314302

315303
if maxsize == 2**31-1:
316304
# crashes 2.2.1 and earlier:
@@ -359,32 +347,32 @@ def __bytes__(self):
359347
# Test exception for unknown format characters, etc.
360348
if verbose:
361349
print('Testing exceptions')
362-
test_exc(b'%g', '1', TypeError, "float argument required, not str")
363-
test_exc(b'%g', b'1', TypeError, "float argument required, not bytes")
364-
test_exc(b'no format', 7, TypeError,
365-
"not all arguments converted during bytes formatting")
366-
test_exc(b'no format', b'1', TypeError,
367-
"not all arguments converted during bytes formatting")
368-
test_exc(b'no format', bytearray(b'1'), TypeError,
369-
"not all arguments converted during bytes formatting")
370-
test_exc(b"%c", -1, OverflowError,
371-
"%c arg not in range(256)")
372-
test_exc(b"%c", 256, OverflowError,
373-
"%c arg not in range(256)")
374-
test_exc(b"%c", 2**128, OverflowError,
375-
"%c arg not in range(256)")
376-
test_exc(b"%c", b"Za", TypeError,
377-
"%c requires an integer in range(256) or a single byte, not a bytes object of length 2")
378-
test_exc(b"%c", "Y", TypeError,
379-
"%c requires an integer in range(256) or a single byte, not str")
380-
test_exc(b"%c", 3.14, TypeError,
381-
"%c requires an integer in range(256) or a single byte, not float")
382-
test_exc(b"%b", "Xc", TypeError,
383-
"%b requires a bytes-like object, "
384-
"or an object that implements __bytes__, not 'str'")
385-
test_exc(b"%s", "Wd", TypeError,
386-
"%b requires a bytes-like object, "
387-
"or an object that implements __bytes__, not 'str'")
350+
self.check_exc(b'%g', '1', TypeError, "float argument required, not str")
351+
self.check_exc(b'%g', b'1', TypeError, "float argument required, not bytes")
352+
self.check_exc(b'no format', 7, TypeError,
353+
"not all arguments converted during bytes formatting")
354+
self.check_exc(b'no format', b'1', TypeError,
355+
"not all arguments converted during bytes formatting")
356+
self.check_exc(b'no format', bytearray(b'1'), TypeError,
357+
"not all arguments converted during bytes formatting")
358+
self.check_exc(b"%c", -1, OverflowError,
359+
r"%c arg not in range\(256\)")
360+
self.check_exc(b"%c", 256, OverflowError,
361+
r"%c arg not in range\(256\)")
362+
self.check_exc(b"%c", 2**128, OverflowError,
363+
r"%c arg not in range\(256\)")
364+
self.check_exc(b"%c", b"Za", TypeError,
365+
r"%c requires an integer in range\(256\) or a single byte, not a bytes object of length 2")
366+
self.check_exc(b"%c", "Y", TypeError,
367+
r"%c requires an integer in range\(256\) or a single byte, not str")
368+
self.check_exc(b"%c", 3.14, TypeError,
369+
r"%c requires an integer in range\(256\) or a single byte, not float")
370+
self.check_exc(b"%b", "Xc", TypeError,
371+
"%b requires a bytes-like object, "
372+
"or an object that implements __bytes__, not 'str'")
373+
self.check_exc(b"%s", "Wd", TypeError,
374+
"%b requires a bytes-like object, "
375+
"or an object that implements __bytes__, not 'str'")
388376

389377
if maxsize == 2**31-1:
390378
# crashes 2.2.1 and earlier:

0 commit comments

Comments
 (0)