summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_builtin.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-07-03 08:25:58 (GMT)
committerGuido van Rossum <guido@python.org>2007-07-03 08:25:58 (GMT)
commitc1f779cb015272412e4da2e5cb61b4e8cdcbf191 (patch)
treeb616052de8a15507e02fdaa4a4723762b9ed11af /Lib/test/test_builtin.py
parentd09413012cb6b03fdae1439eaacc6076ce09f7b7 (diff)
downloadcpython-c1f779cb015272412e4da2e5cb61b4e8cdcbf191.zip
cpython-c1f779cb015272412e4da2e5cb61b4e8cdcbf191.tar.gz
cpython-c1f779cb015272412e4da2e5cb61b4e8cdcbf191.tar.bz2
Merged revisions 56125-56153 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk ........ r56127 | georg.brandl | 2007-06-30 09:32:49 +0200 (Sat, 30 Jun 2007) | 2 lines Fix a place where floor division would be in order. ........ r56135 | guido.van.rossum | 2007-07-01 06:13:54 +0200 (Sun, 01 Jul 2007) | 28 lines Make map() and filter() identical to itertools.imap() and .ifilter(), respectively. I fixed two bootstrap issues, due to the dynamic import of itertools: 1. Starting python requires that map() and filter() are not used until site.py has added build/lib.<arch> to sys.path. 2. Building python requires that setup.py and distutils and everything they use is free of map() and filter() calls. Beyond this, I only fixed the tests in test_builtin.py. Others, please help fixing the remaining tests that are now broken! The fixes are usually simple: a. map(None, X) -> list(X) b. map(F, X) -> list(map(F, X)) c. map(lambda x: F(x), X) -> [F(x) for x in X] d. filter(F, X) -> list(filter(F, X)) e. filter(lambda x: P(x), X) -> [x for x in X if P(x)] Someone, please also contribute a fixer for 2to3 to do this. It can leave map()/filter() calls alone that are already inside a list() or sorted() call or for-loop. Only in rare cases have I seen code that depends on map() of lists of different lengths going to the end of the longest, or on filter() of a string or tuple returning an object of the same type; these will need more thought to fix. ........ r56136 | guido.van.rossum | 2007-07-01 06:22:01 +0200 (Sun, 01 Jul 2007) | 3 lines Make it so that test_decimal fails instead of hangs, to help automated test runners. ........ r56139 | georg.brandl | 2007-07-01 18:20:58 +0200 (Sun, 01 Jul 2007) | 2 lines Fix a few test cases after the map->imap change. ........ r56142 | neal.norwitz | 2007-07-02 06:38:12 +0200 (Mon, 02 Jul 2007) | 1 line Get a bunch more tests passing after converting map/filter to return iterators. ........ r56147 | guido.van.rossum | 2007-07-02 15:32:02 +0200 (Mon, 02 Jul 2007) | 4 lines Fix the remaining failing unit tests (at least on OSX). Also tweaked urllib2 so it doesn't raise socket.gaierror when all network interfaces are turned off. ........
Diffstat (limited to 'Lib/test/test_builtin.py')
-rw-r--r--Lib/test/test_builtin.py119
1 files changed, 34 insertions, 85 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index a2fde02..036a9f2 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -466,11 +466,11 @@ class BuiltinTest(unittest.TestCase):
self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
def test_filter(self):
- self.assertEqual(filter(lambda c: 'a' <= c <= 'z', 'Hello World'), 'elloorld')
- self.assertEqual(filter(None, [1, 'hello', [], [3], '', None, 9, 0]), [1, 'hello', [3], 9])
- self.assertEqual(filter(lambda x: x > 0, [1, -3, 9, 0, 2]), [1, 9, 2])
- self.assertEqual(filter(None, Squares(10)), [1, 4, 9, 16, 25, 36, 49, 64, 81])
- self.assertEqual(filter(lambda x: x%2, Squares(10)), [1, 9, 25, 49, 81])
+ self.assertEqual(list(filter(lambda c: 'a' <= c <= 'z', 'Hello World')), list('elloorld'))
+ self.assertEqual(list(filter(None, [1, 'hello', [], [3], '', None, 9, 0])), [1, 'hello', [3], 9])
+ self.assertEqual(list(filter(lambda x: x > 0, [1, -3, 9, 0, 2])), [1, 9, 2])
+ self.assertEqual(list(filter(None, Squares(10))), [1, 4, 9, 16, 25, 36, 49, 64, 81])
+ self.assertEqual(list(filter(lambda x: x%2, Squares(10))), [1, 9, 25, 49, 81])
def identity(item):
return 1
filter(identity, Squares(5))
@@ -480,67 +480,15 @@ class BuiltinTest(unittest.TestCase):
if index<4:
return 42
raise ValueError
- self.assertRaises(ValueError, filter, lambda x: x, BadSeq())
+ self.assertRaises(ValueError, list, filter(lambda x: x, BadSeq()))
def badfunc():
pass
- self.assertRaises(TypeError, filter, badfunc, range(5))
+ self.assertRaises(TypeError, list, filter(badfunc, range(5)))
# test bltinmodule.c::filtertuple()
- self.assertEqual(filter(None, (1, 2)), (1, 2))
- self.assertEqual(filter(lambda x: x>=3, (1, 2, 3, 4)), (3, 4))
- self.assertRaises(TypeError, filter, 42, (1, 2))
-
- # test bltinmodule.c::filterunicode()
- self.assertEqual(filter(None, "12"), "12")
- self.assertEqual(filter(lambda x: x>="3", "1234"), "34")
- self.assertRaises(TypeError, filter, 42, "12")
- class badstr(str):
- def __getitem__(self, index):
- raise ValueError
- self.assertRaises(ValueError, filter, lambda x: x >="3", badstr("1234"))
-
- class badstr2(str):
- def __getitem__(self, index):
- return 42
- self.assertRaises(TypeError, filter, lambda x: x >=42, badstr2("1234"))
-
- class weirdstr(str):
- def __getitem__(self, index):
- return weirdstr(2*str.__getitem__(self, index))
- self.assertEqual(filter(lambda x: x>="33", weirdstr("1234")), "3344")
-
- class shiftstr(str):
- def __getitem__(self, index):
- return chr(ord(str.__getitem__(self, index))+1)
- self.assertEqual(filter(lambda x: x>="3", shiftstr("1234")), "345")
-
- def test_filter_subclasses(self):
- # test that filter() never returns tuple or str subclasses
- # and that the result always goes through __getitem__
- funcs = (None, bool, lambda x: True)
- class tuple2(tuple):
- def __getitem__(self, index):
- return 2*tuple.__getitem__(self, index)
- class str2(str):
- def __getitem__(self, index):
- return 2*str.__getitem__(self, index)
- inputs = {
- tuple2: {(): (), (1, 2, 3): (2, 4, 6)},
- str2: {"": "", "123": "112233"}
- }
-
- for (cls, inps) in inputs.items():
- for (inp, exp) in inps.items():
- # make sure the output goes through __getitem__
- # even if func is None
- self.assertEqual(
- filter(funcs[0], cls(inp)),
- filter(funcs[1], cls(inp))
- )
- for func in funcs:
- outp = filter(func, cls(inp))
- self.assertEqual(outp, exp)
- self.assert_(not isinstance(outp, cls))
+ self.assertEqual(list(filter(None, (1, 2))), [1, 2])
+ self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4])
+ self.assertRaises(TypeError, list, filter(42, (1, 2)))
def test_float(self):
self.assertEqual(float(3.14), 3.14)
@@ -1102,19 +1050,19 @@ class BuiltinTest(unittest.TestCase):
def test_map(self):
self.assertEqual(
- map(None, 'hello world'),
- ['h','e','l','l','o',' ','w','o','r','l','d']
+ list(map(None, 'hello')),
+ [('h',), ('e',), ('l',), ('l',), ('o',)]
)
self.assertEqual(
- map(None, 'abcd', 'efg'),
- [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]
+ list(map(None, 'abcd', 'efg')),
+ [('a', 'e'), ('b', 'f'), ('c', 'g')]
)
self.assertEqual(
- map(None, range(10)),
- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+ list(map(None, range(3))),
+ [(0,), (1,), (2,)]
)
self.assertEqual(
- map(lambda x: x*x, range(1,4)),
+ list(map(lambda x: x*x, range(1,4))),
[1, 4, 9]
)
try:
@@ -1123,11 +1071,11 @@ class BuiltinTest(unittest.TestCase):
def sqrt(x):
return pow(x, 0.5)
self.assertEqual(
- map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]),
+ list(map(lambda x: list(map(sqrt, x)), [[16, 4], [81, 9]])),
[[4.0, 2.0], [9.0, 3.0]]
)
self.assertEqual(
- map(lambda x, y: x+y, [1,3,2], [9,1,4]),
+ list(map(lambda x, y: x+y, [1,3,2], [9,1,4])),
[10, 4, 6]
)
@@ -1136,28 +1084,28 @@ class BuiltinTest(unittest.TestCase):
for i in v: accu = accu + i
return accu
self.assertEqual(
- map(plus, [1, 3, 7]),
+ list(map(plus, [1, 3, 7])),
[1, 3, 7]
)
self.assertEqual(
- map(plus, [1, 3, 7], [4, 9, 2]),
+ list(map(plus, [1, 3, 7], [4, 9, 2])),
[1+4, 3+9, 7+2]
)
self.assertEqual(
- map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]),
+ list(map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])),
[1+4+1, 3+9+1, 7+2+0]
)
self.assertEqual(
- map(None, Squares(10)),
- [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
+ list(map(None, Squares(10))),
+ [(0,), (1,), (4,), (9,), (16,), (25,), (36,), (49,), (64,), (81,)]
)
self.assertEqual(
- map(int, Squares(10)),
+ list(map(int, Squares(10))),
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
)
self.assertEqual(
- map(None, Squares(3), Squares(2)),
- [(0,0), (1,1), (4,None)]
+ list(map(None, Squares(3), Squares(2))),
+ [(0,0), (1,1)]
)
def Max(a, b):
if a is None:
@@ -1166,19 +1114,20 @@ class BuiltinTest(unittest.TestCase):
return a
return max(a, b)
self.assertEqual(
- map(Max, Squares(3), Squares(2)),
- [0, 1, 4]
+ list(map(Max, Squares(3), Squares(2))),
+ [0, 1]
)
self.assertRaises(TypeError, map)
self.assertRaises(TypeError, map, lambda x: x, 42)
- self.assertEqual(map(None, [42]), [42])
+ self.assertEqual(list(map(None, [42])), [(42,)])
class BadSeq:
- def __getitem__(self, index):
+ def __iter__(self):
raise ValueError
- self.assertRaises(ValueError, map, lambda x: x, BadSeq())
+ yield None
+ self.assertRaises(ValueError, list, map(lambda x: x, BadSeq()))
def badfunc(x):
raise RuntimeError
- self.assertRaises(RuntimeError, map, badfunc, range(5))
+ self.assertRaises(RuntimeError, list, map(badfunc, range(5)))
def test_max(self):
self.assertEqual(max('123123'), '3')