Created PyObject_GenericGetIter().

Factors out the common case of returning self.
This commit is contained in:
Raymond Hettinger 2003-03-17 08:24:35 +00:00
parent 08801db123
commit 0153826964
10 changed files with 30 additions and 147 deletions

View file

@ -385,6 +385,7 @@ PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *); PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *); PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *); PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_GenericGetIter(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *); PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *,
PyObject *, PyObject *); PyObject *, PyObject *);

View file

@ -86,6 +86,9 @@ Build
C API C API
----- -----
- Added PyObject_GenericGetIter() to fill the tp_iter slot for the
typical case where the method returns its self argument.
- The extended type structure used for heap types (new-style - The extended type structure used for heap types (new-style
classes defined by Python code using a class statement) is now classes defined by Python code using a class statement) is now
exported from object.h as PyHeapTypeObject. (SF patch #696193.) exported from object.h as PyHeapTypeObject. (SF patch #696193.)

View file

@ -105,13 +105,6 @@ cycle_next(cycleobject *lz)
} }
} }
static PyObject *
cycle_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(cycle_doc, PyDoc_STRVAR(cycle_doc,
"cycle(iterable) --> cycle object\n\ "cycle(iterable) --> cycle object\n\
\n\ \n\
@ -147,7 +140,7 @@ PyTypeObject cycle_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)cycle_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)cycle_next, /* tp_iternext */ (iternextfunc)cycle_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -261,13 +254,6 @@ dropwhile_next(dropwhileobject *lz)
} }
} }
static PyObject *
dropwhile_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(dropwhile_doc, PyDoc_STRVAR(dropwhile_doc,
"dropwhile(predicate, iterable) --> dropwhile object\n\ "dropwhile(predicate, iterable) --> dropwhile object\n\
\n\ \n\
@ -303,7 +289,7 @@ PyTypeObject dropwhile_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)dropwhile_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)dropwhile_next, /* tp_iternext */ (iternextfunc)dropwhile_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -416,13 +402,6 @@ takewhile_next(takewhileobject *lz)
return NULL; return NULL;
} }
static PyObject *
takewhile_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(takewhile_doc, PyDoc_STRVAR(takewhile_doc,
"takewhile(predicate, iterable) --> takewhile object\n\ "takewhile(predicate, iterable) --> takewhile object\n\
\n\ \n\
@ -458,7 +437,7 @@ PyTypeObject takewhile_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)takewhile_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)takewhile_next, /* tp_iternext */ (iternextfunc)takewhile_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -589,13 +568,6 @@ islice_next(isliceobject *lz)
return item; return item;
} }
static PyObject *
islice_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(islice_doc, PyDoc_STRVAR(islice_doc,
"islice(iterable, [start,] stop [, step]) --> islice object\n\ "islice(iterable, [start,] stop [, step]) --> islice object\n\
\n\ \n\
@ -635,7 +607,7 @@ PyTypeObject islice_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)islice_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)islice_next, /* tp_iternext */ (iternextfunc)islice_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -739,13 +711,6 @@ starmap_next(starmapobject *lz)
return result; return result;
} }
static PyObject *
starmap_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(starmap_doc, PyDoc_STRVAR(starmap_doc,
"starmap(function, sequence) --> starmap object\n\ "starmap(function, sequence) --> starmap object\n\
\n\ \n\
@ -781,7 +746,7 @@ PyTypeObject starmap_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)starmap_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)starmap_next, /* tp_iternext */ (iternextfunc)starmap_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -930,13 +895,6 @@ imap_next(imapobject *lz)
return result; return result;
} }
static PyObject *
imap_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(imap_doc, PyDoc_STRVAR(imap_doc,
"imap(func, *iterables) --> imap object\n\ "imap(func, *iterables) --> imap object\n\
\n\ \n\
@ -975,7 +933,7 @@ PyTypeObject imap_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)imap_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)imap_next, /* tp_iternext */ (iternextfunc)imap_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -1074,13 +1032,6 @@ chain_next(chainobject *lz)
return NULL; return NULL;
} }
static PyObject *
chain_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(chain_doc, PyDoc_STRVAR(chain_doc,
"chain(*iterables) --> chain object\n\ "chain(*iterables) --> chain object\n\
\n\ \n\
@ -1117,7 +1068,7 @@ PyTypeObject chain_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)chain_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)chain_next, /* tp_iternext */ (iternextfunc)chain_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -1231,13 +1182,6 @@ ifilter_next(ifilterobject *lz)
} }
} }
static PyObject *
ifilter_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(ifilter_doc, PyDoc_STRVAR(ifilter_doc,
"ifilter(function or None, sequence) --> ifilter object\n\ "ifilter(function or None, sequence) --> ifilter object\n\
\n\ \n\
@ -1273,7 +1217,7 @@ PyTypeObject ifilter_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)ifilter_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)ifilter_next, /* tp_iternext */ (iternextfunc)ifilter_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -1387,13 +1331,6 @@ ifilterfalse_next(ifilterfalseobject *lz)
} }
} }
static PyObject *
ifilterfalse_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(ifilterfalse_doc, PyDoc_STRVAR(ifilterfalse_doc,
"ifilterfalse(function or None, sequence) --> ifilterfalse object\n\ "ifilterfalse(function or None, sequence) --> ifilterfalse object\n\
\n\ \n\
@ -1429,7 +1366,7 @@ PyTypeObject ifilterfalse_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)ifilterfalse_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)ifilterfalse_next, /* tp_iternext */ (iternextfunc)ifilterfalse_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -1479,13 +1416,6 @@ count_next(countobject *lz)
return PyInt_FromLong(lz->cnt++); return PyInt_FromLong(lz->cnt++);
} }
static PyObject *
count_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(count_doc, PyDoc_STRVAR(count_doc,
"count([firstval]) --> count object\n\ "count([firstval]) --> count object\n\
\n\ \n\
@ -1520,7 +1450,7 @@ PyTypeObject count_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)count_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)count_next, /* tp_iternext */ (iternextfunc)count_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -1665,13 +1595,6 @@ izip_next(izipobject *lz)
return result; return result;
} }
static PyObject *
izip_getiter(PyObject *lz)
{
Py_INCREF(lz);
return lz;
}
PyDoc_STRVAR(izip_doc, PyDoc_STRVAR(izip_doc,
"izip(iter1 [,iter2 [...]]) --> izip object\n\ "izip(iter1 [,iter2 [...]]) --> izip object\n\
\n\ \n\
@ -1711,7 +1634,7 @@ PyTypeObject izip_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)izip_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)izip_next, /* tp_iternext */ (iternextfunc)izip_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -1784,13 +1707,6 @@ repeat_next(repeatobject *ro)
return ro->element; return ro->element;
} }
static PyObject *
repeat_getiter(PyObject *ro)
{
Py_INCREF(ro);
return ro;
}
PyDoc_STRVAR(repeat_doc, PyDoc_STRVAR(repeat_doc,
"repeat(element [,times]) -> create an iterator which returns the element\n\ "repeat(element [,times]) -> create an iterator which returns the element\n\
for the specified number of times. If not specified, returns the element\n\ for the specified number of times. If not specified, returns the element\n\
@ -1825,7 +1741,7 @@ PyTypeObject repeat_type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)repeat_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)repeat_next, /* tp_iternext */ (iternextfunc)repeat_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */

