• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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        testformat(u"%x", fst, '0')
247
248        # Test exception for unknown format characters
249        if verbose:
250            print 'Testing exceptions'
251
252        def test_exc(formatstr, args, exception, excmsg):
253            try:
254                testformat(formatstr, args)
255            except exception, exc:
256                if str(exc) == excmsg:
257                    if verbose:
258                        print "yes"
259                else:
260                    if verbose: print 'no'
261                    print 'Unexpected ', exception, ':', repr(str(exc))
262            except:
263                if verbose: print 'no'
264                print 'Unexpected exception'
265                raise
266            else:
267                raise TestFailed, 'did not get expected exception: %s' % excmsg
268
269        test_exc('abc %a', 1, ValueError,
270                 "unsupported format character 'a' (0x61) at index 5")
271        if have_unicode:
272            test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
273                     "unsupported format character '?' (0x3000) at index 5")
274
275        test_exc('%d', '1', TypeError, "%d format: a number is required, not str")
276        test_exc('%g', '1', TypeError, "float argument required, not str")
277        test_exc('no format', '1', TypeError,
278                 "not all arguments converted during string formatting")
279        test_exc('no format', u'1', TypeError,
280                 "not all arguments converted during string formatting")
281        test_exc(u'no format', '1', TypeError,
282                 "not all arguments converted during string formatting")
283        test_exc(u'no format', u'1', TypeError,
284                 "not all arguments converted during string formatting")
285
286        class Foobar(long):
287            def __oct__(self):
288                # Returning a non-string should not blow up.
289                return self + 1
290
291        test_exc('%o', Foobar(), TypeError,
292                 "expected string or Unicode object, long found")
293
294        if maxsize == 2**31-1:
295            # crashes 2.2.1 and earlier:
296            try:
297                "%*d"%(maxsize, -127)
298            except MemoryError:
299                pass
300            else:
301                raise TestFailed, '"%*d"%(maxsize, -127) should fail'
302
303    def test_invalid_special_methods(self):
304        tests = []
305        for f in 'sriduoxXfge':
306            tests.append(('%' + f, 1, TypeError))
307            tests.append(('%#' + f, 1, TypeError))
308        for r in ['', '-', 'L', '-L']:
309            for f in 'iduoxX':
310                tests.append(('%' + f, r, ValueError))
311                tests.append(('%#' + f, r, ValueError))
312        tests.append(('%o', 'abc', ValueError))
313        for r in ('abc', '0abc', '0x', '0xL'):
314            for f in 'xX':
315                tests.append(('%' + f, r, ValueError))
316        for r in ('0x', '0xL'):
317            for f in 'xX':
318                tests.append(('%#' + f, r, ValueError))
319
320        class X(long):
321            def __repr__(self):
322                return result
323            def __str__(self):
324                return result
325            def __oct__(self):
326                return result
327            def __hex__(self):
328                return result
329            def __float__(self):
330                return result
331        for fmt, result, exc in tests:
332            try:
333                fmt % X()
334            except exc:
335                pass
336            else:
337                self.fail('%s not raised for %r format of %r' %
338                          (exc.__name__, fmt, result))
339
340
341def test_main():
342    test_support.run_unittest(FormatTest)
343
344    def test_precision(self):
345        f = 1.2
346        self.assertEqual(format(f, ".0f"), "1")
347        self.assertEqual(format(f, ".3f"), "1.200")
348        with self.assertRaises(ValueError) as cm:
349            format(f, ".%sf" % (sys.maxsize + 1))
350        self.assertEqual(str(cm.exception), "precision too big")
351
352        c = complex(f)
353        self.assertEqual(format(c, ".0f"), "1+0j")
354        self.assertEqual(format(c, ".3f"), "1.200+0.000j")
355        with self.assertRaises(ValueError) as cm:
356            format(c, ".%sf" % (sys.maxsize + 1))
357        self.assertEqual(str(cm.exception), "precision too big")
358
359    @test_support.cpython_only
360    def test_precision_c_limits(self):
361        from _testcapi import INT_MAX
362
363        f = 1.2
364        with self.assertRaises(ValueError) as cm:
365            format(f, ".%sf" % (INT_MAX + 1))
366
367        c = complex(f)
368        with self.assertRaises(ValueError) as cm:
369            format(c, ".%sf" % (INT_MAX + 1))
370
371
372if __name__ == "__main__":
373    unittest.main()
374