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