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.
........
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 6dfc52e..e3728d8 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -199,9 +199,9 @@
lzip('abc', 'def'))
self.assertEqual([pair for pair in izip('abc', 'def')],
lzip('abc', 'def'))
- ids = map(id, izip('abc', 'def'))
+ ids = list(map(id, izip('abc', 'def')))
self.assertEqual(min(ids), max(ids))
- ids = map(id, list(izip('abc', 'def')))
+ ids = list(map(id, list(izip('abc', 'def'))))
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
def test_iziplongest(self):
@@ -212,7 +212,8 @@
[range(1000), range(0), range(3000,3050), range(1200), range(1500)],
[range(1000), range(0), range(3000,3050), range(1200), range(1500), range(0)],
]:
- target = map(None, *args)
+ target = [tuple([arg[i] if i < len(arg) else None for arg in args])
+ for i in range(max(map(len, args)))]
self.assertEqual(list(izip_longest(*args)), target)
self.assertEqual(list(izip_longest(*args, **{})), target)
target = [tuple((e is None and 'X' or e) for e in t) for t in target] # Replace None fills with 'X'
@@ -224,7 +225,8 @@
self.assertEqual(list(izip_longest([])), list(zip([])))
self.assertEqual(list(izip_longest('abcdef')), list(zip('abcdef')))
- self.assertEqual(list(izip_longest('abc', 'defg', **{})), map(None, 'abc', 'defg')) # empty keyword dict
+ self.assertEqual(list(izip_longest('abc', 'defg', **{})),
+ list(map(None, list('abc')+[None], 'defg'))) # empty keyword dict
self.assertRaises(TypeError, izip_longest, 3)
self.assertRaises(TypeError, izip_longest, range(3), 3)
@@ -244,9 +246,9 @@
list(zip('abc', 'def')))
self.assertEqual([pair for pair in izip_longest('abc', 'def')],
list(zip('abc', 'def')))
- ids = map(id, izip_longest('abc', 'def'))
+ ids = list(map(id, izip_longest('abc', 'def')))
self.assertEqual(min(ids), max(ids))
- ids = map(id, list(izip_longest('abc', 'def')))
+ ids = list(map(id, list(izip_longest('abc', 'def'))))
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
def test_repeat(self):
@@ -432,7 +434,7 @@
result = tee('abc', n)
self.assertEqual(type(result), tuple)
self.assertEqual(len(result), n)
- self.assertEqual(map(list, result), [list('abc')]*n)
+ self.assertEqual([list(x) for x in result], [list('abc')]*n)
# tee pass-through to copyable iterator
a, b = tee('abc')
@@ -642,7 +644,8 @@
def test_ifilter(self):
for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
for g in (G, I, Ig, S, L, R):
- self.assertEqual(list(ifilter(isEven, g(s))), filter(isEven, g(s)))
+ self.assertEqual(list(ifilter(isEven, g(s))),
+ [x for x in g(s) if isEven(x)])
self.assertRaises(TypeError, ifilter, isEven, X(s))
self.assertRaises(TypeError, ifilter, isEven, N(s))
self.assertRaises(ZeroDivisionError, list, ifilter(isEven, E(s)))
@@ -650,7 +653,8 @@
def test_ifilterfalse(self):
for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
for g in (G, I, Ig, S, L, R):
- self.assertEqual(list(ifilterfalse(isEven, g(s))), filter(isOdd, g(s)))
+ self.assertEqual(list(ifilterfalse(isEven, g(s))),
+ [x for x in g(s) if isOdd(x)])
self.assertRaises(TypeError, ifilterfalse, isEven, X(s))
self.assertRaises(TypeError, ifilterfalse, isEven, N(s))
self.assertRaises(ZeroDivisionError, list, ifilterfalse(isEven, E(s)))
@@ -676,8 +680,10 @@
def test_imap(self):
for s in (range(10), range(0), range(100), (7,11), range(20,50,5)):
for g in (G, I, Ig, S, L, R):
- self.assertEqual(list(imap(onearg, g(s))), map(onearg, g(s)))
- self.assertEqual(list(imap(operator.pow, g(s), g(s))), map(operator.pow, g(s), g(s)))
+ self.assertEqual(list(imap(onearg, g(s))),
+ [onearg(x) for x in g(s)])
+ self.assertEqual(list(imap(operator.pow, g(s), g(s))),
+ [x**x for x in g(s)])
self.assertRaises(TypeError, imap, onearg, X(s))
self.assertRaises(TypeError, imap, onearg, N(s))
self.assertRaises(ZeroDivisionError, list, imap(onearg, E(s)))
@@ -694,7 +700,8 @@
for s in (range(10), range(0), range(100), (7,11), range(20,50,5)):
for g in (G, I, Ig, S, L, R):
ss = lzip(s, s)
- self.assertEqual(list(starmap(operator.pow, g(ss))), map(operator.pow, g(s), g(s)))
+ self.assertEqual(list(starmap(operator.pow, g(ss))),
+ [x**x for x in g(s)])
self.assertRaises(TypeError, starmap, operator.pow, X(ss))
self.assertRaises(TypeError, starmap, operator.pow, N(ss))
self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss)))
@@ -849,7 +856,7 @@
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
>>> di = sorted(sorted(d.items()), key=itemgetter(1))
>>> for k, g in groupby(di, itemgetter(1)):
-... print(k, map(itemgetter(0), g))
+... print(k, list(map(itemgetter(0), g)))
...
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
@@ -860,7 +867,7 @@
# same group.
>>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
>>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]):
-... print(map(operator.itemgetter(1), g))
+... print(list(map(operator.itemgetter(1), g)))
...
[1]
[4, 5, 6]