test_format.py revision 7c3b50db6614c677588096191d432e734257a244
1from test.test_support import verbose, have_unicode, TestFailed
2import sys
3from test.test_support import MAX_Py_ssize_t
4maxsize = MAX_Py_ssize_t
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
11overflowok = 1
12overflowrequired = 0
13
14def testformat(formatstr, args, output=None):
15    if verbose:
16        if output:
17            print "%s %% %s =? %s ..." %\
18                (repr(formatstr), repr(args), repr(output)),
19        else:
20            print "%s %% %s works? ..." % (repr(formatstr), repr(args)),
21    try:
22        result = formatstr % args
23    except OverflowError:
24        if not overflowok:
25            raise
26        if verbose:
27            print 'overflow (this is fine)'
28    else:
29        if overflowrequired:
30            if verbose:
31                print 'no'
32            print "overflow expected on %s %% %s" % \
33                  (repr(formatstr), repr(args))
34        elif output and result != output:
35            if verbose:
36                print 'no'
37            print "%s %% %s == %s != %s" % \
38                  (repr(formatstr), repr(args), repr(result), repr(output))
39        else:
40            if verbose:
41                print 'yes'
42
43def testboth(formatstr, *args):
44    testformat(formatstr, *args)
45    if have_unicode:
46        testformat(unicode(formatstr), *args)
47
48
49testboth("%.1d", (1,), "1")
50testboth("%.*d", (sys.maxint,1))  # expect overflow
51testboth("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
52testboth("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
53testboth("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
54
55testboth("%f", (1.0,), "1.000000")
56# these are trying to test the limits of the internal magic-number-length
57# formatting buffer, if that number changes then these tests are less
58# effective
59testboth("%#.*g", (109, -1.e+49/3.))
60testboth("%#.*g", (110, -1.e+49/3.))
61testboth("%#.*g", (110, -1.e+100/3.))
62
63# test some ridiculously large precision, expect overflow
64testboth('%12.*f', (123456, 1.0))
65
66# check for internal overflow validation on length of precision
67overflowrequired = 1
68testboth("%#.*g", (110, -1.e+100/3.))
69testboth("%#.*G", (110, -1.e+100/3.))
70testboth("%#.*f", (110, -1.e+100/3.))
71testboth("%#.*F", (110, -1.e+100/3.))
72overflowrequired = 0
73
74# Formatting of long integers. Overflow is not ok
75overflowok = 0
76testboth("%x", 10L, "a")
77testboth("%x", 100000000000L, "174876e800")
78testboth("%o", 10L, "12")
79testboth("%o", 100000000000L, "1351035564000")
80testboth("%d", 10L, "10")
81testboth("%d", 100000000000L, "100000000000")
82
83big = 123456789012345678901234567890L
84testboth("%d", big, "123456789012345678901234567890")
85testboth("%d", -big, "-123456789012345678901234567890")
86testboth("%5d", -big, "-123456789012345678901234567890")
87testboth("%31d", -big, "-123456789012345678901234567890")
88testboth("%32d", -big, " -123456789012345678901234567890")
89testboth("%-32d", -big, "-123456789012345678901234567890 ")
90testboth("%032d", -big, "-0123456789012345678901234567890")
91testboth("%-032d", -big, "-123456789012345678901234567890 ")
92testboth("%034d", -big, "-000123456789012345678901234567890")
93testboth("%034d", big, "0000123456789012345678901234567890")
94testboth("%0+34d", big, "+000123456789012345678901234567890")
95testboth("%+34d", big, "   +123456789012345678901234567890")
96testboth("%34d", big, "    123456789012345678901234567890")
97testboth("%.2d", big, "123456789012345678901234567890")
98testboth("%.30d", big, "123456789012345678901234567890")
99testboth("%.31d", big, "0123456789012345678901234567890")
100testboth("%32.31d", big, " 0123456789012345678901234567890")
101
102big = 0x1234567890abcdef12345L  # 21 hex digits
103testboth("%x", big, "1234567890abcdef12345")
104testboth("%x", -big, "-1234567890abcdef12345")
105testboth("%5x", -big, "-1234567890abcdef12345")
106testboth("%22x", -big, "-1234567890abcdef12345")
107testboth("%23x", -big, " -1234567890abcdef12345")
108testboth("%-23x", -big, "-1234567890abcdef12345 ")
109testboth("%023x", -big, "-01234567890abcdef12345")
110testboth("%-023x", -big, "-1234567890abcdef12345 ")
111testboth("%025x", -big, "-0001234567890abcdef12345")
112testboth("%025x", big, "00001234567890abcdef12345")
113testboth("%0+25x", big, "+0001234567890abcdef12345")
114testboth("%+25x", big, "   +1234567890abcdef12345")
115testboth("%25x", big, "    1234567890abcdef12345")
116testboth("%.2x", big, "1234567890abcdef12345")
117testboth("%.21x", big, "1234567890abcdef12345")
118testboth("%.22x", big, "01234567890abcdef12345")
119testboth("%23.22x", big, " 01234567890abcdef12345")
120testboth("%-23.22x", big, "01234567890abcdef12345 ")
121testboth("%X", big, "1234567890ABCDEF12345")
122testboth("%#X", big, "0X1234567890ABCDEF12345")
123testboth("%#x", big, "0x1234567890abcdef12345")
124testboth("%#x", -big, "-0x1234567890abcdef12345")
125testboth("%#.23x", -big, "-0x001234567890abcdef12345")
126testboth("%#+.23x", big, "+0x001234567890abcdef12345")
127testboth("%# .23x", big, " 0x001234567890abcdef12345")
128testboth("%#+.23X", big, "+0X001234567890ABCDEF12345")
129testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345")
130testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
131testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
132testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
133# next one gets two leading zeroes from precision, and another from the
134# 0 flag and the width
135testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
136# same, except no 0 flag
137testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
138
139big = 012345670123456701234567012345670L  # 32 octal digits
140testboth("%o", big, "12345670123456701234567012345670")
141testboth("%o", -big, "-12345670123456701234567012345670")
142testboth("%5o", -big, "-12345670123456701234567012345670")
143testboth("%33o", -big, "-12345670123456701234567012345670")
144testboth("%34o", -big, " -12345670123456701234567012345670")
145testboth("%-34o", -big, "-12345670123456701234567012345670 ")
146testboth("%034o", -big, "-012345670123456701234567012345670")
147testboth("%-034o", -big, "-12345670123456701234567012345670 ")
148testboth("%036o", -big, "-00012345670123456701234567012345670")
149testboth("%036o", big, "000012345670123456701234567012345670")
150testboth("%0+36o", big, "+00012345670123456701234567012345670")
151testboth("%+36o", big, "   +12345670123456701234567012345670")
152testboth("%36o", big, "    12345670123456701234567012345670")
153testboth("%.2o", big, "12345670123456701234567012345670")
154testboth("%.32o", big, "12345670123456701234567012345670")
155testboth("%.33o", big, "012345670123456701234567012345670")
156testboth("%34.33o", big, " 012345670123456701234567012345670")
157testboth("%-34.33o", big, "012345670123456701234567012345670 ")
158testboth("%o", big, "12345670123456701234567012345670")
159testboth("%#o", big, "012345670123456701234567012345670")
160testboth("%#o", -big, "-012345670123456701234567012345670")
161testboth("%#.34o", -big, "-0012345670123456701234567012345670")
162testboth("%#+.34o", big, "+0012345670123456701234567012345670")
163testboth("%# .34o", big, " 0012345670123456701234567012345670")
164testboth("%#+.34o", big, "+0012345670123456701234567012345670")
165testboth("%#-+.34o", big, "+0012345670123456701234567012345670")
166testboth("%#-+37.34o", big, "+0012345670123456701234567012345670  ")
167testboth("%#+37.34o", big, "  +0012345670123456701234567012345670")
168# next one gets one leading zero from precision
169testboth("%.33o", big, "012345670123456701234567012345670")
170# base marker shouldn't change that, since "0" is redundant
171testboth("%#.33o", big, "012345670123456701234567012345670")
172# but reduce precision, and base marker should add a zero
173testboth("%#.32o", big, "012345670123456701234567012345670")
174# one leading zero from precision, and another from "0" flag & width
175testboth("%034.33o", big, "0012345670123456701234567012345670")
176# base marker shouldn't change that
177testboth("%0#34.33o", big, "0012345670123456701234567012345670")
178
179# Some small ints, in both Python int and long flavors).
180testboth("%d", 42, "42")
181testboth("%d", -42, "-42")
182testboth("%d", 42L, "42")
183testboth("%d", -42L, "-42")
184testboth("%#x", 1, "0x1")
185testboth("%#x", 1L, "0x1")
186testboth("%#X", 1, "0X1")
187testboth("%#X", 1L, "0X1")
188testboth("%#o", 1, "01")
189testboth("%#o", 1L, "01")
190testboth("%#o", 0, "0")
191testboth("%#o", 0L, "0")
192testboth("%o", 0, "0")
193testboth("%o", 0L, "0")
194testboth("%d", 0, "0")
195testboth("%d", 0L, "0")
196testboth("%#x", 0, "0x0")
197testboth("%#x", 0L, "0x0")
198testboth("%#X", 0, "0X0")
199testboth("%#X", 0L, "0X0")
200
201testboth("%x", 0x42, "42")
202testboth("%x", -0x42, "-42")
203testboth("%x", 0x42L, "42")
204testboth("%x", -0x42L, "-42")
205
206testboth("%o", 042, "42")
207testboth("%o", -042, "-42")
208testboth("%o", 042L, "42")
209testboth("%o", -042L, "-42")
210
211# Test exception for unknown format characters
212if verbose:
213    print 'Testing exceptions'
214
215def test_exc(formatstr, args, exception, excmsg):
216    try:
217        testformat(formatstr, args)
218    except exception, exc:
219        if str(exc) == excmsg:
220            if verbose:
221                print "yes"
222        else:
223            if verbose: print 'no'
224            print 'Unexpected ', exception, ':', repr(str(exc))
225    except:
226        if verbose: print 'no'
227        print 'Unexpected exception'
228        raise
229    else:
230        raise TestFailed, 'did not get expected exception: %s' % excmsg
231
232test_exc('abc %a', 1, ValueError,
233         "unsupported format character 'a' (0x61) at index 5")
234if have_unicode:
235    test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
236             "unsupported format character '?' (0x3000) at index 5")
237
238test_exc('%d', '1', TypeError, "int argument required, not str")
239test_exc('%g', '1', TypeError, "float argument required, not str")
240test_exc('no format', '1', TypeError,
241         "not all arguments converted during string formatting")
242test_exc('no format', u'1', TypeError,
243         "not all arguments converted during string formatting")
244test_exc(u'no format', '1', TypeError,
245         "not all arguments converted during string formatting")
246test_exc(u'no format', u'1', TypeError,
247         "not all arguments converted during string formatting")
248
249class Foobar(long):
250    def __oct__(self):
251        # Returning a non-string should not blow up.
252        return self + 1
253
254test_exc('%o', Foobar(), TypeError,
255         "expected string or Unicode object, long found")
256
257if maxsize == 2**31-1:
258    # crashes 2.2.1 and earlier:
259    try:
260        "%*d"%(maxsize, -127)
261    except MemoryError:
262        pass
263    else:
264        raise TestFailed, '"%*d"%(maxsize, -127) should fail'
265