Back to index

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

Go to the source code of this file.

Classes

struct  partialobject

Defines

#define OFF(x)   offsetof(partialobject, x)

Functions

static PyObjectpartial_new (PyTypeObject *type, PyObject *args, PyObject *kw)
static void partial_dealloc (partialobject *pto)
static PyObjectpartial_call (partialobject *pto, PyObject *args, PyObject *kw)
static int partial_traverse (partialobject *pto, visitproc visit, void *arg)
 PyDoc_STRVAR (partial_doc,"partial(func, *args, **keywords) - new function with partial application\n\ of the given arguments and keywords.\n")
static PyObjectpartial_get_dict (partialobject *pto)
static int partial_set_dict (partialobject *pto, PyObject *value)
static PyObjectpartial_repr (partialobject *pto)
static PyObjectpartial_reduce (partialobject *pto, PyObject *unused)
static PyObjectpartial_setstate (partialobject *pto, PyObject *args)
static PyObjectfunctools_reduce (PyObject *self, PyObject *args)
 PyDoc_STRVAR (functools_reduce_doc,"reduce(function, sequence[, initial]) -> value\n\ \n\ Apply a function of two arguments cumulatively to the items of a sequence,\n\ from left to right, so as to reduce the sequence to a single value.\n\ For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ of the sequence in the calculation, and serves as a default when the\n\ sequence is empty.")
 PyDoc_STRVAR (module_doc,"Tools that operate on functions.")
PyMODINIT_FUNC PyInit__functools (void)

Variables

static PyTypeObject partial_type
static PyMemberDef partial_memberlist []
static PyGetSetDef partial_getsetlist []
static PyMethodDef partial_methods []
static PyMethodDef module_methods []
static struct PyModuleDef

Class Documentation

struct partialobject

Definition at line 14 of file _functoolsmodule.c.

Collaboration diagram for partialobject:
Class Members
PyObject * args
PyObject * dict
PyObject_HEAD PyObject * fn
PyObject * kw
PyObject * weakreflist

Define Documentation

#define OFF (   x)    offsetof(partialobject, x)

Definition at line 147 of file _functoolsmodule.c.


Function Documentation

static PyObject* functools_reduce ( PyObject self,
PyObject args 
) [static]

Definition at line 336 of file _functoolsmodule.c.

{
    PyObject *seq, *func, *result = NULL, *it;

    if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
        return NULL;
    if (result != NULL)
        Py_INCREF(result);

    it = PyObject_GetIter(seq);
    if (it == NULL) {
        if (PyErr_ExceptionMatches(PyExc_TypeError))
            PyErr_SetString(PyExc_TypeError,
                            "reduce() arg 2 must support iteration");
        Py_XDECREF(result);
        return NULL;
    }

    if ((args = PyTuple_New(2)) == NULL)
        goto Fail;

    for (;;) {
        PyObject *op2;

        if (args->ob_refcnt > 1) {
            Py_DECREF(args);
            if ((args = PyTuple_New(2)) == NULL)
                goto Fail;
        }

        op2 = PyIter_Next(it);
        if (op2 == NULL) {
            if (PyErr_Occurred())
                goto Fail;
            break;
        }

        if (result == NULL)
            result = op2;
        else {
            PyTuple_SetItem(args, 0, result);
            PyTuple_SetItem(args, 1, op2);
            if ((result = PyEval_CallObject(func, args)) == NULL)
                goto Fail;
        }
    }

    Py_DECREF(args);

    if (result == NULL)
        PyErr_SetString(PyExc_TypeError,
                   "reduce() of empty sequence with no initial value");

    Py_DECREF(it);
    return result;

Fail:
    Py_XDECREF(args);
    Py_XDECREF(result);
    Py_DECREF(it);
    return NULL;
}

Here is the call graph for this function:

static PyObject* partial_call ( partialobject pto,
PyObject args,
PyObject kw 
) [static]

Definition at line 88 of file _functoolsmodule.c.

{
    PyObject *ret;
    PyObject *argappl = NULL, *kwappl = NULL;

    assert (PyCallable_Check(pto->fn));
    assert (PyTuple_Check(pto->args));
    assert (pto->kw == Py_None  ||  PyDict_Check(pto->kw));

    if (PyTuple_GET_SIZE(pto->args) == 0) {
        argappl = args;
        Py_INCREF(args);
    } else if (PyTuple_GET_SIZE(args) == 0) {
        argappl = pto->args;
        Py_INCREF(pto->args);
    } else {
        argappl = PySequence_Concat(pto->args, args);
        if (argappl == NULL)
            return NULL;
    }

    if (pto->kw == Py_None) {
        kwappl = kw;
        Py_XINCREF(kw);
    } else {
        kwappl = PyDict_Copy(pto->kw);
        if (kwappl == NULL) {
            Py_DECREF(argappl);
            return NULL;
        }
        if (kw != NULL) {
            if (PyDict_Merge(kwappl, kw, 1) != 0) {
                Py_DECREF(argappl);
                Py_DECREF(kwappl);
                return NULL;
            }
        }
    }

    ret = PyObject_Call(pto->fn, argappl, kwappl);
    Py_DECREF(argappl);
    Py_XDECREF(kwappl);
    return ret;
}

