cpython/Modules/_functoolsmodule.c

#include "Python.h"
#include "pycore_call.h"          // _PyObject_CallNoArgs()
#include "pycore_dict.h"          // _PyDict_Pop_KnownHash()
#include "pycore_long.h"          // _PyLong_GetZero()
#include "pycore_moduleobject.h"  // _PyModule_GetState()
#include "pycore_object.h"        // _PyObject_GC_TRACK
#include "pycore_pystate.h"       // _PyThreadState_GET()
#include "pycore_tuple.h"         // _PyTuple_ITEMS()


#include "clinic/_functoolsmodule.c.h"
/*[clinic input]
module _functools
class _functools._lru_cache_wrapper "PyObject *" "&lru_cache_type_spec"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=bece4053896b09c0]*/

/* _functools module written and maintained
   by Hye-Shik Chang <[email protected]>
   with adaptations by Raymond Hettinger <[email protected]>
   Copyright (c) 2004 Python Software Foundation.
   All rights reserved.
*/

_functools_state;

static inline _functools_state *
get_functools_state(PyObject *module)
{}


/* partial object **********************************************************/


// The 'Placeholder' singleton indicates which formal positional
// parameters are to be bound first when using a 'partial' object.

placeholderobject;

static inline _functools_state *
get_functools_state_by_type(PyTypeObject *type);

PyDoc_STRVAR(placeholder_doc,
"The type of the Placeholder singleton.\n\n"
"Used as a placeholder for partial arguments.");

static PyObject *
placeholder_repr(PyObject *op)
{}

static PyObject *
placeholder_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
{}

static PyMethodDef placeholder_methods[] =;

static void
placeholder_dealloc(PyObject* self)
{}

static PyObject *
placeholder_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{}

static int
placeholder_traverse(PyObject *self, visitproc visit, void *arg)
{}

static PyType_Slot placeholder_type_slots[] =;

static PyType_Spec placeholder_type_spec =;


partialobject;

// cast a PyObject pointer PTR to a partialobject pointer (no type checks)
#define _PyPartialObject_CAST(PTR)

static void partial_setvectorcall(partialobject *pto);
static struct PyModuleDef _functools_module;
static PyObject *
partial_call(PyObject *pto, PyObject *args, PyObject *kwargs);

static inline _functools_state *
get_functools_state_by_type(PyTypeObject *type)
{}

// Not converted to argument clinic, because of `*args, **kwargs` arguments.
static PyObject *
partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{}

static int
partial_clear(PyObject *self)
{}

static int
partial_traverse(PyObject *self, visitproc visit, void *arg)
{}

static void
partial_dealloc(PyObject *self)
{}

static PyObject *
partial_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{}

/* Merging keyword arguments using the vectorcall convention is messy, so
 * if we would need to do that, we stop using vectorcall and fall back
 * to using partial_call() instead. */
Py_NO_INLINE static PyObject *
partial_vectorcall_fallback(PyThreadState *tstate, partialobject *pto,
                            PyObject *const *args, size_t nargsf,
                            PyObject *kwnames)
{}

static PyObject *
partial_vectorcall(PyObject *self, PyObject *const *args,
                   size_t nargsf, PyObject *kwnames)
{}

/* Set pto->vectorcall depending on the parameters of the partial object */
static void
partial_setvectorcall(partialobject *pto)
{}


// Not converted to argument clinic, because of `*args, **kwargs` arguments.
static PyObject *
partial_call(PyObject *self, PyObject *args, PyObject *kwargs)
{}

PyDoc_STRVAR(partial_doc,
"partial(func, /, *args, **keywords)\n--\n\n\
Create a new function with partial application of the given arguments\n\
and keywords.");

#define OFF(x)
static PyMemberDef partial_memberlist[] =;

static PyGetSetDef partial_getsetlist[] =;

static PyObject *
partial_repr(PyObject *self)
{}

/* Pickle strategy:
   __reduce__ by itself doesn't support getting kwargs in the unpickle
   operation so we define a __setstate__ that replaces all the information
   about the partial.  If we only replaced part of it someone would use
   it as a hook to do strange things.
 */

