Issue #6615: logging: Used weak references in internal handler list. Thanks to flox...
[python.git] / Lib / test / test_types.py
blob18ab9cc84b8deac47cd6bb3d941c4cbcbe0a38c4
1 # Python test set -- part 6, built-in types
3 from test.test_support import run_unittest, have_unicode, run_with_locale
4 import unittest
5 import sys
6 import locale
8 class TypesTests(unittest.TestCase):
10 def test_truth_values(self):
11 if None: self.fail('None is true instead of false')
12 if 0: self.fail('0 is true instead of false')
13 if 0L: self.fail('0L is true instead of false')
14 if 0.0: self.fail('0.0 is true instead of false')
15 if '': self.fail('\'\' is true instead of false')
16 if not 1: self.fail('1 is false instead of true')
17 if not 1L: self.fail('1L is false instead of true')
18 if not 1.0: self.fail('1.0 is false instead of true')
19 if not 'x': self.fail('\'x\' is false instead of true')
20 if not {'x': 1}: self.fail('{\'x\': 1} is false instead of true')
21 def f(): pass
22 class C: pass
23 import sys
24 x = C()
25 if not f: self.fail('f is false instead of true')
26 if not C: self.fail('C is false instead of true')
27 if not sys: self.fail('sys is false instead of true')
28 if not x: self.fail('x is false instead of true')
30 def test_boolean_ops(self):
31 if 0 or 0: self.fail('0 or 0 is true instead of false')
32 if 1 and 1: pass
33 else: self.fail('1 and 1 is false instead of true')
34 if not 1: self.fail('not 1 is true instead of false')
36 def test_comparisons(self):
37 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
38 else: self.fail('int comparisons failed')
39 if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass
40 else: self.fail('long int comparisons failed')
41 if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass
42 else: self.fail('float comparisons failed')
43 if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass
44 else: self.fail('string comparisons failed')
45 if None is None: pass
46 else: self.fail('identity test failed')
48 def test_float_constructor(self):
49 self.assertRaises(ValueError, float, '')
50 self.assertRaises(ValueError, float, '5\0')
52 def test_zero_division(self):
53 try: 5.0 / 0.0
54 except ZeroDivisionError: pass
55 else: self.fail("5.0 / 0.0 didn't raise ZeroDivisionError")
57 try: 5.0 // 0.0
58 except ZeroDivisionError: pass
59 else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError")
61 try: 5.0 % 0.0
62 except ZeroDivisionError: pass
63 else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError")
65 try: 5 / 0L
66 except ZeroDivisionError: pass
67 else: self.fail("5 / 0L didn't raise ZeroDivisionError")
69 try: 5 // 0L
70 except ZeroDivisionError: pass
71 else: self.fail("5 // 0L didn't raise ZeroDivisionError")
73 try: 5 % 0L
74 except ZeroDivisionError: pass
75 else: self.fail("5 % 0L didn't raise ZeroDivisionError")
77 def test_numeric_types(self):
78 if 0 != 0L or 0 != 0.0 or 0L != 0.0: self.fail('mixed comparisons')
79 if 1 != 1L or 1 != 1.0 or 1L != 1.0: self.fail('mixed comparisons')
80 if -1 != -1L or -1 != -1.0 or -1L != -1.0:
81 self.fail('int/long/float value not equal')
82 # calling built-in types without argument must return 0
83 if int() != 0: self.fail('int() does not return 0')
84 if long() != 0L: self.fail('long() does not return 0L')
85 if float() != 0.0: self.fail('float() does not return 0.0')
86 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
87 else: self.fail('int() does not round properly')
88 if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass
89 else: self.fail('long() does not round properly')
90 if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
91 else: self.fail('float() does not work properly')
93 def test_float_to_string(self):
94 def test(f, result):
95 self.assertEqual(f.__format__('e'), result)
96 self.assertEqual('%e' % f, result)
98 # test all 2 digit exponents, both with __format__ and with
99 # '%' formatting
100 for i in range(-99, 100):
101 test(float('1.5e'+str(i)), '1.500000e{0:+03d}'.format(i))
103 # test some 3 digit exponents
104 self.assertEqual(1.5e100.__format__('e'), '1.500000e+100')
105 self.assertEqual('%e' % 1.5e100, '1.500000e+100')
107 self.assertEqual(1.5e101.__format__('e'), '1.500000e+101')
108 self.assertEqual('%e' % 1.5e101, '1.500000e+101')
110 self.assertEqual(1.5e-100.__format__('e'), '1.500000e-100')
111 self.assertEqual('%e' % 1.5e-100, '1.500000e-100')
113 self.assertEqual(1.5e-101.__format__('e'), '1.500000e-101')
114 self.assertEqual('%e' % 1.5e-101, '1.500000e-101')
116 self.assertEqual('%g' % 1.0, '1')
117 self.assertEqual('%#g' % 1.0, '1.00000')
119 def test_normal_integers(self):
120 # Ensure the first 256 integers are shared
121 a = 256
122 b = 128*2
123 if a is not b: self.fail('256 is not shared')
124 if 12 + 24 != 36: self.fail('int op')
125 if 12 + (-24) != -12: self.fail('int op')
126 if (-12) + 24 != 12: self.fail('int op')
127 if (-12) + (-24) != -36: self.fail('int op')
128 if not 12 < 24: self.fail('int op')
129 if not -24 < -12: self.fail('int op')
130 # Test for a particular bug in integer multiply
131 xsize, ysize, zsize = 238, 356, 4
132 if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
133 self.fail('int mul commutativity')
134 # And another.
135 m = -sys.maxint - 1
136 for divisor in 1, 2, 4, 8, 16, 32:
137 j = m // divisor
138 prod = divisor * j
139 if prod != m:
140 self.fail("%r * %r == %r != %r" % (divisor, j, prod, m))
141 if type(prod) is not int:
142 self.fail("expected type(prod) to be int, not %r" %
143 type(prod))
144 # Check for expected * overflow to long.
145 for divisor in 1, 2, 4, 8, 16, 32:
146 j = m // divisor - 1
147 prod = divisor * j
148 if type(prod) is not long:
149 self.fail("expected type(%r) to be long, not %r" %
150 (prod, type(prod)))
151 # Check for expected * overflow to long.
152 m = sys.maxint
153 for divisor in 1, 2, 4, 8, 16, 32:
154 j = m // divisor + 1
155 prod = divisor * j
156 if type(prod) is not long:
157 self.fail("expected type(%r) to be long, not %r" %
158 (prod, type(prod)))
160 def test_long_integers(self):
161 if 12L + 24L != 36L: self.fail('long op')
162 if 12L + (-24L) != -12L: self.fail('long op')
163 if (-12L) + 24L != 12L: self.fail('long op')
164 if (-12L) + (-24L) != -36L: self.fail('long op')
165 if not 12L < 24L: self.fail('long op')
166 if not -24L < -12L: self.fail('long op')
167 x = sys.maxint
168 if int(long(x)) != x: self.fail('long op')
169 try: y = int(long(x)+1L)
170 except OverflowError: self.fail('long op')
171 if not isinstance(y, long): self.fail('long op')
172 x = -x
173 if int(long(x)) != x: self.fail('long op')
174 x = x-1
175 if int(long(x)) != x: self.fail('long op')
176 try: y = int(long(x)-1L)
177 except OverflowError: self.fail('long op')
178 if not isinstance(y, long): self.fail('long op')
180 try: 5 << -5
181 except ValueError: pass
182 else: self.fail('int negative shift <<')
184 try: 5L << -5L
185 except ValueError: pass
186 else: self.fail('long negative shift <<')
188 try: 5 >> -5
189 except ValueError: pass
190 else: self.fail('int negative shift >>')
192 try: 5L >> -5L
193 except ValueError: pass
194 else: self.fail('long negative shift >>')
196 def test_floats(self):
197 if 12.0 + 24.0 != 36.0: self.fail('float op')
198 if 12.0 + (-24.0) != -12.0: self.fail('float op')
199 if (-12.0) + 24.0 != 12.0: self.fail('float op')
200 if (-12.0) + (-24.0) != -36.0: self.fail('float op')
201 if not 12.0 < 24.0: self.fail('float op')
202 if not -24.0 < -12.0: self.fail('float op')
204 def test_strings(self):
205 if len('') != 0: self.fail('len(\'\')')
206 if len('a') != 1: self.fail('len(\'a\')')
207 if len('abcdef') != 6: self.fail('len(\'abcdef\')')
208 if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation')
209 if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3')
210 if 0*'abcde' != '': self.fail('string repetition 0*')
211 if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string')
212 if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
213 else: self.fail('in/not in string')
214 x = 'x'*103
215 if '%s!'%x != x+'!': self.fail('nasty string formatting bug')
217 #extended slices for strings
218 a = '0123456789'
219 self.assertEqual(a[::], a)
220 self.assertEqual(a[::2], '02468')
221 self.assertEqual(a[1::2], '13579')
222 self.assertEqual(a[::-1],'9876543210')
223 self.assertEqual(a[::-2], '97531')
224 self.assertEqual(a[3::-2], '31')
225 self.assertEqual(a[-100:100:], a)
226 self.assertEqual(a[100:-100:-1], a[::-1])
227 self.assertEqual(a[-100L:100L:2L], '02468')
229 if have_unicode:
230 a = unicode('0123456789', 'ascii')
231 self.assertEqual(a[::], a)
232 self.assertEqual(a[::2], unicode('02468', 'ascii'))
233 self.assertEqual(a[1::2], unicode('13579', 'ascii'))
234 self.assertEqual(a[::-1], unicode('9876543210', 'ascii'))
235 self.assertEqual(a[::-2], unicode('97531', 'ascii'))
236 self.assertEqual(a[3::-2], unicode('31', 'ascii'))
237 self.assertEqual(a[-100:100:], a)
238 self.assertEqual(a[100:-100:-1], a[::-1])
239 self.assertEqual(a[-100L:100L:2L], unicode('02468', 'ascii'))
242 def test_type_function(self):
243 self.assertRaises(TypeError, type, 1, 2)
244 self.assertRaises(TypeError, type, 1, 2, 3, 4)
246 def test_buffers(self):
247 self.assertRaises(ValueError, buffer, 'asdf', -1)
248 cmp(buffer("abc"), buffer("def")) # used to raise a warning: tp_compare didn't return -1, 0, or 1
250 self.assertRaises(TypeError, buffer, None)
252 a = buffer('asdf')
253 hash(a)
254 b = a * 5
255 if a == b:
256 self.fail('buffers should not be equal')
257 if str(b) != ('asdf' * 5):
258 self.fail('repeated buffer has wrong content')
259 if str(a * 0) != '':
260 self.fail('repeated buffer zero times has wrong content')
261 if str(a + buffer('def')) != 'asdfdef':
262 self.fail('concatenation of buffers yields wrong content')
263 if str(buffer(a)) != 'asdf':
264 self.fail('composing buffers failed')
265 if str(buffer(a, 2)) != 'df':
266 self.fail('specifying buffer offset failed')
267 if str(buffer(a, 0, 2)) != 'as':
268 self.fail('specifying buffer size failed')
269 if str(buffer(a, 1, 2)) != 'sd':
270 self.fail('specifying buffer offset and size failed')
271 self.assertRaises(ValueError, buffer, buffer('asdf', 1), -1)
272 if str(buffer(buffer('asdf', 0, 2), 0)) != 'as':
273 self.fail('composing length-specified buffer failed')
274 if str(buffer(buffer('asdf', 0, 2), 0, 5000)) != 'as':
275 self.fail('composing length-specified buffer failed')
276 if str(buffer(buffer('asdf', 0, 2), 0, -1)) != 'as':
277 self.fail('composing length-specified buffer failed')
278 if str(buffer(buffer('asdf', 0, 2), 1, 2)) != 's':
279 self.fail('composing length-specified buffer failed')
281 try: a[1] = 'g'
282 except TypeError: pass
283 else: self.fail("buffer assignment should raise TypeError")
285 try: a[0:1] = 'g'
286 except TypeError: pass
287 else: self.fail("buffer slice assignment should raise TypeError")
289 # array.array() returns an object that does not implement a char buffer,
290 # something which int() uses for conversion.
291 import array
292 try: int(buffer(array.array('c')))
293 except TypeError: pass
294 else: self.fail("char buffer (at C level) not working")
296 def test_int__format__(self):
297 def test(i, format_spec, result):
298 # just make sure I'm not accidentally checking longs
299 assert type(i) == int
300 assert type(format_spec) == str
301 self.assertEqual(i.__format__(format_spec), result)
302 self.assertEqual(i.__format__(unicode(format_spec)), result)
304 test(123456789, 'd', '123456789')
305 test(123456789, 'd', '123456789')
307 test(1, 'c', '\01')
309 # sign and aligning are interdependent
310 test(1, "-", '1')
311 test(-1, "-", '-1')
312 test(1, "-3", ' 1')
313 test(-1, "-3", ' -1')
314 test(1, "+3", ' +1')
315 test(-1, "+3", ' -1')
316 test(1, " 3", ' 1')
317 test(-1, " 3", ' -1')
318 test(1, " ", ' 1')
319 test(-1, " ", '-1')
321 # hex
322 test(3, "x", "3")
323 test(3, "X", "3")
324 test(1234, "x", "4d2")
325 test(-1234, "x", "-4d2")
326 test(1234, "8x", " 4d2")
327 test(-1234, "8x", " -4d2")
328 test(1234, "x", "4d2")
329 test(-1234, "x", "-4d2")
330 test(-3, "x", "-3")
331 test(-3, "X", "-3")
332 test(int('be', 16), "x", "be")
333 test(int('be', 16), "X", "BE")
334 test(-int('be', 16), "x", "-be")
335 test(-int('be', 16), "X", "-BE")
337 # octal
338 test(3, "o", "3")
339 test(-3, "o", "-3")
340 test(65, "o", "101")
341 test(-65, "o", "-101")
342 test(1234, "o", "2322")
343 test(-1234, "o", "-2322")
344 test(1234, "-o", "2322")
345 test(-1234, "-o", "-2322")
346 test(1234, " o", " 2322")
347 test(-1234, " o", "-2322")
348 test(1234, "+o", "+2322")
349 test(-1234, "+o", "-2322")
351 # binary
352 test(3, "b", "11")
353 test(-3, "b", "-11")
354 test(1234, "b", "10011010010")
355 test(-1234, "b", "-10011010010")
356 test(1234, "-b", "10011010010")
357 test(-1234, "-b", "-10011010010")
358 test(1234, " b", " 10011010010")
359 test(-1234, " b", "-10011010010")
360 test(1234, "+b", "+10011010010")
361 test(-1234, "+b", "-10011010010")
363 # alternate (#) formatting
364 test(0, "#b", '0b0')
365 test(0, "-#b", '0b0')
366 test(1, "-#b", '0b1')
367 test(-1, "-#b", '-0b1')
368 test(-1, "-#5b", ' -0b1')
369 test(1, "+#5b", ' +0b1')
370 test(100, "+#b", '+0b1100100')
371 test(100, "#012b", '0b0001100100')
372 test(-100, "#012b", '-0b001100100')
374 test(0, "#o", '0o0')
375 test(0, "-#o", '0o0')
376 test(1, "-#o", '0o1')
377 test(-1, "-#o", '-0o1')
378 test(-1, "-#5o", ' -0o1')
379 test(1, "+#5o", ' +0o1')
380 test(100, "+#o", '+0o144')
381 test(100, "#012o", '0o0000000144')
382 test(-100, "#012o", '-0o000000144')
384 test(0, "#x", '0x0')
385 test(0, "-#x", '0x0')
386 test(1, "-#x", '0x1')
387 test(-1, "-#x", '-0x1')
388 test(-1, "-#5x", ' -0x1')
389 test(1, "+#5x", ' +0x1')
390 test(100, "+#x", '+0x64')
391 test(100, "#012x", '0x0000000064')
392 test(-100, "#012x", '-0x000000064')
393 test(123456, "#012x", '0x000001e240')
394 test(-123456, "#012x", '-0x00001e240')
396 test(0, "#X", '0X0')
397 test(0, "-#X", '0X0')
398 test(1, "-#X", '0X1')
399 test(-1, "-#X", '-0X1')
400 test(-1, "-#5X", ' -0X1')
401 test(1, "+#5X", ' +0X1')
402 test(100, "+#X", '+0X64')
403 test(100, "#012X", '0X0000000064')
404 test(-100, "#012X", '-0X000000064')
405 test(123456, "#012X", '0X000001E240')
406 test(-123456, "#012X", '-0X00001E240')
408 # issue 5782, commas with no specifier type
409 test(1234, '010,', '00,001,234')
411 # make sure these are errors
413 # precision disallowed
414 self.assertRaises(ValueError, 3 .__format__, "1.3")
415 # sign not allowed with 'c'
416 self.assertRaises(ValueError, 3 .__format__, "+c")
417 # format spec must be string
418 self.assertRaises(TypeError, 3 .__format__, None)
419 self.assertRaises(TypeError, 3 .__format__, 0)
421 # can't have ',' with 'c'
422 self.assertRaises(ValueError, 3 .__format__, ",c")
424 # ensure that only int and float type specifiers work
425 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
426 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
427 if not format_spec in 'bcdoxXeEfFgGn%':
428 self.assertRaises(ValueError, 0 .__format__, format_spec)
429 self.assertRaises(ValueError, 1 .__format__, format_spec)
430 self.assertRaises(ValueError, (-1) .__format__, format_spec)
432 # ensure that float type specifiers work; format converts
433 # the int to a float
434 for format_spec in 'eEfFgG%':
435 for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
436 self.assertEqual(value.__format__(format_spec),
437 float(value).__format__(format_spec))
439 def test_long__format__(self):
440 def test(i, format_spec, result):
441 # make sure we're not accidentally checking ints
442 assert type(i) == long
443 assert type(format_spec) == str
444 self.assertEqual(i.__format__(format_spec), result)
445 self.assertEqual(i.__format__(unicode(format_spec)), result)
447 test(10**100, 'd', '1' + '0' * 100)
448 test(10**100+100, 'd', '1' + '0' * 97 + '100')
450 test(123456789L, 'd', '123456789')
451 test(123456789L, 'd', '123456789')
453 # sign and aligning are interdependent
454 test(1L, "-", '1')
455 test(-1L, "-", '-1')
456 test(1L, "-3", ' 1')
457 test(-1L, "-3", ' -1')
458 test(1L, "+3", ' +1')
459 test(-1L, "+3", ' -1')
460 test(1L, " 3", ' 1')
461 test(-1L, " 3", ' -1')
462 test(1L, " ", ' 1')
463 test(-1L, " ", '-1')
465 test(1L, 'c', '\01')
467 # hex
468 test(3L, "x", "3")
469 test(3L, "X", "3")
470 test(1234L, "x", "4d2")
471 test(-1234L, "x", "-4d2")
472 test(1234L, "8x", " 4d2")
473 test(-1234L, "8x", " -4d2")
474 test(1234L, "x", "4d2")
475 test(-1234L, "x", "-4d2")
476 test(-3L, "x", "-3")
477 test(-3L, "X", "-3")
478 test(long('be', 16), "x", "be")
479 test(long('be', 16), "X", "BE")
480 test(-long('be', 16), "x", "-be")
481 test(-long('be', 16), "X", "-BE")
483 # octal
484 test(3L, "o", "3")
485 test(-3L, "o", "-3")
486 test(65L, "o", "101")
487 test(-65L, "o", "-101")
488 test(1234L, "o", "2322")
489 test(-1234L, "o", "-2322")
490 test(1234L, "-o", "2322")
491 test(-1234L, "-o", "-2322")
492 test(1234L, " o", " 2322")
493 test(-1234L, " o", "-2322")
494 test(1234L, "+o", "+2322")
495 test(-1234L, "+o", "-2322")
497 # binary
498 test(3L, "b", "11")
499 test(-3L, "b", "-11")
500 test(1234L, "b", "10011010010")
501 test(-1234L, "b", "-10011010010")
502 test(1234L, "-b", "10011010010")
503 test(-1234L, "-b", "-10011010010")
504 test(1234L, " b", " 10011010010")
505 test(-1234L, " b", "-10011010010")
506 test(1234L, "+b", "+10011010010")
507 test(-1234L, "+b", "-10011010010")
509 # make sure these are errors
511 # precision disallowed
512 self.assertRaises(ValueError, 3L .__format__, "1.3")
513 # sign not allowed with 'c'
514 self.assertRaises(ValueError, 3L .__format__, "+c")
515 # format spec must be string
516 self.assertRaises(TypeError, 3L .__format__, None)
517 self.assertRaises(TypeError, 3L .__format__, 0)
518 # alternate specifier in wrong place
519 self.assertRaises(ValueError, 1L .__format__, "#+5x")
520 self.assertRaises(ValueError, 1L .__format__, "+5#x")
522 # ensure that only int and float type specifiers work
523 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
524 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
525 if not format_spec in 'bcdoxXeEfFgGn%':
526 self.assertRaises(ValueError, 0L .__format__, format_spec)
527 self.assertRaises(ValueError, 1L .__format__, format_spec)
528 self.assertRaises(ValueError, (-1L) .__format__, format_spec)
530 # ensure that float type specifiers work; format converts
531 # the long to a float
532 for format_spec in 'eEfFgG%':
533 for value in [0L, 1L, -1L, 100L, -100L, 1234567890L, -1234567890L]:
534 self.assertEqual(value.__format__(format_spec),
535 float(value).__format__(format_spec))
537 @run_with_locale('LC_NUMERIC', 'en_US.UTF8')
538 def test_float__format__locale(self):
539 # test locale support for __format__ code 'n'
541 for i in range(-10, 10):
542 x = 1234567890.0 * (10.0 ** i)
543 self.assertEqual(locale.format('%g', x, grouping=True), format(x, 'n'))
544 self.assertEqual(locale.format('%.10g', x, grouping=True), format(x, '.10n'))
546 @run_with_locale('LC_NUMERIC', 'en_US.UTF8')
547 def test_int__format__locale(self):
548 # test locale support for __format__ code 'n' for integers
550 x = 123456789012345678901234567890
551 for i in range(0, 30):
552 self.assertEqual(locale.format('%d', x, grouping=True), format(x, 'n'))
554 # move to the next integer to test
555 x = x // 10
557 rfmt = ">20n"
558 lfmt = "<20n"
559 cfmt = "^20n"
560 for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900):
561 self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt)))
562 self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt)))
563 self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt)))
565 def test_float__format__(self):
566 # these should be rewritten to use both format(x, spec) and
567 # x.__format__(spec)
569 def test(f, format_spec, result):
570 assert type(f) == float
571 assert type(format_spec) == str
572 self.assertEqual(f.__format__(format_spec), result)
573 self.assertEqual(f.__format__(unicode(format_spec)), result)
575 test(0.0, 'f', '0.000000')
577 # the default is 'g', except for empty format spec
578 test(0.0, '', '0.0')
579 test(0.01, '', '0.01')
580 test(0.01, 'g', '0.01')
582 # test for issue 3411
583 test(1.23, '1', '1.23')
584 test(-1.23, '1', '-1.23')
585 test(1.23, '1g', '1.23')
586 test(-1.23, '1g', '-1.23')
588 test( 1.0, ' g', ' 1')
589 test(-1.0, ' g', '-1')
590 test( 1.0, '+g', '+1')
591 test(-1.0, '+g', '-1')
592 test(1.1234e200, 'g', '1.1234e+200')
593 test(1.1234e200, 'G', '1.1234E+200')
596 test(1.0, 'f', '1.000000')
598 test(-1.0, 'f', '-1.000000')
600 test( 1.0, ' f', ' 1.000000')
601 test(-1.0, ' f', '-1.000000')
602 test( 1.0, '+f', '+1.000000')
603 test(-1.0, '+f', '-1.000000')
605 # Python versions <= 2.6 switched from 'f' to 'g' formatting for
606 # values larger than 1e50. No longer.
607 f = 1.1234e90
608 for fmt in 'f', 'F':
609 # don't do a direct equality check, since on some
610 # platforms only the first few digits of dtoa
611 # will be reliable
612 result = f.__format__(fmt)
613 self.assertEqual(len(result), 98)
614 self.assertEqual(result[-7], '.')
615 self.assertTrue(result[:12] in ('112340000000', '112339999999'))
616 f = 1.1234e200
617 for fmt in 'f', 'F':
618 result = f.__format__(fmt)
619 self.assertEqual(len(result), 208)
620 self.assertEqual(result[-7], '.')
621 self.assertTrue(result[:12] in ('112340000000', '112339999999'))
624 test( 1.0, 'e', '1.000000e+00')
625 test(-1.0, 'e', '-1.000000e+00')
626 test( 1.0, 'E', '1.000000E+00')
627 test(-1.0, 'E', '-1.000000E+00')
628 test(1.1234e20, 'e', '1.123400e+20')
629 test(1.1234e20, 'E', '1.123400E+20')
631 # No format code means use g, but must have a decimal
632 # and a number after the decimal. This is tricky, because
633 # a totaly empty format specifier means something else.
634 # So, just use a sign flag
635 test(1e200, '+g', '+1e+200')
636 test(1e200, '+', '+1e+200')
637 test(1.1e200, '+g', '+1.1e+200')
638 test(1.1e200, '+', '+1.1e+200')
640 test(1.1e200, '+g', '+1.1e+200')
641 test(1.1e200, '+', '+1.1e+200')
643 # 0 padding
644 test(1234., '010f', '1234.000000')
645 test(1234., '011f', '1234.000000')
646 test(1234., '012f', '01234.000000')
647 test(-1234., '011f', '-1234.000000')
648 test(-1234., '012f', '-1234.000000')
649 test(-1234., '013f', '-01234.000000')
650 test(-1234.12341234, '013f', '-01234.123412')
651 test(-123456.12341234, '011.2f', '-0123456.12')
653 # issue 5782, commas with no specifier type
654 test(1.2, '010,.2', '0,000,001.2')
656 # 0 padding with commas
657 test(1234., '011,f', '1,234.000000')
658 test(1234., '012,f', '1,234.000000')
659 test(1234., '013,f', '01,234.000000')
660 test(-1234., '012,f', '-1,234.000000')
661 test(-1234., '013,f', '-1,234.000000')
662 test(-1234., '014,f', '-01,234.000000')
663 test(-12345., '015,f', '-012,345.000000')
664 test(-123456., '016,f', '-0,123,456.000000')
665 test(-123456., '017,f', '-0,123,456.000000')
666 test(-123456.12341234, '017,f', '-0,123,456.123412')
667 test(-123456.12341234, '013,.2f', '-0,123,456.12')
669 # % formatting
670 test(-1.0, '%', '-100.000000%')
672 # format spec must be string
673 self.assertRaises(TypeError, 3.0.__format__, None)
674 self.assertRaises(TypeError, 3.0.__format__, 0)
676 # other format specifiers shouldn't work on floats,
677 # in particular int specifiers
678 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
679 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
680 if not format_spec in 'eEfFgGn%':
681 self.assertRaises(ValueError, format, 0.0, format_spec)
682 self.assertRaises(ValueError, format, 1.0, format_spec)
683 self.assertRaises(ValueError, format, -1.0, format_spec)
684 self.assertRaises(ValueError, format, 1e100, format_spec)
685 self.assertRaises(ValueError, format, -1e100, format_spec)
686 self.assertRaises(ValueError, format, 1e-100, format_spec)
687 self.assertRaises(ValueError, format, -1e-100, format_spec)
689 # Alternate formatting is not supported
690 self.assertRaises(ValueError, format, 0.0, '#')
691 self.assertRaises(ValueError, format, 0.0, '#20f')
693 def test_format_spec_errors(self):
694 # int, float, and string all share the same format spec
695 # mini-language parser.
697 # Check that we can't ask for too many digits. This is
698 # probably a CPython specific test. It tries to put the width
699 # into a C long.
700 self.assertRaises(ValueError, format, 0, '1'*10000 + 'd')
702 # Similar with the precision.
703 self.assertRaises(ValueError, format, 0, '.' + '1'*10000 + 'd')
705 # And may as well test both.
706 self.assertRaises(ValueError, format, 0, '1'*1000 + '.' + '1'*10000 + 'd')
708 # Make sure commas aren't allowed with various type codes
709 for code in 'xXobns':
710 self.assertRaises(ValueError, format, 0, ',' + code)
712 def test_main():
713 run_unittest(TypesTests)
715 if __name__ == '__main__':
716 test_main()