summaryrefslogtreecommitdiffstats
path: root/Objects/iterobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/iterobject.c')
-rw-r--r--Objects/iterobject.c127
1 files changed, 35 insertions, 92 deletions
diff --git a/Objects/iterobject.c b/Objects/iterobject.c
index da89298..346d2d9 100644
--- a/Objects/iterobject.c
+++ b/Objects/iterobject.c
@@ -1,13 +1,10 @@
/* Iterator objects */
#include "Python.h"
-#include "pycore_object.h"
-#include "pycore_pymem.h"
-#include "pycore_pystate.h"
typedef struct {
PyObject_HEAD
- Py_ssize_t it_index;
+ long it_index;
PyObject *it_seq; /* Set to NULL when iterator is exhausted */
} seqiterobject;
@@ -57,7 +54,7 @@ iter_iternext(PyObject *iterator)
seq = it->it_seq;
if (seq == NULL)
return NULL;
- if (it->it_index == PY_SSIZE_T_MAX) {
+ if (it->it_index == LONG_MAX) {
PyErr_SetString(PyExc_OverflowError,
"iter index too large");
return NULL;
@@ -79,61 +76,25 @@ iter_iternext(PyObject *iterator)
}
static PyObject *
-iter_len(seqiterobject *it, PyObject *Py_UNUSED(ignored))
+iter_len(seqiterobject *it)
{
Py_ssize_t seqsize, len;
if (it->it_seq) {
- if (_PyObject_HasLen(it->it_seq)) {
- seqsize = PySequence_Size(it->it_seq);
- if (seqsize == -1)
- return NULL;
- }
- else {
- Py_RETURN_NOTIMPLEMENTED;
- }
+ seqsize = PySequence_Size(it->it_seq);
+ if (seqsize == -1)
+ return NULL;
len = seqsize - it->it_index;
if (len >= 0)
- return PyLong_FromSsize_t(len);
+ return PyInt_FromSsize_t(len);
}
- return PyLong_FromLong(0);
+ return PyInt_FromLong(0);
}
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
-static PyObject *
-iter_reduce(seqiterobject *it, PyObject *Py_UNUSED(ignored))
-{
- _Py_IDENTIFIER(iter);
- if (it->it_seq != NULL)
- return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
- it->it_seq, it->it_index);
- else
- return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
-}
-
-PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
-
-static PyObject *
-iter_setstate(seqiterobject *it, PyObject *state)
-{
- Py_ssize_t index = PyLong_AsSsize_t(state);
- if (index == -1 && PyErr_Occurred())
- return NULL;
- if (it->it_seq != NULL) {
- if (index < 0)
- index = 0;
- it->it_index = index;
- }
- Py_RETURN_NONE;
-}
-
-PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
-
static PyMethodDef seqiter_methods[] = {
{"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc},
- {"__reduce__", (PyCFunction)iter_reduce, METH_NOARGS, reduce_doc},
- {"__setstate__", (PyCFunction)iter_setstate, METH_O, setstate_doc},
{NULL, NULL} /* sentinel */
};
@@ -144,10 +105,10 @@ PyTypeObject PySeqIter_Type = {
0, /* tp_itemsize */
/* methods */
(destructor)iter_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
+ 0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
- 0, /* tp_as_async */
+ 0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
@@ -212,62 +173,46 @@ calliter_traverse(calliterobject *it, visitproc visit, void *arg)
static PyObject *
calliter_iternext(calliterobject *it)
{
- PyObject *result;
-
- if (it->it_callable == NULL) {
- return NULL;
- }
-
- result = _PyObject_CallNoArg(it->it_callable);
- if (result != NULL) {
- int ok;
-
- ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ);
- if (ok == 0) {
- return result; /* Common case, fast path */
+ if (it->it_callable != NULL) {
+ PyObject *args = PyTuple_New(0);
+ PyObject *result;
+ if (args == NULL)
+ return NULL;
+ result = PyObject_Call(it->it_callable, args, NULL);
+ Py_DECREF(args);
+ if (result != NULL) {
+ int ok;
+ ok = PyObject_RichCompareBool(result,
+ it->it_sentinel,
+ Py_EQ);
+ if (ok == 0)
+ return result; /* Common case, fast path */
+ Py_DECREF(result);
+ if (ok > 0) {
+ Py_CLEAR(it->it_callable);
+ Py_CLEAR(it->it_sentinel);
+ }
}
-
- Py_DECREF(result);
- if (ok > 0) {
+ else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
+ PyErr_Clear();
Py_CLEAR(it->it_callable);
Py_CLEAR(it->it_sentinel);
}
}
- else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
- PyErr_Clear();
- Py_CLEAR(it->it_callable);
- Py_CLEAR(it->it_sentinel);
- }
return NULL;
}
-static PyObject *
-calliter_reduce(calliterobject *it, PyObject *Py_UNUSED(ignored))
-{
- _Py_IDENTIFIER(iter);
- if (it->it_callable != NULL && it->it_sentinel != NULL)
- return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_iter),
- it->it_callable, it->it_sentinel);
- else
- return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
-}
-
-static PyMethodDef calliter_methods[] = {
- {"__reduce__", (PyCFunction)calliter_reduce, METH_NOARGS, reduce_doc},
- {NULL, NULL} /* sentinel */
-};
-
PyTypeObject PyCallIter_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "callable_iterator", /* tp_name */
+ "callable-iterator", /* tp_name */
sizeof(calliterobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)calliter_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
+ 0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
- 0, /* tp_as_async */
+ 0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
@@ -286,7 +231,5 @@ PyTypeObject PyCallIter_Type = {
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)calliter_iternext, /* tp_iternext */
- calliter_methods, /* tp_methods */
+ 0, /* tp_methods */
};
-
-