Index: Doc/library/itertools.rst =================================================================== --- Doc/library/itertools.rst (revision 61062) +++ Doc/library/itertools.rst (working copy) @@ -176,13 +176,10 @@ .. function:: ifilter(predicate, iterable) Make an iterator that filters elements from iterable returning only those for - which the predicate is ``True``. If *predicate* is ``None``, return the items - that are true. This function is the same as the built-in :func:`filter` + which the predicate is true. This function is the same as the built-in :func:`filter` function. Equivalent to:: def ifilter(predicate, iterable): - if predicate is None: - predicate = bool for x in iterable: if predicate(x): yield x Index: Doc/library/functions.rst =================================================================== --- Doc/library/functions.rst (revision 61062) +++ Doc/library/functions.rst (working copy) @@ -403,14 +403,10 @@ Construct an iterator from those elements of *iterable* for which *function* returns true. *iterable* may be either a sequence, a container which - supports iteration, or an iterator. If *function* is ``None``, the identity - function is assumed, that is, all elements of *iterable* that are false are - removed. + supports iteration, or an iterator. Note that ``filter(function, iterable)`` is equivalent to the generator - expression ``(item for item in iterable if function(item))`` if function is - not ``None`` and ``(item for item in iterable if item)`` if function is - ``None``. + expression ``(item for item in iterable if function(item))``. .. function:: float([x]) Index: Lib/test/test_itertools.py =================================================================== --- Lib/test/test_itertools.py (revision 61062) +++ Lib/test/test_itertools.py (working copy) @@ -176,7 +176,7 @@ def test_ifilter(self): self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4]) - self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2]) + self.assertEqual(list(ifilter(bool, [0,1,0,2,0])), [1,2]) self.assertEqual(take(4, ifilter(isEven, count())), [0,2,4,6]) self.assertRaises(TypeError, ifilter) self.assertRaises(TypeError, ifilter, lambda x:x) Index: Lib/test/test_builtin.py =================================================================== --- Lib/test/test_builtin.py (revision 61062) +++ Lib/test/test_builtin.py (working copy) @@ -450,9 +450,9 @@ def test_filter(self): 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(bool, [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(bool, 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 @@ -467,9 +467,10 @@ def badfunc(): pass self.assertRaises(TypeError, list, filter(badfunc, range(5))) + self.assertRaises(TypeError, list, filter(None, range(5))) # test bltinmodule.c::filtertuple() - self.assertEqual(list(filter(None, (1, 2))), [1, 2]) + self.assertEqual(list(filter(bool, (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))) Index: Modules/itertoolsmodule.c =================================================================== --- Modules/itertoolsmodule.c (revision 61062) +++ Modules/itertoolsmodule.c (working copy) @@ -2021,23 +2021,17 @@ assert(PyIter_Check(it)); iternext = *Py_TYPE(it)->tp_iternext; for (;;) { + PyObject *good; item = iternext(it); if (item == NULL) return NULL; - - if (lz->func == Py_None) { - ok = PyObject_IsTrue(item); - } else { - PyObject *good; - good = PyObject_CallFunctionObjArgs(lz->func, - item, NULL); - if (good == NULL) { - Py_DECREF(item); - return NULL; - } - ok = PyObject_IsTrue(good); - Py_DECREF(good); + good = PyObject_CallFunctionObjArgs(lz->func, item, NULL); + if (good == NULL) { + Py_DECREF(item); + return NULL; } + ok = PyObject_IsTrue(good); + Py_DECREF(good); if (ok) return item; Py_DECREF(item); @@ -2045,10 +2039,9 @@ } PyDoc_STRVAR(ifilter_doc, -"ifilter(function or None, sequence) --> ifilter object\n\ +"ifilter(function, sequence) --> ifilter object\n\ \n\ -Return those items of sequence for which function(item) is true.\n\ -If function is None, return the items that are true."); +Return those items of sequence for which function(item) is true."); static PyTypeObject ifilter_type = { PyVarObject_HEAD_INIT(NULL, 0)