View file

@ -2013,13 +2013,6 @@ dictiter_dealloc(dictiterobject *di)
PyObject_Del(di); PyObject_Del(di);
} }
static PyObject *
dictiter_getiter(PyObject *it)
{
Py_INCREF(it);
return it;
}
static PyObject *dictiter_iternext(dictiterobject *di) static PyObject *dictiter_iternext(dictiterobject *di)
{ {
PyObject *key, *value; PyObject *key, *value;
@ -2069,7 +2062,7 @@ PyTypeObject PyDictIter_Type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)dictiter_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)dictiter_iternext, /* tp_iternext */ (iternextfunc)dictiter_iternext, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */

View file

@ -78,13 +78,6 @@ enum_next(enumobject *en)
return result; return result;
} }
static PyObject *
enum_getiter(PyObject *en)
{
Py_INCREF(en);
return en;
}
PyDoc_STRVAR(enum_doc, PyDoc_STRVAR(enum_doc,
"enumerate(iterable) -> create an enumerating-iterator"); "enumerate(iterable) -> create an enumerating-iterator");
@ -117,7 +110,7 @@ PyTypeObject PyEnum_Type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)enum_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)enum_next, /* tp_iternext */ (iternextfunc)enum_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */

View file

@ -43,13 +43,6 @@ iter_traverse(seqiterobject *it, visitproc visit, void *arg)
return visit(it->it_seq, arg); return visit(it->it_seq, arg);
} }
static PyObject *
iter_getiter(PyObject *it)
{
Py_INCREF(it);
return it;
}
static PyObject * static PyObject *
iter_iternext(PyObject *iterator) iter_iternext(PyObject *iterator)
{ {
@ -106,7 +99,7 @@ PyTypeObject PySeqIter_Type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)iter_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)iter_iternext, /* tp_iternext */ (iternextfunc)iter_iternext, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
@ -223,7 +216,7 @@ PyTypeObject PyCallIter_Type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)iter_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)calliter_iternext, /* tp_iternext */ (iternextfunc)calliter_iternext, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */

