[Python-3000-checkins] r56147 - in python/branches/p3yk/Lib: decimal.py encodings/idna.py heapq.py test/test_iter.py test/test_itertools.py test/test_long.py test/test_pwd.py test/test_weakref.py test/test_xml_etree.py test/test_xml_etree_c.py urllib2.py

guido.van.rossum python-3000-checkins at python.org
Mon Jul 2 15:32:03 CEST 2007


Author: guido.van.rossum
Date: Mon Jul  2 15:32:02 2007
New Revision: 56147

Modified:
   python/branches/p3yk/Lib/decimal.py
   python/branches/p3yk/Lib/encodings/idna.py
   python/branches/p3yk/Lib/heapq.py
   python/branches/p3yk/Lib/test/test_iter.py
   python/branches/p3yk/Lib/test/test_itertools.py
   python/branches/p3yk/Lib/test/test_long.py
   python/branches/p3yk/Lib/test/test_pwd.py
   python/branches/p3yk/Lib/test/test_weakref.py
   python/branches/p3yk/Lib/test/test_xml_etree.py
   python/branches/p3yk/Lib/test/test_xml_etree_c.py
   python/branches/p3yk/Lib/urllib2.py
Log:
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.


Modified: python/branches/p3yk/Lib/decimal.py
==============================================================================
--- python/branches/p3yk/Lib/decimal.py	(original)
+++ python/branches/p3yk/Lib/decimal.py	Mon Jul  2 15:32:02 2007
@@ -1193,7 +1193,9 @@
         op1 = _WorkRep(self)
         op2 = _WorkRep(other)
 
-        ans = Decimal((resultsign, map(int, str(op1.int * op2.int)), resultexp))
+        ans = Decimal((resultsign,
+                       tuple(map(int, str(op1.int * op2.int))),
+                       resultexp))
         if shouldround:
             ans = ans._fix(context)
 

Modified: python/branches/p3yk/Lib/encodings/idna.py
==============================================================================
--- python/branches/p3yk/Lib/encodings/idna.py	(original)
+++ python/branches/p3yk/Lib/encodings/idna.py	Mon Jul  2 15:32:02 2007
@@ -38,7 +38,7 @@
             raise UnicodeError("Invalid character %r" % c)
 
     # Check bidi
-    RandAL = map(stringprep.in_table_d1, label)
+    RandAL = [stringprep.in_table_d1(x) for x in label]
     for c in RandAL:
         if c:
             # There is a RandAL char in the string. Must perform further
@@ -47,7 +47,7 @@
             # This is table C.8, which was already checked
             # 2) If a string contains any RandALCat character, the string
             # MUST NOT contain any LCat character.
-            if filter(stringprep.in_table_d2, label):
+            if any(stringprep.in_table_d2(x) for x in label):
                 raise UnicodeError("Violation of BIDI requirement 2")
 
             # 3) If a string contains any RandALCat character, a

Modified: python/branches/p3yk/Lib/heapq.py
==============================================================================
--- python/branches/p3yk/Lib/heapq.py	(original)
+++ python/branches/p3yk/Lib/heapq.py	Mon Jul  2 15:32:02 2007
@@ -129,7 +129,7 @@
 __all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge',
            'nlargest', 'nsmallest']
 
-from itertools import islice, repeat, count, imap, izip, tee
+from itertools import islice, repeat, count, izip, tee
 from operator import itemgetter, neg
 import bisect
 
@@ -225,7 +225,7 @@
     #    O(m) + O(n log m) comparisons.
     h = list(iterable)
     heapify(h)
-    return map(heappop, repeat(h, min(n, len(h))))
+    return list(map(heappop, repeat(h, min(n, len(h)))))
 
 # 'heap' is a heap at all indices >= startpos, except possibly for pos.  pos
 # is the index of a leaf with a possibly out-of-order value.  Restore the
@@ -351,9 +351,9 @@
     Equivalent to:  sorted(iterable, key=key)[:n]
     """
     in1, in2 = tee(iterable)
-    it = izip(imap(key, in1), count(), in2)                 # decorate
+    it = izip(map(key, in1), count(), in2)                  # decorate
     result = _nsmallest(n, it)
-    return map(itemgetter(2), result)                       # undecorate
+    return list(map(itemgetter(2), result))                 # undecorate
 
 _nlargest = nlargest
 def nlargest(n, iterable, key=None):
@@ -362,9 +362,9 @@
     Equivalent to:  sorted(iterable, key=key, reverse=True)[:n]
     """
     in1, in2 = tee(iterable)
-    it = izip(imap(key, in1), imap(neg, count()), in2)      # decorate
+    it = izip(map(key, in1), map(neg, count()), in2)        # decorate
     result = _nlargest(n, it)
