Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 17301e9

Browse files
committed
Issue 2186 and 2187. Move filter from itertools to builtins.
1 parent 45832ea commit 17301e9

5 files changed

Lines changed: 138 additions & 180 deletions

File tree

Doc/library/itertools.rst

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -234,21 +234,6 @@ loops that truncate the stream.
234234
self.currkey = self.keyfunc(self.currvalue)
235235

236236

237-
.. function:: ifilter(predicate, iterable)
238-
239-
Make an iterator that filters elements from iterable returning only those for
240-
which the predicate is ``True``. If *predicate* is ``None``, return the items
241-
that are true. This function is the same as the built-in :func:`filter`
242-
function. Equivalent to::
243-
244-
def ifilter(predicate, iterable):
245-
if predicate is None:
246-
predicate = bool
247-
for x in iterable:
248-
if predicate(x):
249-
yield x
250-
251-
252237
.. function:: ifilterfalse(predicate, iterable)
253238

254239
Make an iterator that filters elements from iterable returning only those for
@@ -606,13 +591,13 @@ which incur interpreter overhead. ::
606591

607592
def any(seq, pred=None):
608593
"Returns True if pred(x) is true for at least one element in the iterable"
609-
for elem in ifilter(pred, seq):
594+
for elem in filter(pred, seq):
610595
return True
611596
return False
612597

613598
def no(seq, pred=None):
614599
"Returns True if pred(x) is false for every element in the iterable"
615-
for elem in ifilter(pred, seq):
600+
for elem in filter(pred, seq):
616601
return False
617602
return True
618603

Lib/filecmp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import os
1313
import stat
1414
import warnings
15-
from itertools import ifilter, ifilterfalse, imap, izip
15+
from itertools import ifilterfalse, imap, izip
1616

1717
__all__ = ["cmp","dircmp","cmpfiles"]
1818

@@ -132,7 +132,7 @@ def phase0(self): # Compare everything except common subdirectories
132132
def phase1(self): # Compute common names
133133
a = dict(izip(imap(os.path.normcase, self.left_list), self.left_list))
134134
b = dict(izip(imap(os.path.normcase, self.right_list), self.right_list))
135-
self.common = list(map(a.__getitem__, ifilter(b.__contains__, a)))
135+
self.common = list(map(a.__getitem__, filter(b.__contains__, a)))
136136
self.left_only = list(map(a.__getitem__, ifilterfalse(b.__contains__, a)))
137137
self.right_only = list(map(b.__getitem__, ifilterfalse(a.__contains__, b)))
138138

Lib/test/test_itertools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from functools import reduce
99
maxsize = test_support.MAX_Py_ssize_t
1010
minsize = -maxsize-1
11+
ifilter = filter
1112

1213
def lzip(*args):
1314
return list(zip(*args))

Modules/itertoolsmodule.c

Lines changed: 0 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,149 +2208,6 @@ static PyTypeObject combinations_type = {
22082208
};
22092209

22102210

