Back to index

python3.2  3.2.2
Classes | Functions | Variables
enumobject.c File Reference
#include "Python.h"

Go to the source code of this file.

Classes

struct  enumobject
struct  reversedobject

Functions

static PyObjectenum_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
static void enum_dealloc (enumobject *en)
static int enum_traverse (enumobject *en, visitproc visit, void *arg)
static PyObjectenum_next_long (enumobject *en, PyObject *next_item)
static PyObjectenum_next (enumobject *en)
 PyDoc_STRVAR (enum_doc,"enumerate(iterable[, start]) -> iterator for index, value of iterable\n""\n""Return an enumerate object. iterable must be another object that supports\n""iteration. The enumerate object yields pairs containing a count (from\n""start, which defaults to zero) and a value yielded by the iterable argument.\n""enumerate is useful for obtaining an indexed list:\n"" (0, seq[0]), (1, seq[1]), (2, seq[2]), ...")
static PyObjectreversed_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
static void reversed_dealloc (reversedobject *ro)
static int reversed_traverse (reversedobject *ro, visitproc visit, void *arg)
static PyObjectreversed_next (reversedobject *ro)
 PyDoc_STRVAR (reversed_doc,"reversed(sequence) -> reverse iterator over values of the sequence\n""\n""Return a reverse iterator")
static PyObjectreversed_len (reversedobject *ro)
 PyDoc_STRVAR (length_hint_doc,"Private method returning an estimate of len(list(it)).")

Variables

PyTypeObject PyEnum_Type
static PyMethodDef reversediter_methods []
PyTypeObject PyReversed_Type

Class Documentation

struct enumobject

Definition at line 5 of file enumobject.c.

Collaboration diagram for enumobject:
Class Members
PyObject_HEAD Py_ssize_t en_index
PyObject * en_longindex
PyObject * en_result
PyObject * en_sit
struct reversedobject

Definition at line 216 of file enumobject.c.

Collaboration diagram for reversedobject:
Class Members
PyObject_HEAD Py_ssize_t index
PyObject * seq

Function Documentation

static void enum_dealloc ( enumobject en) [static]

Definition at line 62 of file enumobject.c.

Here is the call graph for this function:

static PyObject* enum_new ( PyTypeObject type,
PyObject args,
PyObject kwds 
) [static]

Definition at line 14 of file enumobject.c.

{
    enumobject *en;
    PyObject *seq = NULL;
    PyObject *start = NULL;
    static char *kwlist[] = {"iterable", "start", 0};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:enumerate", kwlist,
                                     &seq, &start))
        return NULL;

    en = (enumobject *)type->tp_alloc(type, 0);
    if (en == NULL)
        return NULL;
    if (start != NULL) {
        start = PyNumber_Index(start);
        if (start == NULL) {
            Py_DECREF(en);
            return NULL;
        }
        assert(PyLong_Check(start));
        en->en_index = PyLong_AsSsize_t(start);
        if (en->en_index == -1 && PyErr_Occurred()) {
            PyErr_Clear();
            en->en_index = PY_SSIZE_T_MAX;
            en->en_longindex = start;
        } else {
            en->en_longindex = NULL;
            Py_DECREF(start);
        }
    } else {
        en->en_index = 0;
        en->en_longindex = NULL;
    }
    en->en_sit = PyObject_GetIter(seq);
    if (en->en_sit == NULL) {
        Py_DECREF(en);
        return NULL;
    }
    en->en_result = PyTuple_Pack(2, Py_None, Py_None);
    if (en->en_result == NULL) {
        Py_DECREF(en);
        return NULL;
    }
    return (PyObject *)en;
}

Here is the call graph for this function:

static PyObject* enum_next ( enumobject en) [static]

Definition at line 123 of file enumobject.c.

{
    PyObject *next_index;
    PyObject *next_item;
    PyObject *result = en->en_result;
    PyObject *it = en->en_sit;

    next_item = (*Py_TYPE(it)->tp_iternext)(it);
    if (next_item == NULL)
        return NULL;

    if (en->en_index == PY_SSIZE_T_MAX)
        return enum_next_long(en, next_item);

    next_index = PyLong_FromSsize_t(en->en_index);
    if (next_index == NULL) {
        Py_DECREF(next_item);
        return NULL;
    }
    en->en_index++;

    if (result->ob_refcnt == 1) {
        Py_INCREF(result);
        Py_DECREF(PyTuple_GET_ITEM(result, 0));
        Py_DECREF(PyTuple_GET_ITEM(result, 1));
    } else {
        result = PyTuple_New(2);
        if (result == NULL) {
            Py_DECREF(next_index);
            Py_DECREF(next_item);
            return NULL;
        }
    }
    PyTuple_SET_ITEM(result, 0, next_index);
    PyTuple_SET_ITEM(result, 1, next_item);
    return result;
}

Here is the call graph for this function:

static PyObject* enum_next_long ( enumobject en,
PyObject next_item 
) [static]

Definition at line 81 of file enumobject.c.

