blob: a006bbf6a343c2fd5c1aa72aaa017c3edf35cb9a [file] [log] [blame]
Neal Norwitz88fe4ff2002-07-28 16:44:23 +00001from test.test_support import verbose, have_unicode, TestFailed
Eric S. Raymondd8c628b2001-02-09 11:46:37 +00002import sys
Marc-André Lemburgd70141a2000-06-30 10:26:29 +00003
4# test string formatting operator (I am not sure if this is being tested
5# elsewhere but, surely, some of the given cases are *not* tested because
6# they crash python)
7# test on unicode strings as well
8
Tim Peters38fd5b62000-09-21 05:43:11 +00009overflowok = 1
10
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000011def testformat(formatstr, args, output=None):
Tim Peters38fd5b62000-09-21 05:43:11 +000012 if verbose:
13 if output:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000014 print("%s %% %s =? %s ..." %\
15 (repr(formatstr), repr(args), repr(output)), end=' ')
Tim Peters38fd5b62000-09-21 05:43:11 +000016 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000017 print("%s %% %s works? ..." % (repr(formatstr), repr(args)), end=' ')
Tim Peters38fd5b62000-09-21 05:43:11 +000018 try:
19 result = formatstr % args
20 except OverflowError:
21 if not overflowok:
22 raise
23 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000024 print('overflow (this is fine)')
Tim Peters38fd5b62000-09-21 05:43:11 +000025 else:
26 if output and result != output:
27 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000028 print('no')
29 print("%s %% %s == %s != %s" %\
30 (repr(formatstr), repr(args), repr(result), repr(output)))
Tim Peters38fd5b62000-09-21 05:43:11 +000031 else:
32 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000033 print('yes')
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000034
35def testboth(formatstr, *args):
Tim Peters38fd5b62000-09-21 05:43:11 +000036 testformat(formatstr, *args)
Martin v. Löwis339d0f72001-08-17 18:39:25 +000037 if have_unicode:
Guido van Rossumef87d6e2007-05-02 19:09:54 +000038 testformat(str(formatstr), *args)
Tim Peters38fd5b62000-09-21 05:43:11 +000039
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000040
41testboth("%.1d", (1,), "1")
42testboth("%.*d", (sys.maxint,1)) # expect overflow
43testboth("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
44testboth("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
45testboth("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
46
47testboth("%f", (1.0,), "1.000000")
48# these are trying to test the limits of the internal magic-number-length
49# formatting buffer, if that number changes then these tests are less
50# effective
51testboth("%#.*g", (109, -1.e+49/3.))
52testboth("%#.*g", (110, -1.e+49/3.))
53testboth("%#.*g", (110, -1.e+100/3.))
54
55# test some ridiculously large precision, expect overflow
56testboth('%12.*f', (123456, 1.0))
57
Tim Peters38fd5b62000-09-21 05:43:11 +000058# Formatting of long integers. Overflow is not ok
59overflowok = 0
Guido van Rossume2a383d2007-01-15 16:59:06 +000060testboth("%x", 10, "a")
61testboth("%x", 100000000000, "174876e800")
62testboth("%o", 10, "12")
63testboth("%o", 100000000000, "1351035564000")
64testboth("%d", 10, "10")
65testboth("%d", 100000000000, "100000000000")
Tim Peters38fd5b62000-09-21 05:43:11 +000066
Guido van Rossume2a383d2007-01-15 16:59:06 +000067big = 123456789012345678901234567890
Tim Peters38fd5b62000-09-21 05:43:11 +000068testboth("%d", big, "123456789012345678901234567890")
69testboth("%d", -big, "-123456789012345678901234567890")
70testboth("%5d", -big, "-123456789012345678901234567890")
71testboth("%31d", -big, "-123456789012345678901234567890")
72testboth("%32d", -big, " -123456789012345678901234567890")
73testboth("%-32d", -big, "-123456789012345678901234567890 ")
74testboth("%032d", -big, "-0123456789012345678901234567890")
75testboth("%-032d", -big, "-123456789012345678901234567890 ")
76testboth("%034d", -big, "-000123456789012345678901234567890")
77testboth("%034d", big, "0000123456789012345678901234567890")
78testboth("%0+34d", big, "+000123456789012345678901234567890")
79testboth("%+34d", big, " +123456789012345678901234567890")
80testboth("%34d", big, " 123456789012345678901234567890")
81testboth("%.2d", big, "123456789012345678901234567890")
82testboth("%.30d", big, "123456789012345678901234567890")
83testboth("%.31d", big, "0123456789012345678901234567890")
84testboth("%32.31d", big, " 0123456789012345678901234567890")
85
Guido van Rossume2a383d2007-01-15 16:59:06 +000086big = 0x1234567890abcdef12345 # 21 hex digits
Tim Peters38fd5b62000-09-21 05:43:11 +000087testboth("%x", big, "1234567890abcdef12345")
88testboth("%x", -big, "-1234567890abcdef12345")
89testboth("%5x", -big, "-1234567890abcdef12345")
90testboth("%22x", -big, "-1234567890abcdef12345")
91testboth("%23x", -big, " -1234567890abcdef12345")
92testboth("%-23x", -big, "-1234567890abcdef12345 ")
93testboth("%023x", -big, "-01234567890abcdef12345")
94testboth("%-023x", -big, "-1234567890abcdef12345 ")
95testboth("%025x", -big, "-0001234567890abcdef12345")
96testboth("%025x", big, "00001234567890abcdef12345")
97testboth("%0+25x", big, "+0001234567890abcdef12345")
98testboth("%+25x", big, " +1234567890abcdef12345")
99testboth("%25x", big, " 1234567890abcdef12345")
100testboth("%.2x", big, "1234567890abcdef12345")
101testboth("%.21x", big, "1234567890abcdef12345")
102testboth("%.22x", big, "01234567890abcdef12345")
103testboth("%23.22x", big, " 01234567890abcdef12345")
104testboth("%-23.22x", big, "01234567890abcdef12345 ")
105testboth("%X", big, "1234567890ABCDEF12345")
106testboth("%#X", big, "0X1234567890ABCDEF12345")
107testboth("%#x", big, "0x1234567890abcdef12345")
108testboth("%#x", -big, "-0x1234567890abcdef12345")
109testboth("%#.23x", -big, "-0x001234567890abcdef12345")
110testboth("%#+.23x", big, "+0x001234567890abcdef12345")
111testboth("%# .23x", big, " 0x001234567890abcdef12345")
112testboth("%#+.23X", big, "+0X001234567890ABCDEF12345")
113testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345")
114testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
115testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
116testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
117# next one gets two leading zeroes from precision, and another from the
118# 0 flag and the width
119testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
120# same, except no 0 flag
121testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
122
Guido van Rossume2a383d2007-01-15 16:59:06 +0000123big = 012345670123456701234567012345670 # 32 octal digits
Tim Peters38fd5b62000-09-21 05:43:11 +0000124testboth("%o", big, "12345670123456701234567012345670")
125testboth("%o", -big, "-12345670123456701234567012345670")
126testboth("%5o", -big, "-12345670123456701234567012345670")
127testboth("%33o", -big, "-12345670123456701234567012345670")
128testboth("%34o", -big, " -12345670123456701234567012345670")
129testboth("%-34o", -big, "-12345670123456701234567012345670 ")
130testboth("%034o", -big, "-012345670123456701234567012345670")
131testboth("%-034o", -big, "-12345670123456701234567012345670 ")
132testboth("%036o", -big, "-00012345670123456701234567012345670")
133testboth("%036o", big, "000012345670123456701234567012345670")
134testboth("%0+36o", big, "+00012345670123456701234567012345670")
135testboth("%+36o", big, " +12345670123456701234567012345670")
136testboth("%36o", big, " 12345670123456701234567012345670")
137testboth("%.2o", big, "12345670123456701234567012345670")
138testboth("%.32o", big, "12345670123456701234567012345670")
139testboth("%.33o", big, "012345670123456701234567012345670")
140testboth("%34.33o", big, " 012345670123456701234567012345670")
141testboth("%-34.33o", big, "012345670123456701234567012345670 ")
142testboth("%o", big, "12345670123456701234567012345670")
143testboth("%#o", big, "012345670123456701234567012345670")
144testboth("%#o", -big, "-012345670123456701234567012345670")
145testboth("%#.34o", -big, "-0012345670123456701234567012345670")
146testboth("%#+.34o", big, "+0012345670123456701234567012345670")
147testboth("%# .34o", big, " 0012345670123456701234567012345670")
148testboth("%#+.34o", big, "+0012345670123456701234567012345670")
149testboth("%#-+.34o", big, "+0012345670123456701234567012345670")
150testboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ")
151testboth("%#+37.34o", big, " +0012345670123456701234567012345670")
152# next one gets one leading zero from precision
153testboth("%.33o", big, "012345670123456701234567012345670")
154# base marker shouldn't change that, since "0" is redundant
155testboth("%#.33o", big, "012345670123456701234567012345670")
156# but reduce precision, and base marker should add a zero
157testboth("%#.32o", big, "012345670123456701234567012345670")
158# one leading zero from precision, and another from "0" flag & width
159testboth("%034.33o", big, "0012345670123456701234567012345670")
160# base marker shouldn't change that
161testboth("%0#34.33o", big, "0012345670123456701234567012345670")
Tim Petersa3a3a032000-11-30 05:22:44 +0000162
163# Some small ints, in both Python int and long flavors).
164testboth("%d", 42, "42")
165testboth("%d", -42, "-42")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000166testboth("%d", 42, "42")
167testboth("%d", -42, "-42")
Tim Peters711088d2001-04-12 00:35:51 +0000168testboth("%#x", 1, "0x1")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000169testboth("%#x", 1, "0x1")
Tim Peters711088d2001-04-12 00:35:51 +0000170testboth("%#X", 1, "0X1")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000171testboth("%#X", 1, "0X1")
Tim Peters711088d2001-04-12 00:35:51 +0000172testboth("%#o", 1, "01")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000173testboth("%#o", 1, "01")
Tim Peters711088d2001-04-12 00:35:51 +0000174testboth("%#o", 0, "0")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000175testboth("%#o", 0, "0")
Tim Peters711088d2001-04-12 00:35:51 +0000176testboth("%o", 0, "0")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000177testboth("%o", 0, "0")
Tim Peters711088d2001-04-12 00:35:51 +0000178testboth("%d", 0, "0")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000179testboth("%d", 0, "0")
Tim Petersfff53252001-04-12 18:38:48 +0000180testboth("%#x", 0, "0x0")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000181testboth("%#x", 0, "0x0")
Tim Petersfff53252001-04-12 18:38:48 +0000182testboth("%#X", 0, "0X0")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000183testboth("%#X", 0, "0X0")
Tim Petersa3a3a032000-11-30 05:22:44 +0000184
185testboth("%x", 0x42, "42")
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000186testboth("%x", -0x42, "-42")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000187testboth("%x", 0x42, "42")
188testboth("%x", -0x42, "-42")
Tim Petersa3a3a032000-11-30 05:22:44 +0000189
190testboth("%o", 042, "42")
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000191testboth("%o", -042, "-42")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000192testboth("%o", 042, "42")
193testboth("%o", -042, "-42")
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000194
195# Test exception for unknown format characters
196if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000197 print('Testing exceptions')
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000198
199def test_exc(formatstr, args, exception, excmsg):
200 try:
201 testformat(formatstr, args)
Guido van Rossumb940e112007-01-10 16:19:56 +0000202 except exception as exc:
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000203 if str(exc) == excmsg:
Fredrik Lundhf7850422001-01-17 21:51:36 +0000204 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000205 print("yes")
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000206 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000207 if verbose: print('no')
208 print('Unexpected ', exception, ':', repr(str(exc)))
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000209 except:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000210 if verbose: print('no')
211 print('Unexpected exception')
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000212 raise
Neal Norwitz88fe4ff2002-07-28 16:44:23 +0000213 else:
214 raise TestFailed, 'did not get expected exception: %s' % excmsg
Andrew M. Kuchling4d192b32000-12-15 13:09:06 +0000215
216test_exc('abc %a', 1, ValueError,
217 "unsupported format character 'a' (0x61) at index 5")
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000218if have_unicode:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000219 test_exc(str('abc %\u3000','raw-unicode-escape'), 1, ValueError,
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000220 "unsupported format character '?' (0x3000) at index 5")
Neal Norwitz88fe4ff2002-07-28 16:44:23 +0000221
Thomas Wouters89f507f2006-12-13 04:49:30 +0000222test_exc('%d', '1', TypeError, "int argument required, not str")
223test_exc('%g', '1', TypeError, "float argument required, not str")
Tim Peters77c06fb2002-11-24 02:35:35 +0000224test_exc('no format', '1', TypeError,
Neal Norwitz80a1bf42002-11-12 23:01:12 +0000225 "not all arguments converted during string formatting")
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000226test_exc('no format', '1', TypeError,
Neal Norwitz80a1bf42002-11-12 23:01:12 +0000227 "not all arguments converted during string formatting")
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000228test_exc('no format', '1', TypeError,
Neal Norwitz80a1bf42002-11-12 23:01:12 +0000229 "not all arguments converted during string formatting")
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000230test_exc('no format', '1', TypeError,
Neal Norwitz80a1bf42002-11-12 23:01:12 +0000231 "not all arguments converted during string formatting")
Michael W. Hudson549ab8a2002-10-11 13:46:32 +0000232
Guido van Rossume2a383d2007-01-15 16:59:06 +0000233class Foobar(int):
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000234 def __oct__(self):
235 # Returning a non-string should not blow up.
236 return self + 1
237
238test_exc('%o', Foobar(), TypeError,
Guido van Rossum65eabe32007-01-14 18:52:06 +0000239 "expected string or Unicode object, int found")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000240
Guido van Rossum457bf912003-11-29 23:55:09 +0000241if sys.maxint == 2**31-1:
Michael W. Hudson549ab8a2002-10-11 13:46:32 +0000242 # crashes 2.2.1 and earlier:
243 try:
244 "%*d"%(sys.maxint, -127)
245 except MemoryError:
246 pass
247 else:
248 raise TestFailed, '"%*d"%(sys.maxint, -127) should fail'