2211-
/* ifilter object ************************************************************/
2212-
2213-
typedef struct {
2214-
PyObject_HEAD
2215-
PyObject *func;
2216-
PyObject *it;
2217-
} ifilterobject;
2218-
2219-
static PyTypeObject ifilter_type;
2220-
2221-
static PyObject *
2222-
ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2223-
{
2224-
PyObject *func, *seq;
2225-
PyObject *it;
2226-
ifilterobject *lz;
2227-
2228-
if (type == &ifilter_type && !_PyArg_NoKeywords("ifilter()", kwds))
2229-
return NULL;
2230-
2231-
if (!PyArg_UnpackTuple(args, "ifilter", 2, 2, &func, &seq))
2232-
return NULL;
2233-
2234-
/* Get iterator. */
2235-
it = PyObject_GetIter(seq);
2236-
if (it == NULL)
2237-
return NULL;
2238-
2239-
/* create ifilterobject structure */
2240-
lz = (ifilterobject *)type->tp_alloc(type, 0);
2241-
if (lz == NULL) {
2242-
Py_DECREF(it);
2243-
return NULL;
2244-
}
2245-
Py_INCREF(func);
2246-
lz->func = func;
2247-
lz->it = it;
2248-
2249-
return (PyObject *)lz;
2250-
}
2251-
2252-
static void
2253-
ifilter_dealloc(ifilterobject *lz)
2254-
{
2255-
PyObject_GC_UnTrack(lz);
2256-
Py_XDECREF(lz->func);
2257-
Py_XDECREF(lz->it);
2258-
Py_TYPE(lz)->tp_free(lz);
2259-
}
2260-
2261-
static int
2262-
ifilter_traverse(ifilterobject *lz, visitproc visit, void *arg)
2263-
{
2264-
Py_VISIT(lz->it);
2265-
Py_VISIT(lz->func);
2266-
return 0;
2267-
}
2268-
2269-
static PyObject *
2270-
ifilter_next(ifilterobject *lz)
2271-
{
2272-
PyObject *item;
2273-
PyObject *it = lz->it;
2274-
long ok;
2275-
PyObject *(*iternext)(PyObject *);
2276-
2277-
assert(PyIter_Check(it));
2278-
iternext = *Py_TYPE(it)->tp_iternext;
2279-
for (;;) {
2280-
item = iternext(it);
2281-
if (item == NULL)
2282-
return NULL;
2283-
2284-
if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) {
2285-
ok = PyObject_IsTrue(item);
2286-
} else {
2287-
PyObject *good;
2288-
good = PyObject_CallFunctionObjArgs(lz->func,
2289-
item, NULL);
2290-
if (good == NULL) {
2291-
Py_DECREF(item);
2292-
return NULL;
2293-
}
2294-
ok = PyObject_IsTrue(good);
2295-
Py_DECREF(good);
2296-
}
2297-
if (ok)
2298-
return item;
2299-
Py_DECREF(item);
2300-
}
2301-
}
2302-
2303-
PyDoc_STRVAR(ifilter_doc,
2304-
"ifilter(function or None, sequence) --> ifilter object\n\
2305-
\n\
2306-
Return those items of sequence for which function(item) is true.\n\
2307-
If function is None, return the items that are true.");
2308-
2309-
static PyTypeObject ifilter_type = {
2310-
PyVarObject_HEAD_INIT(NULL, 0)
2311-
"itertools.ifilter", /* tp_name */
2312-
sizeof(ifilterobject), /* tp_basicsize */
2313-
0, /* tp_itemsize */
2314-
/* methods */
2315-
(destructor)ifilter_dealloc, /* tp_dealloc */
2316-
0, /* tp_print */
2317-
0, /* tp_getattr */
2318-
0, /* tp_setattr */
2319-
0, /* tp_compare */
2320-
0, /* tp_repr */
2321-
0, /* tp_as_number */
2322-
0, /* tp_as_sequence */
2323-
0, /* tp_as_mapping */
2324-
0, /* tp_hash */
2325-
0, /* tp_call */
2326-
0, /* tp_str */
2327-
PyObject_GenericGetAttr, /* tp_getattro */
2328-
0, /* tp_setattro */
2329-
0, /* tp_as_buffer */
2330-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2331-
Py_TPFLAGS_BASETYPE, /* tp_flags */
2332-
ifilter_doc, /* tp_doc */
2333-
(traverseproc)ifilter_traverse, /* tp_traverse */
2334-
0, /* tp_clear */
2335-
0, /* tp_richcompare */
2336-
0, /* tp_weaklistoffset */
2337-
PyObject_SelfIter, /* tp_iter */
2338-
(iternextfunc)ifilter_next, /* tp_iternext */
2339-
0, /* tp_methods */
2340-
0, /* tp_members */
2341-
0, /* tp_getset */
2342-
0, /* tp_base */
2343-
0, /* tp_dict */
2344-
0, /* tp_descr_get */
2345-
0, /* tp_descr_set */
2346-
0, /* tp_dictoffset */
2347-
0, /* tp_init */
2348-
0, /* tp_alloc */
2349-
ifilter_new, /* tp_new */
2350-
PyObject_GC_Del, /* tp_free */
2351-
};
2352-
2353-
23542211
/* ifilterfalse object ************************************************************/
23552212

23562213
typedef struct {
@@ -3208,7 +3065,6 @@ repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times\n\
32083065
Iterators terminating on the shortest input sequence:\n\
32093066
izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
32103067
izip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
3211-
ifilter(pred, seq) --> elements of seq where pred(elem) is True\n\
32123068
ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\
32133069
islice(seq, [start,] stop [, step]) --> elements from\n\
32143070
seq[start:stop:step]\n\
@@ -3242,7 +3098,6 @@ inititertools(void)
32423098
&starmap_type,
32433099
&imap_type,
32443100
&chain_type,
3245-
&ifilter_type,
32463101
&ifilterfalse_type,
32473102
&count_type,
32483103
&izip_type,

0 commit comments

Comments
 (0)