{
    static PyObject *one = NULL;
    PyObject *result = en->en_result;
    PyObject *next_index;
    PyObject *stepped_up;

    if (en->en_longindex == NULL) {
        en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
        if (en->en_longindex == NULL)
            return NULL;
    }
    if (one == NULL) {
        one = PyLong_FromLong(1);
        if (one == NULL)
            return NULL;
    }
    next_index = en->en_longindex;
    assert(next_index != NULL);
    stepped_up = PyNumber_Add(next_index, one);
    if (stepped_up == NULL)
        return NULL;
    en->en_longindex = stepped_up;

    if (result->ob_refcnt == 1) {
        Py_INCREF(result);
        Py_DECREF(PyTuple_GET_ITEM(result, 0));
        Py_DECREF(PyTuple_GET_ITEM(result, 1));
    } else {
        result = PyTuple_New(2);
        if (result == NULL) {
            Py_DECREF(next_index);
            Py_DECREF(next_item);
            return NULL;
        }
    }
    PyTuple_SET_ITEM(result, 0, next_index);
    PyTuple_SET_ITEM(result, 1, next_item);
    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int enum_traverse ( enumobject en,
visitproc  visit,
void arg 
) [static]

Definition at line 72 of file enumobject.c.

{
    Py_VISIT(en->en_sit);
    Py_VISIT(en->en_result);
    Py_VISIT(en->en_longindex);
    return 0;
}
PyDoc_STRVAR ( enum_doc  ,
"enumerate(iterable[, start]) -> iterator for  index,
value of iterable\n""\n""Return an enumerate object.iterable must be another object that supports\n""iteration.The enumerate object yields pairs containing a   countfrom\n""start, which defaults to zero) and a value yielded by the iterable argument.\n""enumerate is useful for obtaining an indexed list:\n""(0, seq[0],
(1, seq[1])  ,
(2, seq[2])  ,
..."   
)
PyDoc_STRVAR ( reversed_doc  ,
"reversed(sequence) -> reverse iterator over values of the sequence\n""\n""Return a reverse iterator"   
)
PyDoc_STRVAR ( length_hint_doc  ,
"Private method returning an estimate of len(list(it))."   
)
static void reversed_dealloc ( reversedobject ro) [static]

Definition at line 266 of file enumobject.c.

{
    PyObject_GC_UnTrack(ro);
    Py_XDECREF(ro->seq);
    Py_TYPE(ro)->tp_free(ro);
}

Here is the call graph for this function:

static PyObject* reversed_len ( reversedobject ro) [static]

Definition at line 307 of file enumobject.c.

{
    Py_ssize_t position, seqsize;

    if (ro->seq == NULL)
        return PyLong_FromLong(0);
    seqsize = PySequence_Size(ro->seq);
    if (seqsize == -1)
        return NULL;
    position = ro->index + 1;
    return PyLong_FromSsize_t((seqsize < position)  ?  0  :  position);
}

Here is the call graph for this function:

static PyObject* reversed_new ( PyTypeObject type,
PyObject args,
PyObject kwds 
) [static]

Definition at line 223 of file enumobject.c.

{
    Py_ssize_t n;
    PyObject *seq, *reversed_meth;
    static PyObject *reversed_cache = NULL;
    reversedobject *ro;

    if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds))
        return NULL;

    if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) )
        return NULL;

    reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", &reversed_cache);
    if (reversed_meth != NULL) {
        PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL);
        Py_DECREF(reversed_meth);
        return res;
    }
    else if (PyErr_Occurred())
        return NULL;

    if (!PySequence_Check(seq)) {
        PyErr_SetString(PyExc_TypeError,
                        "argument to reversed() must be a sequence");
        return NULL;
    }

    n = PySequence_Size(seq);
    if (n == -1)
        return NULL;

    ro = (reversedobject *)type->tp_alloc(type, 0);
    if (ro == NULL)
        return NULL;

    ro->index = n-1;
    Py_INCREF(seq);
    ro->seq = seq;
    return (PyObject *)ro;
}

Here is the call graph for this function:

static PyObject* reversed_next ( reversedobject ro) [static]

Definition at line 281 of file enumobject.c.

{
    PyObject *item;
    Py_ssize_t index = ro->index;

    if (index >= 0) {
        item = PySequence_GetItem(ro->seq, index);
        if (item != NULL) {
            ro->index--;
            return item;
        }
        if (PyErr_ExceptionMatches(PyExc_IndexError) ||
            PyErr_ExceptionMatches(PyExc_StopIteration))
            PyErr_Clear();
    }
    ro->index = -1;
    Py_CLEAR(ro->seq);
    return NULL;
}

Here is the call graph for this function:

static int reversed_traverse ( reversedobject ro,
visitproc  visit,
void arg 
) [static]

Definition at line 274 of file enumobject.c.

{
    Py_VISIT(ro->seq);
    return 0;
}

Variable Documentation

Definition at line 170 of file enumobject.c.

Definition at line 327 of file enumobject.c.

Initial value:
 {
    {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc},
    {NULL,              NULL}           
}

Definition at line 322 of file enumobject.c.