-    return map(itemgetter(2), result)                       # undecorate
+    return list(map(itemgetter(2), result))                 # undecorate
 
 if __name__ == "__main__":
     # Simple sanity test

Modified: python/branches/p3yk/Lib/test/test_iter.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_iter.py	(original)
+++ python/branches/p3yk/Lib/test/test_iter.py	Mon Jul  2 15:32:02 2007
@@ -312,13 +312,14 @@
 
     # Test filter()'s use of iterators.
     def test_builtin_filter(self):
-        self.assertEqual(filter(None, SequenceClass(5)), list(range(1, 5)))
-        self.assertEqual(filter(None, SequenceClass(0)), [])
-        self.assertEqual(filter(None, ()), ())
-        self.assertEqual(filter(None, "abc"), "abc")
+        self.assertEqual(list(filter(None, SequenceClass(5))),
+                         list(range(1, 5)))
+        self.assertEqual(list(filter(None, SequenceClass(0))), [])
+        self.assertEqual(list(filter(None, ())), [])
+        self.assertEqual(list(filter(None, "abc")), ["a", "b", "c"])
 
         d = {"one": 1, "two": 2, "three": 3}
-        self.assertEqual(filter(None, d), list(d.keys()))
+        self.assertEqual(list(filter(None, d)), list(d.keys()))
 
         self.assertRaises(TypeError, filter, None, list)
         self.assertRaises(TypeError, filter, None, 42)
@@ -351,8 +352,8 @@
                 return SeqIter(self.vals)
 
         seq = Seq(*([bTrue, bFalse] * 25))
-        self.assertEqual(filter(lambda x: not x, seq), [bFalse]*25)
-        self.assertEqual(filter(lambda x: not x, iter(seq)), [bFalse]*25)
+        self.assertEqual(list(filter(lambda x: not x, seq)), [bFalse]*25)
+        self.assertEqual(list(filter(lambda x: not x, iter(seq))), [bFalse]*25)
 
     # Test max() and min()'s use of iterators.
     def test_builtin_max_min(self):
@@ -388,20 +389,24 @@
 
     # Test map()'s use of iterators.
     def test_builtin_map(self):
-        self.assertEqual(map(None, SequenceClass(5)), list(range(5)))
-        self.assertEqual(map(lambda x: x+1, SequenceClass(5)), list(range(1, 6)))
+        self.assertEqual(list(map(None, SequenceClass(5))),
+                         [(0,), (1,), (2,), (3,), (4,)])
+        self.assertEqual(list(map(lambda x: x+1, SequenceClass(5))),
+                         list(range(1, 6)))
 
         d = {"one": 1, "two": 2, "three": 3}
-        self.assertEqual(map(None, d), list(d.keys()))
-        self.assertEqual(map(lambda k, d=d: (k, d[k]), d), list(d.items()))
+        self.assertEqual(list(map(None, d)), [(k,) for k in d])
+        self.assertEqual(list(map(lambda k, d=d: (k, d[k]), d)),
+                         list(d.items()))
         dkeys = list(d.keys())
         expected = [(i < len(d) and dkeys[i] or None,
                      i,
                      i < len(d) and dkeys[i] or None)
-                    for i in range(5)]
-        self.assertEqual(map(None, d,
-                                   SequenceClass(5),
-                                   iter(d.keys())),
+                    for i in range(3)]
+        self.assertEqual(list(map(None,
+                                  d,
+                                  SequenceClass(5),
+                                  iter(d.keys()))),
                          expected)
 
         f = open(TESTFN, "w")
@@ -412,7 +417,7 @@
             f.close()
         f = open(TESTFN, "r")
         try:
-            self.assertEqual(map(len, f), list(range(1, 21, 2)))
+            self.assertEqual(list(map(len, f)), list(range(1, 21, 2)))
         finally:
             f.close()
             try:

Modified: python/branches/p3yk/Lib/test/test_itertools.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_itertools.py	(original)
+++ python/branches/p3yk/Lib/test/test_itertools.py	Mon Jul  2 15:32:02 2007
@@ -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]

Modified: python/branches/p3yk/Lib/test/test_long.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_long.py	(original)
+++ python/branches/p3yk/Lib/test/test_long.py	Mon Jul  2 15:32:02 2007
@@ -23,9 +23,7 @@
 MAXDIGITS = 15
 
 # build some special values
-special = map(int, [0, 1, 2, BASE, BASE >> 1])
-special.append(0x5555555555555555)
-special.append(0xaaaaaaaaaaaaaaaa)
+special = [0, 1, 2, BASE, BASE >> 1, 0x5555555555555555, 0xaaaaaaaaaaaaaaaa]
 #  some solid strings of one bits
 p2 = 4  # 0 and 1 already added
 for i in range(2*SHIFT):