static PyObject *
partial_reduce(PyObject *self, PyObject *Py_UNUSED(args))
{}

static PyObject *
partial_setstate(PyObject *self, PyObject *state)
{}

static PyMethodDef partial_methods[] =;

static PyType_Slot partial_type_slots[] =;

static PyType_Spec partial_type_spec =;


/* cmp_to_key ***************************************************************/

keyobject;

static int
keyobject_clear(keyobject *ko)
{}

static void
keyobject_dealloc(keyobject *ko)
{}

static int
keyobject_traverse(keyobject *ko, visitproc visit, void *arg)
{}

static PyMemberDef keyobject_members[] =;

static PyObject *
keyobject_text_signature(PyObject *self, void *Py_UNUSED(ignored))
{}

static PyGetSetDef keyobject_getset[] =;

static PyObject *
keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds);

static PyObject *
keyobject_richcompare(PyObject *ko, PyObject *other, int op);

static PyType_Slot keyobject_type_slots[] =;

static PyType_Spec keyobject_type_spec =;

static PyObject *
keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds)
{}

static PyObject *
keyobject_richcompare(PyObject *ko, PyObject *other, int op)
{}

/*[clinic input]
_functools.cmp_to_key

    mycmp: object
        Function that compares two objects.

Convert a cmp= function into a key= function.
[clinic start generated code]*/

static PyObject *
_functools_cmp_to_key_impl(PyObject *module, PyObject *mycmp)
/*[clinic end generated code: output=71eaad0f4fc81f33 input=d1b76f231c0dfeb3]*/
{}

/* reduce (used to be a builtin) ********************************************/

/*[clinic input]
_functools.reduce

    function as func: object
    iterable as seq: object
    /
    initial as result: object = NULL

Apply a function of two arguments cumulatively to the items of an iterable, from left to right.

This effectively reduces the iterable to a single value.  If initial is present,
it is placed before the items of the iterable in the calculation, and serves as
a default when the iterable is empty.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
calculates ((((1 + 2) + 3) + 4) + 5).
[clinic start generated code]*/

static PyObject *
_functools_reduce_impl(PyObject *module, PyObject *func, PyObject *seq,
                       PyObject *result)
/*[clinic end generated code: output=30d898fe1267c79d input=1511e9a8c38581ac]*/
{}

/* lru_cache object **********************************************************/

/* There are four principal algorithmic differences from the pure python version:

   1). The C version relies on the GIL instead of having its own reentrant lock.

   2). The prev/next link fields use borrowed references.

   3). For a full cache, the pure python version rotates the location of the
       root entry so that it never has to move individual links and it can
       limit updates to just the key and result fields.  However, in the C
       version, links are temporarily removed while the cache dict updates are
       occurring. Afterwards, they are appended or prepended back into the
       doubly-linked lists.

   4)  In the Python version, the _HashSeq class is used to prevent __hash__
       from being called more than once.  In the C version, the "known hash"
       variants of dictionary calls as used to the same effect.

*/

struct lru_list_elem;
struct lru_cache_object;

lru_list_elem;

static void
lru_list_elem_dealloc(lru_list_elem *link)
{}

static PyType_Slot lru_list_elem_type_slots[] =;

static PyType_Spec lru_list_elem_type_spec =;


lru_cache_ternaryfunc;

lru_cache_object;

static PyObject *
lru_cache_make_key(PyObject *kwd_mark, PyObject *args,
                   PyObject *kwds, int typed)
{}

static PyObject *
uncached_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds)
{}

static PyObject *
infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds)
{}

static void
lru_cache_extract_link(lru_list_elem *link)
{}

static void
lru_cache_append_link(lru_cache_object *self, lru_list_elem *link)
{}

static void
lru_cache_prepend_link(lru_cache_object *self, lru_list_elem *link)
{}

