Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test.support import verbose, TestFailed |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 2 | import locale |
Eric S. Raymond | d8c628b | 2001-02-09 11:46:37 +0000 | [diff] [blame] | 3 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | import test.support as support |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 5 | import unittest |
| 6 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | maxsize = support.MAX_Py_ssize_t |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 8 | |
| 9 | # test string formatting operator (I am not sure if this is being tested |
| 10 | # elsewhere but, surely, some of the given cases are *not* tested because |
| 11 | # they crash python) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 12 | # test on bytes object as well |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 13 | |
Mark Dickinson | b7be570 | 2010-02-23 13:20:58 +0000 | [diff] [blame] | 14 | def testformat(formatstr, args, output=None, limit=None, overflowok=False): |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 15 | if verbose: |
| 16 | if output: |
Ezio Melotti | 507eb09 | 2013-02-23 07:53:56 +0200 | [diff] [blame] | 17 | print("{!a} % {!a} =? {!a} ...".format(formatstr, args, output), |
| 18 | end=' ') |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 19 | else: |
Ezio Melotti | 507eb09 | 2013-02-23 07:53:56 +0200 | [diff] [blame] | 20 | print("{!a} % {!a} works? ...".format(formatstr, args), end=' ') |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 21 | try: |
| 22 | result = formatstr % args |
| 23 | except OverflowError: |
| 24 | if not overflowok: |
| 25 | raise |
| 26 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 27 | print('overflow (this is fine)') |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 28 | else: |
Mark Dickinson | 5354a1f | 2010-02-07 13:15:37 +0000 | [diff] [blame] | 29 | if output and limit is None and result != output: |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 30 | if verbose: |
| 31 | print('no') |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 32 | raise AssertionError("%r %% %r == %r != %r" % |
| 33 | (formatstr, args, result, output)) |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 34 | # when 'limit' is specified, it determines how many characters |
| 35 | # must match exactly; lengths must always match. |
| 36 | # ex: limit=5, '12345678' matches '12345___' |
| 37 | # (mainly for floating point format tests for which an exact match |
| 38 | # can't be guaranteed due to rounding and representation errors) |
| 39 | elif output and limit is not None and ( |
| 40 | len(result)!=len(output) or result[:limit]!=output[:limit]): |
| 41 | if verbose: |
| 42 | print('no') |
| 43 | print("%s %% %s == %s != %s" % \ |
| 44 | (repr(formatstr), repr(args), repr(result), repr(output))) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 45 | else: |
| 46 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 47 | print('yes') |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 48 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 49 | def testcommon(formatstr, args, output=None, limit=None, overflowok=False): |
| 50 | # if formatstr is a str, test str, bytes, and bytearray; |
Miss Islington (bot) | 4bd1d05 | 2019-08-30 13:42:54 -0700 | [diff] [blame] | 51 | # otherwise, test bytes and bytearray |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 52 | if isinstance(formatstr, str): |
| 53 | testformat(formatstr, args, output, limit, overflowok) |
| 54 | b_format = formatstr.encode('ascii') |
| 55 | else: |
| 56 | b_format = formatstr |
| 57 | ba_format = bytearray(b_format) |
| 58 | b_args = [] |
| 59 | if not isinstance(args, tuple): |
| 60 | args = (args, ) |
| 61 | b_args = tuple(args) |
| 62 | if output is None: |
| 63 | b_output = ba_output = None |
| 64 | else: |
| 65 | if isinstance(output, str): |
| 66 | b_output = output.encode('ascii') |
| 67 | else: |
| 68 | b_output = output |
| 69 | ba_output = bytearray(b_output) |
| 70 | testformat(b_format, b_args, b_output, limit, overflowok) |
| 71 | testformat(ba_format, b_args, ba_output, limit, overflowok) |
| 72 | |
Serhiy Storchaka | 9f8ad3f | 2017-03-08 05:51:19 +0200 | [diff] [blame] | 73 | def test_exc(formatstr, args, exception, excmsg): |
| 74 | try: |
| 75 | testformat(formatstr, args) |
| 76 | except exception as exc: |
| 77 | if str(exc) == excmsg: |
| 78 | if verbose: |
| 79 | print("yes") |
| 80 | else: |
| 81 | if verbose: print('no') |
| 82 | print('Unexpected ', exception, ':', repr(str(exc))) |
| 83 | except: |
| 84 | if verbose: print('no') |
| 85 | print('Unexpected exception') |
| 86 | raise |
| 87 | else: |
| 88 | raise TestFailed('did not get expected exception: %s' % excmsg) |
| 89 | |
| 90 | def test_exc_common(formatstr, args, exception, excmsg): |
| 91 | # test str and bytes |
| 92 | test_exc(formatstr, args, exception, excmsg) |
| 93 | test_exc(formatstr.encode('ascii'), args, exception, excmsg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 94 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 95 | class FormatTest(unittest.TestCase): |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 96 | |
| 97 | def test_common_format(self): |
| 98 | # test the format identifiers that work the same across |
| 99 | # str, bytes, and bytearrays (integer, float, oct, hex) |
Serhiy Storchaka | 9f8ad3f | 2017-03-08 05:51:19 +0200 | [diff] [blame] | 100 | testcommon("%%", (), "%") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 101 | testcommon("%.1d", (1,), "1") |
| 102 | testcommon("%.*d", (sys.maxsize,1), overflowok=True) # expect overflow |
| 103 | testcommon("%.100d", (1,), '00000000000000000000000000000000000000' |
Mark Dickinson | b7be570 | 2010-02-23 13:20:58 +0000 | [diff] [blame] | 104 | '000000000000000000000000000000000000000000000000000000' |
| 105 | '00000001', overflowok=True) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 106 | testcommon("%#.117x", (1,), '0x00000000000000000000000000000000000' |
Mark Dickinson | b7be570 | 2010-02-23 13:20:58 +0000 | [diff] [blame] | 107 | '000000000000000000000000000000000000000000000000000000' |
| 108 | '0000000000000000000000000001', |
| 109 | overflowok=True) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 110 | testcommon("%#.118x", (1,), '0x00000000000000000000000000000000000' |
Mark Dickinson | b7be570 | 2010-02-23 13:20:58 +0000 | [diff] [blame] | 111 | '000000000000000000000000000000000000000000000000000000' |
| 112 | '00000000000000000000000000001', |
| 113 | overflowok=True) |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 114 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 115 | testcommon("%f", (1.0,), "1.000000") |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 116 | # these are trying to test the limits of the internal magic-number-length |
| 117 | # formatting buffer, if that number changes then these tests are less |
| 118 | # effective |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 119 | testcommon("%#.*g", (109, -1.e+49/3.)) |
| 120 | testcommon("%#.*g", (110, -1.e+49/3.)) |
| 121 | testcommon("%#.*g", (110, -1.e+100/3.)) |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 122 | # test some ridiculously large precision, expect overflow |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 123 | testcommon('%12.*f', (123456, 1.0)) |
Mark Dickinson | 5354a1f | 2010-02-07 13:15:37 +0000 | [diff] [blame] | 124 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 125 | # check for internal overflow validation on length of precision |
Mark Dickinson | 5354a1f | 2010-02-07 13:15:37 +0000 | [diff] [blame] | 126 | # these tests should no longer cause overflow in Python |
| 127 | # 2.7/3.1 and later. |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 128 | testcommon("%#.*g", (110, -1.e+100/3.)) |
| 129 | testcommon("%#.*G", (110, -1.e+100/3.)) |
| 130 | testcommon("%#.*f", (110, -1.e+100/3.)) |
| 131 | testcommon("%#.*F", (110, -1.e+100/3.)) |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 132 | # Formatting of integers. Overflow is not ok |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 133 | testcommon("%x", 10, "a") |
| 134 | testcommon("%x", 100000000000, "174876e800") |
| 135 | testcommon("%o", 10, "12") |
| 136 | testcommon("%o", 100000000000, "1351035564000") |
| 137 | testcommon("%d", 10, "10") |
| 138 | testcommon("%d", 100000000000, "100000000000") |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 139 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 140 | big = 123456789012345678901234567890 |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 141 | testcommon("%d", big, "123456789012345678901234567890") |
| 142 | testcommon("%d", -big, "-123456789012345678901234567890") |
| 143 | testcommon("%5d", -big, "-123456789012345678901234567890") |
| 144 | testcommon("%31d", -big, "-123456789012345678901234567890") |
| 145 | testcommon("%32d", -big, " -123456789012345678901234567890") |
| 146 | testcommon("%-32d", -big, "-123456789012345678901234567890 ") |
| 147 | testcommon("%032d", -big, "-0123456789012345678901234567890") |
| 148 | testcommon("%-032d", -big, "-123456789012345678901234567890 ") |
| 149 | testcommon("%034d", -big, "-000123456789012345678901234567890") |
| 150 | testcommon("%034d", big, "0000123456789012345678901234567890") |
| 151 | testcommon("%0+34d", big, "+000123456789012345678901234567890") |
| 152 | testcommon("%+34d", big, " +123456789012345678901234567890") |
| 153 | testcommon("%34d", big, " 123456789012345678901234567890") |
| 154 | testcommon("%.2d", big, "123456789012345678901234567890") |
| 155 | testcommon("%.30d", big, "123456789012345678901234567890") |
| 156 | testcommon("%.31d", big, "0123456789012345678901234567890") |
| 157 | testcommon("%32.31d", big, " 0123456789012345678901234567890") |
| 158 | testcommon("%d", float(big), "123456________________________", 6) |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 159 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 160 | big = 0x1234567890abcdef12345 # 21 hex digits |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 161 | testcommon("%x", big, "1234567890abcdef12345") |
| 162 | testcommon("%x", -big, "-1234567890abcdef12345") |
| 163 | testcommon("%5x", -big, "-1234567890abcdef12345") |
| 164 | testcommon("%22x", -big, "-1234567890abcdef12345") |
| 165 | testcommon("%23x", -big, " -1234567890abcdef12345") |
| 166 | testcommon("%-23x", -big, "-1234567890abcdef12345 ") |
| 167 | testcommon("%023x", -big, "-01234567890abcdef12345") |
| 168 | testcommon("%-023x", -big, "-1234567890abcdef12345 ") |
| 169 | testcommon("%025x", -big, "-0001234567890abcdef12345") |
| 170 | testcommon("%025x", big, "00001234567890abcdef12345") |
| 171 | testcommon("%0+25x", big, "+0001234567890abcdef12345") |
| 172 | testcommon("%+25x", big, " +1234567890abcdef12345") |
| 173 | testcommon("%25x", big, " 1234567890abcdef12345") |
| 174 | testcommon("%.2x", big, "1234567890abcdef12345") |
| 175 | testcommon("%.21x", big, "1234567890abcdef12345") |
| 176 | testcommon("%.22x", big, "01234567890abcdef12345") |
| 177 | testcommon("%23.22x", big, " 01234567890abcdef12345") |
| 178 | testcommon("%-23.22x", big, "01234567890abcdef12345 ") |
| 179 | testcommon("%X", big, "1234567890ABCDEF12345") |
| 180 | testcommon("%#X", big, "0X1234567890ABCDEF12345") |
| 181 | testcommon("%#x", big, "0x1234567890abcdef12345") |
| 182 | testcommon("%#x", -big, "-0x1234567890abcdef12345") |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 183 | testcommon("%#27x", big, " 0x1234567890abcdef12345") |
| 184 | testcommon("%#-27x", big, "0x1234567890abcdef12345 ") |
| 185 | testcommon("%#027x", big, "0x00001234567890abcdef12345") |
| 186 | testcommon("%#.23x", big, "0x001234567890abcdef12345") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 187 | testcommon("%#.23x", -big, "-0x001234567890abcdef12345") |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 188 | testcommon("%#27.23x", big, " 0x001234567890abcdef12345") |
| 189 | testcommon("%#-27.23x", big, "0x001234567890abcdef12345 ") |
| 190 | testcommon("%#027.23x", big, "0x00001234567890abcdef12345") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 191 | testcommon("%#+.23x", big, "+0x001234567890abcdef12345") |
| 192 | testcommon("%# .23x", big, " 0x001234567890abcdef12345") |
| 193 | testcommon("%#+.23X", big, "+0X001234567890ABCDEF12345") |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 194 | # next one gets two leading zeroes from precision, and another from the |
| 195 | # 0 flag and the width |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 196 | testcommon("%#+027.23X", big, "+0X0001234567890ABCDEF12345") |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 197 | testcommon("%# 027.23X", big, " 0X0001234567890ABCDEF12345") |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 198 | # same, except no 0 flag |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 199 | testcommon("%#+27.23X", big, " +0X001234567890ABCDEF12345") |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 200 | testcommon("%#-+27.23x", big, "+0x001234567890abcdef12345 ") |
| 201 | testcommon("%#- 27.23x", big, " 0x001234567890abcdef12345 ") |
| 202 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 203 | big = 0o12345670123456701234567012345670 # 32 octal digits |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 204 | testcommon("%o", big, "12345670123456701234567012345670") |
| 205 | testcommon("%o", -big, "-12345670123456701234567012345670") |
| 206 | testcommon("%5o", -big, "-12345670123456701234567012345670") |
| 207 | testcommon("%33o", -big, "-12345670123456701234567012345670") |
| 208 | testcommon("%34o", -big, " -12345670123456701234567012345670") |
| 209 | testcommon("%-34o", -big, "-12345670123456701234567012345670 ") |
| 210 | testcommon("%034o", -big, "-012345670123456701234567012345670") |
| 211 | testcommon("%-034o", -big, "-12345670123456701234567012345670 ") |
| 212 | testcommon("%036o", -big, "-00012345670123456701234567012345670") |
| 213 | testcommon("%036o", big, "000012345670123456701234567012345670") |
| 214 | testcommon("%0+36o", big, "+00012345670123456701234567012345670") |
| 215 | testcommon("%+36o", big, " +12345670123456701234567012345670") |
| 216 | testcommon("%36o", big, " 12345670123456701234567012345670") |
| 217 | testcommon("%.2o", big, "12345670123456701234567012345670") |
| 218 | testcommon("%.32o", big, "12345670123456701234567012345670") |
| 219 | testcommon("%.33o", big, "012345670123456701234567012345670") |
| 220 | testcommon("%34.33o", big, " 012345670123456701234567012345670") |
| 221 | testcommon("%-34.33o", big, "012345670123456701234567012345670 ") |
| 222 | testcommon("%o", big, "12345670123456701234567012345670") |
| 223 | testcommon("%#o", big, "0o12345670123456701234567012345670") |
| 224 | testcommon("%#o", -big, "-0o12345670123456701234567012345670") |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 225 | testcommon("%#38o", big, " 0o12345670123456701234567012345670") |
| 226 | testcommon("%#-38o", big, "0o12345670123456701234567012345670 ") |
| 227 | testcommon("%#038o", big, "0o000012345670123456701234567012345670") |
| 228 | testcommon("%#.34o", big, "0o0012345670123456701234567012345670") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 229 | testcommon("%#.34o", -big, "-0o0012345670123456701234567012345670") |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 230 | testcommon("%#38.34o", big, " 0o0012345670123456701234567012345670") |
| 231 | testcommon("%#-38.34o", big, "0o0012345670123456701234567012345670 ") |
| 232 | testcommon("%#038.34o", big, "0o000012345670123456701234567012345670") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 233 | testcommon("%#+.34o", big, "+0o0012345670123456701234567012345670") |
| 234 | testcommon("%# .34o", big, " 0o0012345670123456701234567012345670") |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 235 | testcommon("%#+38.34o", big, " +0o0012345670123456701234567012345670") |
| 236 | testcommon("%#-+38.34o", big, "+0o0012345670123456701234567012345670 ") |
| 237 | testcommon("%#- 38.34o", big, " 0o0012345670123456701234567012345670 ") |
| 238 | testcommon("%#+038.34o", big, "+0o00012345670123456701234567012345670") |
| 239 | testcommon("%# 038.34o", big, " 0o00012345670123456701234567012345670") |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 240 | # next one gets one leading zero from precision |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 241 | testcommon("%.33o", big, "012345670123456701234567012345670") |
Martin Panter | 41176ae | 2016-12-11 01:07:29 +0000 | [diff] [blame] | 242 | # base marker added in spite of leading zero (different to Python 2) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 243 | testcommon("%#.33o", big, "0o012345670123456701234567012345670") |
Martin Panter | 41176ae | 2016-12-11 01:07:29 +0000 | [diff] [blame] | 244 | # reduce precision, and base marker is always added |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 245 | testcommon("%#.32o", big, "0o12345670123456701234567012345670") |
Martin Panter | 41176ae | 2016-12-11 01:07:29 +0000 | [diff] [blame] | 246 | # one leading zero from precision, plus two from "0" flag & width |
| 247 | testcommon("%035.33o", big, "00012345670123456701234567012345670") |
| 248 | # base marker shouldn't change the size |
| 249 | testcommon("%0#35.33o", big, "0o012345670123456701234567012345670") |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 250 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 251 | # Some small ints, in both Python int and flavors). |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 252 | testcommon("%d", 42, "42") |
| 253 | testcommon("%d", -42, "-42") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 254 | testcommon("%d", 42.0, "42") |
| 255 | testcommon("%#x", 1, "0x1") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 256 | testcommon("%#X", 1, "0X1") |
| 257 | testcommon("%#o", 1, "0o1") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 258 | testcommon("%#o", 0, "0o0") |
| 259 | testcommon("%o", 0, "0") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 260 | testcommon("%d", 0, "0") |
| 261 | testcommon("%#x", 0, "0x0") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 262 | testcommon("%#X", 0, "0X0") |
| 263 | testcommon("%x", 0x42, "42") |
| 264 | testcommon("%x", -0x42, "-42") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 265 | testcommon("%o", 0o42, "42") |
| 266 | testcommon("%o", -0o42, "-42") |
| 267 | # alternate float formatting |
| 268 | testcommon('%g', 1.1, '1.1') |
| 269 | testcommon('%#g', 1.1, '1.10000') |
| 270 | |
Serhiy Storchaka | 9f8ad3f | 2017-03-08 05:51:19 +0200 | [diff] [blame] | 271 | if verbose: |
| 272 | print('Testing exceptions') |
| 273 | test_exc_common('%', (), ValueError, "incomplete format") |
| 274 | test_exc_common('% %s', 1, ValueError, |
| 275 | "unsupported format character '%' (0x25) at index 2") |
| 276 | test_exc_common('%d', '1', TypeError, |
| 277 | "%d format: a number is required, not str") |
| 278 | test_exc_common('%d', b'1', TypeError, |
| 279 | "%d format: a number is required, not bytes") |
| 280 | test_exc_common('%x', '1', TypeError, |
| 281 | "%x format: an integer is required, not str") |
| 282 | test_exc_common('%x', 3.14, TypeError, |
| 283 | "%x format: an integer is required, not float") |
| 284 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 285 | def test_str_format(self): |
Amaury Forgeot d'Arc | a083f1e | 2008-09-10 23:51:42 +0000 | [diff] [blame] | 286 | testformat("%r", "\u0378", "'\\u0378'") # non printable |
| 287 | testformat("%a", "\u0378", "'\\u0378'") # non printable |
Amaury Forgeot d'Arc | a819caa | 2008-07-04 17:57:09 +0000 | [diff] [blame] | 288 | testformat("%r", "\u0374", "'\u0374'") # printable |
| 289 | testformat("%a", "\u0374", "'\\u0374'") # printable |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 290 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 291 | # Test exception for unknown format characters, etc. |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 292 | if verbose: |
| 293 | print('Testing exceptions') |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 294 | test_exc('abc %b', 1, ValueError, |
| 295 | "unsupported format character 'b' (0x62) at index 5") |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 296 | #test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError, |
| 297 | # "unsupported format character '?' (0x3000) at index 5") |
Serhiy Storchaka | 6db1f6f | 2016-06-06 13:00:03 +0300 | [diff] [blame] | 298 | test_exc('%g', '1', TypeError, "must be real number, not str") |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 299 | test_exc('no format', '1', TypeError, |
| 300 | "not all arguments converted during string formatting") |
Serhiy Storchaka | 2c7b5a9 | 2015-03-30 09:19:08 +0300 | [diff] [blame] | 301 | test_exc('%c', -1, OverflowError, "%c arg not in range(0x110000)") |
| 302 | test_exc('%c', sys.maxunicode+1, OverflowError, |
| 303 | "%c arg not in range(0x110000)") |
| 304 | #test_exc('%c', 2**128, OverflowError, "%c arg not in range(0x110000)") |
| 305 | test_exc('%c', 3.14, TypeError, "%c requires int or char") |
| 306 | test_exc('%c', 'ab', TypeError, "%c requires int or char") |
| 307 | test_exc('%c', b'x', TypeError, "%c requires int or char") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 308 | |
| 309 | if maxsize == 2**31-1: |
| 310 | # crashes 2.2.1 and earlier: |
| 311 | try: |
| 312 | "%*d"%(maxsize, -127) |
| 313 | except MemoryError: |
| 314 | pass |
| 315 | else: |
| 316 | raise TestFailed('"%*d"%(maxsize, -127) should fail') |
| 317 | |
| 318 | def test_bytes_and_bytearray_format(self): |
| 319 | # %c will insert a single byte, either from an int in range(256), or |
| 320 | # from a bytes argument of length 1, not from a str. |
| 321 | testcommon(b"%c", 7, b"\x07") |
| 322 | testcommon(b"%c", b"Z", b"Z") |
| 323 | testcommon(b"%c", bytearray(b"Z"), b"Z") |
Victor Stinner | 0cdad1e | 2015-10-09 22:50:36 +0200 | [diff] [blame] | 324 | testcommon(b"%5c", 65, b" A") |
| 325 | testcommon(b"%-5c", 65, b"A ") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 326 | # %b will insert a series of bytes, either from a type that supports |
| 327 | # the Py_buffer protocol, or something that has a __bytes__ method |
| 328 | class FakeBytes(object): |
| 329 | def __bytes__(self): |
| 330 | return b'123' |
| 331 | fb = FakeBytes() |
| 332 | testcommon(b"%b", b"abc", b"abc") |
| 333 | testcommon(b"%b", bytearray(b"def"), b"def") |
| 334 | testcommon(b"%b", fb, b"123") |
Xiang Zhang | 7e2a54c | 2017-03-14 15:07:15 +0800 | [diff] [blame] | 335 | testcommon(b"%b", memoryview(b"abc"), b"abc") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 336 | # # %s is an alias for %b -- should only be used for Py2/3 code |
| 337 | testcommon(b"%s", b"abc", b"abc") |
| 338 | testcommon(b"%s", bytearray(b"def"), b"def") |
| 339 | testcommon(b"%s", fb, b"123") |
Xiang Zhang | 7e2a54c | 2017-03-14 15:07:15 +0800 | [diff] [blame] | 340 | testcommon(b"%s", memoryview(b"abc"), b"abc") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 341 | # %a will give the equivalent of |
| 342 | # repr(some_obj).encode('ascii', 'backslashreplace') |
| 343 | testcommon(b"%a", 3.14, b"3.14") |
| 344 | testcommon(b"%a", b"ghi", b"b'ghi'") |
| 345 | testcommon(b"%a", "jkl", b"'jkl'") |
| 346 | testcommon(b"%a", "\u0544", b"'\\u0544'") |
Ethan Furman | 62e977f | 2015-03-11 08:17:00 -0700 | [diff] [blame] | 347 | # %r is an alias for %a |
| 348 | testcommon(b"%r", 3.14, b"3.14") |
| 349 | testcommon(b"%r", b"ghi", b"b'ghi'") |
| 350 | testcommon(b"%r", "jkl", b"'jkl'") |
| 351 | testcommon(b"%r", "\u0544", b"'\\u0544'") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 352 | |
| 353 | # Test exception for unknown format characters, etc. |
| 354 | if verbose: |
| 355 | print('Testing exceptions') |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 356 | test_exc(b'%g', '1', TypeError, "float argument required, not str") |
| 357 | test_exc(b'%g', b'1', TypeError, "float argument required, not bytes") |
| 358 | test_exc(b'no format', 7, TypeError, |
| 359 | "not all arguments converted during bytes formatting") |
| 360 | test_exc(b'no format', b'1', TypeError, |
| 361 | "not all arguments converted during bytes formatting") |
| 362 | test_exc(b'no format', bytearray(b'1'), TypeError, |
| 363 | "not all arguments converted during bytes formatting") |
Serhiy Storchaka | 41525e3 | 2015-04-03 20:53:46 +0300 | [diff] [blame] | 364 | test_exc(b"%c", -1, OverflowError, |
| 365 | "%c arg not in range(256)") |
| 366 | test_exc(b"%c", 256, OverflowError, |
| 367 | "%c arg not in range(256)") |
| 368 | test_exc(b"%c", 2**128, OverflowError, |
| 369 | "%c arg not in range(256)") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 370 | test_exc(b"%c", b"Za", TypeError, |
| 371 | "%c requires an integer in range(256) or a single byte") |
Serhiy Storchaka | 2c7b5a9 | 2015-03-30 09:19:08 +0300 | [diff] [blame] | 372 | test_exc(b"%c", "Y", TypeError, |
| 373 | "%c requires an integer in range(256) or a single byte") |
| 374 | test_exc(b"%c", 3.14, TypeError, |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 375 | "%c requires an integer in range(256) or a single byte") |
| 376 | test_exc(b"%b", "Xc", TypeError, |
Xiang Zhang | 7e2a54c | 2017-03-14 15:07:15 +0800 | [diff] [blame] | 377 | "%b requires a bytes-like object, " |
| 378 | "or an object that implements __bytes__, not 'str'") |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 379 | test_exc(b"%s", "Wd", TypeError, |
Xiang Zhang | 7e2a54c | 2017-03-14 15:07:15 +0800 | [diff] [blame] | 380 | "%b requires a bytes-like object, " |
| 381 | "or an object that implements __bytes__, not 'str'") |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 382 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 383 | if maxsize == 2**31-1: |
| 384 | # crashes 2.2.1 and earlier: |
| 385 | try: |
| 386 | "%*d"%(maxsize, -127) |
| 387 | except MemoryError: |
| 388 | pass |
| 389 | else: |
| 390 | raise TestFailed('"%*d"%(maxsize, -127) should fail') |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 391 | |
Serhiy Storchaka | c9ad8b7 | 2016-12-28 09:54:22 +0200 | [diff] [blame] | 392 | def test_nul(self): |
| 393 | # test the null character |
| 394 | testcommon("a\0b", (), 'a\0b') |
| 395 | testcommon("a%cb", (0,), 'a\0b') |
| 396 | testformat("a%sb", ('c\0d',), 'ac\0db') |
| 397 | testcommon(b"a%sb", (b'c\0d',), b'ac\0db') |
| 398 | |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 399 | def test_non_ascii(self): |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 400 | testformat("\u20ac=%f", (1.0,), "\u20ac=1.000000") |
| 401 | |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 402 | self.assertEqual(format("abc", "\u2007<5"), "abc\u2007\u2007") |
| 403 | self.assertEqual(format(123, "\u2007<5"), "123\u2007\u2007") |
| 404 | self.assertEqual(format(12.3, "\u2007<6"), "12.3\u2007\u2007") |
| 405 | self.assertEqual(format(0j, "\u2007<4"), "0j\u2007\u2007") |
| 406 | self.assertEqual(format(1+2j, "\u2007<8"), "(1+2j)\u2007\u2007") |
| 407 | |
| 408 | self.assertEqual(format("abc", "\u2007>5"), "\u2007\u2007abc") |
| 409 | self.assertEqual(format(123, "\u2007>5"), "\u2007\u2007123") |
| 410 | self.assertEqual(format(12.3, "\u2007>6"), "\u2007\u200712.3") |
| 411 | self.assertEqual(format(1+2j, "\u2007>8"), "\u2007\u2007(1+2j)") |
| 412 | self.assertEqual(format(0j, "\u2007>4"), "\u2007\u20070j") |
| 413 | |
| 414 | self.assertEqual(format("abc", "\u2007^5"), "\u2007abc\u2007") |
| 415 | self.assertEqual(format(123, "\u2007^5"), "\u2007123\u2007") |
| 416 | self.assertEqual(format(12.3, "\u2007^6"), "\u200712.3\u2007") |
| 417 | self.assertEqual(format(1+2j, "\u2007^8"), "\u2007(1+2j)\u2007") |
| 418 | self.assertEqual(format(0j, "\u2007^4"), "\u20070j\u2007") |
| 419 | |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 420 | def test_locale(self): |
| 421 | try: |
Stefan Krah | 0509d94 | 2012-02-27 10:18:51 +0100 | [diff] [blame] | 422 | oldloc = locale.setlocale(locale.LC_ALL) |
| 423 | locale.setlocale(locale.LC_ALL, '') |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 424 | except locale.Error as err: |
| 425 | self.skipTest("Cannot set locale: {}".format(err)) |
| 426 | try: |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 427 | localeconv = locale.localeconv() |
| 428 | sep = localeconv['thousands_sep'] |
| 429 | point = localeconv['decimal_point'] |
| 430 | |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 431 | text = format(123456789, "n") |
| 432 | self.assertIn(sep, text) |
| 433 | self.assertEqual(text.replace(sep, ''), '123456789') |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 434 | |
| 435 | text = format(1234.5, "n") |
| 436 | self.assertIn(sep, text) |
| 437 | self.assertIn(point, text) |
| 438 | self.assertEqual(text.replace(sep, ''), '1234' + point + '5') |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 439 | finally: |
| 440 | locale.setlocale(locale.LC_ALL, oldloc) |
| 441 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 442 | @support.cpython_only |
| 443 | def test_optimisations(self): |
| 444 | text = "abcde" # 5 characters |
| 445 | |
| 446 | self.assertIs("%s" % text, text) |
| 447 | self.assertIs("%.5s" % text, text) |
| 448 | self.assertIs("%.10s" % text, text) |
| 449 | self.assertIs("%1s" % text, text) |
| 450 | self.assertIs("%5s" % text, text) |
| 451 | |
| 452 | self.assertIs("{0}".format(text), text) |
| 453 | self.assertIs("{0:s}".format(text), text) |
| 454 | self.assertIs("{0:.5s}".format(text), text) |
| 455 | self.assertIs("{0:.10s}".format(text), text) |
| 456 | self.assertIs("{0:1s}".format(text), text) |
| 457 | self.assertIs("{0:5s}".format(text), text) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 458 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 459 | self.assertIs(text % (), text) |
| 460 | self.assertIs(text.format(), text) |
| 461 | |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 462 | def test_precision(self): |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 463 | f = 1.2 |
| 464 | self.assertEqual(format(f, ".0f"), "1") |
| 465 | self.assertEqual(format(f, ".3f"), "1.200") |
| 466 | with self.assertRaises(ValueError) as cm: |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 467 | format(f, ".%sf" % (sys.maxsize + 1)) |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 468 | |
| 469 | c = complex(f) |
Mark Dickinson | ef8627b | 2013-10-13 11:04:36 +0100 | [diff] [blame] | 470 | self.assertEqual(format(c, ".0f"), "1+0j") |
| 471 | self.assertEqual(format(c, ".3f"), "1.200+0.000j") |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 472 | with self.assertRaises(ValueError) as cm: |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 473 | format(c, ".%sf" % (sys.maxsize + 1)) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 474 | |
| 475 | @support.cpython_only |
| 476 | def test_precision_c_limits(self): |
| 477 | from _testcapi import INT_MAX |
| 478 | |
| 479 | f = 1.2 |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 480 | with self.assertRaises(ValueError) as cm: |
| 481 | format(f, ".%sf" % (INT_MAX + 1)) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 482 | |
| 483 | c = complex(f) |
| 484 | with self.assertRaises(ValueError) as cm: |
Mark Dickinson | ef8627b | 2013-10-13 11:04:36 +0100 | [diff] [blame] | 485 | format(c, ".%sf" % (INT_MAX + 1)) |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 486 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 487 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 488 | if __name__ == "__main__": |
| 489 | unittest.main() |