Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 1 | from test.test_support import verbose, have_unicode, TestFailed |
Eric S. Raymond | d8c628b | 2001-02-09 11:46:37 +0000 | [diff] [blame] | 2 | import sys |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame^] | 3 | from test.test_support import MAX_Py_ssize_t |
| 4 | maxsize = MAX_Py_ssize_t |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 5 | |
| 6 | # test string formatting operator (I am not sure if this is being tested |
| 7 | # elsewhere but, surely, some of the given cases are *not* tested because |
| 8 | # they crash python) |
| 9 | # test on unicode strings as well |
| 10 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11 | overflowok = 1 |
| 12 | |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 13 | def testformat(formatstr, args, output=None): |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14 | if verbose: |
| 15 | if output: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 16 | print("%s %% %s =? %s ..." %\ |
| 17 | (repr(formatstr), repr(args), repr(output)), end=' ') |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 18 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 19 | print("%s %% %s works? ..." % (repr(formatstr), repr(args)), end=' ') |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 20 | try: |
| 21 | result = formatstr % args |
| 22 | except OverflowError: |
| 23 | if not overflowok: |
| 24 | raise |
| 25 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 26 | print('overflow (this is fine)') |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 27 | else: |
| 28 | if output and result != output: |
| 29 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 30 | print('no') |
| 31 | print("%s %% %s == %s != %s" %\ |
| 32 | (repr(formatstr), repr(args), repr(result), repr(output))) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 33 | else: |
| 34 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 35 | print('yes') |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 36 | |
| 37 | def testboth(formatstr, *args): |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 38 | testformat(formatstr, *args) |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 39 | if have_unicode: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 40 | testformat(str(formatstr), *args) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 41 | |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 42 | |
| 43 | testboth("%.1d", (1,), "1") |
| 44 | testboth("%.*d", (sys.maxint,1)) # expect overflow |
| 45 | testboth("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') |
| 46 | testboth("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') |
| 47 | testboth("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') |
| 48 | |
| 49 | testboth("%f", (1.0,), "1.000000") |
| 50 | # these are trying to test the limits of the internal magic-number-length |
| 51 | # formatting buffer, if that number changes then these tests are less |
| 52 | # effective |
| 53 | testboth("%#.*g", (109, -1.e+49/3.)) |
| 54 | testboth("%#.*g", (110, -1.e+49/3.)) |
| 55 | testboth("%#.*g", (110, -1.e+100/3.)) |
| 56 | |
| 57 | # test some ridiculously large precision, expect overflow |
| 58 | testboth('%12.*f', (123456, 1.0)) |
| 59 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 60 | # Formatting of long integers. Overflow is not ok |
| 61 | overflowok = 0 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 62 | testboth("%x", 10, "a") |
| 63 | testboth("%x", 100000000000, "174876e800") |
| 64 | testboth("%o", 10, "12") |
| 65 | testboth("%o", 100000000000, "1351035564000") |
| 66 | testboth("%d", 10, "10") |
| 67 | testboth("%d", 100000000000, "100000000000") |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 69 | big = 123456789012345678901234567890 |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 70 | testboth("%d", big, "123456789012345678901234567890") |
| 71 | testboth("%d", -big, "-123456789012345678901234567890") |
| 72 | testboth("%5d", -big, "-123456789012345678901234567890") |
| 73 | testboth("%31d", -big, "-123456789012345678901234567890") |
| 74 | testboth("%32d", -big, " -123456789012345678901234567890") |
| 75 | testboth("%-32d", -big, "-123456789012345678901234567890 ") |
| 76 | testboth("%032d", -big, "-0123456789012345678901234567890") |
| 77 | testboth("%-032d", -big, "-123456789012345678901234567890 ") |
| 78 | testboth("%034d", -big, "-000123456789012345678901234567890") |
| 79 | testboth("%034d", big, "0000123456789012345678901234567890") |
| 80 | testboth("%0+34d", big, "+000123456789012345678901234567890") |
| 81 | testboth("%+34d", big, " +123456789012345678901234567890") |
| 82 | testboth("%34d", big, " 123456789012345678901234567890") |
| 83 | testboth("%.2d", big, "123456789012345678901234567890") |
| 84 | testboth("%.30d", big, "123456789012345678901234567890") |
| 85 | testboth("%.31d", big, "0123456789012345678901234567890") |
| 86 | testboth("%32.31d", big, " 0123456789012345678901234567890") |
| 87 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 88 | big = 0x1234567890abcdef12345 # 21 hex digits |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 89 | testboth("%x", big, "1234567890abcdef12345") |
| 90 | testboth("%x", -big, "-1234567890abcdef12345") |
| 91 | testboth("%5x", -big, "-1234567890abcdef12345") |
| 92 | testboth("%22x", -big, "-1234567890abcdef12345") |
| 93 | testboth("%23x", -big, " -1234567890abcdef12345") |
| 94 | testboth("%-23x", -big, "-1234567890abcdef12345 ") |
| 95 | testboth("%023x", -big, "-01234567890abcdef12345") |
| 96 | testboth("%-023x", -big, "-1234567890abcdef12345 ") |
| 97 | testboth("%025x", -big, "-0001234567890abcdef12345") |
| 98 | testboth("%025x", big, "00001234567890abcdef12345") |
| 99 | testboth("%0+25x", big, "+0001234567890abcdef12345") |
| 100 | testboth("%+25x", big, " +1234567890abcdef12345") |
| 101 | testboth("%25x", big, " 1234567890abcdef12345") |
| 102 | testboth("%.2x", big, "1234567890abcdef12345") |
| 103 | testboth("%.21x", big, "1234567890abcdef12345") |
| 104 | testboth("%.22x", big, "01234567890abcdef12345") |
| 105 | testboth("%23.22x", big, " 01234567890abcdef12345") |
| 106 | testboth("%-23.22x", big, "01234567890abcdef12345 ") |
| 107 | testboth("%X", big, "1234567890ABCDEF12345") |
| 108 | testboth("%#X", big, "0X1234567890ABCDEF12345") |
| 109 | testboth("%#x", big, "0x1234567890abcdef12345") |
| 110 | testboth("%#x", -big, "-0x1234567890abcdef12345") |
| 111 | testboth("%#.23x", -big, "-0x001234567890abcdef12345") |
| 112 | testboth("%#+.23x", big, "+0x001234567890abcdef12345") |
| 113 | testboth("%# .23x", big, " 0x001234567890abcdef12345") |
| 114 | testboth("%#+.23X", big, "+0X001234567890ABCDEF12345") |
| 115 | testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345") |
| 116 | testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345") |
| 117 | testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ") |
| 118 | testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345") |
| 119 | # next one gets two leading zeroes from precision, and another from the |
| 120 | # 0 flag and the width |
| 121 | testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345") |
| 122 | # same, except no 0 flag |
| 123 | testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345") |
| 124 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 125 | big = 012345670123456701234567012345670 # 32 octal digits |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 126 | testboth("%o", big, "12345670123456701234567012345670") |
| 127 | testboth("%o", -big, "-12345670123456701234567012345670") |
| 128 | testboth("%5o", -big, "-12345670123456701234567012345670") |
| 129 | testboth("%33o", -big, "-12345670123456701234567012345670") |
| 130 | testboth("%34o", -big, " -12345670123456701234567012345670") |
| 131 | testboth("%-34o", -big, "-12345670123456701234567012345670 ") |
| 132 | testboth("%034o", -big, "-012345670123456701234567012345670") |
| 133 | testboth("%-034o", -big, "-12345670123456701234567012345670 ") |
| 134 | testboth("%036o", -big, "-00012345670123456701234567012345670") |
| 135 | testboth("%036o", big, "000012345670123456701234567012345670") |
| 136 | testboth("%0+36o", big, "+00012345670123456701234567012345670") |
| 137 | testboth("%+36o", big, " +12345670123456701234567012345670") |
| 138 | testboth("%36o", big, " 12345670123456701234567012345670") |
| 139 | testboth("%.2o", big, "12345670123456701234567012345670") |
| 140 | testboth("%.32o", big, "12345670123456701234567012345670") |
| 141 | testboth("%.33o", big, "012345670123456701234567012345670") |
| 142 | testboth("%34.33o", big, " 012345670123456701234567012345670") |
| 143 | testboth("%-34.33o", big, "012345670123456701234567012345670 ") |
| 144 | testboth("%o", big, "12345670123456701234567012345670") |
| 145 | testboth("%#o", big, "012345670123456701234567012345670") |
| 146 | testboth("%#o", -big, "-012345670123456701234567012345670") |
| 147 | testboth("%#.34o", -big, "-0012345670123456701234567012345670") |
| 148 | testboth("%#+.34o", big, "+0012345670123456701234567012345670") |
| 149 | testboth("%# .34o", big, " 0012345670123456701234567012345670") |
| 150 | testboth("%#+.34o", big, "+0012345670123456701234567012345670") |
| 151 | testboth("%#-+.34o", big, "+0012345670123456701234567012345670") |
| 152 | testboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ") |
| 153 | testboth("%#+37.34o", big, " +0012345670123456701234567012345670") |
| 154 | # next one gets one leading zero from precision |
| 155 | testboth("%.33o", big, "012345670123456701234567012345670") |
| 156 | # base marker shouldn't change that, since "0" is redundant |
| 157 | testboth("%#.33o", big, "012345670123456701234567012345670") |
| 158 | # but reduce precision, and base marker should add a zero |
| 159 | testboth("%#.32o", big, "012345670123456701234567012345670") |
| 160 | # one leading zero from precision, and another from "0" flag & width |
| 161 | testboth("%034.33o", big, "0012345670123456701234567012345670") |
| 162 | # base marker shouldn't change that |
| 163 | testboth("%0#34.33o", big, "0012345670123456701234567012345670") |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 164 | |
| 165 | # Some small ints, in both Python int and long flavors). |
| 166 | testboth("%d", 42, "42") |
| 167 | testboth("%d", -42, "-42") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 168 | testboth("%d", 42, "42") |
| 169 | testboth("%d", -42, "-42") |
Tim Peters | 711088d | 2001-04-12 00:35:51 +0000 | [diff] [blame] | 170 | testboth("%#x", 1, "0x1") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 171 | testboth("%#x", 1, "0x1") |
Tim Peters | 711088d | 2001-04-12 00:35:51 +0000 | [diff] [blame] | 172 | testboth("%#X", 1, "0X1") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 173 | testboth("%#X", 1, "0X1") |
Tim Peters | 711088d | 2001-04-12 00:35:51 +0000 | [diff] [blame] | 174 | testboth("%#o", 1, "01") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 175 | testboth("%#o", 1, "01") |
Tim Peters | 711088d | 2001-04-12 00:35:51 +0000 | [diff] [blame] | 176 | testboth("%#o", 0, "0") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 177 | testboth("%#o", 0, "0") |
Tim Peters | 711088d | 2001-04-12 00:35:51 +0000 | [diff] [blame] | 178 | testboth("%o", 0, "0") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 179 | testboth("%o", 0, "0") |
Tim Peters | 711088d | 2001-04-12 00:35:51 +0000 | [diff] [blame] | 180 | testboth("%d", 0, "0") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 181 | testboth("%d", 0, "0") |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 182 | testboth("%#x", 0, "0x0") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 183 | testboth("%#x", 0, "0x0") |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 184 | testboth("%#X", 0, "0X0") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 185 | testboth("%#X", 0, "0X0") |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 186 | |
| 187 | testboth("%x", 0x42, "42") |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 188 | testboth("%x", -0x42, "-42") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 189 | testboth("%x", 0x42, "42") |
| 190 | testboth("%x", -0x42, "-42") |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 191 | |
| 192 | testboth("%o", 042, "42") |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 193 | testboth("%o", -042, "-42") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 194 | testboth("%o", 042, "42") |
| 195 | testboth("%o", -042, "-42") |
Andrew M. Kuchling | 4d192b3 | 2000-12-15 13:09:06 +0000 | [diff] [blame] | 196 | |
| 197 | # Test exception for unknown format characters |
| 198 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 199 | print('Testing exceptions') |
Andrew M. Kuchling | 4d192b3 | 2000-12-15 13:09:06 +0000 | [diff] [blame] | 200 | |
| 201 | def test_exc(formatstr, args, exception, excmsg): |
| 202 | try: |
| 203 | testformat(formatstr, args) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 204 | except exception as exc: |
Andrew M. Kuchling | 4d192b3 | 2000-12-15 13:09:06 +0000 | [diff] [blame] | 205 | if str(exc) == excmsg: |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 206 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 207 | print("yes") |
Andrew M. Kuchling | 4d192b3 | 2000-12-15 13:09:06 +0000 | [diff] [blame] | 208 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 209 | if verbose: print('no') |
| 210 | print('Unexpected ', exception, ':', repr(str(exc))) |
Andrew M. Kuchling | 4d192b3 | 2000-12-15 13:09:06 +0000 | [diff] [blame] | 211 | except: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 212 | if verbose: print('no') |
| 213 | print('Unexpected exception') |
Andrew M. Kuchling | 4d192b3 | 2000-12-15 13:09:06 +0000 | [diff] [blame] | 214 | raise |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 215 | else: |
| 216 | raise TestFailed, 'did not get expected exception: %s' % excmsg |
Andrew M. Kuchling | 4d192b3 | 2000-12-15 13:09:06 +0000 | [diff] [blame] | 217 | |
| 218 | test_exc('abc %a', 1, ValueError, |
| 219 | "unsupported format character 'a' (0x61) at index 5") |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 220 | if have_unicode: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 221 | test_exc(str('abc %\u3000','raw-unicode-escape'), 1, ValueError, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 222 | "unsupported format character '?' (0x3000) at index 5") |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 223 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 224 | test_exc('%d', '1', TypeError, "int argument required, not str") |
| 225 | test_exc('%g', '1', TypeError, "float argument required, not str") |
Tim Peters | 77c06fb | 2002-11-24 02:35:35 +0000 | [diff] [blame] | 226 | test_exc('no format', '1', TypeError, |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 227 | "not all arguments converted during string formatting") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 228 | test_exc('no format', '1', TypeError, |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 229 | "not all arguments converted during string formatting") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 230 | test_exc('no format', '1', TypeError, |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 231 | "not all arguments converted during string formatting") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 232 | test_exc('no format', '1', TypeError, |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 233 | "not all arguments converted during string formatting") |
Michael W. Hudson | 549ab8a | 2002-10-11 13:46:32 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 235 | class Foobar(int): |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 236 | def __oct__(self): |
| 237 | # Returning a non-string should not blow up. |
| 238 | return self + 1 |
| 239 | |
| 240 | test_exc('%o', Foobar(), TypeError, |
Guido van Rossum | 65eabe3 | 2007-01-14 18:52:06 +0000 | [diff] [blame] | 241 | "expected string or Unicode object, int found") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame^] | 243 | if maxsize == 2**31-1: |
Michael W. Hudson | 549ab8a | 2002-10-11 13:46:32 +0000 | [diff] [blame] | 244 | # crashes 2.2.1 and earlier: |
| 245 | try: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame^] | 246 | "%*d"%(maxsize, -127) |
Michael W. Hudson | 549ab8a | 2002-10-11 13:46:32 +0000 | [diff] [blame] | 247 | except MemoryError: |
| 248 | pass |
| 249 | else: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame^] | 250 | raise TestFailed, '"%*d"%(maxsize, -127) should fail' |