Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 18 additions & 54 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,65 +566,37 @@ typedef struct {

#define _filterobject_CAST(op) ((filterobject *)(op))

static PyObject *
filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *func, *seq;
PyObject *it;
filterobject *lz;

if ((type == &PyFilter_Type || type->tp_init == PyFilter_Type.tp_init) &&
!_PyArg_NoKeywords("filter", kwds))
return NULL;

if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
return NULL;

/* Get iterator. */
it = PyObject_GetIter(seq);
if (it == NULL)
return NULL;
/*[clinic input]
class filter "filterobject *" "&PyFilter_Type"
@vectorcall
@classmethod
filter.__new__ as filter_new

/* create filterobject structure */
lz = (filterobject *)type->tp_alloc(type, 0);
if (lz == NULL) {
Py_DECREF(it);
return NULL;
}
function as func: object
iterable as seq: object
/

lz->func = Py_NewRef(func);
lz->it = it;
Return an iterator over items of iterable where function(item) is true.

return (PyObject *)lz;
}
If function is None, return the items that are true.
[clinic start generated code]*/

static PyObject *
filter_vectorcall(PyObject *type, PyObject * const*args,
size_t nargsf, PyObject *kwnames)
filter_new_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
/*[clinic end generated code: output=2134a9f0032a6037 input=57836123a59c4d91]*/
{
PyTypeObject *tp = _PyType_CAST(type);
if (tp == &PyFilter_Type && !_PyArg_NoKwnames("filter", kwnames)) {
return NULL;
}

Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (!_PyArg_CheckPositional("filter", nargs, 2, 2)) {
return NULL;
}

PyObject *it = PyObject_GetIter(args[1]);
PyObject *it = PyObject_GetIter(seq);
if (it == NULL) {
return NULL;
}

filterobject *lz = (filterobject *)tp->tp_alloc(tp, 0);

filterobject *lz = (filterobject *)type->tp_alloc(type, 0);
if (lz == NULL) {
Py_DECREF(it);
return NULL;
}

lz->func = Py_NewRef(args[0]);
lz->func = Py_NewRef(func);
lz->it = it;

return (PyObject *)lz;
Expand Down Expand Up @@ -699,14 +671,6 @@ static PyMethodDef filter_methods[] = {
{NULL, NULL} /* sentinel */
};

PyDoc_STRVAR(filter_doc,
"filter(function, iterable, /)\n\
--\n\
\n\
Return an iterator yielding those items of iterable for which\n\
function(item) is true. If function is None, return the items that\n\
are true.");

PyTypeObject PyFilter_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"filter", /* tp_name */
Expand All @@ -730,7 +694,7 @@ PyTypeObject PyFilter_Type = {
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
filter_doc, /* tp_doc */
filter_new__doc__, /* tp_doc */
filter_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
Expand All @@ -749,7 +713,7 @@ PyTypeObject PyFilter_Type = {
PyType_GenericAlloc, /* tp_alloc */
filter_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
.tp_vectorcall = filter_vectorcall
.tp_vectorcall = filter_vectorcall,
};


Expand Down
63 changes: 62 additions & 1 deletion Python/clinic/bltinmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading