blob: ace1f1f3c95886f8a1927ec1135c5dd199fc198a [file] [log] [blame]
Walter Dörwald02427052007-05-22 16:27:09 +00001from test.test_support import verbose, TestFailed
Eric S. Raymondd8c628b2001-02-09 11:46:37 +00002import sys
Guido van Rossum360e4b82007-05-14 22:51:27 +00003from test.test_support import MAX_Py_ssize_t
4maxsize = MAX_Py_ssize_t
Marc-André Lemburgd70141a2000-06-30 10:26:29 +00005
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 Peters38fd5b62000-09-21 05:43:11 +000011overflowok = 1
12
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000013def testformat(formatstr, args, output=None):
Tim Peters38fd5b62000-09-21 05:43:11 +000014 if verbose:
15 if output:
Walter Dörwald38e43c22007-06-09 16:13:23 +000016 print("%r %% %r =? %r ..." %\
17 (formatstr, args, output), end=' ')
Tim Peters38fd5b62000-09-21 05:43:11 +000018 else:
Walter Dörwald38e43c22007-06-09 16:13:23 +000019 print("%r %% %r works? ..." % (formatstr, args), end=' ')
Tim Peters38fd5b62000-09-21 05:43:11 +000020 try:
21 result = formatstr % args
22 except OverflowError:
23 if not overflowok:
24 raise
25 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000026 print('overflow (this is fine)')
Tim Peters38fd5b62000-09-21 05:43:11 +000027 else:
28 if output and result != output:
29 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000030 print('no')
Walter Dörwald38e43c22007-06-09 16:13:23 +000031 print("%r %% %r == %r != %r" %\
32 (formatstr, args, result, output))
Tim Peters38fd5b62000-09-21 05:43:11 +000033 else:
34 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000035 print('yes')
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000036
37def testboth(formatstr, *args):
Walter Dörwald88effc12007-05-21 21:02:54 +000038 testformat(str8(formatstr), *args)
Tim Peters38fd5b62000-09-21 05:43:11 +000039 testformat(formatstr, *args)
Tim Peters38fd5b62000-09-21 05:43:11 +000040
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000041
42testboth("%.1d", (1,), "1")
43testboth("%.*d", (sys.maxint,1)) # expect overflow
44testboth("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
45testboth("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
46testboth("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
47
48testboth("%f", (1.0,), "1.000000")
49# these are trying to test the limits of the internal magic-number-length
50# formatting buffer, if that number changes then these tests are less
51# effective
52testboth("%#.*g", (109, -1.e+49/3.))
53testboth("%#.*g", (110, -1.e+49/3.))
54testboth("%#.*g", (110, -1.e+100/3.))
55
56# test some ridiculously large precision, expect overflow
57testboth('%12.*f', (123456, 1.0))
58
Tim Peters38fd5b62000-09-21 05:43:11 +000059# Formatting of long integers. Overflow is not ok
60overflowok = 0
Guido van Rossume2a383d2007-01-15 16:59:06 +000061testboth("%x", 10, "a")
62testboth("%x", 100000000000, "174876e800")
63testboth("%o", 10, "12")
64testboth("%o", 100000000000, "1351035564000")
65testboth("%d", 10, "10")
66testboth("%d", 100000000000, "100000000000")
Tim Peters38fd5b62000-09-21 05:43:11 +000067
Guido van Rossume2a383d2007-01-15 16:59:06 +000068big = 123456789012345678901234567890
Tim Peters38fd5b62000-09-21 05:43:11 +000069testboth("%d", big, "123456789012345678901234567890")
70testboth("%d", -big, "-123456789012345678901234567890")
71testboth("%5d", -big, "-123456789012345678901234567890")
72testboth("%31d", -big, "-123456789012345678901234567890")
73testboth("%32d", -big, " -123456789012345678901234567890")
74testboth("%-32d", -big, "-123456789012345678901234567890 ")
75testboth("%032d", -big, "-0123456789012345678901234567890")
76testboth("%-032d", -big, "-123456789012345678901234567890 ")
77testboth("%034d", -big, "-000123456789012345678901234567890")
78testboth("%034d", big, "0000123456789012345678901234567890")
79testboth("%0+34d", big, "+000123456789012345678901234567890")
80testboth("%+34d", big, " +123456789012345678901234567890")
81testboth("%34d", big, " 123456789012345678901234567890")
82testboth("%.2d", big, "123456789012345678901234567890")
83testboth("%.30d", big, "123456789012345678901234567890")
84testboth("%.31d", big, "0123456789012345678901234567890")
85testboth("%32.31d", big, " 0123456789012345678901234567890")
86
Guido van Rossume2a383d2007-01-15 16:59:06 +000087big = 0x1234567890abcdef12345 # 21 hex digits
Tim Peters38fd5b62000-09-21 05:43:11 +000088testboth("%x", big, "1234567890abcdef12345")
89testboth("%x", -big, "-1234567890abcdef12345")
90testboth("%5x", -big, "-1234567890abcdef12345")
91testboth("%22x", -big, "-1234567890abcdef12345")
92testboth("%23x", -big, " -1234567890abcdef12345")
93testboth("%-23x", -big, "-1234567890abcdef12345 ")
94testboth("%023x", -big, "-01234567890abcdef12345")
95testboth("%-023x", -big, "-1234567890abcdef12345 ")
96testboth("%025x", -big, "-0001234567890abcdef12345")
97testboth("%025x", big, "00001234567890abcdef12345")
98testboth("%0+25x", big, "+0001234567890abcdef12345")
99testboth("%+25x", big, " +1234567890abcdef12345")
100testboth("%25x", big, " 1234567890abcdef12345")
101testboth("%.2x", big, "1234567890abcdef12345")
102testboth("%.21x", big, "1234567890abcdef12345")
103testboth("%.22x", big, "01234567890abcdef12345")
104testboth("%23.22x", big, " 01234567890abcdef12345")
105testboth("%-23.22x", big, "01234567890abcdef12345 ")
106testboth("%X", big, "1234567890ABCDEF12345")
107testboth("%#X", big, "0X1234567890ABCDEF12345")
108testboth("%#x", big, "0x1234567890abcdef12345")
109testboth("%#x", -big, "-0x1234567890abcdef12345")
110testboth("%#.23x", -big, "-0x001234567890abcdef12345")
111testboth("%#+.23x", big, "+0x001234567890abcdef12345")
112testboth("%# .23x", big, " 0x001234567890abcdef12345")
113testboth("%#+.23X", big, "+0X001234567890ABCDEF12345")
114testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345")
115testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
116testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
117testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
118# next one gets two leading zeroes from precision, and another from the
119# 0 flag and the width
120testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
121# same, except no 0 flag
122testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
123
Guido van Rossume2a383d2007-01-15 16:59:06 +0000124big = 012345670123456701234567012345670 # 32 octal digits
Tim Peters38fd5b62000-09-21 05:43:11 +0000125testboth("%o", big, "12345670123456701234567012345670")
126testboth("%o", -big, "-12345670123456701234567012345670")
127testboth("%5o", -big, "-12345670123456701234567012345670")
128testboth("%33o", -big, "-12345670123456701234567012345670")
129testboth("%34o", -big, " -12345670123456701234567012345670")
130testboth("%-34o", -big, "-12345670123456701234567012345670 ")
131testboth("%034o", -big, "-012345670123456701234567012345670")
132testboth("%-034o", -big, "-12345670123456701234567012345670 ")
133testboth("%036o", -big, "-00012345670123456701234567012345670")
134testboth("%036o", big, "000012345670123456701234567012345670")
135testboth("%0+36o", big, "+00012345670123456701234567012345670")
136testboth("%+36o", big, " +12345670123456701234567012345670")
137testboth("%36o", big, " 12345670123456701234567012345670")
138testboth("%.2o", big, "12345670123456701234567012345670")
139testboth("%.32o", big, "12345670123456701234567012345670")
140testboth("%.33o", big, "012345670123456701234567012345670")
141testboth("%34.33o", big, " 012345670123456701234567012345670")
142testboth("%-34.33o", big, "012345670123456701234567012345670 ")
143testboth("%o", big, "12345670123456701234567012345670")
144testboth("%#o", big, "012345670123456701234567012345670")
145testboth("%#o", -big, "-012345670123456701234567012345670")
146testboth("%#.34o", -big, "-0012345670123456701234567012345670")
147testboth("%#+.34o", big, "+0012345670123456701234567012345670")
148testboth("%# .34o", big, " 0012345670123456701234567012345670")
149testboth("%#+.34o", big, "+0012345670123456701234567012345670")
150testboth("%#-+.34o", big, "+0012345670123456701234567012345670")
151testboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ")
152testboth("%#+37.34o", big, " +0012345670123456701234567012345670")
153# next one gets one leading zero from precision
154testboth("%.33o", big, "012345670123456701234567012345670")
155# base marker shouldn't change that, since "0" is redundant
156testboth("%#.33o", big, "012345670123456701234567012345670")
157# but reduce precision, and base marker should add a zero
158testboth("%#.32o", big, "012345670123456701234567012345670")
159# one leading zero from precision, and another from "0" flag & width
160testboth("%034.33o", big, "0012345670123456701234567012345670")
161# base marker shouldn't change that
162testboth("%0#34.33o", big, "0012345670123456701234567012345670")
Tim Petersa3a3a032000-11-30 05:22:44 +0000163
164# Some small ints, in both Python int and long flavors).
165testboth("%d", 42, "42")
166testboth("%d", -42, "-42")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000167testboth("%#x", 1, "0x1")
Tim Peters711088d2001-04-12 00:35:51 +0000168testboth("%#X", 1, "0X1")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000169testboth("%#o", 1, "01")
Tim Peters711088d2001-04-12 00:35:51 +0000170testboth("%#o", 0, "0")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000171testboth("%o", 0, "0")
Tim Peters711088d2001-04-12 00:35:51 +0000172testboth("%d", 0, "0")
Tim Petersfff53252001-04-12 18:38:48 +0000173testboth("%#x", 0, "0x0")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000174testboth("%#X", 0, "0X0")
Tim Petersa3a3a032000-11-30 05:22:44 +0000175
176testboth("%x", 0x42, "42")
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000177testboth("%x", -0x42, "-42")
Tim Petersa3a3a032000-11-30 05:22:44 +0000178
179testboth("%o", 042, "42")
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000180testboth("%o", -042, "-42")
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000181
182# Test exception for unknown format characters
183if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000184 print('Testing exceptions')
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000185
186def test_exc(formatstr, args, exception, excmsg):
187 try:
188 testformat(formatstr, args)
Guido van Rossumb940e112007-01-10 16:19:56 +0000189 except exception as exc:
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000190 if str(exc) == excmsg:
Fredrik Lundhf7850422001-01-17 21:51:36 +0000191 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000192 print("yes")
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000193 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000194 if verbose: print('no')
195 print('Unexpected ', exception, ':', repr(str(exc)))
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000196 except:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000197 if verbose: print('no')
198 print('Unexpected exception')
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000199 raise
Neal Norwitz88fe4ff2002-07-28 16:44:23 +0000200 else:
201 raise TestFailed, 'did not get expected exception: %s' % excmsg
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000202
203test_exc('abc %a', 1, ValueError,
204 "unsupported format character 'a' (0x61) at index 5")
Walter Dörwald88effc12007-05-21 21:02:54 +0000205test_exc(str(b'abc %\u3000', 'raw-unicode-escape'), 1, ValueError,
206 "unsupported format character '?' (0x3000) at index 5")
Neal Norwitz88fe4ff2002-07-28 16:44:23 +0000207
Walter Dörwald88effc12007-05-21 21:02:54 +0000208test_exc('%d', '1', TypeError, "an integer is required")
209test_exc('%g', '1', TypeError, "a float is required")
Tim Peters77c06fb2002-11-24 02:35:35 +0000210test_exc('no format', '1', TypeError,
Neal Norwitz80a1bf42002-11-12 23:01:12 +0000211 "not all arguments converted during string formatting")
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000212test_exc('no format', '1', TypeError,
Neal Norwitz80a1bf42002-11-12 23:01:12 +0000213 "not all arguments converted during string formatting")
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000214test_exc('no format', '1', TypeError,
Neal Norwitz80a1bf42002-11-12 23:01:12 +0000215 "not all arguments converted during string formatting")
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000216test_exc('no format', '1', TypeError,
Neal Norwitz80a1bf42002-11-12 23:01:12 +0000217 "not all arguments converted during string formatting")
Michael W. Hudson549ab8a2002-10-11 13:46:32 +0000218
Guido van Rossume2a383d2007-01-15 16:59:06 +0000219class Foobar(int):
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000220 def __oct__(self):
221 # Returning a non-string should not blow up.
222 return self + 1
223
224test_exc('%o', Foobar(), TypeError,
Walter Dörwald38e43c22007-06-09 16:13:23 +0000225 "expected string, int found")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000226
Guido van Rossum360e4b82007-05-14 22:51:27 +0000227if maxsize == 2**31-1:
Michael W. Hudson549ab8a2002-10-11 13:46:32 +0000228 # crashes 2.2.1 and earlier:
229 try:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000230 "%*d"%(maxsize, -127)
Michael W. Hudson549ab8a2002-10-11 13:46:32 +0000231 except MemoryError:
232 pass
233 else:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000234 raise TestFailed, '"%*d"%(maxsize, -127) should fail'