Here is the call graph for this function:

static void partial_dealloc ( partialobject pto) [static]

Definition at line 75 of file _functoolsmodule.c.

{
    PyObject_GC_UnTrack(pto);
    if (pto->weakreflist != NULL)
        PyObject_ClearWeakRefs((PyObject *) pto);
    Py_XDECREF(pto->fn);
    Py_XDECREF(pto->args);
    Py_XDECREF(pto->kw);
    Py_XDECREF(pto->dict);
    Py_TYPE(pto)->tp_free(pto);
}

Here is the call graph for this function:

static PyObject* partial_get_dict ( partialobject pto) [static]

Definition at line 159 of file _functoolsmodule.c.

{
    if (pto->dict == NULL) {
        pto->dict = PyDict_New();
        if (pto->dict == NULL)
            return NULL;
    }
    Py_INCREF(pto->dict);
    return pto->dict;
}

Here is the call graph for this function:

static PyObject* partial_new ( PyTypeObject type,
PyObject args,
PyObject kw 
) [static]

Definition at line 26 of file _functoolsmodule.c.

{
    PyObject *func;
    partialobject *pto;

    if (PyTuple_GET_SIZE(args) < 1) {
        PyErr_SetString(PyExc_TypeError,
                        "type 'partial' takes at least one argument");
        return NULL;
    }

    func = PyTuple_GET_ITEM(args, 0);
    if (!PyCallable_Check(func)) {
        PyErr_SetString(PyExc_TypeError,
                        "the first argument must be callable");
        return NULL;
    }

    /* create partialobject structure */
    pto = (partialobject *)type->tp_alloc(type, 0);
    if (pto == NULL)
        return NULL;

    pto->fn = func;
    Py_INCREF(func);
    pto->args = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX);
    if (pto->args == NULL) {
        pto->kw = NULL;
        Py_DECREF(pto);
        return NULL;
    }
    if (kw != NULL) {
        pto->kw = PyDict_Copy(kw);
        if (pto->kw == NULL) {
            Py_DECREF(pto);
            return NULL;
        }
    } else {
        pto->kw = Py_None;
        Py_INCREF(Py_None);
    }

    pto->weakreflist = NULL;
    pto->dict = NULL;

    return (PyObject *)pto;
}

Here is the call graph for this function:

static PyObject* partial_reduce ( partialobject pto,
PyObject unused 
) [static]

Definition at line 249 of file _functoolsmodule.c.

{
    return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn,
                         pto->args, pto->kw,
                         pto->dict ? pto->dict : Py_None);
}

Here is the call graph for this function:

static PyObject* partial_repr ( partialobject pto) [static]

Definition at line 200 of file _functoolsmodule.c.

{
    PyObject *result;
    PyObject *arglist;
    PyObject *tmp;
    Py_ssize_t i, n;

    arglist = PyUnicode_FromString("");
    if (arglist == NULL) {
        return NULL;
    }
    /* Pack positional arguments */
    assert (PyTuple_Check(pto->args));
    n = PyTuple_GET_SIZE(pto->args);
    for (i = 0; i < n; i++) {
        tmp = PyUnicode_FromFormat("%U, %R", arglist,
                                   PyTuple_GET_ITEM(pto->args, i));
        Py_DECREF(arglist);
        if (tmp == NULL)
            return NULL;
        arglist = tmp;
    }
    /* Pack keyword arguments */
    assert (pto->kw == Py_None  ||  PyDict_Check(pto->kw));
    if (pto->kw != Py_None) {
        PyObject *key, *value;
        for (i = 0; PyDict_Next(pto->kw, &i, &key, &value);) {
            tmp = PyUnicode_FromFormat("%U, %U=%R", arglist,
                                       key, value);
            Py_DECREF(arglist);
            if (tmp == NULL)
                return NULL;
            arglist = tmp;
        }
    }
    result = PyUnicode_FromFormat("%s(%R%U)", Py_TYPE(pto)->tp_name,
                                  pto->fn, arglist);
    Py_DECREF(arglist);
    return result;
}

Here is the call graph for this function:

static int partial_set_dict ( partialobject pto,
PyObject value 
) [static]

