|
7 | 7 | #include "pycore_tuple.h" // _PyTuple_ITEMS()
|
8 | 8 | #include "structmember.h" // PyMemberDef
|
9 | 9 |
|
| 10 | +#include "clinic/_functoolsmodule.c.h" |
| 11 | +/*[clinic input] |
| 12 | +module _functools |
| 13 | +class _functools._lru_cache_wrapper "PyObject *" "&lru_cache_type_spec" |
| 14 | +[clinic start generated code]*/ |
| 15 | +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=bece4053896b09c0]*/ |
| 16 | + |
10 | 17 | /* _functools module written and maintained
|
11 | 18 | by Hye-Shik Chang <[email protected]>
|
12 | 19 | with adaptations by Raymond Hettinger <[email protected]>
|
@@ -58,6 +65,7 @@ get_functools_state_by_type(PyTypeObject *type)
|
58 | 65 | return get_functools_state(module);
|
59 | 66 | }
|
60 | 67 |
|
| 68 | +// Not converted to argument clinic, because of `*args, **kwargs` arguments. |
61 | 69 | static PyObject *
|
62 | 70 | partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
63 | 71 | {
|
@@ -282,6 +290,7 @@ partial_setvectorcall(partialobject *pto)
|
282 | 290 | }
|
283 | 291 |
|
284 | 292 |
|
| 293 | +// Not converted to argument clinic, because of `*args, **kwargs` arguments. |
285 | 294 | static PyObject *
|
286 | 295 | partial_call(partialobject *pto, PyObject *args, PyObject *kwargs)
|
287 | 296 | {
|
@@ -625,33 +634,37 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
|
625 | 634 | return answer;
|
626 | 635 | }
|
627 | 636 |
|
| 637 | +/*[clinic input] |
| 638 | +_functools.cmp_to_key |
| 639 | +
|
| 640 | + mycmp: object |
| 641 | + Function that compares two objects. |
| 642 | +
|
| 643 | +Convert a cmp= function into a key= function. |
| 644 | +[clinic start generated code]*/ |
| 645 | + |
628 | 646 | static PyObject *
|
629 |
| -functools_cmp_to_key(PyObject *self, PyObject *args, PyObject *kwds) |
| 647 | +_functools_cmp_to_key_impl(PyObject *module, PyObject *mycmp) |
| 648 | +/*[clinic end generated code: output=71eaad0f4fc81f33 input=d1b76f231c0dfeb3]*/ |
630 | 649 | {
|
631 |
| - PyObject *cmp; |
632 |
| - static char *kwargs[] = {"mycmp", NULL}; |
633 | 650 | keyobject *object;
|
634 | 651 | _functools_state *state;
|
635 | 652 |
|
636 |
| - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:cmp_to_key", kwargs, &cmp)) |
637 |
| - return NULL; |
638 |
| - |
639 |
| - state = get_functools_state(self); |
| 653 | + state = get_functools_state(module); |
640 | 654 | object = PyObject_GC_New(keyobject, state->keyobject_type);
|
641 | 655 | if (!object)
|
642 | 656 | return NULL;
|
643 |
| - Py_INCREF(cmp); |
644 |
| - object->cmp = cmp; |
| 657 | + Py_INCREF(mycmp); |
| 658 | + object->cmp = mycmp; |
645 | 659 | object->object = NULL;
|
646 | 660 | PyObject_GC_Track(object);
|
647 | 661 | return (PyObject *)object;
|
648 | 662 | }
|
649 | 663 |
|
650 |
| -PyDoc_STRVAR(functools_cmp_to_key_doc, |
651 |
| -"Convert a cmp= function into a key= function."); |
652 |
| - |
653 | 664 | /* reduce (used to be a builtin) ********************************************/
|
654 | 665 |
|
| 666 | +// Not converted to argument clinic, because of `args` in-place modification. |
| 667 | +// AC will affect performance. |
655 | 668 | static PyObject *
|
656 | 669 | functools_reduce(PyObject *self, PyObject *args)
|
657 | 670 | {
|
@@ -1299,25 +1312,41 @@ lru_cache_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
1299 | 1312 | return PyMethod_New(self, obj);
|
1300 | 1313 | }
|
1301 | 1314 |
|
| 1315 | +/*[clinic input] |
| 1316 | +_functools._lru_cache_wrapper.cache_info |
| 1317 | +
|
| 1318 | +Report cache statistics |
| 1319 | +[clinic start generated code]*/ |
| 1320 | + |
1302 | 1321 | static PyObject *
|
1303 |
| -lru_cache_cache_info(lru_cache_object *self, PyObject *unused) |
| 1322 | +_functools__lru_cache_wrapper_cache_info_impl(PyObject *self) |
| 1323 | +/*[clinic end generated code: output=cc796a0b06dbd717 input=f05e5b6ebfe38645]*/ |
1304 | 1324 | {
|
1305 |
| - if (self->maxsize == -1) { |
1306 |
| - return PyObject_CallFunction(self->cache_info_type, "nnOn", |
1307 |
| - self->hits, self->misses, Py_None, |
1308 |
| - PyDict_GET_SIZE(self->cache)); |
1309 |
| - } |
1310 |
| - return PyObject_CallFunction(self->cache_info_type, "nnnn", |
1311 |
| - self->hits, self->misses, self->maxsize, |
1312 |
| - PyDict_GET_SIZE(self->cache)); |
| 1325 | + lru_cache_object *_self = (lru_cache_object *) self; |
| 1326 | + if (_self->maxsize == -1) { |
| 1327 | + return PyObject_CallFunction(_self->cache_info_type, "nnOn", |
| 1328 | + _self->hits, _self->misses, Py_None, |
| 1329 | + PyDict_GET_SIZE(_self->cache)); |
| 1330 | + } |
| 1331 | + return PyObject_CallFunction(_self->cache_info_type, "nnnn", |
| 1332 | + _self->hits, _self->misses, _self->maxsize, |
| 1333 | + PyDict_GET_SIZE(_self->cache)); |
1313 | 1334 | }
|
1314 | 1335 |
|
| 1336 | +/*[clinic input] |
| 1337 | +_functools._lru_cache_wrapper.cache_clear |
| 1338 | +
|
| 1339 | +Clear the cache and cache statistics |
| 1340 | +[clinic start generated code]*/ |
| 1341 | + |
1315 | 1342 | static PyObject *
|
1316 |
| -lru_cache_cache_clear(lru_cache_object *self, PyObject *unused) |
| 1343 | +_functools__lru_cache_wrapper_cache_clear_impl(PyObject *self) |
| 1344 | +/*[clinic end generated code: output=58423b35efc3e381 input=6ca59dba09b12584]*/ |
1317 | 1345 | {
|
1318 |
| - lru_list_elem *list = lru_cache_unlink_list(self); |
1319 |
| - self->hits = self->misses = 0; |
1320 |
| - PyDict_Clear(self->cache); |
| 1346 | + lru_cache_object *_self = (lru_cache_object *) self; |
| 1347 | + lru_list_elem *list = lru_cache_unlink_list(_self); |
| 1348 | + _self->hits = _self->misses = 0; |
| 1349 | + PyDict_Clear(_self->cache); |
1321 | 1350 | lru_cache_clear_list(list);
|
1322 | 1351 | Py_RETURN_NONE;
|
1323 | 1352 | }
|
@@ -1381,8 +1410,8 @@ cache_info_type: namedtuple class with the fields:\n\
|
1381 | 1410 | );
|
1382 | 1411 |
|
1383 | 1412 | static PyMethodDef lru_cache_methods[] = {
|
1384 |
| - {"cache_info", (PyCFunction)lru_cache_cache_info, METH_NOARGS}, |
1385 |
| - {"cache_clear", (PyCFunction)lru_cache_cache_clear, METH_NOARGS}, |
| 1413 | + _FUNCTOOLS__LRU_CACHE_WRAPPER_CACHE_INFO_METHODDEF |
| 1414 | + _FUNCTOOLS__LRU_CACHE_WRAPPER_CACHE_CLEAR_METHODDEF |
1386 | 1415 | {"__reduce__", (PyCFunction)lru_cache_reduce, METH_NOARGS},
|
1387 | 1416 | {"__copy__", (PyCFunction)lru_cache_copy, METH_VARARGS},
|
1388 | 1417 | {"__deepcopy__", (PyCFunction)lru_cache_deepcopy, METH_VARARGS},
|
@@ -1432,8 +1461,7 @@ PyDoc_STRVAR(_functools_doc,
|
1432 | 1461 |
|
1433 | 1462 | static PyMethodDef _functools_methods[] = {
|
1434 | 1463 | {"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
|
1435 |
| - {"cmp_to_key", _PyCFunction_CAST(functools_cmp_to_key), |
1436 |
| - METH_VARARGS | METH_KEYWORDS, functools_cmp_to_key_doc}, |
| 1464 | + _FUNCTOOLS_CMP_TO_KEY_METHODDEF |
1437 | 1465 | {NULL, NULL} /* sentinel */
|
1438 | 1466 | };
|
1439 | 1467 |
|
|
0 commit comments