View file

@ -2398,14 +2398,6 @@ listiter_traverse(listiterobject *it, visitproc visit, void *arg)
return visit((PyObject *)it->it_seq, arg); return visit((PyObject *)it->it_seq, arg);
} }
static PyObject *
listiter_getiter(PyObject *it)
{
Py_INCREF(it);
return it;
}
static PyObject * static PyObject *
listiter_next(listiterobject *it) listiter_next(listiterobject *it)
{ {
@ -2458,7 +2450,7 @@ PyTypeObject PyListIter_Type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)listiter_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)listiter_next, /* tp_iternext */ (iternextfunc)listiter_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */

View file

@ -1300,6 +1300,13 @@ _PyObject_GetDictPtr(PyObject *obj)
/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
PyObject *
PyObject_GenericGetIter(PyObject *obj)
{
Py_INCREF(obj);
return obj;
}
PyObject * PyObject *
PyObject_GenericGetAttr(PyObject *obj, PyObject *name) PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
{ {

View file

@ -245,13 +245,6 @@ range_iter(PyObject *seq)
return (PyObject *)it; return (PyObject *)it;
} }
static PyObject *
rangeiter_getiter(PyObject *it)
{
Py_INCREF(it);
return it;
}
static PyObject * static PyObject *
rangeiter_next(rangeiterobject *r) rangeiter_next(rangeiterobject *r)
{ {
@ -288,7 +281,7 @@ static PyTypeObject Pyrangeiter_Type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)rangeiter_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)rangeiter_next, /* tp_iternext */ (iternextfunc)rangeiter_next, /* tp_iternext */
0, /* tp_methods */ 0, /* tp_methods */
}; };

View file

@ -779,14 +779,6 @@ tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
return visit((PyObject *)it->it_seq, arg); return visit((PyObject *)it->it_seq, arg);
} }
static PyObject *
tupleiter_getiter(PyObject *it)
{
Py_INCREF(it);
return it;
}
static PyObject * static PyObject *
tupleiter_next(tupleiterobject *it) tupleiter_next(tupleiterobject *it)
{ {
@ -839,6 +831,6 @@ PyTypeObject PyTupleIter_Type = {
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
(getiterfunc)tupleiter_getiter, /* tp_iter */ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)tupleiter_next, /* tp_iternext */ (iternextfunc)tupleiter_next, /* tp_iternext */
}; };