aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Modules/_functoolsmodule.c
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.com>2024-02-12 07:53:52 +0300
committerDaniil Cherednik <dcherednik@ydb.tech>2024-02-14 14:26:16 +0000
commit31f2a419764a8ba77c2a970cfc80056c6cd06756 (patch)
treec1995d239eba8571cefc640f6648e1d5dd4ce9e2 /contrib/tools/python3/src/Modules/_functoolsmodule.c
parentfe2ef02b38d9c85d80060963b265a1df9f38c3bb (diff)
downloadydb-31f2a419764a8ba77c2a970cfc80056c6cd06756.tar.gz
Update Python from 3.11.8 to 3.12.2
Diffstat (limited to 'contrib/tools/python3/src/Modules/_functoolsmodule.c')
-rw-r--r--contrib/tools/python3/src/Modules/_functoolsmodule.c156
1 files changed, 81 insertions, 75 deletions
diff --git a/contrib/tools/python3/src/Modules/_functoolsmodule.c b/contrib/tools/python3/src/Modules/_functoolsmodule.c
index 3abb7fd710..a8001d7122 100644
--- a/contrib/tools/python3/src/Modules/_functoolsmodule.c
+++ b/contrib/tools/python3/src/Modules/_functoolsmodule.c
@@ -1,5 +1,6 @@
#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
@@ -7,6 +8,13 @@
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "structmember.h" // PyMemberDef
+#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 <perky@FreeBSD.org>
with adaptations by Raymond Hettinger <python@rcn.com>
@@ -58,6 +66,7 @@ get_functools_state_by_type(PyTypeObject *type)
return get_functools_state(module);
}
+// Not converted to argument clinic, because of `*args, **kwargs` arguments.
static PyObject *
partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
@@ -97,8 +106,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
if (pto == NULL)
return NULL;
- pto->fn = func;
- Py_INCREF(func);
+ pto->fn = Py_NewRef(func);
nargs = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX);
if (nargs == NULL) {
@@ -123,8 +131,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
pto->kw = PyDict_New();
}
else if (Py_REFCNT(kw) == 1) {
- Py_INCREF(kw);
- pto->kw = kw;
+ pto->kw = Py_NewRef(kw);
}
else {
pto->kw = PyDict_Copy(kw);
@@ -282,6 +289,7 @@ partial_setvectorcall(partialobject *pto)
}
+// Not converted to argument clinic, because of `*args, **kwargs` arguments.
static PyObject *
partial_call(partialobject *pto, PyObject *args, PyObject *kwargs)
{
@@ -293,8 +301,7 @@ partial_call(partialobject *pto, PyObject *args, PyObject *kwargs)
PyObject *kwargs2;
if (PyDict_GET_SIZE(pto->kw) == 0) {
/* kwargs can be NULL */
- kwargs2 = kwargs;
- Py_XINCREF(kwargs2);
+ kwargs2 = Py_XNewRef(kwargs);
}
else {
/* bpo-27840, bpo-29318: dictionary of keyword parameters must be
@@ -454,8 +461,7 @@ partial_setstate(partialobject *pto, PyObject *state)
else
Py_INCREF(dict);
- Py_INCREF(fn);
- Py_SETREF(pto->fn, fn);
+ Py_SETREF(pto->fn, Py_NewRef(fn));
Py_SETREF(pto->args, fnargs);
Py_SETREF(pto->kw, kw);
Py_XSETREF(pto->dict, dict);
@@ -579,10 +585,8 @@ keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds)
if (result == NULL) {
return NULL;
}
- Py_INCREF(ko->cmp);
- result->cmp = ko->cmp;
- Py_INCREF(object);
- result->object = object;
+ result->cmp = Py_NewRef(ko->cmp);
+ result->object = Py_NewRef(object);
PyObject_GC_Track(result);
return (PyObject *)result;
}
@@ -625,33 +629,36 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
return answer;
}
+/*[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(PyObject *self, PyObject *args, PyObject *kwds)
+_functools_cmp_to_key_impl(PyObject *module, PyObject *mycmp)
+/*[clinic end generated code: output=71eaad0f4fc81f33 input=d1b76f231c0dfeb3]*/
{
- PyObject *cmp;
- static char *kwargs[] = {"mycmp", NULL};
keyobject *object;
_functools_state *state;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:cmp_to_key", kwargs, &cmp))
- return NULL;
-
- state = get_functools_state(self);
+ state = get_functools_state(module);
object = PyObject_GC_New(keyobject, state->keyobject_type);
if (!object)
return NULL;
- Py_INCREF(cmp);
- object->cmp = cmp;
+ object->cmp = Py_NewRef(mycmp);
object->object = NULL;
PyObject_GC_Track(object);
return (PyObject *)object;
}
-PyDoc_STRVAR(functools_cmp_to_key_doc,
-"Convert a cmp= function into a key= function.");
-
/* reduce (used to be a builtin) ********************************************/
+// Not converted to argument clinic, because of `args` in-place modification.
+// AC will affect performance.
static PyObject *
functools_reduce(PyObject *self, PyObject *args)
{
@@ -824,12 +831,10 @@ lru_cache_make_key(PyObject *kwd_mark, PyObject *args,
if (PyUnicode_CheckExact(key) || PyLong_CheckExact(key)) {
/* For common scalar keys, save space by
dropping the enclosing args tuple */
- Py_INCREF(key);
- return key;
+ return Py_NewRef(key);
}
}
- Py_INCREF(args);
- return args;
+ return Py_NewRef(args);
}
key_size = PyTuple_GET_SIZE(args);
@@ -845,31 +850,25 @@ lru_cache_make_key(PyObject *kwd_mark, PyObject *args,
key_pos = 0;
for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) {
PyObject *item = PyTuple_GET_ITEM(args, pos);
- Py_INCREF(item);
- PyTuple_SET_ITEM(key, key_pos++, item);
+ PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(item));
}
if (kwds_size) {
- Py_INCREF(kwd_mark);
- PyTuple_SET_ITEM(key, key_pos++, kwd_mark);
+ PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(kwd_mark));
for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) {
- Py_INCREF(keyword);
- PyTuple_SET_ITEM(key, key_pos++, keyword);
- Py_INCREF(value);
- PyTuple_SET_ITEM(key, key_pos++, value);
+ PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(keyword));
+ PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(value));
}
assert(key_pos == PyTuple_GET_SIZE(args) + kwds_size * 2 + 1);
}
if (typed) {
for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) {
PyObject *item = (PyObject *)Py_TYPE(PyTuple_GET_ITEM(args, pos));
- Py_INCREF(item);
- PyTuple_SET_ITEM(key, key_pos++, item);
+ PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(item));
}
if (kwds_size) {
for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) {
PyObject *item = (PyObject *)Py_TYPE(value);
- Py_INCREF(item);
- PyTuple_SET_ITEM(key, key_pos++, item);
+ PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(item));
}
}
}
@@ -1071,8 +1070,7 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
return NULL;
}
lru_cache_append_link(self, link);
- Py_INCREF(result); /* for return */
- return result;
+ return Py_NewRef(result);
}
/* Since the cache is full, we need to evict an old key and add
a new key. Rather than free the old link and allocate a new
@@ -1217,16 +1215,12 @@ lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw)
obj->wrapper = wrapper;
obj->typed = typed;
obj->cache = cachedict;
- Py_INCREF(func);
- obj->func = func;
+ obj->func = Py_NewRef(func);
obj->misses = obj->hits = 0;
obj->maxsize = maxsize;
- Py_INCREF(state->kwd_mark);
- obj->kwd_mark = state->kwd_mark;
- Py_INCREF(state->lru_list_elem_type);
- obj->lru_list_elem_type = state->lru_list_elem_type;
- Py_INCREF(cache_info_type);
- obj->cache_info_type = cache_info_type;
+ obj->kwd_mark = Py_NewRef(state->kwd_mark);
+ obj->lru_list_elem_type = (PyTypeObject*)Py_NewRef(state->lru_list_elem_type);
+ obj->cache_info_type = Py_NewRef(cache_info_type);
obj->dict = NULL;
obj->weakreflist = NULL;
return (PyObject *)obj;
@@ -1249,8 +1243,7 @@ lru_cache_clear_list(lru_list_elem *link)
{
while (link != NULL) {
lru_list_elem *next = link->next;
- Py_DECREF(link);
- link = next;
+ Py_SETREF(link, next);
}
}
@@ -1293,31 +1286,46 @@ static PyObject *
lru_cache_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{
if (obj == Py_None || obj == NULL) {
- Py_INCREF(self);
- return self;
+ return Py_NewRef(self);
}
return PyMethod_New(self, obj);
}
+/*[clinic input]
+_functools._lru_cache_wrapper.cache_info
+
+Report cache statistics
+[clinic start generated code]*/
+
static PyObject *
-lru_cache_cache_info(lru_cache_object *self, PyObject *unused)
+_functools__lru_cache_wrapper_cache_info_impl(PyObject *self)
+/*[clinic end generated code: output=cc796a0b06dbd717 input=f05e5b6ebfe38645]*/
{
- if (self->maxsize == -1) {
- return PyObject_CallFunction(self->cache_info_type, "nnOn",
- self->hits, self->misses, Py_None,
- PyDict_GET_SIZE(self->cache));
- }
- return PyObject_CallFunction(self->cache_info_type, "nnnn",
- self->hits, self->misses, self->maxsize,
- PyDict_GET_SIZE(self->cache));
+ lru_cache_object *_self = (lru_cache_object *) self;
+ if (_self->maxsize == -1) {
+ return PyObject_CallFunction(_self->cache_info_type, "nnOn",
+ _self->hits, _self->misses, Py_None,
+ PyDict_GET_SIZE(_self->cache));
+ }
+ return PyObject_CallFunction(_self->cache_info_type, "nnnn",
+ _self->hits, _self->misses, _self->maxsize,
+ PyDict_GET_SIZE(_self->cache));
}
+/*[clinic input]
+_functools._lru_cache_wrapper.cache_clear
+
+Clear the cache and cache statistics
+[clinic start generated code]*/
+
static PyObject *
-lru_cache_cache_clear(lru_cache_object *self, PyObject *unused)
+_functools__lru_cache_wrapper_cache_clear_impl(PyObject *self)
+/*[clinic end generated code: output=58423b35efc3e381 input=6ca59dba09b12584]*/
{
- lru_list_elem *list = lru_cache_unlink_list(self);
- self->hits = self->misses = 0;
- PyDict_Clear(self->cache);
+ lru_cache_object *_self = (lru_cache_object *) self;
+ lru_list_elem *list = lru_cache_unlink_list(_self);
+ _self->hits = _self->misses = 0;
+ PyDict_Clear(_self->cache);
lru_cache_clear_list(list);
Py_RETURN_NONE;
}
@@ -1331,15 +1339,13 @@ lru_cache_reduce(PyObject *self, PyObject *unused)
static PyObject *
lru_cache_copy(PyObject *self, PyObject *unused)
{
- Py_INCREF(self);
- return self;
+ return Py_NewRef(self);
}
static PyObject *
lru_cache_deepcopy(PyObject *self, PyObject *unused)
{
- Py_INCREF(self);
- return self;
+ return Py_NewRef(self);
}
static int
@@ -1381,8 +1387,8 @@ cache_info_type: namedtuple class with the fields:\n\
);
static PyMethodDef lru_cache_methods[] = {
- {"cache_info", (PyCFunction)lru_cache_cache_info, METH_NOARGS},
- {"cache_clear", (PyCFunction)lru_cache_cache_clear, METH_NOARGS},
+ _FUNCTOOLS__LRU_CACHE_WRAPPER_CACHE_INFO_METHODDEF
+ _FUNCTOOLS__LRU_CACHE_WRAPPER_CACHE_CLEAR_METHODDEF
{"__reduce__", (PyCFunction)lru_cache_reduce, METH_NOARGS},
{"__copy__", (PyCFunction)lru_cache_copy, METH_VARARGS},
{"__deepcopy__", (PyCFunction)lru_cache_deepcopy, METH_VARARGS},
@@ -1432,8 +1438,7 @@ PyDoc_STRVAR(_functools_doc,
static PyMethodDef _functools_methods[] = {
{"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
- {"cmp_to_key", _PyCFunction_CAST(functools_cmp_to_key),
- METH_VARARGS | METH_KEYWORDS, functools_cmp_to_key_doc},
+ _FUNCTOOLS_CMP_TO_KEY_METHODDEF
{NULL, NULL} /* sentinel */
};
@@ -1515,6 +1520,7 @@ _functools_free(void *module)
static struct PyModuleDef_Slot _functools_slots[] = {
{Py_mod_exec, _functools_exec},
+ {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{0, NULL}
};