]> granicus.if.org Git - python/commitdiff
Merged revisions 56125-56153 via svnmerge from
authorGuido van Rossum <guido@python.org>
Tue, 3 Jul 2007 08:25:58 +0000 (08:25 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 3 Jul 2007 08:25:58 +0000 (08:25 +0000)
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.
........

45 files changed:
Lib/cgi.py
Lib/csv.py
Lib/decimal.py
Lib/difflib.py
Lib/distutils/dist.py
Lib/distutils/sysconfig.py
Lib/distutils/version.py
Lib/encodings/idna.py
Lib/filecmp.py
Lib/heapq.py
Lib/logging/config.py
Lib/mhlib.py
Lib/optparse.py
Lib/pydoc.py
Lib/string.py
Lib/test/test_bisect.py
Lib/test/test_builtin.py
Lib/test/test_bytes.py
Lib/test/test_cgi.py
Lib/test/test_datetime.py
Lib/test/test_decimal.py
Lib/test/test_extcall.py
Lib/test/test_functools.py
Lib/test/test_genexps.py
Lib/test/test_grp.py
Lib/test/test_hash.py
Lib/test/test_heapq.py
Lib/test/test_inspect.py
Lib/test/test_iter.py
Lib/test/test_itertools.py
Lib/test/test_long.py
Lib/test/test_mailbox.py
Lib/test/test_mhlib.py
Lib/test/test_old_mailbox.py
Lib/test/test_operator.py
Lib/test/test_pwd.py
Lib/test/test_weakref.py
Lib/test/test_xml_etree.py
Lib/test/test_xml_etree_c.py
Lib/textwrap.py
Lib/unittest.py
Lib/urllib2.py
Lib/zipfile.py
Python/bltinmodule.c
setup.py

index f756330fc9392eeb88d7dda8ab3b44a84c5f1837..ab126d53bd65e526123f2f8208a5a59bd123fa92 100755 (executable)
@@ -572,7 +572,7 @@ class FieldStorage:
         if key in self:
             value = self[key]
             if type(value) is type([]):
-                return map(attrgetter('value'), value)
+                return [x.value for x in value]
             else:
                 return value.value
         else:
@@ -594,7 +594,7 @@ class FieldStorage:
         if key in self:
             value = self[key]
             if type(value) is type([]):
-                return map(attrgetter('value'), value)
+                return [x.value for x in value]
             else:
                 return [value.value]
         else:
index 758c79a4d88bc00129d142320cb4318a6197d663..cd0f7d34baebf4555020c770d2e680854851d54b 100644 (file)
@@ -253,7 +253,7 @@ class Sniffer:
         additional chunks as necessary.
         """
 
-        data = filter(None, data.split('\n'))
+        data = list(filter(None, data.split('\n')))
 
         ascii = [chr(c) for c in range(127)] # 7-bit ASCII
 
index 2611f79789ff1f94c4a45cbd60ef249dc1abcb0a..17d67d541d0e1d9e8fa49771479c5ce874ff86c2 100644 (file)
@@ -841,7 +841,7 @@ class Decimal(object):
         if context is None:
             context = getcontext()
 
-        tmp = map(str, self._int)
+        tmp = list(map(str, self._int))
         numdigits = len(self._int)
         leftdigits = self._exp + numdigits
         if eng and not self:  # self = 0eX wants 0[.0[0]]eY, not [[0]0]0eY
@@ -1193,7 +1193,9 @@ class Decimal(object):
         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)
 
@@ -3145,7 +3147,7 @@ def _string2exact(s):
     exp -= len(fracpart)
 
     mantissa = intpart + fracpart
-    tmp = map(int, mantissa)
+    tmp = list(map(int, mantissa))
     backup = tmp
     while tmp and tmp[0] == 0:
         del tmp[0]
index f99265031084e334c17679d26331ba681f893c30..13704158b469d7095d8d3d0dccd012e511baf37e 100644 (file)
@@ -587,7 +587,7 @@ class SequenceMatcher:
         Each group is in the same format as returned by get_opcodes().
 
         >>> from pprint import pprint
-        >>> a = map(str, range(1,40))
+        >>> a = list(map(str, range(1,40)))
         >>> b = a[:]
         >>> b[8:8] = ['i']     # Make an insertion
         >>> b[20] += 'x'       # Make a replacement
index c01724d83a2876eec589ef01c8b8141cb86354f1..8f614765efc2b1343020a3d4e0b4f86714b85ada 100644 (file)
@@ -112,8 +112,7 @@ Common commands: (see '--help-commands' for more)
         ('obsoletes', None,
          "print the list of packages/modules made obsolete")
         ]
-    display_option_names = map(lambda x: translate_longopt(x[0]),
-                               display_options)
+    display_option_names = [translate_longopt(x[0]) for x in display_options]
 
     # negative options are options that exclude other options
     negative_opt = {'quiet': 'verbose'}
@@ -805,7 +804,7 @@ Common commands: (see '--help-commands' for more)
             pkgs = (pkgs or "").split(",")
             for i in range(len(pkgs)):
                 pkgs[i] = pkgs[i].strip()
-            pkgs = filter(None, pkgs)
+            pkgs = [p for p in pkgs if p]
             if "distutils.command" not in pkgs:
                 pkgs.insert(0, "distutils.command")
             self.command_packages = pkgs
index 51f23a24a554d71ad0c613a6f9b91e848d6a2586..346707fa90dc8d3af4a5e767e5564c5b015e9c59 100644 (file)
@@ -372,7 +372,7 @@ def _init_posix():
         if cur_target == '':
             cur_target = cfg_target
             os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
-        elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
+        elif [int(x) for x in cfg_target.split('.')] > [int(x) for x in cur_target.split('.')]:
             my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
                 % (cur_target, cfg_target))
             raise DistutilsPlatformError(my_msg)
index 2db6b18f1e6e9dd53376fc66fb8fcefd26954807..de20e21f70ed1965cb5db172a068a30c9adc6bf6 100644 (file)
@@ -148,7 +148,7 @@ class StrictVersion (Version):
         if patch:
             self.version = tuple(map(int, [major, minor, patch]))
         else:
-            self.version = tuple(map(int, [major, minor]) + [0])
+            self.version = tuple(map(int, [major, minor])) + (0,)
 
         if prerelease:
             self.prerelease = (prerelease[0], int(prerelease_num))
index 55e1643c25daf124f94717c1c4bb1bd909b0a377..b81e5fa158efbb6347362e991ea99ca99bb79aa1 100644 (file)
@@ -38,7 +38,7 @@ def nameprep(label):
             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 @@ def nameprep(label):
             # 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
index 9c0ce5a85127b04b63f457f4b7b75c0b17b01407..ac01613479194a354ff33a7d28cace26d1a56442 100644 (file)
@@ -132,9 +132,9 @@ class dircmp:
     def phase1(self): # Compute common names
         a = dict(izip(imap(os.path.normcase, self.left_list), self.left_list))
         b = dict(izip(imap(os.path.normcase, self.right_list), self.right_list))
-        self.common = map(a.__getitem__, ifilter(b.__contains__, a))
-        self.left_only = map(a.__getitem__, ifilterfalse(b.__contains__, a))
-        self.right_only = map(b.__getitem__, ifilterfalse(a.__contains__, b))
+        self.common = list(map(a.__getitem__, ifilter(b.__contains__, a)))
+        self.left_only = list(map(a.__getitem__, ifilterfalse(b.__contains__, a)))
+        self.right_only = list(map(b.__getitem__, ifilterfalse(a.__contains__, b)))
 
     def phase2(self): # Distinguish files, directories, funnies
         self.common_dirs = []
index f64d8ba8628bf90f5654b4cb9e4ef7870d65060c..f3d0669934dd0a954711681193a094423d6c4c38 100644 (file)
@@ -129,7 +129,7 @@ From all times, sorting has always been a Great Art! :-)
 __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 @@ def nsmallest(n, iterable):
     #    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 @@ def nsmallest(n, iterable, key=None):
     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 @@ def nlargest(n, iterable, key=None):
     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
index 0bf79a5a2064320ae8331e63d154c440ffad8c89..99755e2ec9cc75bd40096c992bdcc5cbfbeee1a5 100644 (file)
@@ -176,7 +176,7 @@ def _install_loggers(cp, handlers):
     # configure the root first
     llist = cp.get("loggers", "keys")
     llist = llist.split(",")
-    llist = map(lambda x: x.strip(), llist)
+    llist = list(map(lambda x: x.strip(), llist))
     llist.remove("root")
     sectname = "logger_root"
     root = logging.root
index 4b3202091216ce81d32e73406040ca694e298b20..c06e1a656643e0ab29427a031698fcbd3bccf7b5 100644 (file)
@@ -282,8 +282,7 @@ class Folder:
         for name in os.listdir(self.getfullname()):
             if match(name):
                 append(name)
-        messages = map(int, messages)
-        messages.sort()
+        messages = sorted(map(int, messages))
         if messages:
             self.last = messages[-1]
         else:
index ed51b938204a12372265175ba3842dd432889a88..e648a281e4ca7a173d81b3e492ea4892d251d324 100644 (file)
@@ -573,7 +573,7 @@ class Option:
         # Filter out None because early versions of Optik had exactly
         # one short option and one long option, either of which
         # could be None.
-        opts = filter(None, opts)
+        opts = [opt for opt in opts if opt]
         if not opts:
             raise TypeError("at least one option string must be supplied")
         return opts
index 45943334dd3ffdfc02395521ce65ec9c57af549a..3b3f1bd959da08cf14e25175571eecccfd972bfd 100755 (executable)
@@ -468,9 +468,9 @@ class HTMLDoc(Doc):
     def multicolumn(self, list, format, cols=4):
         """Format a list of items into a multi-column list."""
         result = ''
-        rows = (len(list)+cols-1)/cols
+        rows = (len(list)+cols-1)//cols
         for col in range(cols):
-            result = result + '<td width="%d%%" valign=top>' % (100/cols)
+            result = result + '<td width="%d%%" valign=top>' % (100//cols)
             for i in range(rows*col, rows*col+rows):
                 if i < len(list):
                     result = result + format(list[i]) + '<br>\n'
index fd8e91be731d1c19d4ebc784af9c3ef41e13787c..51b2067ab9430436b145cf62cbcd9cd89c15f056 100644 (file)
@@ -30,9 +30,7 @@ printable = digits + letters + punctuation + whitespace
 
 # Case conversion helpers
 # Use str to convert Unicode literal in case of -U
-l = map(chr, range(256))
-_idmap = str('').join(l)
-del l
+_idmap = str('').join(chr(c) for c in range(256))
 
 # Functions which aren't available as string methods.
 
@@ -63,11 +61,10 @@ def maketrans(fromstr, tostr):
         raise ValueError, "maketrans arguments must have same length"
     global _idmapL
     if not _idmapL:
-        _idmapL = map(None, _idmap)
+        _idmapL = list(_idmap)
     L = _idmapL[:]
-    fromstr = map(ord, fromstr)
-    for i in range(len(fromstr)):
-        L[fromstr[i]] = tostr[i]
+    for i, c in enumerate(fromstr):
+        L[ord(c)] = tostr[i]
     return ''.join(L)
 
 
index 0baeb1414fe9402c54e13115e050963ad6ed47dc..95eafbe06ba8d7fd5359158da13bc24a2d9694c2 100644 (file)
@@ -223,7 +223,7 @@ This example uses bisect() to look up a letter grade for an exam total
     ...
     >>> grade(66)
     'C'
-    >>> map(grade, [33, 99, 77, 44, 12, 88])
+    >>> list(map(grade, [33, 99, 77, 44, 12, 88]))
     ['E', 'A', 'B', 'D', 'F', 'A']
 
 """
index a2fde023e3120beb53e7896aa822d6be129307c4..036a9f274f9c435861ebf48d7b82dfe47e28c878 100644 (file)
@@ -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')
index 10833217050411b96ae0ebb6240e6713fb530256..a48cf9cbd3b24690218796fa880060d97cce23b0 100644 (file)
@@ -158,7 +158,7 @@ class BytesTest(unittest.TestCase):
                 b = b"x"*20
                 n = f.readinto(b)
             self.assertEqual(n, len(short_sample))
-            self.assertEqual(b, sample)
+            self.assertEqual(list(b), list(sample))
             # Test writing in binary mode
             with open(tfn, "wb") as f:
                 f.write(b)
@@ -172,7 +172,7 @@ class BytesTest(unittest.TestCase):
                 pass
 
     def test_reversed(self):
-        input = map(ord, "Hello")
+        input = list(map(ord, "Hello"))
         b = bytes(input)
         output = list(reversed(b))
         input.reverse()
@@ -469,7 +469,7 @@ class BytesTest(unittest.TestCase):
         self.assertEqual(b"".join([]), bytes())
         self.assertEqual(b"".join([bytes()]), bytes())
         for part in [("abc",), ("a", "bc"), ("ab", "c"), ("a", "b", "c")]:
-            lst = map(bytes, part)
+            lst = list(map(bytes, part))
             self.assertEqual(b"".join(lst), bytes("abc"))
             self.assertEqual(b"".join(tuple(lst)), bytes("abc"))
             self.assertEqual(b"".join(iter(lst)), bytes("abc"))
index 0bf18a15e44656edb75fd2917b6544c64e84e799..6d5bfd668129b129348b1835b7ca102fb34c0bf4 100644 (file)
@@ -121,10 +121,11 @@ def norm(seq):
     return sorted(seq, key=repr)
 
 def first_elts(list):
-    return map(lambda x:x[0], list)
+    return [p[0] for p in list]
 
 def first_second_elts(list):
-    return map(lambda p:(p[0], p[1][0]), list)
+    return [(p[0], p[1][0]) for p in list]
+
 
 class CgiTests(unittest.TestCase):
 
index b827658579fcae906c43bc6bec34d829f3dac95c..3a0b7af5f83d74c61b1e568f0ac329b845b6e1c0 100644 (file)
@@ -830,8 +830,7 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
             320  348  376
             325  353  381
         """
-        iso_long_years = map(int, ISO_LONG_YEARS_TABLE.split())
-        iso_long_years.sort()
+        iso_long_years = sorted(map(int, ISO_LONG_YEARS_TABLE.split()))
         L = []
         for i in range(400):
             d = self.theclass(2000+i, 12, 31)
index 641f51c3c337b8a07238ad9fcb22603449d1de2d..f515405da95757db2f5385ce5c7735b1cba95c64 100644 (file)
@@ -39,7 +39,7 @@ except ImportError:
     threading = None
 
 # Useful Test Constant
-Signals = getcontext().flags.keys()
+Signals = tuple(getcontext().flags.keys())
 
 # Tests are built around these assumed context defaults.
 # test_main() restores the original context.
@@ -171,7 +171,7 @@ class DecimalTest(unittest.TestCase):
             return self.eval_equation(s)
 
     def eval_directive(self, s):
-        funct, value = map(lambda x: x.strip().lower(), s.split(':'))
+        funct, value = (x.strip().lower() for x in s.split(':'))
         if funct == 'rounding':
             value = RoundingDict[value]
         else:
@@ -842,7 +842,7 @@ class DecimalUsabilityTest(unittest.TestCase):
         self.assertNotEqual(da, object)
 
         # sortable
-        a = map(Decimal, range(100))
+        a = list(map(Decimal, range(100)))
         b =  a[:]
         random.shuffle(a)
         a.sort()
index b183189fcf211436c06911f594d3efcf2ad760a4..40ebad0bcf5b7ed14a168927148c9ef1aac90067 100644 (file)
@@ -263,8 +263,7 @@ for args in ['', 'a', 'ab']:
         for vararg in ['', 'v']:
             for kwarg in ['', 'k']:
                 name = 'z' + args + defargs + vararg + kwarg
-                arglist = list(args) + map(
-                    lambda x: '%s="%s"' % (x, x), defargs)
+                arglist = list(args) + ['%s="%s"' % (x, x) for x in defargs]
                 if vararg: arglist.append('*' + vararg)
                 if kwarg: arglist.append('**' + kwarg)
                 decl = (('def %s(%s): print("ok %s", a, b, d, e, v, ' +
index 55c549f6ab244a0fdaff075aaff977ffcf6da0c5..a1e5d13af3c16830f97c1e672b78c89e7efc5fa2 100644 (file)
@@ -19,6 +19,7 @@ def capture(*args, **kw):
     """capture all positional and keyword arguments"""
     return args, kw
 
+
 class TestPartial(unittest.TestCase):
 
     thetype = functools.partial
@@ -28,7 +29,7 @@ class TestPartial(unittest.TestCase):
         self.assertEqual(p(3, 4, b=30, c=40),
                          ((1, 2, 3, 4), dict(a=10, b=30, c=40)))
         p = self.thetype(map, lambda x: x*10)
-        self.assertEqual(p([1,2,3,4]), [10, 20, 30, 40])
+        self.assertEqual(list(p([1,2,3,4])), [10, 20, 30, 40])
 
     def test_attributes(self):
         p = self.thetype(capture, 1, 2, a=10, b=20)
@@ -134,7 +135,7 @@ class TestPartial(unittest.TestCase):
         self.assertRaises(ReferenceError, getattr, p, 'func')
 
     def test_with_bound_and_unbound_methods(self):
-        data = map(str, range(10))
+        data = list(map(str, range(10)))
         join = self.thetype(str.join, '')
         self.assertEqual(join(data), '0123456789')
         join = self.thetype(''.join)
index 7b5fdc451e1deccfd3fd4f0e2687e29f626688b0..ee582e30521109eac8839d252b31ba6424d2fb2e 100644 (file)
@@ -128,7 +128,7 @@ Verify late binding for the innermost for-expression
 
 Verify re-use of tuples (a side benefit of using genexps over listcomps)
 
-    >>> tupleids = map(id, ((i,i) for i in range(10)))
+    >>> tupleids = list(map(id, ((i,i) for i in range(10))))
     >>> int(max(tupleids) - min(tupleids))
     0
 
index ede9c72821be181f9497e629bcdbf0a1a7efdb6c..978dee98cb5a8ac45454fe30753daed36e7c7e2b 100755 (executable)
@@ -54,7 +54,7 @@ class GroupDatabaseTestCase(unittest.TestCase):
         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'
@@ -71,7 +71,7 @@ class GroupDatabaseTestCase(unittest.TestCase):
                 except IndexError:
                     # should never happen... if so, just forget it
                     break
-            fakename = ''.join(map(None, chars))
+            fakename = ''.join(chars)
 
         self.assertRaises(KeyError, grp.getgrnam, fakename)
 
index 9578537be9fe953acc8c00eaa89d4d63ef815f2c..eff0c7c5e5370a1d16bfdb8a89177cb300f6ffe9 100644 (file)
@@ -11,7 +11,7 @@ class HashEqualityTestCase(unittest.TestCase):
     def same_hash(self, *objlist):
         # Hash each object given and fail if
         # the hash values are not all the same.
-        hashed = map(hash, objlist)
+        hashed = list(map(hash, objlist))
         for h in hashed[1:]:
             if h != hashed[0]:
                 self.fail("hashed values differ: %r" % (objlist,))
index ceaf3cc0eed564d267e092c887d8f2dd9a1e85a2..62f9662ee6bddd1e61f71f870ca2731c142a178d 100644 (file)
@@ -130,16 +130,17 @@ class TestHeap(unittest.TestCase):
         data = [(random.randrange(2000), i) for i in range(1000)]
         for f in (None, lambda x:  x[0] * 547 % 2000):
             for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
-                self.assertEqual(nsmallest(n, data), sorted(data)[:n])
-                self.assertEqual(nsmallest(n, data, key=f),
+                self.assertEqual(list(nsmallest(n, data)), sorted(data)[:n])
+                self.assertEqual(list(nsmallest(n, data, key=f)),
                                  sorted(data, key=f)[:n])
 
     def test_nlargest(self):
         data = [(random.randrange(2000), i) for i in range(1000)]
         for f in (None, lambda x:  x[0] * 547 % 2000):
             for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
-                self.assertEqual(nlargest(n, data), sorted(data, reverse=True)[:n])
-                self.assertEqual(nlargest(n, data, key=f),
+                self.assertEqual(list(nlargest(n, data)),
+                                 sorted(data, reverse=True)[:n])
+                self.assertEqual(list(nlargest(n, data, key=f)),
                                  sorted(data, key=f, reverse=True)[:n])
 
 
@@ -279,8 +280,8 @@ class TestErrorHandling(unittest.TestCase):
         for f in  (nlargest, nsmallest):
             for s in ("123", "", range(1000), (1, 1.2), range(2000,2200,5)):
                 for g in (G, I, Ig, L, R):
-                    self.assertEqual(f(2, g(s)), f(2,s))
-                self.assertEqual(f(2, S(s)), [])
+                    self.assertEqual(list(f(2, g(s))), list(f(2,s)))
+                self.assertEqual(list(f(2, S(s))), [])
                 self.assertRaises(TypeError, f, 2, X(s))
                 self.assertRaises(TypeError, f, 2, N(s))
                 self.assertRaises(ZeroDivisionError, f, 2, E(s))
index bdd7c34eb0ce84f0dfdcfb8e5941bf5751b0f9df..75bd4087e512b8013f8e7513b3a535a974e857ed 100644 (file)
@@ -44,7 +44,7 @@ class IsTestBase(unittest.TestCase):
 
 class TestPredicates(IsTestBase):
     def test_thirteen(self):
-        count = len(filter(lambda x:x.startswith('is'), dir(inspect)))
+        count = len([x for x in dir(inspect) if x.startswith('is')])
         # Doc/lib/libinspect.tex claims there are 13 such functions
         expected = 13
         err_msg = "There are %d (not %d) is* functions" % (count, expected)
index f7712db7b706d795c149156a1e0fd1036ad3e9f4..b92c50aad773aba170b83415ea76df52cc026c35 100644 (file)
@@ -305,13 +305,14 @@ class TestCase(unittest.TestCase):
 
     # 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)
@@ -344,8 +345,8 @@ class TestCase(unittest.TestCase):
                 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):
@@ -381,20 +382,24 @@ class TestCase(unittest.TestCase):
 
     # 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")
@@ -405,7 +410,7 @@ class TestCase(unittest.TestCase):
             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:
index 6dfc52e11b02965c97690a895c85246d0c9fc25b..e3728d8d3325ac6dbeb2e4d6e5d8af052addc5d0 100644 (file)
@@ -199,9 +199,9 @@ class TestBasicOps(unittest.TestCase):
                          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 @@ class TestBasicOps(unittest.TestCase):
                 [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 @@ class TestBasicOps(unittest.TestCase):
         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 @@ class TestBasicOps(unittest.TestCase):
                          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 @@ class TestBasicOps(unittest.TestCase):
             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 @@ class TestVariousIteratorArgs(unittest.TestCase):
     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 @@ class TestVariousIteratorArgs(unittest.TestCase):
     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 @@ class TestVariousIteratorArgs(unittest.TestCase):
     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 @@ class TestVariousIteratorArgs(unittest.TestCase):
         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 @@ Samuele
 >>> 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 @@ Samuele
 # 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]
index 9e56d31c8cd0e5468d5ed539fe1c34b213d91849..140a2b04b1a4262c0996c2dddd4ed6a82bf2d879 100644 (file)
@@ -23,9 +23,7 @@ KARATSUBA_CUTOFF = 70   # from longobject.c
 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 @@ for i in range(2*SHIFT):
     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):
index 84a9e5cdc58aa2794f0910295332a0390dc1560c..e426117cc169f4bdf36d879e353ee0d76caa5580 100644 (file)
@@ -1669,7 +1669,7 @@ class MaildirTestCase(unittest.TestCase):
         self._msgfiles = []
 
     def tearDown(self):
-        map(os.unlink, self._msgfiles)
+        list(map(os.unlink, self._msgfiles))
         os.rmdir(os.path.join(self._dir, "cur"))
         os.rmdir(os.path.join(self._dir, "tmp"))
         os.rmdir(os.path.join(self._dir, "new"))
index f425cb9c0b96362b2b6870ae86195d5de6607dcf..1b1af6a09c5acf8d05c00306b5e9fbdfef9e58ed 100644 (file)
@@ -179,18 +179,17 @@ class MhlibTests(unittest.TestCase):
 
         folders = mh.listallfolders()
         folders.sort()
-        tfolders = map(normF, ['deep', 'deep/f1', 'deep/f2', 'deep/f2/f3',
-                                'inbox', 'wide'])
-        tfolders.sort()
+        tfolders = sorted(map(normF, ['deep', 'deep/f1', 'deep/f2',
+                                      'deep/f2/f3', 'inbox', 'wide']))
         eq(folders, tfolders)
 
         folders = mh.listsubfolders('deep')
         folders.sort()
-        eq(folders, map(normF, ['deep/f1', 'deep/f2']))
+        eq(folders, list(map(normF, ['deep/f1', 'deep/f2'])))
 
         folders = mh.listallsubfolders('deep')
         folders.sort()
-        eq(folders, map(normF, ['deep/f1', 'deep/f2', 'deep/f2/f3']))
+        eq(folders, list(map(normF, ['deep/f1', 'deep/f2', 'deep/f2/f3'])))
         eq(mh.listsubfolders(normF('deep/f2')), [normF('deep/f2/f3')])
 
         eq(mh.listsubfolders('inbox'), [])
index 7bd555758a83023b13e204d7407f687037d437ab..b8815062f4841cc1e57f56e07f4a50d765673dab 100644 (file)
@@ -35,7 +35,7 @@ class MaildirTestCase(unittest.TestCase):
         self._msgfiles = []
 
     def tearDown(self):
-        map(os.unlink, self._msgfiles)
+        list(map(os.unlink, self._msgfiles))
         os.rmdir(os.path.join(self._dir, "cur"))
         os.rmdir(os.path.join(self._dir, "tmp"))
         os.rmdir(os.path.join(self._dir, "new"))
index 8b705e4b3a33539e5ffcd7bea1ceedf4060f374f..6142a7ffb2ff5385bc2231d57c0dc9c880b36121 100644 (file)
@@ -393,12 +393,12 @@ class OperatorTestCase(unittest.TestCase):
         # example used in the docs
         inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
         getcount = operator.itemgetter(1)
-        self.assertEqual(map(getcount, inventory), [3, 2, 5, 1])
+        self.assertEqual(list(map(getcount, inventory)), [3, 2, 5, 1])
         self.assertEqual(sorted(inventory, key=getcount),
             [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)])
 
         # multiple gets
-        data = map(str, range(20))
+        data = list(map(str, range(20)))
         self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5'))
         self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data)
 
index a50ed2168e7e493b785e32f9cfb54c9ff535f049..7abf905af64cf213355fd5e3061dcda08cb3a8c8 100644 (file)
@@ -59,7 +59,7 @@ class PwdTest(unittest.TestCase):
         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 @@ class PwdTest(unittest.TestCase):
                 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)
 
index 92d80318b3035bdc1dc70fbff86d241420eacd47..1399ade6538392d4db4764d2c9ff7ee7f02c034f 100644 (file)
@@ -740,15 +740,15 @@ class MappingTestCase(TestBase):
         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 @@ class MappingTestCase(TestBase):
 
     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
index 4e21fd9973183aaf6932e44c95b6b2c6b599c210..1c5edd0d663a8651182d7f6bdff12fa13a1daadd 100644 (file)
@@ -53,7 +53,7 @@ def summarize(elem):
     return elem.tag
 
 def summarize_list(seq):
-    return map(summarize, seq)
+    return list(map(summarize, seq))
 
 def interface():
     """
index 1479247c92f82634f7e7e04f8e7a1b791526dfc9..49cdfdee14dcba509c69445873d873def41556e3 100644 (file)
@@ -51,7 +51,7 @@ def summarize(elem):
     return elem.tag
 
 def summarize_list(seq):
-    return map(summarize, seq)
+    return list(map(summarize, seq))
 
 def interface():
     """
index 22a625250d74638cd4070305823f75cafe59e041..3afc269241f44915ea56fa91570efa5e12eadda9 100644 (file)
@@ -63,8 +63,8 @@ class TextWrapper:
 
     unicode_whitespace_trans = {}
     uspace = ord(' ')
-    for x in map(ord, _whitespace):
-        unicode_whitespace_trans[x] = uspace
+    for x in _whitespace:
+        unicode_whitespace_trans[ord(x)] = uspace
 
     # This funky little regex is just the trick for splitting
     # text up into word-wrappable chunks.  E.g.
@@ -136,7 +136,7 @@ class TextWrapper:
           'use', ' ', 'the', ' ', '-b', ' ', 'option!'
         """
         chunks = self.wordsep_re.split(text)
-        chunks = filter(None, chunks)  # remove empty chunks
+        chunks = [c for c in chunks if c]
         return chunks
 
     def _fix_sentence_endings(self, chunks):
index a4ced3dd4e2dc377512b8ad22eb3ebc6999835c1..16f062b3ab5cabe7d21dfd586c7c3e69334b2715 100644 (file)
@@ -586,7 +586,7 @@ class TestLoader:
         """
         def isTestMethod(attrname, testCaseClass=testCaseClass, prefix=self.testMethodPrefix):
             return attrname.startswith(prefix) and hasattr(getattr(testCaseClass, attrname), '__call__')
-        testFnNames = filter(isTestMethod, dir(testCaseClass))
+        testFnNames = list(filter(isTestMethod, dir(testCaseClass)))
         if self.sortTestMethodsUsing:
             testFnNames.sort(self.sortTestMethodsUsing)
         return testFnNames
@@ -725,7 +725,7 @@ class TextTestRunner:
         self.stream.writeln()
         if not result.wasSuccessful():
             self.stream.write("FAILED (")
-            failed, errored = map(len, (result.failures, result.errors))
+            failed, errored = len(result.failures), len(result.errors)
             if failed:
                 self.stream.write("failures=%d" % failed)
             if errored:
index 4ae7df4bdf3497fbbe80dd7f30b6cf0f6941d68b..f15da217ccdf24b41f6c25b6a8ef0e316e6134ed 100644 (file)
@@ -1222,7 +1222,7 @@ class FileHandler(BaseHandler):
             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:
@@ -1230,6 +1230,12 @@ class FileHandler(BaseHandler):
             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
@@ -1259,7 +1265,7 @@ class FTPHandler(BaseHandler):
             raise URLError(msg)
         path, attrs = splitattr(req.get_selector())
         dirs = path.split('/')
-        dirs = map(unquote, dirs)
+        dirs = list(map(unquote, dirs))
         dirs, file = dirs[:-1], dirs[-1]
         if dirs and not dirs[0]:
             dirs = dirs[1:]
index 2fb1ea564372715abc6e28811222a00c85cbc891..4791aea5ad45fffac8a3bb19c2f3c0ebfcc4daef 100644 (file)
@@ -793,7 +793,7 @@ class ZipFile:
             #  completely random, while the 12th contains the MSB of the CRC,
             #  and is used to check the correctness of the password.
             bytes = zef_file.read(12)
-            h = map(zd, bytes[0:12])
+            h = list(map(zd, bytes[0:12]))
             if h[11] != ((zinfo.CRC>>24) & 255):
                 raise RuntimeError, "Bad password for file %s" % name
 
index a2e48b1d9cc8c8b0503c4d5796f136be24bdd975..db9ac2c4f89638b8c5541e91648aa216d5017aa1 100644 (file)
@@ -23,11 +23,6 @@ const char *Py_FileSystemDefaultEncoding = "utf-8";
 const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
 #endif
 
-/* Forward */
-static PyObject *filterstring(PyObject *, PyObject *);
-static PyObject *filterunicode(PyObject *, PyObject *);
-static PyObject *filtertuple (PyObject *, PyObject *);
-
 static PyObject *
 builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
 {
@@ -263,121 +258,26 @@ Return the binary representation of an integer or long integer.");
 static PyObject *
 builtin_filter(PyObject *self, PyObject *args)
 {
-       PyObject *func, *seq, *result, *it, *arg;
-       Py_ssize_t len;   /* guess for result list size */
-       register Py_ssize_t j;
-
-       if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
+       PyObject *itertools, *ifilter, *result;
+       itertools = PyImport_ImportModule("itertools");
+       if (itertools == NULL)
                return NULL;
-
-       /* Strings and tuples return a result of the same type. */
-       if (PyString_Check(seq))
-               return filterstring(func, seq);
-       if (PyUnicode_Check(seq))
-               return filterunicode(func, seq);
-       if (PyTuple_Check(seq))
-               return filtertuple(func, seq);
-
-       /* Pre-allocate argument list tuple. */
-       arg = PyTuple_New(1);
-       if (arg == NULL)
+       ifilter = PyObject_GetAttrString(itertools, "ifilter");
+       Py_DECREF(itertools);
+       if (ifilter == NULL)
                return NULL;
-
-       /* Get iterator. */
-       it = PyObject_GetIter(seq);
-       if (it == NULL)
-               goto Fail_arg;
-
-       /* Guess a result list size. */
-       len = _PyObject_LengthHint(seq);
-       if (len < 0) {
-               if (!PyErr_ExceptionMatches(PyExc_TypeError)  &&
-                   !PyErr_ExceptionMatches(PyExc_AttributeError)) {
-                       goto Fail_it;
-               }
-               PyErr_Clear();
-               len = 8;        /* arbitrary */
-       }
-
-       /* Get a result list. */
-       if (PyList_Check(seq) && seq->ob_refcnt == 1) {
-               /* Eww - can modify the list in-place. */
-               Py_INCREF(seq);
-               result = seq;
-       }
-       else {
-               result = PyList_New(len);
-               if (result == NULL)
-                       goto Fail_it;
-       }
-
-       /* Build the result list. */
-       j = 0;
-       for (;;) {
-               PyObject *item;
-               int ok;
-
-               item = PyIter_Next(it);
-               if (item == NULL) {
-                       if (PyErr_Occurred())
-                               goto Fail_result_it;
-                       break;
-               }
-
-               if (func == (PyObject *)&PyBool_Type || func == Py_None) {
-                       ok = PyObject_IsTrue(item);
-               }
-               else {
-                       PyObject *good;
-                       PyTuple_SET_ITEM(arg, 0, item);
-                       good = PyObject_Call(func, arg, NULL);
-                       PyTuple_SET_ITEM(arg, 0, NULL);
-                       if (good == NULL) {
-                               Py_DECREF(item);
-                               goto Fail_result_it;
-                       }
-                       ok = PyObject_IsTrue(good);
-                       Py_DECREF(good);
-               }
-               if (ok) {
-                       if (j < len)
-                               PyList_SET_ITEM(result, j, item);
-                       else {
-                               int status = PyList_Append(result, item);
-                               Py_DECREF(item);
-                               if (status < 0)
-                                       goto Fail_result_it;
-                       }
-                       ++j;
-               }
-               else
-                       Py_DECREF(item);
-       }
-
-
-       /* Cut back result list if len is too big. */
-       if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
-               goto Fail_result_it;
-
-       Py_DECREF(it);
-       Py_DECREF(arg);
+       result = PyObject_Call(ifilter, args, NULL);
+       Py_DECREF(ifilter);
        return result;
-
-Fail_result_it:
-       Py_DECREF(result);
-Fail_it:
-       Py_DECREF(it);
-Fail_arg:
-       Py_DECREF(arg);
-       return NULL;
 }
 
 PyDoc_STRVAR(filter_doc,
-"filter(function or None, sequence) -> list, tuple, or string\n"
-"\n"
-"Return those items of sequence for which function(item) is true.  If\n"
-"function is None, return the items that are true.  If sequence is a tuple\n"
-"or string, return the same type, else return a list.");
+"filter(predicate, iterable) -> iterator\n\
+\n\
+Return an iterator yielding only those elements of the input iterable\n\
+for which the predicate (a Boolean function) returns true.\n\
+If the predicate is None, 'lambda x: bool(x)' is assumed.\n\
+(This is identical to itertools.ifilter().)");
 
 
 static PyObject *
@@ -940,168 +840,28 @@ simultaneously existing objects.  (Hint: it's the object's memory address.)");
 static PyObject *
 builtin_map(PyObject *self, PyObject *args)
 {
-       typedef struct {
-               PyObject *it;   /* the iterator object */
-               int saw_StopIteration;  /* bool:  did the iterator end? */
-       } sequence;
-
-       PyObject *func, *result;
-       sequence *seqs = NULL, *sqp;
-       Py_ssize_t n, len;
-       register int i, j;
-
-       n = PyTuple_Size(args);
-       if (n < 2) {
-               PyErr_SetString(PyExc_TypeError,
-                               "map() requires at least two args");
+       PyObject *itertools, *imap, *result;
+       itertools = PyImport_ImportModule("itertools");
+       if (itertools == NULL)
                return NULL;
-       }
-
-       func = PyTuple_GetItem(args, 0);
-       n--;
-
-       if (func == Py_None && n == 1) {
-               /* map(None, S) is the same as list(S). */
-               return PySequence_List(PyTuple_GetItem(args, 1));
-       }
-
-       /* Get space for sequence descriptors.  Must NULL out the iterator
-        * pointers so that jumping to Fail_2 later doesn't see trash.
-        */
-       if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
-               PyErr_NoMemory();
+       imap = PyObject_GetAttrString(itertools, "imap");
+       Py_DECREF(itertools);
+       if (imap == NULL)
                return NULL;
-       }
-       for (i = 0; i < n; ++i) {
-               seqs[i].it = (PyObject*)NULL;
-               seqs[i].saw_StopIteration = 0;
-       }
-
-       /* Do a first pass to obtain iterators for the arguments, and set len
-        * to the largest of their lengths.
-        */
-       len = 0;
-       for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
-               PyObject *curseq;
-               Py_ssize_t curlen;
-
-               /* Get iterator. */
-               curseq = PyTuple_GetItem(args, i+1);
-               sqp->it = PyObject_GetIter(curseq);
-               if (sqp->it == NULL) {
-                       static char errmsg[] =
-                           "argument %d to map() must support iteration";
-                       char errbuf[sizeof(errmsg) + 25];
-                       PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
-                       PyErr_SetString(PyExc_TypeError, errbuf);
-                       goto Fail_2;
-               }
-
-               /* Update len. */
-               curlen = _PyObject_LengthHint(curseq);
-               if (curlen < 0) {
-                       if (!PyErr_ExceptionMatches(PyExc_TypeError)  &&
-                           !PyErr_ExceptionMatches(PyExc_AttributeError)) {
-                               goto Fail_2;
-                       }
-                       PyErr_Clear();
-                       curlen = 8;  /* arbitrary */
-               }
-               if (curlen > len)
-                       len = curlen;
-       }
-
-       /* Get space for the result list. */
-       if ((result = (PyObject *) PyList_New(len)) == NULL)
-               goto Fail_2;
-
-       /* Iterate over the sequences until all have stopped. */
-       for (i = 0; ; ++i) {
-               PyObject *alist, *item=NULL, *value;
-               int numactive = 0;
-
-               if (func == Py_None && n == 1)
-                       alist = NULL;
-               else if ((alist = PyTuple_New(n)) == NULL)
-                       goto Fail_1;
-
-               for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
-                       if (sqp->saw_StopIteration) {
-                               Py_INCREF(Py_None);
-                               item = Py_None;
-                       }
-                       else {
-                               item = PyIter_Next(sqp->it);
-                               if (item)
-                                       ++numactive;
-                               else {
-                                       if (PyErr_Occurred()) {
-                                               Py_XDECREF(alist);
-                                               goto Fail_1;
-                                       }
-                                       Py_INCREF(Py_None);
-                                       item = Py_None;
-                                       sqp->saw_StopIteration = 1;
-                               }
-                       }
-                       if (alist)
-                               PyTuple_SET_ITEM(alist, j, item);
-                       else
-                               break;
-               }
-
-               if (!alist)
-                       alist = item;
-
-               if (numactive == 0) {
-                       Py_DECREF(alist);
-                       break;
-               }
-
-               if (func == Py_None)
-                       value = alist;
-               else {
-                       value = PyEval_CallObject(func, alist);
-                       Py_DECREF(alist);
-                       if (value == NULL)
-                               goto Fail_1;
-               }
-               if (i >= len) {
-                       int status = PyList_Append(result, value);
-                       Py_DECREF(value);
-                       if (status < 0)
-                               goto Fail_1;
-               }
-               else if (PyList_SetItem(result, i, value) < 0)
-                       goto Fail_1;
-       }
-
-       if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
-               goto Fail_1;
-
-       goto Succeed;
-
-Fail_1:
-       Py_DECREF(result);
-Fail_2:
-       result = NULL;
-Succeed:
-       assert(seqs);
-       for (i = 0; i < n; ++i)
-               Py_XDECREF(seqs[i].it);
-       PyMem_DEL(seqs);
+       result = PyObject_Call(imap, args, NULL);
+       Py_DECREF(imap);
        return result;
 }
 
 PyDoc_STRVAR(map_doc,
-"map(function, sequence[, sequence, ...]) -> list\n\
+"map(function, iterable[, iterable, ...]) -> iterator\n\
 \n\
-Return a list of the results of applying the function to the items of\n\
-the argument sequence(s).  If more than one sequence is given, the\n\
-function is called with an argument list consisting of the corresponding\n\
-item of each sequence, substituting None for missing values when not all\n\
-sequences have the same length.  If the function is None, return a list of\n\
-the items of the sequence (or a list of tuples if more than one sequence).");
+Return an iterator yielding the results of applying the function to the\n\
+items of the argument iterables(s).  If more than one iterable is given,\n\
+the function is called with an argument list consisting of the\n\
+corresponding item of each iterable, until an iterable is exhausted.\n\
+If the function is None, 'lambda *a: a' is assumed.\n\
+(This is identical to itertools.imap().)");
 
 
 static PyObject *
@@ -1570,29 +1330,36 @@ builtin_input(PyObject *self, PyObject *args)
        /* First of all, flush stderr */
        tmp = PyObject_CallMethod(ferr, "flush", "");
        if (tmp == NULL)
-               return NULL;
-       Py_DECREF(tmp);
+               PyErr_Clear();
+       else
+               Py_DECREF(tmp);
 
        /* We should only use (GNU) readline if Python's sys.stdin and
           sys.stdout are the same as C's stdin and stdout, because we
           need to pass it those. */
        tmp = PyObject_CallMethod(fin, "fileno", "");
-       if (tmp == NULL)
-               return NULL;
-       fd = PyInt_AsLong(tmp);
-       if (fd < 0 && PyErr_Occurred())
-               return NULL;
-       Py_DECREF(tmp);
-       tty = fd == fileno(stdin) && isatty(fd);
-       if (tty) {
-               tmp = PyObject_CallMethod(fout, "fileno", "");
-               if (tmp == NULL)
-                       return NULL;
+       if (tmp == NULL) {
+               PyErr_Clear();
+               tty = 0;
+       }
+       else {
                fd = PyInt_AsLong(tmp);
                Py_DECREF(tmp);
                if (fd < 0 && PyErr_Occurred())
                        return NULL;
-               tty = fd == fileno(stdout) && isatty(fd);
+               tty = fd == fileno(stdin) && isatty(fd);
+       }
+       if (tty) {
+               tmp = PyObject_CallMethod(fout, "fileno", "");
+               if (tmp == NULL)
+                       PyErr_Clear();
+               else {
+                       fd = PyInt_AsLong(tmp);
+                       Py_DECREF(tmp);
+                       if (fd < 0 && PyErr_Occurred())
+                               return NULL;
+                       tty = fd == fileno(stdout) && isatty(fd);
+               }
        }
 
        /* If we're interactive, use (GNU) readline */
@@ -1603,8 +1370,9 @@ builtin_input(PyObject *self, PyObject *args)
                PyObject *result;
                tmp = PyObject_CallMethod(fout, "flush", "");
                if (tmp == NULL)
-                       return NULL;
-               Py_DECREF(tmp);
+                       PyErr_Clear();
+               else
+                       Py_DECREF(tmp);
                if (promptarg != NULL) {
                        po = PyObject_Str(promptarg);
                        if (po == NULL)
@@ -1652,8 +1420,9 @@ builtin_input(PyObject *self, PyObject *args)
        }
        tmp = PyObject_CallMethod(fout, "flush", "");
        if (tmp == NULL)
-               return NULL;
-       Py_DECREF(tmp);
+               PyErr_Clear();
+       else
+               Py_DECREF(tmp);
        return PyFile_GetLine(fin, -1);
 }
 
@@ -1921,7 +1690,7 @@ PyDoc_STRVAR(zip_doc,
 Return an iterator yielding tuples, where each tuple contains the\n\
 corresponding element from each of the argument iterables.\n\
 The returned iterator ends when the shortest argument iterable is exhausted.\n\
-NOTE: This is implemented using itertools.izip().");
+(This is identical to itertools.izip().)");
 
 
 static PyMethodDef builtin_methods[] = {
@@ -2048,262 +1817,3 @@ _PyBuiltin_Init(void)
 #undef ADD_TO_ALL
 #undef SETBUILTIN
 }
-
-/* Helper for filter(): filter a tuple through a function */
-
-static PyObject *
-filtertuple(PyObject *func, PyObject *tuple)
-{
-       PyObject *result;
-       Py_ssize_t i, j;
-       Py_ssize_t len = PyTuple_Size(tuple);
-
-       if (len == 0) {
-               if (PyTuple_CheckExact(tuple))
-                       Py_INCREF(tuple);
-               else
-                       tuple = PyTuple_New(0);
-               return tuple;
-       }
-
-       if ((result = PyTuple_New(len)) == NULL)
-               return NULL;
-
-       for (i = j = 0; i < len; ++i) {
-               PyObject *item, *good;
-               int ok;
-
-               if (tuple->ob_type->tp_as_sequence &&
-                   tuple->ob_type->tp_as_sequence->sq_item) {
-                       item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
-                       if (item == NULL)
-                               goto Fail_1;
-               } else {
-                       PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
-                       goto Fail_1;
-               }
-               if (func == Py_None) {
-                       Py_INCREF(item);
-                       good = item;
-               }
-               else {
-                       PyObject *arg = PyTuple_Pack(1, item);
-                       if (arg == NULL) {
-                               Py_DECREF(item);
-                               goto Fail_1;
-                       }
-                       good = PyEval_CallObject(func, arg);
-                       Py_DECREF(arg);
-                       if (good == NULL) {
-                               Py_DECREF(item);
-                               goto Fail_1;
-                       }
-               }
-               ok = PyObject_IsTrue(good);
-               Py_DECREF(good);
-               if (ok) {
-                       if (PyTuple_SetItem(result, j++, item) < 0)
-                               goto Fail_1;
-               }
-               else
-                       Py_DECREF(item);
-       }
-
-       if (_PyTuple_Resize(&result, j) < 0)
-               return NULL;
-
-       return result;
-
-Fail_1:
-       Py_DECREF(result);
-       return NULL;
-}
-
-
-/* Helper for filter(): filter a string through a function */
-
-static PyObject *
-filterstring(PyObject *func, PyObject *strobj)
-{
-       PyObject *result;
-       Py_ssize_t i, j;
-       Py_ssize_t len = PyString_Size(strobj);
-       Py_ssize_t outlen = len;
-
-       if (func == Py_None) {
-               /* If it's a real string we can return the original,
-                * as no character is ever false and __getitem__
-                * does return this character. If it's a subclass
-                * we must go through the __getitem__ loop */
-               if (PyString_CheckExact(strobj)) {
-                       Py_INCREF(strobj);
-                       return strobj;
-               }
-       }
-       if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
-               return NULL;
-
-       for (i = j = 0; i < len; ++i) {
-               PyObject *item;
-               int ok;
-
-               item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
-               if (item == NULL)
-                       goto Fail_1;
-               if (func==Py_None) {
-                       ok = 1;
-               } else {
-                       PyObject *arg, *good;
-                       arg = PyTuple_Pack(1, item);
-                       if (arg == NULL) {
-                               Py_DECREF(item);
-                               goto Fail_1;
-                       }
-                       good = PyEval_CallObject(func, arg);
-                       Py_DECREF(arg);
-                       if (good == NULL) {
-                               Py_DECREF(item);
-                               goto Fail_1;
-                       }
-                       ok = PyObject_IsTrue(good);
-                       Py_DECREF(good);
-               }
-               if (ok) {
-                       Py_ssize_t reslen;
-                       if (!PyString_Check(item)) {
-                               PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
-                                       " __getitem__ returned different type");
-                               Py_DECREF(item);
-                               goto Fail_1;
-                       }
-                       reslen = PyString_GET_SIZE(item);
-                       if (reslen == 1) {
-                               PyString_AS_STRING(result)[j++] =
-                                       PyString_AS_STRING(item)[0];
-                       } else {
-                               /* do we need more space? */
-                               Py_ssize_t need = j + reslen + len-i-1;
-                               if (need > outlen) {
-                                       /* overallocate, to avoid reallocations */
-                                       if (need<2*outlen)
-                                               need = 2*outlen;
-                                       if (_PyString_Resize(&result, need)) {
-                                               Py_DECREF(item);
-                                               return NULL;
-                                       }
-                                       outlen = need;
-                               }
-                               memcpy(
-                                       PyString_AS_STRING(result) + j,
-                                       PyString_AS_STRING(item),
-                                       reslen
-                               );
-                               j += reslen;
-                       }
-               }
-               Py_DECREF(item);
-       }
-
-       if (j < outlen)
-               _PyString_Resize(&result, j);
-
-       return result;
-
-Fail_1:
-       Py_DECREF(result);
-       return NULL;
-}
-
-/* Helper for filter(): filter a Unicode object through a function */
-
-static PyObject *
-filterunicode(PyObject *func, PyObject *strobj)
-{
-       PyObject *result;
-       register Py_ssize_t i, j;
-       Py_ssize_t len = PyUnicode_GetSize(strobj);
-       Py_ssize_t outlen = len;
-
-       if (func == Py_None) {
-               /* If it's a real string we can return the original,
-                * as no character is ever false and __getitem__
-                * does return this character. If it's a subclass
-                * we must go through the __getitem__ loop */
-               if (PyUnicode_CheckExact(strobj)) {
-                       Py_INCREF(strobj);
-                       return strobj;
-               }
-       }
-       if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
-               return NULL;
-
-       for (i = j = 0; i < len; ++i) {
-               PyObject *item, *arg, *good;
-               int ok;
-
-               item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
-               if (item == NULL)
-                       goto Fail_1;
-               if (func == Py_None) {
-                       ok = 1;
-               } else {
-                       arg = PyTuple_Pack(1, item);
-                       if (arg == NULL) {
-                               Py_DECREF(item);
-                               goto Fail_1;
-                       }
-                       good = PyEval_CallObject(func, arg);
-                       Py_DECREF(arg);
-                       if (good == NULL) {
-                               Py_DECREF(item);
-                               goto Fail_1;
-                       }
-                       ok = PyObject_IsTrue(good);
-                       Py_DECREF(good);
-               }
-               if (ok) {
-                       Py_ssize_t reslen;
-                       if (!PyUnicode_Check(item)) {
-                               PyErr_SetString(PyExc_TypeError,
-                               "can't filter unicode to unicode:"
-                               " __getitem__ returned different type");
-                               Py_DECREF(item);
-                               goto Fail_1;
-                       }
-                       reslen = PyUnicode_GET_SIZE(item);
-                       if (reslen == 1)
-                               PyUnicode_AS_UNICODE(result)[j++] =
-                                       PyUnicode_AS_UNICODE(item)[0];
-                       else {
-                               /* do we need more space? */
-                               Py_ssize_t need = j + reslen + len - i - 1;
-                               if (need > outlen) {
-                                       /* overallocate,
-                                          to avoid reallocations */
-                                       if (need < 2 * outlen)
-                                               need = 2 * outlen;
-                                       if (PyUnicode_Resize(
-                                               &result, need) < 0) {
-                                               Py_DECREF(item);
-                                               goto Fail_1;
-                                       }
-                                       outlen = need;
-                               }
-                               memcpy(PyUnicode_AS_UNICODE(result) + j,
-                                      PyUnicode_AS_UNICODE(item),
-                                      reslen*sizeof(Py_UNICODE));
-                               j += reslen;
-                       }
-               }
-               Py_DECREF(item);
-       }
-
-       if (j < outlen)
-               PyUnicode_Resize(&result, j);
-
-       return result;
-
-Fail_1:
-       Py_DECREF(result);
-       return NULL;
-}
index b1e1c1e938632d4a4bb5f9f5cc4a55e5d6990754..87673d17964da8516a5081fce39fa4264d9f399c 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -728,7 +728,7 @@ class PyBuildExt(build_ext):
                     db_incdir.replace("include", 'lib64'),
                     db_incdir.replace("include", 'lib'),
                 ]
-                db_dirs_to_check = filter(os.path.isdir, db_dirs_to_check)
+                db_dirs_to_check = [x for x in db_dirs_to_check if os.path.isdir(x)]
 
                 # Look for a version specific db-X.Y before an ambiguoius dbX
                 # XXX should we -ever- look for a dbX name?  Do any
@@ -1555,7 +1555,7 @@ def main():
           description = "A high-level object-oriented programming language",
           long_description = SUMMARY.strip(),
           license = "PSF license",
-          classifiers = filter(None, CLASSIFIERS.split("\n")),
+          classifiers = [x for x in CLASSIFIERS.split("\n") if x],
           platforms = ["Many"],
 
           # Build info