diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-03-08 03:51:19 (GMT) |
---|---|---|
committer | Xiang Zhang <angwerzx@126.com> | 2017-03-08 03:51:19 (GMT) |
commit | 9f8ad3f39e0a92ed37d012b9dd237399524f0d51 (patch) | |
tree | 1c68aab772250ca4867e95b3f6d6f802a5b3f2bc /Lib/test/test_format.py | |
parent | c393ee858932f79bd6dabf31550f9a53ea90bc68 (diff) | |
download | cpython-9f8ad3f39e0a92ed37d012b9dd237399524f0d51.zip cpython-9f8ad3f39e0a92ed37d012b9dd237399524f0d51.tar.gz cpython-9f8ad3f39e0a92ed37d012b9dd237399524f0d51.tar.bz2 |
bpo-29568: Disable any characters between two percents for escaped percent "%%" in the format string for classic string formatting. (GH-513)
Diffstat (limited to 'Lib/test/test_format.py')
-rw-r--r-- | Lib/test/test_format.py | 77 |
1 files changed, 36 insertions, 41 deletions
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py index 83eb29f..b283501 100644 --- a/Lib/test/test_format.py +++ b/Lib/test/test_format.py @@ -70,12 +70,34 @@ 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 test_common_format(self): # test the format identifiers that work the same across # str, bytes, and bytearrays (integer, float, oct, hex) + testcommon("%%", (), "%") testcommon("%.1d", (1,), "1") testcommon("%.*d", (sys.maxsize,1), overflowok=True) # expect overflow testcommon("%.100d", (1,), '00000000000000000000000000000000000000' @@ -246,6 +268,20 @@ class FormatTest(unittest.TestCase): testcommon('%g', 1.1, '1.1') testcommon('%#g', 1.1, '1.10000') + 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 number is required, not str") + test_exc_common('%d', b'1', TypeError, + "%d format: a 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") + def test_str_format(self): testformat("%r", "\u0378", "'\\u0378'") # non printable testformat("%a", "\u0378", "'\\u0378'") # non printable @@ -255,29 +291,10 @@ class FormatTest(unittest.TestCase): # Test exception for unknown format characters, etc. if verbose: print('Testing exceptions') - 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) 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('%d', '1', TypeError, "%d format: a number is required, not str") - test_exc('%x', '1', TypeError, "%x format: an integer is required, not str") - test_exc('%x', 3.14, TypeError, "%x format: an integer is required, not float") test_exc('%g', '1', TypeError, "must be real number, not str") test_exc('no format', '1', TypeError, "not all arguments converted during string formatting") @@ -334,28 +351,6 @@ class FormatTest(unittest.TestCase): # Test exception for unknown format characters, etc. if verbose: print('Testing exceptions') - 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) - test_exc(b'%d', '1', TypeError, - "%d format: a number is required, not str") - test_exc(b'%d', b'1', TypeError, - "%d format: a number is required, not bytes") - test_exc(b'%x', 3.14, TypeError, - "%x format: an integer is required, not float") 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, |