Definition at line 171 of file _functoolsmodule.c.

{
    PyObject *tmp;

    /* It is illegal to del p.__dict__ */
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError,
                        "a partial object's dictionary may not be deleted");
        return -1;
    }
    /* Can only set __dict__ to a dictionary */
    if (!PyDict_Check(value)) {
        PyErr_SetString(PyExc_TypeError,
                        "setting partial object's dictionary to a non-dict");
        return -1;
    }
    tmp = pto->dict;
    Py_INCREF(value);
    pto->dict = value;
    Py_XDECREF(tmp);
    return 0;
}

Here is the call graph for this function:

static PyObject* partial_setstate ( partialobject pto,
PyObject args 
) [static]

Definition at line 257 of file _functoolsmodule.c.

{
    PyObject *fn, *fnargs, *kw, *dict;
    if (!PyArg_ParseTuple(args, "(OOOO):__setstate__",
                          &fn, &fnargs, &kw, &dict))
        return NULL;
    Py_XDECREF(pto->fn);
    Py_XDECREF(pto->args);
    Py_XDECREF(pto->kw);
    Py_XDECREF(pto->dict);
    pto->fn = fn;
    pto->args = fnargs;
    pto->kw = kw;
    if (dict != Py_None) {
      pto->dict = dict;
      Py_INCREF(dict);
    } else {
      pto->dict = NULL;
    }
    Py_INCREF(fn);
    Py_INCREF(fnargs);
    Py_INCREF(kw);
    Py_RETURN_NONE;
}

Here is the call graph for this function:

static int partial_traverse ( partialobject pto,
visitproc  visit,
void arg 
) [static]

Definition at line 134 of file _functoolsmodule.c.

{
    Py_VISIT(pto->fn);
    Py_VISIT(pto->args);
    Py_VISIT(pto->kw);
    Py_VISIT(pto->dict);
    return 0;
}
PyDoc_STRVAR ( partial_doc  ,
"partial(func, *args, **keywords) - new function with partial application\n\ of the given arguments and keywords.\n  
)
PyDoc_STRVAR ( functools_reduce_doc  ,
"reduce(function, sequence[, initial]) -> value\n\\n\Apply a function of two arguments cumulatively to the items of a  sequence,
\n\from left to  right,
so as to reduce the sequence to a single value.\n\For  example,
reduce(lambda x, y:x+y,[1, 2, 3, 4, 5]) calculates\n\((((1+2)+3)+4)+5).If initial is  present,
it is placed before the items\n\of the sequence in the  calculation,
and serves as a default when the\n\sequence is empty."   
)
PyDoc_STRVAR ( module_doc  ,
"Tools that operate on functions."   
)

Definition at line 433 of file _functoolsmodule.c.

{
    int i;
    PyObject *m;
    char *name;
    PyTypeObject *typelist[] = {
        &partial_type,
        NULL
    };

    m = PyModule_Create(&_functoolsmodule);
    if (m == NULL)
        return NULL;

    for (i=0 ; typelist[i] != NULL ; i++) {
        if (PyType_Ready(typelist[i]) < 0) {
            Py_DECREF(m);
            return NULL;
        }
        name = strchr(typelist[i]->tp_name, '.');
        assert (name != NULL);
        Py_INCREF(typelist[i]);
        PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
    }
    return m;
}

Here is the call graph for this function:


Variable Documentation

Initial value:
 {
    {"reduce",          functools_reduce,     METH_VARARGS, functools_reduce_doc},
    {NULL,              NULL}           
}

Definition at line 414 of file _functoolsmodule.c.

Initial value:

Definition at line 194 of file _functoolsmodule.c.

Initial value:
 {
    {"func",            T_OBJECT,       OFF(fn),        READONLY,
     "function object to use in future partial calls"},
    {"args",            T_OBJECT,       OFF(args),      READONLY,
     "tuple of arguments to future partial calls"},
    {"keywords",        T_OBJECT,       OFF(kw),        READONLY,
     "dictionary of keyword arguments to future partial calls"},
    {NULL}  
}

Definition at line 148 of file _functoolsmodule.c.

Initial value:
 {
    {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS},
    {"__setstate__", (PyCFunction)partial_setstate, METH_VARARGS},
    {NULL,              NULL}           
}

Definition at line 282 of file _functoolsmodule.c.

static PyTypeObject partial_type [static]

Definition at line 23 of file _functoolsmodule.c.

struct PyModuleDef [static]
Initial value:
 {
    PyModuleDef_HEAD_INIT,
    "_functools",
    module_doc,
    -1,
    module_methods,
    NULL,
    NULL,
    NULL,
    NULL
}

Definition at line 420 of file _functoolsmodule.c.