/* General note on reentrancy:

   There are four dictionary calls in the bounded_lru_cache_wrapper():
   1) The initial check for a cache match.  2) The post user-function
   check for a cache match.  3) The deletion of the oldest entry.
   4) The addition of the newest entry.

   In all four calls, we have a known hash which lets use avoid a call
   to __hash__().  That leaves only __eq__ as a possible source of a
   reentrant call.

   The __eq__ method call is always made for a cache hit (dict access #1).
   Accordingly, we have make sure not modify the cache state prior to
   this call.

   The __eq__ method call is never made for the deletion (dict access #3)
   because it is an identity match.

   For the other two accesses (#2 and #4), calls to __eq__ only occur
   when some other entry happens to have an exactly matching hash (all
   64-bits).  Though rare, this can happen, so we have to make sure to
   either call it at the top of its code path before any cache
   state modifications (dict access #2) or be prepared to restore
   invariants at the end of the code path (dict access #4).

   Another possible source of reentrancy is a decref which can trigger
   arbitrary code execution.  To make the code easier to reason about,
   the decrefs are deferred to the end of the each possible code path
   so that we know the cache is a consistent state.
 */

static PyObject *
bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds)
{}

static PyObject *
lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{}

static lru_list_elem *
lru_cache_unlink_list(lru_cache_object *self)
{}

static void
lru_cache_clear_list(lru_list_elem *link)
{}

static int
lru_cache_tp_clear(lru_cache_object *self)
{}

static void
lru_cache_dealloc(lru_cache_object *obj)
{}

static PyObject *
lru_cache_call(lru_cache_object *self, PyObject *args, PyObject *kwds)
{}

static PyObject *
lru_cache_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{}

/*[clinic input]
@critical_section
_functools._lru_cache_wrapper.cache_info

Report cache statistics
[clinic start generated code]*/

static PyObject *
_functools__lru_cache_wrapper_cache_info_impl(PyObject *self)
/*[clinic end generated code: output=cc796a0b06dbd717 input=00e1acb31aa21ecc]*/
{}

/*[clinic input]
@critical_section
_functools._lru_cache_wrapper.cache_clear

Clear the cache and cache statistics
[clinic start generated code]*/

static PyObject *
_functools__lru_cache_wrapper_cache_clear_impl(PyObject *self)
/*[clinic end generated code: output=58423b35efc3e381 input=dfa33acbecf8b4b2]*/
{}

static PyObject *
lru_cache_reduce(PyObject *self, PyObject *unused)
{}

static PyObject *
lru_cache_copy(PyObject *self, PyObject *unused)
{}

static PyObject *
lru_cache_deepcopy(PyObject *self, PyObject *unused)
{}

static int
lru_cache_tp_traverse(lru_cache_object *self, visitproc visit, void *arg)
{}


PyDoc_STRVAR(lru_cache_doc,
"Create a cached callable that wraps another function.\n\
\n\
user_function:      the function being cached\n\
\n\
maxsize:  0         for no caching\n\
          None      for unlimited cache size\n\
          n         for a bounded cache\n\
\n\
typed:    False     cache f(3) and f(3.0) as identical calls\n\
          True      cache f(3) and f(3.0) as distinct calls\n\
\n\
cache_info_type:    namedtuple class with the fields:\n\
                        hits misses currsize maxsize\n"
);

static PyMethodDef lru_cache_methods[] =;

static PyGetSetDef lru_cache_getsetlist[] =;

static PyMemberDef lru_cache_memberlist[] =;

static PyType_Slot lru_cache_type_slots[] =;

static PyType_Spec lru_cache_type_spec =;


/* module level code ********************************************************/

PyDoc_STRVAR(_functools_doc,
"Tools that operate on functions.");

static PyMethodDef _functools_methods[] =;

static int
_functools_exec(PyObject *module)
{}

static int
_functools_traverse(PyObject *module, visitproc visit, void *arg)
{}

static int
_functools_clear(PyObject *module)
{}

static void
_functools_free(void *module)
{}

static struct PyModuleDef_Slot _functools_slots[] =;

static struct PyModuleDef _functools_module =;

PyMODINIT_FUNC
PyInit__functools(void)
{}