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