@@ -33,8 +31,7 @@
     p2 = p2 << 1
 del p2
 # add complements & negations
-special = special + map(lambda x: ~x, special) + \
-                    map(lambda x: -x, special)
+special += [~x for x in special] + [-x for x in special]
 
 
 class LongTest(unittest.TestCase):

Modified: python/branches/p3yk/Lib/test/test_pwd.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_pwd.py	(original)
+++ python/branches/p3yk/Lib/test/test_pwd.py	Mon Jul  2 15:32:02 2007
@@ -59,7 +59,7 @@
         namei = 0
         fakename = allnames[namei]
         while fakename in bynames:
-            chars = map(None, fakename)
+            chars = list(fakename)
             for i in range(len(chars)):
                 if chars[i] == 'z':
                     chars[i] = 'A'
@@ -76,7 +76,7 @@
                 except IndexError:
                     # should never happen... if so, just forget it
                     break
-            fakename = ''.join(map(None, chars))
+            fakename = ''.join(chars)
 
         self.assertRaises(KeyError, pwd.getpwnam, fakename)
 

Modified: python/branches/p3yk/Lib/test/test_weakref.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_weakref.py	(original)
+++ python/branches/p3yk/Lib/test/test_weakref.py	Mon Jul  2 15:32:02 2007
@@ -740,15 +740,15 @@
         items2 = dict.copy().items()
         items1.sort()
         items2.sort()
-        self.assert_(items1 == items2,
+        self.assertEqual(items1, items2,
                      "cloning of weak-valued dictionary did not work!")
         del items1, items2
-        self.assert_(len(dict) == self.COUNT)
+        self.assertEqual(len(dict), self.COUNT)
         del objects[0]
-        self.assert_(len(dict) == (self.COUNT - 1),
+        self.assertEqual(len(dict), self.COUNT - 1,
                      "deleting object did not cause dictionary update")
         del objects, o
-        self.assert_(len(dict) == 0,
+        self.assertEqual(len(dict), 0,
                      "deleting the values did not clear the dictionary")
         # regression on SF bug #447152:
         dict = weakref.WeakValueDictionary()
@@ -875,14 +875,14 @@
 
     def make_weak_keyed_dict(self):
         dict = weakref.WeakKeyDictionary()
-        objects = map(Object, range(self.COUNT))
+        objects = list(map(Object, range(self.COUNT)))
         for o in objects:
             dict[o] = o.arg
         return dict, objects
 
     def make_weak_valued_dict(self):
         dict = weakref.WeakValueDictionary()
-        objects = map(Object, range(self.COUNT))
+        objects = list(map(Object, range(self.COUNT)))
         for o in objects:
             dict[o.arg] = o
         return dict, objects

Modified: python/branches/p3yk/Lib/test/test_xml_etree.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_xml_etree.py	(original)
+++ python/branches/p3yk/Lib/test/test_xml_etree.py	Mon Jul  2 15:32:02 2007
@@ -53,7 +53,7 @@
     return elem.tag
 
 def summarize_list(seq):
-    return map(summarize, seq)
+    return list(map(summarize, seq))
 
 def interface():
     """

Modified: python/branches/p3yk/Lib/test/test_xml_etree_c.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_xml_etree_c.py	(original)
+++ python/branches/p3yk/Lib/test/test_xml_etree_c.py	Mon Jul  2 15:32:02 2007
@@ -51,7 +51,7 @@
     return elem.tag
 
 def summarize_list(seq):
-    return map(summarize, seq)
+    return list(map(summarize, seq))
 
 def interface():
     """

Modified: python/branches/p3yk/Lib/urllib2.py
==============================================================================
--- python/branches/p3yk/Lib/urllib2.py	(original)
+++ python/branches/p3yk/Lib/urllib2.py	Mon Jul  2 15:32:02 2007
@@ -1227,7 +1227,7 @@
             if host:
                 host, port = splitport(host)
             if not host or \
-                (not port and socket.gethostbyname(host) in self.get_names()):
+                (not port and _safe_gethostbyname(host) in self.get_names()):
                 return addinfourl(open(localfile, 'rb'),
                                   headers, 'file:'+file)
         except OSError as msg:
@@ -1235,6 +1235,12 @@
             raise URLError(msg)
         raise URLError('file not on local host')
 
+def _safe_gethostbyname(host):
+    try:
+        return socket.gethostbyname(host)
+    except socket.gaierror:
+        return None
+
 class FTPHandler(BaseHandler):
     def ftp_open(self, req):
         import ftplib


More information about the Python-3000-checkins mailing list