summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Include/object.h1
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/itertoolsmodule.c108
-rw-r--r--Objects/dictobject.c9
-rw-r--r--Objects/enumobject.c9
-rw-r--r--Objects/iterobject.c11
-rw-r--r--Objects/listobject.c10
-rw-r--r--Objects/object.c7
-rw-r--r--Objects/rangeobject.c9
-rw-r--r--Objects/tupleobject.c10
10 files changed, 30 insertions, 147 deletions
diff --git a/Include/object.h b/Include/object.h
index 7b93230..291e605 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -385,6 +385,7 @@ PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
+PyAPI_FUNC(PyObject *) PyObject_GenericGetIter(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *,
PyObject *, PyObject *);
diff --git a/Misc/NEWS b/Misc/NEWS
index 0158c0f..eda988c 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -86,6 +86,9 @@ Build
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
classes defined by Python code using a class statement) is now
exported from object.h as PyHeapTypeObject. (SF patch #696193.)
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 5e359de..b069296 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -105,13 +105,6 @@ cycle_next(cycleobject *lz)
}
}
-static PyObject *
-cycle_getiter(PyObject *lz)
-{
- Py_INCREF(lz);
- return lz;
-}
-
PyDoc_STRVAR(cycle_doc,
"cycle(iterable) --> cycle object\n\
\n\
@@ -147,7 +140,7 @@ PyTypeObject cycle_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)cycle_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)cycle_next, /* tp_iternext */
0, /* tp_methods */
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,
"dropwhile(predicate, iterable) --> dropwhile object\n\
\n\
@@ -303,7 +289,7 @@ PyTypeObject dropwhile_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)dropwhile_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)dropwhile_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
@@ -416,13 +402,6 @@ takewhile_next(takewhileobject *lz)
return NULL;
}
-static PyObject *
-takewhile_getiter(PyObject *lz)
-{
- Py_INCREF(lz);
- return lz;
-}
-
PyDoc_STRVAR(takewhile_doc,
"takewhile(predicate, iterable) --> takewhile object\n\
\n\
@@ -458,7 +437,7 @@ PyTypeObject takewhile_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)takewhile_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)takewhile_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
@@ -589,13 +568,6 @@ islice_next(isliceobject *lz)
return item;
}
-static PyObject *
-islice_getiter(PyObject *lz)
-{
- Py_INCREF(lz);
- return lz;
-}
-
PyDoc_STRVAR(islice_doc,
"islice(iterable, [start,] stop [, step]) --> islice object\n\
\n\
@@ -635,7 +607,7 @@ PyTypeObject islice_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)islice_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)islice_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
@@ -739,13 +711,6 @@ starmap_next(starmapobject *lz)
return result;
}
-static PyObject *
-starmap_getiter(PyObject *lz)
-{
- Py_INCREF(lz);
- return lz;
-}
-
PyDoc_STRVAR(starmap_doc,
"starmap(function, sequence) --> starmap object\n\
\n\
@@ -781,7 +746,7 @@ PyTypeObject starmap_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)starmap_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)starmap_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
@@ -930,13 +895,6 @@ imap_next(imapobject *lz)
return result;
}
-static PyObject *
-imap_getiter(PyObject *lz)
-{
- Py_INCREF(lz);
- return lz;
-}
-
PyDoc_STRVAR(imap_doc,
"imap(func, *iterables) --> imap object\n\
\n\
@@ -975,7 +933,7 @@ PyTypeObject imap_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)imap_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)imap_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
@@ -1074,13 +1032,6 @@ chain_next(chainobject *lz)
return NULL;
}
-static PyObject *
-chain_getiter(PyObject *lz)
-{
- Py_INCREF(lz);
- return lz;
-}
-
PyDoc_STRVAR(chain_doc,
"chain(*iterables) --> chain object\n\
\n\
@@ -1117,7 +1068,7 @@ PyTypeObject chain_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)chain_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)chain_next, /* tp_iternext */
0, /* tp_methods */
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,
"ifilter(function or None, sequence) --> ifilter object\n\
\n\
@@ -1273,7 +1217,7 @@ PyTypeObject ifilter_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)ifilter_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)ifilter_next, /* tp_iternext */
0, /* tp_methods */
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,
"ifilterfalse(function or None, sequence) --> ifilterfalse object\n\
\n\
@@ -1429,7 +1366,7 @@ PyTypeObject ifilterfalse_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)ifilterfalse_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)ifilterfalse_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
@@ -1479,13 +1416,6 @@ count_next(countobject *lz)
return PyInt_FromLong(lz->cnt++);
}
-static PyObject *
-count_getiter(PyObject *lz)
-{
- Py_INCREF(lz);
- return lz;
-}
-
PyDoc_STRVAR(count_doc,
"count([firstval]) --> count object\n\
\n\
@@ -1520,7 +1450,7 @@ PyTypeObject count_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)count_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)count_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
@@ -1665,13 +1595,6 @@ izip_next(izipobject *lz)
return result;
}
-static PyObject *
-izip_getiter(PyObject *lz)
-{
- Py_INCREF(lz);
- return lz;
-}
-
PyDoc_STRVAR(izip_doc,
"izip(iter1 [,iter2 [...]]) --> izip object\n\
\n\
@@ -1711,7 +1634,7 @@ PyTypeObject izip_type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)izip_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)izip_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
@@ -1784,13 +1707,6 @@ repeat_next(repeatobject *ro)
return ro->element;
}
-static PyObject *
-repeat_getiter(PyObject *ro)
-{
- Py_INCREF(ro);
- return ro;
-}
-
PyDoc_STRVAR(repeat_doc,
"repeat(element [,times]) -> create an iterator which 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_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)repeat_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)repeat_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 633f2ce..52e516f 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -2013,13 +2013,6 @@ dictiter_dealloc(dictiterobject *di)
PyObject_Del(di);
}
-static PyObject *
-dictiter_getiter(PyObject *it)
-{
- Py_INCREF(it);
- return it;
-}
-
static PyObject *dictiter_iternext(dictiterobject *di)
{
PyObject *key, *value;
@@ -2069,7 +2062,7 @@ PyTypeObject PyDictIter_Type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)dictiter_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)dictiter_iternext, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
diff --git a/Objects/enumobject.c b/Objects/enumobject.c
index f69a002..cd6ca67 100644
--- a/Objects/enumobject.c
+++ b/Objects/enumobject.c
@@ -78,13 +78,6 @@ enum_next(enumobject *en)
return result;
}
-static PyObject *
-enum_getiter(PyObject *en)
-{
- Py_INCREF(en);
- return en;
-}
-
PyDoc_STRVAR(enum_doc,
"enumerate(iterable) -> create an enumerating-iterator");
@@ -117,7 +110,7 @@ PyTypeObject PyEnum_Type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)enum_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)enum_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
diff --git a/Objects/iterobject.c b/Objects/iterobject.c
index 2e1caae..7970fa6 100644
--- a/Objects/iterobject.c
+++ b/Objects/iterobject.c
@@ -44,13 +44,6 @@ iter_traverse(seqiterobject *it, visitproc visit, void *arg)
}
static PyObject *
-iter_getiter(PyObject *it)
-{
- Py_INCREF(it);
- return it;
-}
-
-static PyObject *
iter_iternext(PyObject *iterator)
{
seqiterobject *it;
@@ -106,7 +99,7 @@ PyTypeObject PySeqIter_Type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)iter_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)iter_iternext, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
@@ -223,7 +216,7 @@ PyTypeObject PyCallIter_Type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)iter_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)calliter_iternext, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 79403cc..75ad235 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2398,14 +2398,6 @@ listiter_traverse(listiterobject *it, visitproc visit, void *arg)
return visit((PyObject *)it->it_seq, arg);
}
-
-static PyObject *
-listiter_getiter(PyObject *it)
-{
- Py_INCREF(it);
- return it;
-}
-
static PyObject *
listiter_next(listiterobject *it)
{
@@ -2458,7 +2450,7 @@ PyTypeObject PyListIter_Type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)listiter_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)listiter_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
diff --git a/Objects/object.c b/Objects/object.c
index bf8056c..eb4d8f7 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1301,6 +1301,13 @@ _PyObject_GetDictPtr(PyObject *obj)
/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
PyObject *
+PyObject_GenericGetIter(PyObject *obj)
+{
+ Py_INCREF(obj);
+ return obj;
+}
+
+PyObject *
PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
{
PyTypeObject *tp = obj->ob_type;
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index 9c7b74e..5dbbf13 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -246,13 +246,6 @@ range_iter(PyObject *seq)
}
static PyObject *
-rangeiter_getiter(PyObject *it)
-{
- Py_INCREF(it);
- return it;
-}
-
-static PyObject *
rangeiter_next(rangeiterobject *r)
{
if (r->index < r->len)
@@ -288,7 +281,7 @@ static PyTypeObject Pyrangeiter_Type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)rangeiter_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)rangeiter_next, /* tp_iternext */
0, /* tp_methods */
};
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index d6d0aaa..ede18e0 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -779,14 +779,6 @@ tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
return visit((PyObject *)it->it_seq, arg);
}
-
-static PyObject *
-tupleiter_getiter(PyObject *it)
-{
- Py_INCREF(it);
- return it;
-}
-
static PyObject *
tupleiter_next(tupleiterobject *it)
{
@@ -839,6 +831,6 @@ PyTypeObject PyTupleIter_Type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- (getiterfunc)tupleiter_getiter, /* tp_iter */
+ PyObject_GenericGetIter, /* tp_iter */
(iternextfunc)tupleiter_next, /* tp_iternext */
};