annotate Lib/test/test_builtin.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 93223603c992
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1 # Python test set -- built-in functions
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
2
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
3 import test.test_support, unittest
38055
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 37473
diff changeset
4 from test.test_support import fcmp, have_unicode, TESTFN, unlink, \
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 37473
diff changeset
5 run_unittest, run_with_locale
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
6 from operator import neg
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
7
45275
a50c1fb7d920 Rename rational.Rational to fractions.Fraction, to avoid name clash
Mark Dickinson <dickinsm@gmail.com>
parents: 45142
diff changeset
8 import sys, warnings, cStringIO, random, fractions, UserDict
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
9 warnings.filterwarnings("ignore", "hex../oct.. of negative int",
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
10 FutureWarning, __name__)
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
11 warnings.filterwarnings("ignore", "integer argument expected",
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
12 DeprecationWarning, "unittest")
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
13
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
14 # count the number of test runs.
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
15 # used to skip running test_execfile() multiple times
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
16 numruns = 0
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
17
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
18 class Squares:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
19
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
20 def __init__(self, max):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
21 self.max = max
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
22 self.sofar = []
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
23
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
24 def __len__(self): return len(self.sofar)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
25
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
26 def __getitem__(self, i):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
27 if not 0 <= i < self.max: raise IndexError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
28 n = len(self.sofar)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
29 while n <= i:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
30 self.sofar.append(n*n)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
31 n += 1
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
32 return self.sofar[i]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
33
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
34 class StrSquares:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
35
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
36 def __init__(self, max):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
37 self.max = max
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
38 self.sofar = []
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
39
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
40 def __len__(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
41 return len(self.sofar)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
42
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
43 def __getitem__(self, i):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
44 if not 0 <= i < self.max:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
45 raise IndexError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
46 n = len(self.sofar)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
47 while n <= i:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
48 self.sofar.append(str(n*n))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
49 n += 1
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
50 return self.sofar[i]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
51
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
52 class BitBucket:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
53 def write(self, line):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
54 pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
55
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
56
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
57 class TestFailingBool:
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
58 def __nonzero__(self):
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
59 raise RuntimeError
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
60
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
61 class TestFailingIter:
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
62 def __iter__(self):
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
63 raise RuntimeError
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
64
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
65 class BuiltinTest(unittest.TestCase):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
66
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
67 def test_import(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
68 __import__('sys')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
69 __import__('time')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
70 __import__('string')
41168
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41125
diff changeset
71 __import__(name='sys')
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41125
diff changeset
72 __import__(name='time', level=0)
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
73 self.assertRaises(ImportError, __import__, 'spamspam')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
74 self.assertRaises(TypeError, __import__, 1, 2, 3, 4)
37473
20ffc2ca03e0 Fix __import__("") to raise ValueError rather than return None.
Thomas Wouters <thomas@python.org>
parents: 37344
diff changeset
75 self.assertRaises(ValueError, __import__, '')
41168
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41125
diff changeset
76 self.assertRaises(TypeError, __import__, 'sys', name='sys')
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
77
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
78 def test_abs(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
79 # int
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
80 self.assertEqual(abs(0), 0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
81 self.assertEqual(abs(1234), 1234)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
82 self.assertEqual(abs(-1234), 1234)
39984
ca887d55c592 Fix integer negation and absolute value to not rely
Martin v. Löwis <martin@v.loewis.de>
parents: 39415
diff changeset
83 self.assertTrue(abs(-sys.maxint-1) > 0)
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
84 # float
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
85 self.assertEqual(abs(0.0), 0.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
86 self.assertEqual(abs(3.14), 3.14)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
87 self.assertEqual(abs(-3.14), 3.14)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
88 # long
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
89 self.assertEqual(abs(0L), 0L)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
90 self.assertEqual(abs(1234L), 1234L)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
91 self.assertEqual(abs(-1234L), 1234L)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
92 # str
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
93 self.assertRaises(TypeError, abs, 'a')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
94
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
95 def test_all(self):
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
96 self.assertEqual(all([2, 4, 6]), True)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
97 self.assertEqual(all([2, None, 6]), False)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
98 self.assertRaises(RuntimeError, all, [2, TestFailingBool(), 6])
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
99 self.assertRaises(RuntimeError, all, TestFailingIter())
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
100 self.assertRaises(TypeError, all, 10) # Non-iterable
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
101 self.assertRaises(TypeError, all) # No args
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
102 self.assertRaises(TypeError, all, [2, 4, 6], []) # Too many args
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
103 self.assertEqual(all([]), True) # Empty iterator
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
104 S = [50, 60]
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
105 self.assertEqual(all(x > 42 for x in S), True)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
106 S = [50, 40, 60]
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
107 self.assertEqual(all(x > 42 for x in S), False)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
108
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
109 def test_any(self):
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
110 self.assertEqual(any([None, None, None]), False)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
111 self.assertEqual(any([None, 4, None]), True)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
112 self.assertRaises(RuntimeError, any, [None, TestFailingBool(), 6])
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
113 self.assertRaises(RuntimeError, all, TestFailingIter())
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
114 self.assertRaises(TypeError, any, 10) # Non-iterable
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
115 self.assertRaises(TypeError, any) # No args
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
116 self.assertRaises(TypeError, any, [2, 4, 6], []) # Too many args
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
117 self.assertEqual(any([]), False) # Empty iterator
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
118 S = [40, 60, 30]
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
119 self.assertEqual(any(x > 42 for x in S), True)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
120 S = [10, 20, 30]
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
121 self.assertEqual(any(x > 42 for x in S), False)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34422
diff changeset
122
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
123 def test_neg(self):
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
124 x = -sys.maxint-1
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
125 self.assert_(isinstance(x, int))
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
126 self.assertEqual(-x, sys.maxint+1)
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
127
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
128 def test_apply(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
129 def f0(*args):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
130 self.assertEqual(args, ())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
131 def f1(a1):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
132 self.assertEqual(a1, 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
133 def f2(a1, a2):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
134 self.assertEqual(a1, 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
135 self.assertEqual(a2, 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
136 def f3(a1, a2, a3):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
137 self.assertEqual(a1, 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
138 self.assertEqual(a2, 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
139 self.assertEqual(a3, 3)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
140 apply(f0, ())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
141 apply(f1, (1,))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
142 apply(f2, (1, 2))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
143 apply(f3, (1, 2, 3))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
144
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
145 # A PyCFunction that takes only positional parameters should allow an
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
146 # empty keyword dictionary to pass without a complaint, but raise a
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
147 # TypeError if the dictionary is non-empty.
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
148 apply(id, (1,), {})
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
149 self.assertRaises(TypeError, apply, id, (1,), {"foo": 1})
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
150 self.assertRaises(TypeError, apply)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
151 self.assertRaises(TypeError, apply, id, 42)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
152 self.assertRaises(TypeError, apply, id, (42,), 42)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
153
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
154 def test_callable(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
155 self.assert_(callable(len))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
156 def f(): pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
157 self.assert_(callable(f))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
158 class C:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
159 def meth(self): pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
160 self.assert_(callable(C))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
161 x = C()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
162 self.assert_(callable(x.meth))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
163 self.assert_(not callable(x))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
164 class D(C):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
165 def __call__(self): pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
166 y = D()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
167 self.assert_(callable(y))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
168 y()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
169
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
170 def test_chr(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
171 self.assertEqual(chr(32), ' ')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
172 self.assertEqual(chr(65), 'A')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
173 self.assertEqual(chr(97), 'a')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
174 self.assertEqual(chr(0xff), '\xff')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
175 self.assertRaises(ValueError, chr, 256)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
176 self.assertRaises(TypeError, chr)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
177
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
178 def test_cmp(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
179 self.assertEqual(cmp(-1, 1), -1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
180 self.assertEqual(cmp(1, -1), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
181 self.assertEqual(cmp(1, 1), 0)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 30167
diff changeset
182 # verify that circular objects are not handled
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
183 a = []; a.append(a)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
184 b = []; b.append(b)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
185 from UserList import UserList
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
186 c = UserList(); c.append(c)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 30167
diff changeset
187 self.assertRaises(RuntimeError, cmp, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 30167
diff changeset
188 self.assertRaises(RuntimeError, cmp, b, c)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 30167
diff changeset
189 self.assertRaises(RuntimeError, cmp, c, a)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 30167
diff changeset
190 self.assertRaises(RuntimeError, cmp, a, c)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 30167
diff changeset
191 # okay, now break the cycles
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
192 a.pop(); b.pop(); c.pop()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
193 self.assertRaises(TypeError, cmp)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
194
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
195 def test_coerce(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
196 self.assert_(not fcmp(coerce(1, 1.1), (1.0, 1.1)))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
197 self.assertEqual(coerce(1, 1L), (1L, 1L))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
198 self.assert_(not fcmp(coerce(1L, 1.1), (1.0, 1.1)))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
199 self.assertRaises(TypeError, coerce)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
200 class BadNumber:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
201 def __coerce__(self, other):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
202 raise ValueError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
203 self.assertRaises(ValueError, coerce, 42, BadNumber())
27484
5ba7934a7ba0 Fix SF bug# 676155, RuntimeWarning with tp_compare
Neal Norwitz <nnorwitz@gmail.com>
parents: 27415
diff changeset
204 self.assertRaises(OverflowError, coerce, 0.5, int("12345" * 1000))
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
205
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
206 def test_compile(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
207 compile('print 1\n', '', 'exec')
27772
3566b84e66b9 patch 680474 that fixes bug 679880: compile/eval/exec refused utf-8 bom
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
208 bom = '\xef\xbb\xbf'
3566b84e66b9 patch 680474 that fixes bug 679880: compile/eval/exec refused utf-8 bom
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
209 compile(bom + 'print 1\n', '', 'exec')
41168
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41125
diff changeset
210 compile(source='pass', filename='?', mode='exec')
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41125
diff changeset
211 compile(dont_inherit=0, filename='tmp', source='0', mode='eval')
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41125
diff changeset
212 compile('pass', '?', dont_inherit=1, mode='exec')
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
213 self.assertRaises(TypeError, compile)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
214 self.assertRaises(ValueError, compile, 'print 42\n', '<string>', 'badmode')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
215 self.assertRaises(ValueError, compile, 'print 42\n', '<string>', 'single', 0xff)
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
216 self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
41168
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41125
diff changeset
217 self.assertRaises(TypeError, compile, 'pass', '?', 'exec',
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41125
diff changeset
218 mode='eval', source='0', filename='tmp')
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27772
diff changeset
219 if have_unicode:
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27772
diff changeset
220 compile(unicode('print u"\xc3\xa5"\n', 'utf8'), '', 'exec')
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
221 self.assertRaises(TypeError, compile, unichr(0), 'f', 'exec')
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
222 self.assertRaises(ValueError, compile, unicode('a = 1'), 'f', 'bad')
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
223
41168
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41125
diff changeset
224
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
225 def test_delattr(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
226 import sys
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
227 sys.spam = 1
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
228 delattr(sys, 'spam')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
229 self.assertRaises(TypeError, delattr)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
230
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
231 def test_dir(self):
41125
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
232 # dir(wrong number of arguments)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
233 self.assertRaises(TypeError, dir, 42, 42)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
234
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
235 # dir() - local scope
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
236 local_var = 1
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
237 self.assert_('local_var' in dir())
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
238
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
239 # dir(module)
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
240 import sys
41125
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
241 self.assert_('exit' in dir(sys))
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
242
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
243 # dir(module_with_invalid__dict__)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
244 import types
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
245 class Foo(types.ModuleType):
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
246 __dict__ = 8
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
247 f = Foo("foo")
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
248 self.assertRaises(TypeError, dir, f)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
249
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
250 # dir(type)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
251 self.assert_("strip" in dir(str))
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
252 self.assert_("__mro__" not in dir(str))
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
253
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
254 # dir(obj)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
255 class Foo(object):
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
256 def __init__(self):
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
257 self.x = 7
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
258 self.y = 8
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
259 self.z = 9
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
260 f = Foo()
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
261 self.assert_("y" in dir(f))
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
262
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
263 # dir(obj_no__dict__)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
264 class Foo(object):
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
265 __slots__ = []
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
266 f = Foo()
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
267 self.assert_("__repr__" in dir(f))
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
268
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
269 # dir(obj_no__class__with__dict__)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
270 # (an ugly trick to cause getattr(f, "__class__") to fail)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
271 class Foo(object):
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
272 __slots__ = ["__class__", "__dict__"]
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
273 def __init__(self):
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
274 self.bar = "wow"
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
275 f = Foo()
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
276 self.assert_("__repr__" not in dir(f))
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
277 self.assert_("bar" in dir(f))
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
278
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
279 # dir(obj_using __dir__)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
280 class Foo(object):
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
281 def __dir__(self):
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
282 return ["kan", "ga", "roo"]
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
283 f = Foo()
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
284 self.assert_(dir(f) == ["ga", "kan", "roo"])
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
285
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
286 # dir(obj__dir__not_list)
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
287 class Foo(object):
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
288 def __dir__(self):
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
289 return 7
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
290 f = Foo()
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
291 self.assertRaises(TypeError, dir, f)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
293 def test_divmod(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
294 self.assertEqual(divmod(12, 7), (1, 5))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
295 self.assertEqual(divmod(-12, 7), (-2, 2))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
296 self.assertEqual(divmod(12, -7), (-2, -2))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
297 self.assertEqual(divmod(-12, -7), (1, -5))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
298
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
299 self.assertEqual(divmod(12L, 7L), (1L, 5L))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
300 self.assertEqual(divmod(-12L, 7L), (-2L, 2L))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
301 self.assertEqual(divmod(12L, -7L), (-2L, -2L))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
302 self.assertEqual(divmod(-12L, -7L), (1L, -5L))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
304 self.assertEqual(divmod(12, 7L), (1, 5L))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
305 self.assertEqual(divmod(-12, 7L), (-2, 2L))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
306 self.assertEqual(divmod(12L, -7), (-2L, -2))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
307 self.assertEqual(divmod(-12L, -7), (1L, -5))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
308
33629
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
309 self.assertEqual(divmod(-sys.maxint-1, -1),
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
310 (sys.maxint+1, 0))
c84dbe717d49 Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
311
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
312 self.assert_(not fcmp(divmod(3.25, 1.0), (3.0, 0.25)))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
313 self.assert_(not fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
314 self.assert_(not fcmp(divmod(3.25, -1.0), (-4.0, -0.75)))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
315 self.assert_(not fcmp(divmod(-3.25, -1.0), (3.0, -0.25)))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
316
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
317 self.assertRaises(TypeError, divmod)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
318
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
319 def test_eval(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
320 self.assertEqual(eval('1+1'), 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
321 self.assertEqual(eval(' 1+1\n'), 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
322 globals = {'a': 1, 'b': 2}
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
323 locals = {'b': 200, 'c': 300}
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
324 self.assertEqual(eval('a', globals) , 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
325 self.assertEqual(eval('a', globals, locals), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
326 self.assertEqual(eval('b', globals, locals), 200)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
327 self.assertEqual(eval('c', globals, locals), 300)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
328 if have_unicode:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
329 self.assertEqual(eval(unicode('1+1')), 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
330 self.assertEqual(eval(unicode(' 1+1\n')), 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
331 globals = {'a': 1, 'b': 2}
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
332 locals = {'b': 200, 'c': 300}
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
333 if have_unicode:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
334 self.assertEqual(eval(unicode('a'), globals), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
335 self.assertEqual(eval(unicode('a'), globals, locals), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
336 self.assertEqual(eval(unicode('b'), globals, locals), 200)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
337 self.assertEqual(eval(unicode('c'), globals, locals), 300)
27772
3566b84e66b9 patch 680474 that fixes bug 679880: compile/eval/exec refused utf-8 bom
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
338 bom = '\xef\xbb\xbf'
3566b84e66b9 patch 680474 that fixes bug 679880: compile/eval/exec refused utf-8 bom
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
339 self.assertEqual(eval(bom + 'a', globals, locals), 1)
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27772
diff changeset
340 self.assertEqual(eval(unicode('u"\xc3\xa5"', 'utf8'), globals),
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27772
diff changeset
341 unicode('\xc3\xa5', 'utf8'))
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
342 self.assertRaises(TypeError, eval)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
343 self.assertRaises(TypeError, eval, ())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
344
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
345 def test_general_eval(self):
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
346 # Tests that general mappings can be used for the locals argument
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
347
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
348 class M:
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
349 "Test mapping interface versus possible calls from eval()."
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
350 def __getitem__(self, key):
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
351 if key == 'a':
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
352 return 12
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
353 raise KeyError
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
354 def keys(self):
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
355 return list('xyz')
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
356
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
357 m = M()
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
358 g = globals()
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
359 self.assertEqual(eval('a', g, m), 12)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
360 self.assertRaises(NameError, eval, 'b', g, m)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
361 self.assertEqual(eval('dir()', g, m), list('xyz'))
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
362 self.assertEqual(eval('globals()', g, m), g)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
363 self.assertEqual(eval('locals()', g, m), m)
32507
682f74088602 * Fix missing return after error message is set.
Raymond Hettinger <python@rcn.com>
parents: 32463
diff changeset
364 self.assertRaises(TypeError, eval, 'a', m)
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
365 class A:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
366 "Non-mapping"
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
367 pass
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
368 m = A()
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
369 self.assertRaises(TypeError, eval, 'a', g, m)
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
370
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
371 # Verify that dict subclasses work as well
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
372 class D(dict):
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
373 def __getitem__(self, key):
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
374 if key == 'a':
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
375 return 12
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
376 return dict.__getitem__(self, key)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
377 def keys(self):
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
378 return list('xyz')
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
379
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
380 d = D()
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
381 self.assertEqual(eval('a', g, d), 12)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
382 self.assertRaises(NameError, eval, 'b', g, d)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
383 self.assertEqual(eval('dir()', g, d), list('xyz'))
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
384 self.assertEqual(eval('globals()', g, d), g)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
385 self.assertEqual(eval('locals()', g, d), d)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
386
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
387 # Verify locals stores (used by list comps)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
388 eval('[locals() for i in (2,3)]', g, d)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
389 eval('[locals() for i in (2,3)]', g, UserDict.UserDict())
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
390
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
391 class SpreadSheet:
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
392 "Sample application showing nested, calculated lookups."
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
393 _cells = {}
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
394 def __setitem__(self, key, formula):
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
395 self._cells[key] = formula
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 38283
diff changeset
396 def __getitem__(self, key):
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
397 return eval(self._cells[key], globals(), self)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
398
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
399 ss = SpreadSheet()
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
400 ss['a1'] = '5'
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
401 ss['a2'] = 'a1*6'
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
402 ss['a3'] = 'a2*7'
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
403 self.assertEqual(ss['a3'], 210)
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
404
32981
50c122bc7aac SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents: 32890
diff changeset
405 # Verify that dir() catches a non-list returned by eval
50c122bc7aac SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents: 32890
diff changeset
406 # SF bug #1004669
50c122bc7aac SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents: 32890
diff changeset
407 class C:
50c122bc7aac SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents: 32890
diff changeset
408 def __getitem__(self, item):
50c122bc7aac SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents: 32890
diff changeset
409 raise KeyError(item)
50c122bc7aac SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents: 32890
diff changeset
410 def keys(self):
50c122bc7aac SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents: 32890
diff changeset
411 return 'a'
50c122bc7aac SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents: 32890
diff changeset
412 self.assertRaises(TypeError, eval, 'dir()', globals(), C())
50c122bc7aac SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents: 32890
diff changeset
413
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
414 # Done outside of the method test_z to get the correct scope
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
415 z = 0
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
416 f = open(TESTFN, 'w')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
417 f.write('z = z+1\n')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
418 f.write('z = z*2\n')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
419 f.close()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
420 execfile(TESTFN)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
421
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
422 def test_execfile(self):
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
423 global numruns
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
424 if numruns:
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
425 return
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
426 numruns += 1
34131
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
427
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
428 globals = {'a': 1, 'b': 2}
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
429 locals = {'b': 200, 'c': 300}
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
430
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
431 self.assertEqual(self.__class__.z, 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
432 globals['z'] = 0
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
433 execfile(TESTFN, globals)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
434 self.assertEqual(globals['z'], 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
435 locals['z'] = 0
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
436 execfile(TESTFN, globals, locals)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
437 self.assertEqual(locals['z'], 2)
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
438
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
439 class M:
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
440 "Test mapping interface versus possible calls from execfile()."
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
441 def __init__(self):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
442 self.z = 10
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
443 def __getitem__(self, key):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
444 if key == 'z':
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
445 return self.z
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
446 raise KeyError
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
447 def __setitem__(self, key, value):
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
448 if key == 'z':
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
449 self.z = value
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
450 return
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
451 raise KeyError
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
452
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
453 locals = M()
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
454 locals['z'] = 0
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
455 execfile(TESTFN, globals, locals)
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
456 self.assertEqual(locals['z'], 2)
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32507
diff changeset
457
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
458 unlink(TESTFN)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
459 self.assertRaises(TypeError, execfile)
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
460 self.assertRaises(TypeError, execfile, TESTFN, {}, ())
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
461 import os
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
462 self.assertRaises(IOError, execfile, os.curdir)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
463 self.assertRaises(IOError, execfile, "I_dont_exist")
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
464
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
465 def test_filter(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
466 self.assertEqual(filter(lambda c: 'a' <= c <= 'z', 'Hello World'), 'elloorld')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
467 self.assertEqual(filter(None, [1, 'hello', [], [3], '', None, 9, 0]), [1, 'hello', [3], 9])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
468 self.assertEqual(filter(lambda x: x > 0, [1, -3, 9, 0, 2]), [1, 9, 2])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
469 self.assertEqual(filter(None, Squares(10)), [1, 4, 9, 16, 25, 36, 49, 64, 81])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
470 self.assertEqual(filter(lambda x: x%2, Squares(10)), [1, 9, 25, 49, 81])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
471 def identity(item):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
472 return 1
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
473 filter(identity, Squares(5))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
474 self.assertRaises(TypeError, filter)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
475 class BadSeq(object):
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
476 def __getitem__(self, index):
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
477 if index<4:
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
478 return 42
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
479 raise ValueError
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
480 self.assertRaises(ValueError, filter, lambda x: x, BadSeq())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
481 def badfunc():
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
482 pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
483 self.assertRaises(TypeError, filter, badfunc, range(5))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
484
27415
2742ea93ab23 Fix comment typos
Walter Dörwald <walter@livinglogic.de>
parents: 27385
diff changeset
485 # test bltinmodule.c::filtertuple()
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
486 self.assertEqual(filter(None, (1, 2)), (1, 2))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
487 self.assertEqual(filter(lambda x: x>=3, (1, 2, 3, 4)), (3, 4))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
488 self.assertRaises(TypeError, filter, 42, (1, 2))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
489
27415
2742ea93ab23 Fix comment typos
Walter Dörwald <walter@livinglogic.de>
parents: 27385
diff changeset
490 # test bltinmodule.c::filterstring()
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
491 self.assertEqual(filter(None, "12"), "12")
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
492 self.assertEqual(filter(lambda x: x>="3", "1234"), "34")
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
493 self.assertRaises(TypeError, filter, 42, "12")
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
494 class badstr(str):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
495 def __getitem__(self, index):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
496 raise ValueError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
497 self.assertRaises(ValueError, filter, lambda x: x >="3", badstr("1234"))
27415
2742ea93ab23 Fix comment typos
Walter Dörwald <walter@livinglogic.de>
parents: 27385
diff changeset
498
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
499 class badstr2(str):
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
500 def __getitem__(self, index):
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
501 return 42
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
502 self.assertRaises(TypeError, filter, lambda x: x >=42, badstr2("1234"))
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
503
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
504 class weirdstr(str):
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
505 def __getitem__(self, index):
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
506 return weirdstr(2*str.__getitem__(self, index))
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
507 self.assertEqual(filter(lambda x: x>="33", weirdstr("1234")), "3344")
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
508
27676
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
509 class shiftstr(str):
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
510 def __getitem__(self, index):
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
511 return chr(ord(str.__getitem__(self, index))+1)
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
512 self.assertEqual(filter(lambda x: x>="3", shiftstr("1234")), "345")
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
513
27385
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27303
diff changeset
514 if have_unicode:
27415
2742ea93ab23 Fix comment typos
Walter Dörwald <walter@livinglogic.de>
parents: 27385
diff changeset
515 # test bltinmodule.c::filterunicode()
27385
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27303
diff changeset
516 self.assertEqual(filter(None, unicode("12")), unicode("12"))
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27303
diff changeset
517 self.assertEqual(filter(lambda x: x>="3", unicode("1234")), unicode("34"))
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27303
diff changeset
518 self.assertRaises(TypeError, filter, 42, unicode("12"))
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27303
diff changeset
519 self.assertRaises(ValueError, filter, lambda x: x >="3", badstr(unicode("1234")))
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
520
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
521 class badunicode(unicode):
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
522 def __getitem__(self, index):
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
523 return 42
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
524 self.assertRaises(TypeError, filter, lambda x: x >=42, badunicode("1234"))
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
525
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
526 class weirdunicode(unicode):
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
527 def __getitem__(self, index):
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
528 return weirdunicode(2*unicode.__getitem__(self, index))
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
529 self.assertEqual(
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
530 filter(lambda x: x>=unicode("33"), weirdunicode("1234")), unicode("3344"))
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27484
diff changeset
531
27676
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
532 class shiftunicode(unicode):
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
533 def __getitem__(self, index):
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
534 return unichr(ord(unicode.__getitem__(self, index))+1)
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
535 self.assertEqual(
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
536 filter(lambda x: x>=unicode("3"), shiftunicode("1234")),
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
537 unicode("345")
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
538 )
95f3a1cd00e1 Add a test that checks that filter() honors the sq_item slot for
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
539
27681
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27676
diff changeset
540 def test_filter_subclasses(self):
30167
dc4249dd9e35 Make a copy of L before appending, so the global L remains
Walter Dörwald <walter@livinglogic.de>
parents: 30037
diff changeset
541 # test that filter() never returns tuple, str or unicode subclasses
dc4249dd9e35 Make a copy of L before appending, so the global L remains
Walter Dörwald <walter@livinglogic.de>
parents: 30037
diff changeset
542 # and that the result always goes through __getitem__
dc4249dd9e35 Make a copy of L before appending, so the global L remains
Walter Dörwald <walter@livinglogic.de>
parents: 30037
diff changeset
543 funcs = (None, bool, lambda x: True)
27681
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27676
diff changeset
544 class tuple2(tuple):
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
545 def __getitem__(self, index):
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
546 return 2*tuple.__getitem__(self, index)
27681
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27676
diff changeset
547 class str2(str):
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
548 def __getitem__(self, index):
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
549 return 2*str.__getitem__(self, index)
27681
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27676
diff changeset
550 inputs = {
27795
2042e2051d97 Change filtertuple() to use tp_as_sequence->sq_item
Walter Dörwald <walter@livinglogic.de>
parents: 27786
diff changeset
551 tuple2: {(): (), (1, 2, 3): (2, 4, 6)},
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
552 str2: {"": "", "123": "112233"}
27681
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27676
diff changeset
553 }
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27676
diff changeset
554 if have_unicode:
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27676
diff changeset
555 class unicode2(unicode):
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
556 def __getitem__(self, index):
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
557 return 2*unicode.__getitem__(self, index)
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
558 inputs[unicode2] = {
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
559 unicode(): unicode(),
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
560 unicode("123"): unicode("112233")
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
561 }
27681
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27676
diff changeset
562
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
563 for (cls, inps) in inputs.iteritems():
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
564 for (inp, exp) in inps.iteritems():
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
565 # make sure the output goes through __getitem__
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
566 # even if func is None
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
567 self.assertEqual(
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
568 filter(funcs[0], cls(inp)),
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
569 filter(funcs[1], cls(inp))
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
570 )
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
571 for func in funcs:
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
572 outp = filter(func, cls(inp))
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
573 self.assertEqual(outp, exp)
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27781
diff changeset
574 self.assert_(not isinstance(outp, cls))
27681
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27676
diff changeset
575
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
576 def test_getattr(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
577 import sys
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
578 self.assert_(getattr(sys, 'stdout') is sys.stdout)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
579 self.assertRaises(TypeError, getattr, sys, 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
580 self.assertRaises(TypeError, getattr, sys, 1, "foo")
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
581 self.assertRaises(TypeError, getattr)
35309
5fe9ceeca111 Disable a few other tests, that can't work if Python is compiled without
Walter Dörwald <walter@livinglogic.de>
parents: 34931
diff changeset
582 if have_unicode:
5fe9ceeca111 Disable a few other tests, that can't work if Python is compiled without
Walter Dörwald <walter@livinglogic.de>
parents: 34931
diff changeset
583 self.assertRaises(UnicodeError, getattr, sys, unichr(sys.maxunicode))
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
584
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
585 def test_hasattr(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
586 import sys
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
587 self.assert_(hasattr(sys, 'stdout'))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
588 self.assertRaises(TypeError, hasattr, sys, 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
589 self.assertRaises(TypeError, hasattr)
35309
5fe9ceeca111 Disable a few other tests, that can't work if Python is compiled without
Walter Dörwald <walter@livinglogic.de>
parents: 34931
diff changeset
590 if have_unicode:
5fe9ceeca111 Disable a few other tests, that can't work if Python is compiled without
Walter Dörwald <walter@livinglogic.de>
parents: 34931
diff changeset
591 self.assertRaises(UnicodeError, hasattr, sys, unichr(sys.maxunicode))
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
592
46925
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46649
diff changeset
593 # Check that hasattr allows SystemExit and KeyboardInterrupts by
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46649
diff changeset
594 class A:
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46649
diff changeset
595 def __getattr__(self, what):
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46649
diff changeset
596 raise KeyboardInterrupt
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46649
diff changeset
597 self.assertRaises(KeyboardInterrupt, hasattr, A(), "b")
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46649
diff changeset
598 class B:
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46649
diff changeset
599 def __getattr__(self, what):
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46649
diff changeset
600 raise SystemExit
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46649
diff changeset
601 self.assertRaises(SystemExit, hasattr, B(), "b")
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46649
diff changeset
602
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
603 def test_hash(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
604 hash(None)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
605 self.assertEqual(hash(1), hash(1L))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
606 self.assertEqual(hash(1), hash(1.0))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
607 hash('spam')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
608 if have_unicode:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
609 self.assertEqual(hash('spam'), hash(unicode('spam')))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
610 hash((0,1,2,3))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
611 def f(): pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
612 self.assertRaises(TypeError, hash, [])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
613 self.assertRaises(TypeError, hash, {})
39410
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39407
diff changeset
614 # Bug 1536021: Allow hash to return long objects
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39407
diff changeset
615 class X:
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39407
diff changeset
616 def __hash__(self):
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39407
diff changeset
617 return 2**100
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39407
diff changeset
618 self.assertEquals(type(hash(X())), int)
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39407
diff changeset
619 class Y(object):
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39407
diff changeset
620 def __hash__(self):
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39407
diff changeset
621 return 2**100
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39407
diff changeset
622 self.assertEquals(type(hash(Y())), int)
39415
9a8081cf4a70 Fix and test for an infinite C recursion.
Armin Rigo <arigo@tunes.org>
parents: 39410
diff changeset
623 class Z(long):
9a8081cf4a70 Fix and test for an infinite C recursion.
Armin Rigo <arigo@tunes.org>
parents: 39410
diff changeset
624 def __hash__(self):
9a8081cf4a70 Fix and test for an infinite C recursion.
Armin Rigo <arigo@tunes.org>
parents: 39410
diff changeset
625 return self
9a8081cf4a70 Fix and test for an infinite C recursion.
Armin Rigo <arigo@tunes.org>
parents: 39410
diff changeset
626 self.assertEquals(hash(Z(42)), hash(42L))
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
627
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
628 def test_hex(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
629 self.assertEqual(hex(16), '0x10')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
630 self.assertEqual(hex(16L), '0x10L')
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 30929
diff changeset
631 self.assertEqual(hex(-16), '-0x10')
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
632 self.assertEqual(hex(-16L), '-0x10L')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
633 self.assertRaises(TypeError, hex, {})
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
634
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
635 def test_id(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
636 id(None)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
637 id(1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
638 id(1L)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
639 id(1.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
640 id('spam')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
641 id((0,1,2,3))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
642 id([0,1,2,3])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
643 id({'spam': 1, 'eggs': 2, 'ham': 3})
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
644
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
645 # Test input() later, together with raw_input
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
646
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
647 def test_intern(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
648 self.assertRaises(TypeError, intern)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
649 s = "never interned before"
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
650 self.assert_(intern(s) is s)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
651 s2 = s.swapcase().swapcase()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
652 self.assert_(intern(s2) is s)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
653
33019
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
654 # Subclasses of string can't be interned, because they
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
655 # provide too much opportunity for insane things to happen.
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
656 # We don't want them in the interned dict and if they aren't
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
657 # actually interned, we don't want to create the appearance
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
658 # that they are by allowing intern() to succeeed.
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
659 class S(str):
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
660 def __hash__(self):
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
661 return 123
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
662
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
663 self.assertRaises(TypeError, intern, S("abc"))
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
664
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
665 # It's still safe to pass these strings to routines that
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
666 # call intern internally, e.g. PyObject_SetAttr().
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
667 s = S("abc")
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
668 setattr(s, s, s)
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
669 self.assertEqual(getattr(s, s), s)
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32981
diff changeset
670
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
671 def test_iter(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
672 self.assertRaises(TypeError, iter)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
673 self.assertRaises(TypeError, iter, 42, 42)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
674 lists = [("1", "2"), ["1", "2"], "12"]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
675 if have_unicode:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
676 lists.append(unicode("12"))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
677 for l in lists:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
678 i = iter(l)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
679 self.assertEqual(i.next(), '1')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
680 self.assertEqual(i.next(), '2')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
681 self.assertRaises(StopIteration, i.next)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
682
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
683 def test_isinstance(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
684 class C:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
685 pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
686 class D(C):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
687 pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
688 class E:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
689 pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
690 c = C()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
691 d = D()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
692 e = E()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
693 self.assert_(isinstance(c, C))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
694 self.assert_(isinstance(d, C))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
695 self.assert_(not isinstance(e, C))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
696 self.assert_(not isinstance(c, D))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
697 self.assert_(not isinstance('foo', E))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
698 self.assertRaises(TypeError, isinstance, E, 'foo')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
699 self.assertRaises(TypeError, isinstance)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
700
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
701 def test_issubclass(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
702 class C:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
703 pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
704 class D(C):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
705 pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
706 class E:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
707 pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
708 c = C()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
709 d = D()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
710 e = E()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
711 self.assert_(issubclass(D, C))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
712 self.assert_(issubclass(C, C))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
713 self.assert_(not issubclass(C, D))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
714 self.assertRaises(TypeError, issubclass, 'foo', E)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
715 self.assertRaises(TypeError, issubclass, E, 'foo')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
716 self.assertRaises(TypeError, issubclass)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
717
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
718 def test_len(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
719 self.assertEqual(len('123'), 3)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
720 self.assertEqual(len(()), 0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
721 self.assertEqual(len((1, 2, 3, 4)), 4)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
722 self.assertEqual(len([1, 2, 3, 4]), 4)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
723 self.assertEqual(len({}), 0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
724 self.assertEqual(len({'a':1, 'b': 2}), 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
725 class BadSeq:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
726 def __len__(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
727 raise ValueError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
728 self.assertRaises(ValueError, len, BadSeq())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
729
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
730 def test_map(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
731 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
732 map(None, 'hello world'),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
733 ['h','e','l','l','o',' ','w','o','r','l','d']
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
734 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
735 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
736 map(None, 'abcd', 'efg'),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
737 [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
738 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
739 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
740 map(None, range(10)),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
741 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
742 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
743 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
744 map(lambda x: x*x, range(1,4)),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
745 [1, 4, 9]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
746 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
747 try:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
748 from math import sqrt
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
749 except ImportError:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
750 def sqrt(x):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
751 return pow(x, 0.5)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
752 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
753 map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
754 [[4.0, 2.0], [9.0, 3.0]]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
755 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
756 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
757 map(lambda x, y: x+y, [1,3,2], [9,1,4]),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
758 [10, 4, 6]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
759 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
760
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
761 def plus(*v):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
762 accu = 0
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
763 for i in v: accu = accu + i
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
764 return accu
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
765 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
766 map(plus, [1, 3, 7]),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
767 [1, 3, 7]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
768 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
769 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
770 map(plus, [1, 3, 7], [4, 9, 2]),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
771 [1+4, 3+9, 7+2]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
772 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
773 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
774 map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
775 [1+4+1, 3+9+1, 7+2+0]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
776 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
777 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
778 map(None, Squares(10)),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
779 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
780 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
781 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
782 map(int, Squares(10)),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
783 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
784 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
785 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
786 map(None, Squares(3), Squares(2)),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
787 [(0,0), (1,1), (4,None)]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
788 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
789 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
790 map(max, Squares(3), Squares(2)),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
791 [0, 1, 4]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
792 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
793 self.assertRaises(TypeError, map)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
794 self.assertRaises(TypeError, map, lambda x: x, 42)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
795 self.assertEqual(map(None, [42]), [42])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
796 class BadSeq:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
797 def __getitem__(self, index):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
798 raise ValueError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
799 self.assertRaises(ValueError, map, lambda x: x, BadSeq())
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
800 def badfunc(x):
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
801 raise RuntimeError
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
802 self.assertRaises(RuntimeError, map, badfunc, range(5))
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
803
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
804 def test_max(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
805 self.assertEqual(max('123123'), '3')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
806 self.assertEqual(max(1, 2, 3), 3)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
807 self.assertEqual(max((1, 2, 3, 1, 2, 3)), 3)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
808 self.assertEqual(max([1, 2, 3, 1, 2, 3]), 3)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
809
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
810 self.assertEqual(max(1, 2L, 3.0), 3.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
811 self.assertEqual(max(1L, 2.0, 3), 3)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
812 self.assertEqual(max(1.0, 2, 3L), 3L)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
813
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
814 for stmt in (
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
815 "max(key=int)", # no args
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
816 "max(1, key=int)", # single arg not iterable
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
817 "max(1, 2, keystone=int)", # wrong keyword
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
818 "max(1, 2, key=int, abc=int)", # two many keywords
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
819 "max(1, 2, key=1)", # keyfunc is not callable
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
820 ):
34131
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
821 try:
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
822 exec(stmt) in globals()
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
823 except TypeError:
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
824 pass
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
825 else:
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
826 self.fail(stmt)
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
827
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
828 self.assertEqual(max((1,), key=neg), 1) # one elem iterable
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
829 self.assertEqual(max((1,2), key=neg), 1) # two elem iterable
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
830 self.assertEqual(max(1, 2, key=neg), 1) # two elems
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
831
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
832 data = [random.randrange(200) for i in range(100)]
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
833 keys = dict((elem, random.randrange(50)) for elem in data)
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
834 f = keys.__getitem__
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
835 self.assertEqual(max(data, key=f),
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
836 sorted(reversed(data), key=f)[-1])
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
837
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
838 def test_min(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
839 self.assertEqual(min('123123'), '1')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
840 self.assertEqual(min(1, 2, 3), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
841 self.assertEqual(min((1, 2, 3, 1, 2, 3)), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
842 self.assertEqual(min([1, 2, 3, 1, 2, 3]), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
843
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
844 self.assertEqual(min(1, 2L, 3.0), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
845 self.assertEqual(min(1L, 2.0, 3), 1L)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
846 self.assertEqual(min(1.0, 2, 3L), 1.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
847
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
848 self.assertRaises(TypeError, min)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
849 self.assertRaises(TypeError, min, 42)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
850 self.assertRaises(ValueError, min, ())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
851 class BadSeq:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
852 def __getitem__(self, index):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
853 raise ValueError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
854 self.assertRaises(ValueError, min, BadSeq())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
855 class BadNumber:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
856 def __cmp__(self, other):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
857 raise ValueError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
858 self.assertRaises(ValueError, min, (42, BadNumber()))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
859
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
860 for stmt in (
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
861 "min(key=int)", # no args
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
862 "min(1, key=int)", # single arg not iterable
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
863 "min(1, 2, keystone=int)", # wrong keyword
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
864 "min(1, 2, key=int, abc=int)", # two many keywords
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
865 "min(1, 2, key=1)", # keyfunc is not callable
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
866 ):
34131
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
867 try:
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
868 exec(stmt) in globals()
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
869 except TypeError:
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
870 pass
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
871 else:
5912b4181584 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34085
diff changeset
872 self.fail(stmt)
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
873
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
874 self.assertEqual(min((1,), key=neg), 1) # one elem iterable
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
875 self.assertEqual(min((1,2), key=neg), 2) # two elem iterable
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
876 self.assertEqual(min(1, 2, key=neg), 2) # two elems
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
877
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
878 data = [random.randrange(200) for i in range(100)]
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
879 keys = dict((elem, random.randrange(50)) for elem in data)
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
880 f = keys.__getitem__
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
881 self.assertEqual(min(data, key=f),
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
882 sorted(data, key=f)[0])
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
883
46607
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
884 def test_next(self):
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
885 it = iter(range(2))
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
886 self.assertEqual(next(it), 0)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
887 self.assertEqual(next(it), 1)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
888 self.assertRaises(StopIteration, next, it)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
889 self.assertRaises(StopIteration, next, it)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
890 self.assertEquals(next(it, 42), 42)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
891
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
892 class Iter(object):
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
893 def __iter__(self):
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
894 return self
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
895 def next(self):
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
896 raise StopIteration
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
897
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
898 it = iter(Iter())
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
899 self.assertEquals(next(it, 42), 42)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
900 self.assertRaises(StopIteration, next, it)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
901
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
902 def gen():
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
903 yield 1
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
904 return
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
905
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
906 it = gen()
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
907 self.assertEquals(next(it), 1)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
908 self.assertRaises(StopIteration, next, it)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
909 self.assertEquals(next(it, 42), 42)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 45791
diff changeset
910
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
911 def test_oct(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
912 self.assertEqual(oct(100), '0144')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
913 self.assertEqual(oct(100L), '0144L')
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 30929
diff changeset
914 self.assertEqual(oct(-100), '-0144')
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
915 self.assertEqual(oct(-100L), '-0144L')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
916 self.assertRaises(TypeError, oct, ())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
917
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
918 def write_testfile(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
919 # NB the first 4 lines are also used to test input and raw_input, below
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
920 fp = open(TESTFN, 'w')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
921 try:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
922 fp.write('1+1\n')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
923 fp.write('1+1\n')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
924 fp.write('The quick brown fox jumps over the lazy dog')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
925 fp.write('.\n')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
926 fp.write('Dear John\n')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
927 fp.write('XXX'*100)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
928 fp.write('YYY'*100)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
929 finally:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
930 fp.close()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
931
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
932 def test_open(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
933 self.write_testfile()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
934 fp = open(TESTFN, 'r')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
935 try:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
936 self.assertEqual(fp.readline(4), '1+1\n')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
937 self.assertEqual(fp.readline(4), '1+1\n')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
938 self.assertEqual(fp.readline(), 'The quick brown fox jumps over the lazy dog.\n')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
939 self.assertEqual(fp.readline(4), 'Dear')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
940 self.assertEqual(fp.readline(100), ' John\n')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
941 self.assertEqual(fp.read(300), 'XXX'*100)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
942 self.assertEqual(fp.read(1000), 'YYY'*100)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
943 finally:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
944 fp.close()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
945 unlink(TESTFN)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
946
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
947 def test_ord(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
948 self.assertEqual(ord(' '), 32)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
949 self.assertEqual(ord('A'), 65)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
950 self.assertEqual(ord('a'), 97)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
951 if have_unicode:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
952 self.assertEqual(ord(unichr(sys.maxunicode)), sys.maxunicode)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
953 self.assertRaises(TypeError, ord, 42)
35309
5fe9ceeca111 Disable a few other tests, that can't work if Python is compiled without
Walter Dörwald <walter@livinglogic.de>
parents: 34931
diff changeset
954 if have_unicode:
5fe9ceeca111 Disable a few other tests, that can't work if Python is compiled without
Walter Dörwald <walter@livinglogic.de>
parents: 34931
diff changeset
955 self.assertRaises(TypeError, ord, unicode("12"))
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
956
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
957 def test_pow(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
958 self.assertEqual(pow(0,0), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
959 self.assertEqual(pow(0,1), 0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
960 self.assertEqual(pow(1,0), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
961 self.assertEqual(pow(1,1), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
962
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
963 self.assertEqual(pow(2,0), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
964 self.assertEqual(pow(2,10), 1024)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
965 self.assertEqual(pow(2,20), 1024*1024)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
966 self.assertEqual(pow(2,30), 1024*1024*1024)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
967
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
968 self.assertEqual(pow(-2,0), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
969 self.assertEqual(pow(-2,1), -2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
970 self.assertEqual(pow(-2,2), 4)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
971 self.assertEqual(pow(-2,3), -8)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
972
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
973 self.assertEqual(pow(0L,0), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
974 self.assertEqual(pow(0L,1), 0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
975 self.assertEqual(pow(1L,0), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
976 self.assertEqual(pow(1L,1), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
977
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
978 self.assertEqual(pow(2L,0), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
979 self.assertEqual(pow(2L,10), 1024)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
980 self.assertEqual(pow(2L,20), 1024*1024)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
981 self.assertEqual(pow(2L,30), 1024*1024*1024)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
982
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
983 self.assertEqual(pow(-2L,0), 1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
984 self.assertEqual(pow(-2L,1), -2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
985 self.assertEqual(pow(-2L,2), 4)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
986 self.assertEqual(pow(-2L,3), -8)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
987
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
988 self.assertAlmostEqual(pow(0.,0), 1.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
989 self.assertAlmostEqual(pow(0.,1), 0.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
990 self.assertAlmostEqual(pow(1.,0), 1.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
991 self.assertAlmostEqual(pow(1.,1), 1.)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
992
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
993 self.assertAlmostEqual(pow(2.,0), 1.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
994 self.assertAlmostEqual(pow(2.,10), 1024.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
995 self.assertAlmostEqual(pow(2.,20), 1024.*1024.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
996 self.assertAlmostEqual(pow(2.,30), 1024.*1024.*1024.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
997
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
998 self.assertAlmostEqual(pow(-2.,0), 1.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
999 self.assertAlmostEqual(pow(-2.,1), -2.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1000 self.assertAlmostEqual(pow(-2.,2), 4.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1001 self.assertAlmostEqual(pow(-2.,3), -8.)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1002
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1003 for x in 2, 2L, 2.0:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1004 for y in 10, 10L, 10.0:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1005 for z in 1000, 1000L, 1000.0:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1006 if isinstance(x, float) or \
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1007 isinstance(y, float) or \
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1008 isinstance(z, float):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1009 self.assertRaises(TypeError, pow, x, y, z)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1010 else:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1011 self.assertAlmostEqual(pow(x, y, z), 24.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1012
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1013 self.assertRaises(TypeError, pow, -1, -2, 3)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1014 self.assertRaises(ValueError, pow, 1, 2, 0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1015 self.assertRaises(TypeError, pow, -1L, -2L, 3L)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1016 self.assertRaises(ValueError, pow, 1L, 2L, 0L)
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1017 # Will return complex in 3.0:
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1018 self.assertRaises(ValueError, pow, -342.43, 0.234)
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1019
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1020 self.assertRaises(TypeError, pow)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1021
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1022 def test_range(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1023 self.assertEqual(range(3), [0, 1, 2])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1024 self.assertEqual(range(1, 5), [1, 2, 3, 4])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1025 self.assertEqual(range(0), [])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1026 self.assertEqual(range(-3), [])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1027 self.assertEqual(range(1, 10, 3), [1, 4, 7])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1028 self.assertEqual(range(5, -5, -3), [5, 2, -1, -4])
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1029
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1030 # Now test range() with longs
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1031 self.assertEqual(range(-2**100), [])
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1032 self.assertEqual(range(0, -2**100), [])
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1033 self.assertEqual(range(0, 2**100, -1), [])
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1034 self.assertEqual(range(0, 2**100, -1), [])
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1035
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1036 a = long(10 * sys.maxint)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1037 b = long(100 * sys.maxint)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1038 c = long(50 * sys.maxint)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1039
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1040 self.assertEqual(range(a, a+2), [a, a+1])
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1041 self.assertEqual(range(a+2, a, -1L), [a+2, a+1])
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1042 self.assertEqual(range(a+4, a, -2), [a+4, a+2])
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1043
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1044 seq = range(a, b, c)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1045 self.assert_(a in seq)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1046 self.assert_(b not in seq)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1047 self.assertEqual(len(seq), 2)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1048
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1049 seq = range(b, a, -c)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1050 self.assert_(b in seq)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1051 self.assert_(a not in seq)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1052 self.assertEqual(len(seq), 2)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1053
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1054 seq = range(-a, -b, -c)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1055 self.assert_(-a in seq)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1056 self.assert_(-b not in seq)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1057 self.assertEqual(len(seq), 2)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1058
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1059 self.assertRaises(TypeError, range)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1060 self.assertRaises(TypeError, range, 1, 2, 3, 4)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1061 self.assertRaises(ValueError, range, 1, 2, 0)
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
1062 self.assertRaises(ValueError, range, a, a + 1, long(0))
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
1063
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
1064 class badzero(int):
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
1065 def __cmp__(self, other):
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
1066 raise RuntimeError
48213
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 48115
diff changeset
1067 __hash__ = None # Invalid cmp makes this unhashable
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35906
diff changeset
1068 self.assertRaises(RuntimeError, range, a, a + 1, badzero(1))
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1069
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1070 # Reject floats when it would require PyLongs to represent.
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1071 # (smaller floats still accepted, but deprecated)
28614
4c6585253e6f test_range(): The C code changed to raise TypeError in one of these
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1072 self.assertRaises(TypeError, range, 1e100, 1e101, 1e101)
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
1073
28620
ddd906effd9c Add a few errors tests for range().
Walter Dörwald <walter@livinglogic.de>
parents: 28614
diff changeset
1074 self.assertRaises(TypeError, range, 0, "spam")
ddd906effd9c Add a few errors tests for range().
Walter Dörwald <walter@livinglogic.de>
parents: 28614
diff changeset
1075 self.assertRaises(TypeError, range, 0, 42, "spam")
ddd906effd9c Add a few errors tests for range().
Walter Dörwald <walter@livinglogic.de>
parents: 28614
diff changeset
1076
ddd906effd9c Add a few errors tests for range().
Walter Dörwald <walter@livinglogic.de>
parents: 28614
diff changeset
1077 self.assertRaises(OverflowError, range, -sys.maxint, sys.maxint)
ddd906effd9c Add a few errors tests for range().
Walter Dörwald <walter@livinglogic.de>
parents: 28614
diff changeset
1078 self.assertRaises(OverflowError, range, 0, 2*sys.maxint)
ddd906effd9c Add a few errors tests for range().
Walter Dörwald <walter@livinglogic.de>
parents: 28614
diff changeset
1079
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1080 def test_input_and_raw_input(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1081 self.write_testfile()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1082 fp = open(TESTFN, 'r')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1083 savestdin = sys.stdin
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1084 savestdout = sys.stdout # Eats the echo
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1085 try:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1086 sys.stdin = fp
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1087 sys.stdout = BitBucket()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1088 self.assertEqual(input(), 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1089 self.assertEqual(input('testing\n'), 2)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1090 self.assertEqual(raw_input(), 'The quick brown fox jumps over the lazy dog.')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1091 self.assertEqual(raw_input('testing\n'), 'Dear John')
39407
5691624269a5 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39387
diff changeset
1092
39385
5009de504f92 Bug #1535165: fixed a segfault in input() and raw_input() when
Georg Brandl <georg@python.org>
parents: 38989
diff changeset
1093 # SF 1535165: don't segfault on closed stdin
5009de504f92 Bug #1535165: fixed a segfault in input() and raw_input() when
Georg Brandl <georg@python.org>
parents: 38989
diff changeset
1094 # sys.stdout must be a regular file for triggering
5009de504f92 Bug #1535165: fixed a segfault in input() and raw_input() when
Georg Brandl <georg@python.org>
parents: 38989
diff changeset
1095 sys.stdout = savestdout
5009de504f92 Bug #1535165: fixed a segfault in input() and raw_input() when
Georg Brandl <georg@python.org>
parents: 38989
diff changeset
1096 sys.stdin.close()
39387
2e630b2d5b44 Don't produce output in test_builtin.
Georg Brandl <georg@python.org>
parents: 39385
diff changeset
1097 self.assertRaises(ValueError, input)
39385
5009de504f92 Bug #1535165: fixed a segfault in input() and raw_input() when
Georg Brandl <georg@python.org>
parents: 38989
diff changeset
1098
5009de504f92 Bug #1535165: fixed a segfault in input() and raw_input() when
Georg Brandl <georg@python.org>
parents: 38989
diff changeset
1099 sys.stdout = BitBucket()
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1100 sys.stdin = cStringIO.StringIO("NULL\0")
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1101 self.assertRaises(TypeError, input, 42, 42)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1102 sys.stdin = cStringIO.StringIO(" 'whitespace'")
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1103 self.assertEqual(input(), 'whitespace')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1104 sys.stdin = cStringIO.StringIO()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1105 self.assertRaises(EOFError, input)
31445
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1106
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1107 # SF 876178: make sure input() respect future options.
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1108 sys.stdin = cStringIO.StringIO('1/2')
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1109 sys.stdout = cStringIO.StringIO()
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1110 exec compile('print input()', 'test_builtin_tmp', 'exec')
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1111 sys.stdin.seek(0, 0)
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1112 exec compile('from __future__ import division;print input()',
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1113 'test_builtin_tmp', 'exec')
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1114 sys.stdin.seek(0, 0)
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1115 exec compile('print input()', 'test_builtin_tmp', 'exec')
37268
a7834844dcf6 Part of bug 1459808: fiddle test_input_and_raw_input()
Tim Peters <tim.peters@gmail.com>
parents: 36259
diff changeset
1116 # The result we expect depends on whether new division semantics
a7834844dcf6 Part of bug 1459808: fiddle test_input_and_raw_input()
Tim Peters <tim.peters@gmail.com>
parents: 36259
diff changeset
1117 # are already in effect.
a7834844dcf6 Part of bug 1459808: fiddle test_input_and_raw_input()
Tim Peters <tim.peters@gmail.com>
parents: 36259
diff changeset
1118 if 1/2 == 0:
a7834844dcf6 Part of bug 1459808: fiddle test_input_and_raw_input()
Tim Peters <tim.peters@gmail.com>
parents: 36259
diff changeset
1119 # This test was compiled with old semantics.
a7834844dcf6 Part of bug 1459808: fiddle test_input_and_raw_input()
Tim Peters <tim.peters@gmail.com>
parents: 36259
diff changeset
1120 expected = ['0', '0.5', '0']
a7834844dcf6 Part of bug 1459808: fiddle test_input_and_raw_input()
Tim Peters <tim.peters@gmail.com>
parents: 36259
diff changeset
1121 else:
a7834844dcf6 Part of bug 1459808: fiddle test_input_and_raw_input()
Tim Peters <tim.peters@gmail.com>
parents: 36259
diff changeset
1122 # This test was compiled with new semantics (e.g., -Qnew
a7834844dcf6 Part of bug 1459808: fiddle test_input_and_raw_input()
Tim Peters <tim.peters@gmail.com>
parents: 36259
diff changeset
1123 # was given on the command line.
a7834844dcf6 Part of bug 1459808: fiddle test_input_and_raw_input()
Tim Peters <tim.peters@gmail.com>
parents: 36259
diff changeset
1124 expected = ['0.5', '0.5', '0.5']
a7834844dcf6 Part of bug 1459808: fiddle test_input_and_raw_input()
Tim Peters <tim.peters@gmail.com>
parents: 36259
diff changeset
1125 self.assertEqual(sys.stdout.getvalue().splitlines(), expected)
31445
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31210
diff changeset
1126
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1127 del sys.stdout
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1128 self.assertRaises(RuntimeError, input, 'prompt')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1129 del sys.stdin
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1130 self.assertRaises(RuntimeError, input, 'prompt')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1131 finally:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1132 sys.stdin = savestdin
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1133 sys.stdout = savestdout
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1134 fp.close()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1135 unlink(TESTFN)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1136
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1137 def test_reduce(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1138 self.assertEqual(reduce(lambda x, y: x+y, ['a', 'b', 'c'], ''), 'abc')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1139 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1140 reduce(lambda x, y: x+y, [['a', 'c'], [], ['d', 'w']], []),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1141 ['a','c','d','w']
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1142 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1143 self.assertEqual(reduce(lambda x, y: x*y, range(2,8), 1), 5040)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1144 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1145 reduce(lambda x, y: x*y, range(2,21), 1L),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1146 2432902008176640000L
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1147 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1148 self.assertEqual(reduce(lambda x, y: x+y, Squares(10)), 285)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1149 self.assertEqual(reduce(lambda x, y: x+y, Squares(10), 0), 285)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1150 self.assertEqual(reduce(lambda x, y: x+y, Squares(0), 0), 0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1151 self.assertRaises(TypeError, reduce)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1152 self.assertRaises(TypeError, reduce, 42, 42)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1153 self.assertRaises(TypeError, reduce, 42, 42, 42)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1154 self.assertEqual(reduce(42, "1"), "1") # func is never called with one item
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1155 self.assertEqual(reduce(42, "", "1"), "1") # func is never called with one item
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1156 self.assertRaises(TypeError, reduce, 42, (42, 42))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1157
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1158 class BadSeq:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1159 def __getitem__(self, index):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1160 raise ValueError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1161 self.assertRaises(ValueError, reduce, 42, BadSeq())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1162
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1163 def test_reload(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1164 import marshal
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1165 reload(marshal)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1166 import string
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1167 reload(string)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1168 ## import sys
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1169 ## self.assertRaises(ImportError, reload, sys)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1170
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1171 def test_repr(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1172 self.assertEqual(repr(''), '\'\'')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1173 self.assertEqual(repr(0), '0')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1174 self.assertEqual(repr(0L), '0L')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1175 self.assertEqual(repr(()), '()')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1176 self.assertEqual(repr([]), '[]')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1177 self.assertEqual(repr({}), '{}')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1178 a = []
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1179 a.append(a)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1180 self.assertEqual(repr(a), '[[...]]')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1181 a = {}
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1182 a[0] = a
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1183 self.assertEqual(repr(a), '{0: {...}}')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1184
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1185 def test_round(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1186 self.assertEqual(round(0.0), 0.0)
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1187 self.assertEqual(type(round(0.0)), float) # Will be int in 3.0.
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1188 self.assertEqual(round(1.0), 1.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1189 self.assertEqual(round(10.0), 10.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1190 self.assertEqual(round(1000000000.0), 1000000000.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1191 self.assertEqual(round(1e20), 1e20)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1192
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1193 self.assertEqual(round(-1.0), -1.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1194 self.assertEqual(round(-10.0), -10.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1195 self.assertEqual(round(-1000000000.0), -1000000000.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1196 self.assertEqual(round(-1e20), -1e20)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1197
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1198 self.assertEqual(round(0.1), 0.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1199 self.assertEqual(round(1.1), 1.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1200 self.assertEqual(round(10.1), 10.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1201 self.assertEqual(round(1000000000.1), 1000000000.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1202
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1203 self.assertEqual(round(-1.1), -1.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1204 self.assertEqual(round(-10.1), -10.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1205 self.assertEqual(round(-1000000000.1), -1000000000.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1206
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1207 self.assertEqual(round(0.9), 1.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1208 self.assertEqual(round(9.9), 10.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1209 self.assertEqual(round(999999999.9), 1000000000.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1210
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1211 self.assertEqual(round(-0.9), -1.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1212 self.assertEqual(round(-9.9), -10.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1213 self.assertEqual(round(-999999999.9), -1000000000.0)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1214
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1215 self.assertEqual(round(-8.0, -1), -10.0)
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1216 self.assertEqual(type(round(-8.0, -1)), float)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1217
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1218 self.assertEqual(type(round(-8.0, 0)), float)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1219 self.assertEqual(type(round(-8.0, 1)), float)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1220
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1221 # Check half rounding behaviour.
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1222 self.assertEqual(round(5.5), 6)
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1223 self.assertEqual(round(6.5), 7)
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1224 self.assertEqual(round(-5.5), -6)
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1225 self.assertEqual(round(-6.5), -7)
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1226
53232
93223603c992 Issue #1869: Fix a couple of minor round() issues.
Mark Dickinson <dickinsm@gmail.com>
parents: 48213
diff changeset
1227 # Issue #1869: integral floats should remain unchanged
93223603c992 Issue #1869: Fix a couple of minor round() issues.
Mark Dickinson <dickinsm@gmail.com>
parents: 48213
diff changeset
1228 self.assertEqual(round(5e15+1), 5e15+1)
93223603c992 Issue #1869: Fix a couple of minor round() issues.
Mark Dickinson <dickinsm@gmail.com>
parents: 48213
diff changeset
1229
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1230 # Check behavior on ints
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1231 self.assertEqual(round(0), 0)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1232 self.assertEqual(round(8), 8)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1233 self.assertEqual(round(-8), -8)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1234 self.assertEqual(type(round(0)), float) # Will be int in 3.0.
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1235 self.assertEqual(type(round(-8, -1)), float)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1236 self.assertEqual(type(round(-8, 0)), float)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1237 self.assertEqual(type(round(-8, 1)), float)
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1238
37344
c6cf11acb41f Patch #1460496: round() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 37268
diff changeset
1239 # test new kwargs
c6cf11acb41f Patch #1460496: round() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 37268
diff changeset
1240 self.assertEqual(round(number=-8.0, ndigits=-1), -10.0)
c6cf11acb41f Patch #1460496: round() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 37268
diff changeset
1241
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1242 self.assertRaises(TypeError, round)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1243
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1244 # test generic rounding delegation for reals
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1245 class TestRound(object):
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1246 def __float__(self):
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1247 return 23.0
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1248
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1249 class TestNoRound(object):
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1250 pass
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1251
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1252 self.assertEqual(round(TestRound()), 23)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1253
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1254 self.assertRaises(TypeError, round, 1, 2, 3)
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1255 self.assertRaises(TypeError, round, TestNoRound())
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1256
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1257 t = TestNoRound()
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1258 t.__float__ = lambda *args: args
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1259 self.assertRaises(TypeError, round, t)
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44482
diff changeset
1260 self.assertRaises(TypeError, round, t, 0)
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41168
diff changeset
1261
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1262 def test_setattr(self):
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
1263 setattr(sys, 'spam', 1)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
1264 self.assertEqual(sys.spam, 1)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
1265 self.assertRaises(TypeError, setattr, sys, 1, 'spam')
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27795
diff changeset
1266 self.assertRaises(TypeError, setattr)
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1267
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1268 def test_sum(self):
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1269 self.assertEqual(sum([]), 0)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1270 self.assertEqual(sum(range(2,8)), 27)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1271 self.assertEqual(sum(iter(range(2,8))), 27)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1272 self.assertEqual(sum(Squares(10)), 285)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1273 self.assertEqual(sum(iter(Squares(10))), 285)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1274 self.assertEqual(sum([[1], [2], [3]], []), [1, 2, 3])
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1275
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1276 self.assertRaises(TypeError, sum)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1277 self.assertRaises(TypeError, sum, 42)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1278 self.assertRaises(TypeError, sum, ['a', 'b', 'c'])
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1279 self.assertRaises(TypeError, sum, ['a', 'b', 'c'], '')
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1280 self.assertRaises(TypeError, sum, [[1], [2], [3]])
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1281 self.assertRaises(TypeError, sum, [{2:3}])
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1282 self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3})
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1283
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1284 class BadSeq:
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1285 def __getitem__(self, index):
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1286 raise ValueError
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1287 self.assertRaises(ValueError, sum, BadSeq())
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28620
diff changeset
1288
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1289 def test_type(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1290 self.assertEqual(type(''), type('123'))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1291 self.assertNotEqual(type(''), type(()))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1292
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1293 def test_unichr(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1294 if have_unicode:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1295 self.assertEqual(unichr(32), unicode(' '))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1296 self.assertEqual(unichr(65), unicode('A'))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1297 self.assertEqual(unichr(97), unicode('a'))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1298 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1299 unichr(sys.maxunicode),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1300 unicode('\\U%08x' % (sys.maxunicode), 'unicode-escape')
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1301 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1302 self.assertRaises(ValueError, unichr, sys.maxunicode+1)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1303 self.assertRaises(TypeError, unichr)
48115
4550144b1560 #3479: unichr(2**32) used to return u'\x00'.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47822
diff changeset
1304 self.assertRaises((OverflowError, ValueError), unichr, 2**32)
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1305
34422
13934343a53b Use decorators.
Guido van Rossum <guido@python.org>
parents: 34131
diff changeset
1306 # We don't want self in vars(), so these are static methods
13934343a53b Use decorators.
Guido van Rossum <guido@python.org>
parents: 34131
diff changeset
1307
13934343a53b Use decorators.
Guido van Rossum <guido@python.org>
parents: 34131
diff changeset
1308 @staticmethod
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1309 def get_vars_f0():
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1310 return vars()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1311
34422
13934343a53b Use decorators.
Guido van Rossum <guido@python.org>
parents: 34131
diff changeset
1312 @staticmethod
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1313 def get_vars_f2():
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1314 BuiltinTest.get_vars_f0()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1315 a = 1
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1316 b = 2
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1317 return vars()
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1318
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1319 def test_vars(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30801
diff changeset
1320 self.assertEqual(set(vars()), set(dir()))
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1321 import sys
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30801
diff changeset
1322 self.assertEqual(set(vars(sys)), set(dir(sys)))
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1323 self.assertEqual(self.get_vars_f0(), {})
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1324 self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2})
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1325 self.assertRaises(TypeError, vars, 42, 42)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1326 self.assertRaises(TypeError, vars, 42)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1327
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1328 def test_zip(self):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1329 a = (1, 2, 3)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1330 b = (4, 5, 6)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1331 t = [(1, 4), (2, 5), (3, 6)]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1332 self.assertEqual(zip(a, b), t)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1333 b = [4, 5, 6]
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1334 self.assertEqual(zip(a, b), t)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1335 b = (4, 5, 6, 7)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1336 self.assertEqual(zip(a, b), t)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1337 class I:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1338 def __getitem__(self, i):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1339 if i < 0 or i > 2: raise IndexError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1340 return i + 4
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1341 self.assertEqual(zip(a, I()), t)
30037
0504ba8efbb6 As discussed on python-dev, changed builtin.zip() to handle zero arguments
Raymond Hettinger <python@rcn.com>
parents: 29532
diff changeset
1342 self.assertEqual(zip(), [])
0504ba8efbb6 As discussed on python-dev, changed builtin.zip() to handle zero arguments
Raymond Hettinger <python@rcn.com>
parents: 29532
diff changeset
1343 self.assertEqual(zip(*[]), [])
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1344 self.assertRaises(TypeError, zip, None)
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1345 class G:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1346 pass
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1347 self.assertRaises(TypeError, zip, a, G())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1348
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1349 # Make sure zip doesn't try to allocate a billion elements for the
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1350 # result list when one of its arguments doesn't say how long it is.
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1351 # A MemoryError is the most likely failure mode.
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1352 class SequenceWithoutALength:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1353 def __getitem__(self, i):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1354 if i == 5:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1355 raise IndexError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1356 else:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1357 return i
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1358 self.assertEqual(
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1359 zip(SequenceWithoutALength(), xrange(2**30)),
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1360 list(enumerate(range(5)))
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1361 )
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1362
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1363 class BadSeq:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1364 def __getitem__(self, i):
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1365 if i == 5:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1366 raise ValueError
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1367 else:
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1368 return i
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1369 self.assertRaises(ValueError, zip, BadSeq(), BadSeq())
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1370
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1371 def test_format(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1372 # Test the basic machinery of the format() builtin. Don't test
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1373 # the specifics of the various formatters
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1374 self.assertEqual(format(3, ''), '3')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1375
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1376 # Returns some classes to use for various tests. There's
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1377 # an old-style version, and a new-style version
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1378 def classes_new():
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1379 class A(object):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1380 def __init__(self, x):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1381 self.x = x
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1382 def __format__(self, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1383 return str(self.x) + format_spec
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1384 class DerivedFromA(A):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1385 pass
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1386
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1387 class Simple(object): pass
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1388 class DerivedFromSimple(Simple):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1389 def __init__(self, x):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1390 self.x = x
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1391 def __format__(self, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1392 return str(self.x) + format_spec
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1393 class DerivedFromSimple2(DerivedFromSimple): pass
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1394 return A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1395
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1396 # In 3.0, classes_classic has the same meaning as classes_new
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1397 def classes_classic():
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1398 class A:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1399 def __init__(self, x):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1400 self.x = x
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1401 def __format__(self, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1402 return str(self.x) + format_spec
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1403 class DerivedFromA(A):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1404 pass
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1405
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1406 class Simple: pass
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1407 class DerivedFromSimple(Simple):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1408 def __init__(self, x):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1409 self.x = x
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1410 def __format__(self, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1411 return str(self.x) + format_spec
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1412 class DerivedFromSimple2(DerivedFromSimple): pass
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1413 return A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1414
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1415 def class_test(A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1416 self.assertEqual(format(A(3), 'spec'), '3spec')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1417 self.assertEqual(format(DerivedFromA(4), 'spec'), '4spec')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1418 self.assertEqual(format(DerivedFromSimple(5), 'abc'), '5abc')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1419 self.assertEqual(format(DerivedFromSimple2(10), 'abcdef'),
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1420 '10abcdef')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1421
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1422 class_test(*classes_new())
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1423 class_test(*classes_classic())
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1424
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1425 def empty_format_spec(value):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1426 # test that:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1427 # format(x, '') == str(x)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1428 # format(x) == str(x)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1429 self.assertEqual(format(value, ""), str(value))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1430 self.assertEqual(format(value), str(value))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1431
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1432 # for builtin types, format(x, "") == str(x)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1433 empty_format_spec(17**13)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1434 empty_format_spec(1.0)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1435 empty_format_spec(3.1415e104)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1436 empty_format_spec(-3.1415e104)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1437 empty_format_spec(3.1415e-104)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1438 empty_format_spec(-3.1415e-104)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1439 empty_format_spec(object)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1440 empty_format_spec(None)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1441
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1442 # TypeError because self.__format__ returns the wrong type
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1443 class BadFormatResult:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1444 def __format__(self, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1445 return 1.0
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1446 self.assertRaises(TypeError, format, BadFormatResult(), "")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1447
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1448 # TypeError because format_spec is not unicode or str
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1449 self.assertRaises(TypeError, format, object(), 4)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1450 self.assertRaises(TypeError, format, object(), object())
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1451
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1452 # tests for object.__format__ really belong elsewhere, but
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1453 # there's no good place to put them
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1454 x = object().__format__('')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1455 self.assert_(x.startswith('<object object at'))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1456
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1457 # first argument to object.__format__ must be string
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1458 self.assertRaises(TypeError, object().__format__, 3)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1459 self.assertRaises(TypeError, object().__format__, object())
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1460 self.assertRaises(TypeError, object().__format__, None)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1461
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1462 # make sure we can take a subclass of str as a format spec
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1463 class DerivedFromStr(str): pass
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1464 self.assertEqual(format(0, DerivedFromStr('10')), ' 0')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45275
diff changeset
1465
45436
67a9d3385851 Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
Eric Smith <eric@trueblade.com>
parents: 45419
diff changeset
1466 def test_bin(self):
67a9d3385851 Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
Eric Smith <eric@trueblade.com>
parents: 45419
diff changeset
1467 self.assertEqual(bin(0), '0b0')
67a9d3385851 Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
Eric Smith <eric@trueblade.com>
parents: 45419
diff changeset
1468 self.assertEqual(bin(1), '0b1')
67a9d3385851 Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
Eric Smith <eric@trueblade.com>
parents: 45419
diff changeset
1469 self.assertEqual(bin(-1), '-0b1')
67a9d3385851 Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
Eric Smith <eric@trueblade.com>
parents: 45419
diff changeset
1470 self.assertEqual(bin(2**65), '0b1' + '0' * 65)
67a9d3385851 Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
Eric Smith <eric@trueblade.com>
parents: 45419
diff changeset
1471 self.assertEqual(bin(2**65-1), '0b' + '1' * 65)
67a9d3385851 Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
Eric Smith <eric@trueblade.com>
parents: 45419
diff changeset
1472 self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65)
67a9d3385851 Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
Eric Smith <eric@trueblade.com>
parents: 45419
diff changeset
1473 self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65)
67a9d3385851 Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
Eric Smith <eric@trueblade.com>
parents: 45419
diff changeset
1474
45419
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1475 class TestSorted(unittest.TestCase):
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1476
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1477 def test_basic(self):
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1478 data = range(100)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1479 copy = data[:]
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1480 random.shuffle(copy)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1481 self.assertEqual(data, sorted(copy))
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1482 self.assertNotEqual(data, copy)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1483
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1484 data.reverse()
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1485 random.shuffle(copy)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1486 self.assertEqual(data, sorted(copy, cmp=lambda x, y: cmp(y,x)))
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1487 self.assertNotEqual(data, copy)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1488 random.shuffle(copy)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1489 self.assertEqual(data, sorted(copy, key=lambda x: -x))
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1490 self.assertNotEqual(data, copy)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1491 random.shuffle(copy)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1492 self.assertEqual(data, sorted(copy, reverse=1))
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1493 self.assertNotEqual(data, copy)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1494
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1495 def test_inputtypes(self):
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1496 s = 'abracadabra'
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1497 types = [list, tuple]
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1498 if have_unicode:
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1499 types.insert(0, unicode)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1500 for T in types:
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1501 self.assertEqual(sorted(s), sorted(T(s)))
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1502
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1503 s = ''.join(dict.fromkeys(s).keys()) # unique letters only
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1504 types = [set, frozenset, list, tuple, dict.fromkeys]
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1505 if have_unicode:
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1506 types.insert(0, unicode)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1507 for T in types:
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1508 self.assertEqual(sorted(s), sorted(T(s)))
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1509
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1510 def test_baddecorator(self):
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1511 data = 'The quick Brown fox Jumped over The lazy Dog'.split()
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1512 self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0)
7d26230bb763 Moved test_format into the correct TestCase.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1513
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1514 def test_main(verbose=None):
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1515 test_classes = (BuiltinTest, TestSorted)
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1516
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1517 run_unittest(*test_classes)
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1518
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1519 # verify reference counting
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1520 if verbose and hasattr(sys, "gettotalrefcount"):
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1521 import gc
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1522 counts = [None] * 5
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1523 for i in xrange(len(counts)):
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1524 run_unittest(*test_classes)
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1525 gc.collect()
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1526 counts[i] = sys.gettotalrefcount()
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1527 print counts
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1528
27303
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1529
0f8d189310b4 Combine test_b1.py and test_b2.py into test_builtin.py,
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
1530 if __name__ == "__main__":
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33629
diff changeset
1531 test_main(verbose=True)