/src/Python-3.8.3/Modules/_functoolsmodule.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "Python.h" |
2 | | #include "pycore_pymem.h" |
3 | | #include "pycore_pystate.h" |
4 | | #include "pycore_tupleobject.h" |
5 | | #include "structmember.h" |
6 | | |
7 | | /* _functools module written and maintained |
8 | | by Hye-Shik Chang <perky@FreeBSD.org> |
9 | | with adaptations by Raymond Hettinger <python@rcn.com> |
10 | | Copyright (c) 2004, 2005, 2006 Python Software Foundation. |
11 | | All rights reserved. |
12 | | */ |
13 | | |
14 | | /* partial object **********************************************************/ |
15 | | |
16 | | typedef struct { |
17 | | PyObject_HEAD |
18 | | PyObject *fn; |
19 | | PyObject *args; |
20 | | PyObject *kw; |
21 | | PyObject *dict; |
22 | | PyObject *weakreflist; /* List of weak references */ |
23 | | int use_fastcall; |
24 | | } partialobject; |
25 | | |
26 | | static PyTypeObject partial_type; |
27 | | |
28 | | static PyObject * |
29 | | partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
30 | 1 | { |
31 | 1 | PyObject *func, *pargs, *nargs, *pkw; |
32 | 1 | partialobject *pto; |
33 | | |
34 | 1 | if (PyTuple_GET_SIZE(args) < 1) { |
35 | 0 | PyErr_SetString(PyExc_TypeError, |
36 | 0 | "type 'partial' takes at least one argument"); |
37 | 0 | return NULL; |
38 | 0 | } |
39 | | |
40 | 1 | pargs = pkw = NULL; |
41 | 1 | func = PyTuple_GET_ITEM(args, 0); |
42 | 1 | if (Py_TYPE(func) == &partial_type && type == &partial_type) { |
43 | 0 | partialobject *part = (partialobject *)func; |
44 | 0 | if (part->dict == NULL) { |
45 | 0 | pargs = part->args; |
46 | 0 | pkw = part->kw; |
47 | 0 | func = part->fn; |
48 | 0 | assert(PyTuple_Check(pargs)); |
49 | 0 | assert(PyDict_Check(pkw)); |
50 | 0 | } |
51 | 0 | } |
52 | 1 | if (!PyCallable_Check(func)) { |
53 | 0 | PyErr_SetString(PyExc_TypeError, |
54 | 0 | "the first argument must be callable"); |
55 | 0 | return NULL; |
56 | 0 | } |
57 | | |
58 | | /* create partialobject structure */ |
59 | 1 | pto = (partialobject *)type->tp_alloc(type, 0); |
60 | 1 | if (pto == NULL) |
61 | 0 | return NULL; |
62 | | |
63 | 1 | pto->fn = func; |
64 | 1 | Py_INCREF(func); |
65 | | |
66 | 1 | nargs = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX); |
67 | 1 | if (nargs == NULL) { |
68 | 0 | Py_DECREF(pto); |
69 | 0 | return NULL; |
70 | 0 | } |
71 | 1 | if (pargs == NULL) { |
72 | 1 | pto->args = nargs; |
73 | 1 | } |
74 | 0 | else { |
75 | 0 | pto->args = PySequence_Concat(pargs, nargs); |
76 | 0 | Py_DECREF(nargs); |
77 | 0 | if (pto->args == NULL) { |
78 | 0 | Py_DECREF(pto); |
79 | 0 | return NULL; |
80 | 0 | } |
81 | 0 | assert(PyTuple_Check(pto->args)); |
82 | 0 | } |
83 | | |
84 | 1 | if (pkw == NULL || PyDict_GET_SIZE(pkw) == 0) { |
85 | 1 | if (kw == NULL) { |
86 | 1 | pto->kw = PyDict_New(); |
87 | 1 | } |
88 | 0 | else if (Py_REFCNT(kw) == 1) { |
89 | 0 | Py_INCREF(kw); |
90 | 0 | pto->kw = kw; |
91 | 0 | } |
92 | 0 | else { |
93 | 0 | pto->kw = PyDict_Copy(kw); |
94 | 0 | } |
95 | 1 | } |
96 | 0 | else { |
97 | 0 | pto->kw = PyDict_Copy(pkw); |
98 | 0 | if (kw != NULL && pto->kw != NULL) { |
99 | 0 | if (PyDict_Merge(pto->kw, kw, 1) != 0) { |
100 | 0 | Py_DECREF(pto); |
101 | 0 | return NULL; |
102 | 0 | } |
103 | 0 | } |
104 | 0 | } |
105 | 1 | if (pto->kw == NULL) { |
106 | 0 | Py_DECREF(pto); |
107 | 0 | return NULL; |
108 | 0 | } |
109 | | |
110 | 1 | pto->use_fastcall = (_PyVectorcall_Function(func) != NULL); |
111 | | |
112 | 1 | return (PyObject *)pto; |
113 | 1 | } |
114 | | |
115 | | static void |
116 | | partial_dealloc(partialobject *pto) |
117 | 1 | { |
118 | | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
119 | 1 | PyObject_GC_UnTrack(pto); |
120 | 1 | if (pto->weakreflist != NULL) |
121 | 0 | PyObject_ClearWeakRefs((PyObject *) pto); |
122 | 1 | Py_XDECREF(pto->fn); |
123 | 1 | Py_XDECREF(pto->args); |
124 | 1 | Py_XDECREF(pto->kw); |
125 | 1 | Py_XDECREF(pto->dict); |
126 | 1 | Py_TYPE(pto)->tp_free(pto); |
127 | 1 | } |
128 | | |
129 | | static PyObject * |
130 | | partial_fastcall(partialobject *pto, PyObject **args, Py_ssize_t nargs, |
131 | | PyObject *kwargs) |
132 | 0 | { |
133 | 0 | PyObject *small_stack[_PY_FASTCALL_SMALL_STACK]; |
134 | 0 | PyObject *ret; |
135 | 0 | PyObject **stack, **stack_buf = NULL; |
136 | 0 | Py_ssize_t nargs2, pto_nargs; |
137 | |
|
138 | 0 | pto_nargs = PyTuple_GET_SIZE(pto->args); |
139 | 0 | nargs2 = pto_nargs + nargs; |
140 | |
|
141 | 0 | if (pto_nargs == 0) { |
142 | 0 | stack = args; |
143 | 0 | } |
144 | 0 | else if (nargs == 0) { |
145 | 0 | stack = _PyTuple_ITEMS(pto->args); |
146 | 0 | } |
147 | 0 | else { |
148 | 0 | if (nargs2 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) { |
149 | 0 | stack = small_stack; |
150 | 0 | } |
151 | 0 | else { |
152 | 0 | stack_buf = PyMem_Malloc(nargs2 * sizeof(PyObject *)); |
153 | 0 | if (stack_buf == NULL) { |
154 | 0 | PyErr_NoMemory(); |
155 | 0 | return NULL; |
156 | 0 | } |
157 | 0 | stack = stack_buf; |
158 | 0 | } |
159 | | |
160 | | /* use borrowed references */ |
161 | 0 | memcpy(stack, |
162 | 0 | _PyTuple_ITEMS(pto->args), |
163 | 0 | pto_nargs * sizeof(PyObject*)); |
164 | 0 | memcpy(&stack[pto_nargs], |
165 | 0 | args, |
166 | 0 | nargs * sizeof(PyObject*)); |
167 | 0 | } |
168 | | |
169 | 0 | ret = _PyObject_FastCallDict(pto->fn, stack, nargs2, kwargs); |
170 | 0 | PyMem_Free(stack_buf); |
171 | 0 | return ret; |
172 | 0 | } |
173 | | |
174 | | static PyObject * |
175 | | partial_call_impl(partialobject *pto, PyObject *args, PyObject *kwargs) |
176 | 0 | { |
177 | 0 | PyObject *ret, *args2; |
178 | | |
179 | | /* Note: tupleconcat() is optimized for empty tuples */ |
180 | 0 | args2 = PySequence_Concat(pto->args, args); |
181 | 0 | if (args2 == NULL) { |
182 | 0 | return NULL; |
183 | 0 | } |
184 | 0 | assert(PyTuple_Check(args2)); |
185 | |
|
186 | 0 | ret = PyObject_Call(pto->fn, args2, kwargs); |
187 | 0 | Py_DECREF(args2); |
188 | 0 | return ret; |
189 | 0 | } |
190 | | |
191 | | static PyObject * |
192 | | partial_call(partialobject *pto, PyObject *args, PyObject *kwargs) |
193 | 0 | { |
194 | 0 | PyObject *kwargs2, *res; |
195 | |
|
196 | 0 | assert (PyCallable_Check(pto->fn)); |
197 | 0 | assert (PyTuple_Check(pto->args)); |
198 | 0 | assert (PyDict_Check(pto->kw)); |
199 | |
|
200 | 0 | if (PyDict_GET_SIZE(pto->kw) == 0) { |
201 | | /* kwargs can be NULL */ |
202 | 0 | kwargs2 = kwargs; |
203 | 0 | Py_XINCREF(kwargs2); |
204 | 0 | } |
205 | 0 | else { |
206 | | /* bpo-27840, bpo-29318: dictionary of keyword parameters must be |
207 | | copied, because a function using "**kwargs" can modify the |
208 | | dictionary. */ |
209 | 0 | kwargs2 = PyDict_Copy(pto->kw); |
210 | 0 | if (kwargs2 == NULL) { |
211 | 0 | return NULL; |
212 | 0 | } |
213 | | |
214 | 0 | if (kwargs != NULL) { |
215 | 0 | if (PyDict_Merge(kwargs2, kwargs, 1) != 0) { |
216 | 0 | Py_DECREF(kwargs2); |
217 | 0 | return NULL; |
218 | 0 | } |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | | |
223 | 0 | if (pto->use_fastcall) { |
224 | 0 | res = partial_fastcall(pto, |
225 | 0 | _PyTuple_ITEMS(args), |
226 | 0 | PyTuple_GET_SIZE(args), |
227 | 0 | kwargs2); |
228 | 0 | } |
229 | 0 | else { |
230 | 0 | res = partial_call_impl(pto, args, kwargs2); |
231 | 0 | } |
232 | 0 | Py_XDECREF(kwargs2); |
233 | 0 | return res; |
234 | 0 | } |
235 | | |
236 | | static int |
237 | | partial_traverse(partialobject *pto, visitproc visit, void *arg) |
238 | 0 | { |
239 | 0 | Py_VISIT(pto->fn); |
240 | 0 | Py_VISIT(pto->args); |
241 | 0 | Py_VISIT(pto->kw); |
242 | 0 | Py_VISIT(pto->dict); |
243 | 0 | return 0; |
244 | 0 | } |
245 | | |
246 | | PyDoc_STRVAR(partial_doc, |
247 | | "partial(func, *args, **keywords) - new function with partial application\n\ |
248 | | of the given arguments and keywords.\n"); |
249 | | |
250 | | #define OFF(x) offsetof(partialobject, x) |
251 | | static PyMemberDef partial_memberlist[] = { |
252 | | {"func", T_OBJECT, OFF(fn), READONLY, |
253 | | "function object to use in future partial calls"}, |
254 | | {"args", T_OBJECT, OFF(args), READONLY, |
255 | | "tuple of arguments to future partial calls"}, |
256 | | {"keywords", T_OBJECT, OFF(kw), READONLY, |
257 | | "dictionary of keyword arguments to future partial calls"}, |
258 | | {NULL} /* Sentinel */ |
259 | | }; |
260 | | |
261 | | static PyGetSetDef partial_getsetlist[] = { |
262 | | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
263 | | {NULL} /* Sentinel */ |
264 | | }; |
265 | | |
266 | | static PyObject * |
267 | | partial_repr(partialobject *pto) |
268 | 0 | { |
269 | 0 | PyObject *result = NULL; |
270 | 0 | PyObject *arglist; |
271 | 0 | Py_ssize_t i, n; |
272 | 0 | PyObject *key, *value; |
273 | 0 | int status; |
274 | |
|
275 | 0 | status = Py_ReprEnter((PyObject *)pto); |
276 | 0 | if (status != 0) { |
277 | 0 | if (status < 0) |
278 | 0 | return NULL; |
279 | 0 | return PyUnicode_FromString("..."); |
280 | 0 | } |
281 | | |
282 | 0 | arglist = PyUnicode_FromString(""); |
283 | 0 | if (arglist == NULL) |
284 | 0 | goto done; |
285 | | /* Pack positional arguments */ |
286 | 0 | assert (PyTuple_Check(pto->args)); |
287 | 0 | n = PyTuple_GET_SIZE(pto->args); |
288 | 0 | for (i = 0; i < n; i++) { |
289 | 0 | Py_SETREF(arglist, PyUnicode_FromFormat("%U, %R", arglist, |
290 | 0 | PyTuple_GET_ITEM(pto->args, i))); |
291 | 0 | if (arglist == NULL) |
292 | 0 | goto done; |
293 | 0 | } |
294 | | /* Pack keyword arguments */ |
295 | 0 | assert (PyDict_Check(pto->kw)); |
296 | 0 | for (i = 0; PyDict_Next(pto->kw, &i, &key, &value);) { |
297 | | /* Prevent key.__str__ from deleting the value. */ |
298 | 0 | Py_INCREF(value); |
299 | 0 | Py_SETREF(arglist, PyUnicode_FromFormat("%U, %S=%R", arglist, |
300 | 0 | key, value)); |
301 | 0 | Py_DECREF(value); |
302 | 0 | if (arglist == NULL) |
303 | 0 | goto done; |
304 | 0 | } |
305 | 0 | result = PyUnicode_FromFormat("%s(%R%U)", Py_TYPE(pto)->tp_name, |
306 | 0 | pto->fn, arglist); |
307 | 0 | Py_DECREF(arglist); |
308 | |
|
309 | 0 | done: |
310 | 0 | Py_ReprLeave((PyObject *)pto); |
311 | 0 | return result; |
312 | 0 | } |
313 | | |
314 | | /* Pickle strategy: |
315 | | __reduce__ by itself doesn't support getting kwargs in the unpickle |
316 | | operation so we define a __setstate__ that replaces all the information |
317 | | about the partial. If we only replaced part of it someone would use |
318 | | it as a hook to do strange things. |
319 | | */ |
320 | | |
321 | | static PyObject * |
322 | | partial_reduce(partialobject *pto, PyObject *unused) |
323 | 0 | { |
324 | 0 | return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn, |
325 | 0 | pto->args, pto->kw, |
326 | 0 | pto->dict ? pto->dict : Py_None); |
327 | 0 | } |
328 | | |
329 | | static PyObject * |
330 | | partial_setstate(partialobject *pto, PyObject *state) |
331 | 0 | { |
332 | 0 | PyObject *fn, *fnargs, *kw, *dict; |
333 | |
|
334 | 0 | if (!PyTuple_Check(state) || |
335 | 0 | !PyArg_ParseTuple(state, "OOOO", &fn, &fnargs, &kw, &dict) || |
336 | 0 | !PyCallable_Check(fn) || |
337 | 0 | !PyTuple_Check(fnargs) || |
338 | 0 | (kw != Py_None && !PyDict_Check(kw))) |
339 | 0 | { |
340 | 0 | PyErr_SetString(PyExc_TypeError, "invalid partial state"); |
341 | 0 | return NULL; |
342 | 0 | } |
343 | | |
344 | 0 | if(!PyTuple_CheckExact(fnargs)) |
345 | 0 | fnargs = PySequence_Tuple(fnargs); |
346 | 0 | else |
347 | 0 | Py_INCREF(fnargs); |
348 | 0 | if (fnargs == NULL) |
349 | 0 | return NULL; |
350 | | |
351 | 0 | if (kw == Py_None) |
352 | 0 | kw = PyDict_New(); |
353 | 0 | else if(!PyDict_CheckExact(kw)) |
354 | 0 | kw = PyDict_Copy(kw); |
355 | 0 | else |
356 | 0 | Py_INCREF(kw); |
357 | 0 | if (kw == NULL) { |
358 | 0 | Py_DECREF(fnargs); |
359 | 0 | return NULL; |
360 | 0 | } |
361 | | |
362 | 0 | if (dict == Py_None) |
363 | 0 | dict = NULL; |
364 | 0 | else |
365 | 0 | Py_INCREF(dict); |
366 | |
|
367 | 0 | Py_INCREF(fn); |
368 | 0 | pto->use_fastcall = (_PyVectorcall_Function(fn) != NULL); |
369 | 0 | Py_SETREF(pto->fn, fn); |
370 | 0 | Py_SETREF(pto->args, fnargs); |
371 | 0 | Py_SETREF(pto->kw, kw); |
372 | 0 | Py_XSETREF(pto->dict, dict); |
373 | 0 | Py_RETURN_NONE; |
374 | 0 | } |
375 | | |
376 | | static PyMethodDef partial_methods[] = { |
377 | | {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS}, |
378 | | {"__setstate__", (PyCFunction)partial_setstate, METH_O}, |
379 | | {NULL, NULL} /* sentinel */ |
380 | | }; |
381 | | |
382 | | static PyTypeObject partial_type = { |
383 | | PyVarObject_HEAD_INIT(NULL, 0) |
384 | | "functools.partial", /* tp_name */ |
385 | | sizeof(partialobject), /* tp_basicsize */ |
386 | | 0, /* tp_itemsize */ |
387 | | /* methods */ |
388 | | (destructor)partial_dealloc, /* tp_dealloc */ |
389 | | 0, /* tp_vectorcall_offset */ |
390 | | 0, /* tp_getattr */ |
391 | | 0, /* tp_setattr */ |
392 | | 0, /* tp_as_async */ |
393 | | (reprfunc)partial_repr, /* tp_repr */ |
394 | | 0, /* tp_as_number */ |
395 | | 0, /* tp_as_sequence */ |
396 | | 0, /* tp_as_mapping */ |
397 | | 0, /* tp_hash */ |
398 | | (ternaryfunc)partial_call, /* tp_call */ |
399 | | 0, /* tp_str */ |
400 | | PyObject_GenericGetAttr, /* tp_getattro */ |
401 | | PyObject_GenericSetAttr, /* tp_setattro */ |
402 | | 0, /* tp_as_buffer */ |
403 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
404 | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
405 | | partial_doc, /* tp_doc */ |
406 | | (traverseproc)partial_traverse, /* tp_traverse */ |
407 | | 0, /* tp_clear */ |
408 | | 0, /* tp_richcompare */ |
409 | | offsetof(partialobject, weakreflist), /* tp_weaklistoffset */ |
410 | | 0, /* tp_iter */ |
411 | | 0, /* tp_iternext */ |
412 | | partial_methods, /* tp_methods */ |
413 | | partial_memberlist, /* tp_members */ |
414 | | partial_getsetlist, /* tp_getset */ |
415 | | 0, /* tp_base */ |
416 | | 0, /* tp_dict */ |
417 | | 0, /* tp_descr_get */ |
418 | | 0, /* tp_descr_set */ |
419 | | offsetof(partialobject, dict), /* tp_dictoffset */ |
420 | | 0, /* tp_init */ |
421 | | 0, /* tp_alloc */ |
422 | | partial_new, /* tp_new */ |
423 | | PyObject_GC_Del, /* tp_free */ |
424 | | }; |
425 | | |
426 | | |
427 | | /* cmp_to_key ***************************************************************/ |
428 | | |
429 | | typedef struct { |
430 | | PyObject_HEAD |
431 | | PyObject *cmp; |
432 | | PyObject *object; |
433 | | } keyobject; |
434 | | |
435 | | static void |
436 | | keyobject_dealloc(keyobject *ko) |
437 | 0 | { |
438 | 0 | Py_DECREF(ko->cmp); |
439 | 0 | Py_XDECREF(ko->object); |
440 | 0 | PyObject_FREE(ko); |
441 | 0 | } |
442 | | |
443 | | static int |
444 | | keyobject_traverse(keyobject *ko, visitproc visit, void *arg) |
445 | 0 | { |
446 | 0 | Py_VISIT(ko->cmp); |
447 | 0 | if (ko->object) |
448 | 0 | Py_VISIT(ko->object); |
449 | 0 | return 0; |
450 | 0 | } |
451 | | |
452 | | static int |
453 | | keyobject_clear(keyobject *ko) |
454 | 0 | { |
455 | 0 | Py_CLEAR(ko->cmp); |
456 | 0 | if (ko->object) |
457 | 0 | Py_CLEAR(ko->object); |
458 | 0 | return 0; |
459 | 0 | } |
460 | | |
461 | | static PyMemberDef keyobject_members[] = { |
462 | | {"obj", T_OBJECT, |
463 | | offsetof(keyobject, object), 0, |
464 | | PyDoc_STR("Value wrapped by a key function.")}, |
465 | | {NULL} |
466 | | }; |
467 | | |
468 | | static PyObject * |
469 | | keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds); |
470 | | |
471 | | static PyObject * |
472 | | keyobject_richcompare(PyObject *ko, PyObject *other, int op); |
473 | | |
474 | | static PyTypeObject keyobject_type = { |
475 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
476 | | "functools.KeyWrapper", /* tp_name */ |
477 | | sizeof(keyobject), /* tp_basicsize */ |
478 | | 0, /* tp_itemsize */ |
479 | | /* methods */ |
480 | | (destructor)keyobject_dealloc, /* tp_dealloc */ |
481 | | 0, /* tp_vectorcall_offset */ |
482 | | 0, /* tp_getattr */ |
483 | | 0, /* tp_setattr */ |
484 | | 0, /* tp_as_async */ |
485 | | 0, /* tp_repr */ |
486 | | 0, /* tp_as_number */ |
487 | | 0, /* tp_as_sequence */ |
488 | | 0, /* tp_as_mapping */ |
489 | | 0, /* tp_hash */ |
490 | | (ternaryfunc)keyobject_call, /* tp_call */ |
491 | | 0, /* tp_str */ |
492 | | PyObject_GenericGetAttr, /* tp_getattro */ |
493 | | 0, /* tp_setattro */ |
494 | | 0, /* tp_as_buffer */ |
495 | | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
496 | | 0, /* tp_doc */ |
497 | | (traverseproc)keyobject_traverse, /* tp_traverse */ |
498 | | (inquiry)keyobject_clear, /* tp_clear */ |
499 | | keyobject_richcompare, /* tp_richcompare */ |
500 | | 0, /* tp_weaklistoffset */ |
501 | | 0, /* tp_iter */ |
502 | | 0, /* tp_iternext */ |
503 | | 0, /* tp_methods */ |
504 | | keyobject_members, /* tp_members */ |
505 | | 0, /* tp_getset */ |
506 | | }; |
507 | | |
508 | | static PyObject * |
509 | | keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds) |
510 | 0 | { |
511 | 0 | PyObject *object; |
512 | 0 | keyobject *result; |
513 | 0 | static char *kwargs[] = {"obj", NULL}; |
514 | |
|
515 | 0 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:K", kwargs, &object)) |
516 | 0 | return NULL; |
517 | 0 | result = PyObject_New(keyobject, &keyobject_type); |
518 | 0 | if (!result) |
519 | 0 | return NULL; |
520 | 0 | Py_INCREF(ko->cmp); |
521 | 0 | result->cmp = ko->cmp; |
522 | 0 | Py_INCREF(object); |
523 | 0 | result->object = object; |
524 | 0 | return (PyObject *)result; |
525 | 0 | } |
526 | | |
527 | | static PyObject * |
528 | | keyobject_richcompare(PyObject *ko, PyObject *other, int op) |
529 | 0 | { |
530 | 0 | PyObject *res; |
531 | 0 | PyObject *x; |
532 | 0 | PyObject *y; |
533 | 0 | PyObject *compare; |
534 | 0 | PyObject *answer; |
535 | 0 | PyObject* stack[2]; |
536 | |
|
537 | 0 | if (Py_TYPE(other) != &keyobject_type){ |
538 | 0 | PyErr_Format(PyExc_TypeError, "other argument must be K instance"); |
539 | 0 | return NULL; |
540 | 0 | } |
541 | 0 | compare = ((keyobject *) ko)->cmp; |
542 | 0 | assert(compare != NULL); |
543 | 0 | x = ((keyobject *) ko)->object; |
544 | 0 | y = ((keyobject *) other)->object; |
545 | 0 | if (!x || !y){ |
546 | 0 | PyErr_Format(PyExc_AttributeError, "object"); |
547 | 0 | return NULL; |
548 | 0 | } |
549 | | |
550 | | /* Call the user's comparison function and translate the 3-way |
551 | | * result into true or false (or error). |
552 | | */ |
553 | 0 | stack[0] = x; |
554 | 0 | stack[1] = y; |
555 | 0 | res = _PyObject_FastCall(compare, stack, 2); |
556 | 0 | if (res == NULL) { |
557 | 0 | return NULL; |
558 | 0 | } |
559 | | |
560 | 0 | answer = PyObject_RichCompare(res, _PyLong_Zero, op); |
561 | 0 | Py_DECREF(res); |
562 | 0 | return answer; |
563 | 0 | } |
564 | | |
565 | | static PyObject * |
566 | | functools_cmp_to_key(PyObject *self, PyObject *args, PyObject *kwds) |
567 | 0 | { |
568 | 0 | PyObject *cmp; |
569 | 0 | static char *kwargs[] = {"mycmp", NULL}; |
570 | 0 | keyobject *object; |
571 | |
|
572 | 0 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:cmp_to_key", kwargs, &cmp)) |
573 | 0 | return NULL; |
574 | 0 | object = PyObject_New(keyobject, &keyobject_type); |
575 | 0 | if (!object) |
576 | 0 | return NULL; |
577 | 0 | Py_INCREF(cmp); |
578 | 0 | object->cmp = cmp; |
579 | 0 | object->object = NULL; |
580 | 0 | return (PyObject *)object; |
581 | 0 | } |
582 | | |
583 | | PyDoc_STRVAR(functools_cmp_to_key_doc, |
584 | | "Convert a cmp= function into a key= function."); |
585 | | |
586 | | /* reduce (used to be a builtin) ********************************************/ |
587 | | |
588 | | static PyObject * |
589 | | functools_reduce(PyObject *self, PyObject *args) |
590 | 0 | { |
591 | 0 | PyObject *seq, *func, *result = NULL, *it; |
592 | |
|
593 | 0 | if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result)) |
594 | 0 | return NULL; |
595 | 0 | if (result != NULL) |
596 | 0 | Py_INCREF(result); |
597 | |
|
598 | 0 | it = PyObject_GetIter(seq); |
599 | 0 | if (it == NULL) { |
600 | 0 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
601 | 0 | PyErr_SetString(PyExc_TypeError, |
602 | 0 | "reduce() arg 2 must support iteration"); |
603 | 0 | Py_XDECREF(result); |
604 | 0 | return NULL; |
605 | 0 | } |
606 | | |
607 | 0 | if ((args = PyTuple_New(2)) == NULL) |
608 | 0 | goto Fail; |
609 | | |
610 | 0 | for (;;) { |
611 | 0 | PyObject *op2; |
612 | |
|
613 | 0 | if (args->ob_refcnt > 1) { |
614 | 0 | Py_DECREF(args); |
615 | 0 | if ((args = PyTuple_New(2)) == NULL) |
616 | 0 | goto Fail; |
617 | 0 | } |
618 | | |
619 | 0 | op2 = PyIter_Next(it); |
620 | 0 | if (op2 == NULL) { |
621 | 0 | if (PyErr_Occurred()) |
622 | 0 | goto Fail; |
623 | 0 | break; |
624 | 0 | } |
625 | | |
626 | 0 | if (result == NULL) |
627 | 0 | result = op2; |
628 | 0 | else { |
629 | | /* Update the args tuple in-place */ |
630 | 0 | assert(args->ob_refcnt == 1); |
631 | 0 | Py_XSETREF(_PyTuple_ITEMS(args)[0], result); |
632 | 0 | Py_XSETREF(_PyTuple_ITEMS(args)[1], op2); |
633 | 0 | if ((result = PyObject_Call(func, args, NULL)) == NULL) { |
634 | 0 | goto Fail; |
635 | 0 | } |
636 | 0 | } |
637 | 0 | } |
638 | | |
639 | 0 | Py_DECREF(args); |
640 | |
|
641 | 0 | if (result == NULL) |
642 | 0 | PyErr_SetString(PyExc_TypeError, |
643 | 0 | "reduce() of empty sequence with no initial value"); |
644 | |
|
645 | 0 | Py_DECREF(it); |
646 | 0 | return result; |
647 | | |
648 | 0 | Fail: |
649 | 0 | Py_XDECREF(args); |
650 | 0 | Py_XDECREF(result); |
651 | 0 | Py_DECREF(it); |
652 | 0 | return NULL; |
653 | 0 | } |
654 | | |
655 | | PyDoc_STRVAR(functools_reduce_doc, |
656 | | "reduce(function, sequence[, initial]) -> value\n\ |
657 | | \n\ |
658 | | Apply a function of two arguments cumulatively to the items of a sequence,\n\ |
659 | | from left to right, so as to reduce the sequence to a single value.\n\ |
660 | | For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ |
661 | | ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ |
662 | | of the sequence in the calculation, and serves as a default when the\n\ |
663 | | sequence is empty."); |
664 | | |
665 | | /* lru_cache object **********************************************************/ |
666 | | |
667 | | /* There are four principal algorithmic differences from the pure python version: |
668 | | |
669 | | 1). The C version relies on the GIL instead of having its own reentrant lock. |
670 | | |
671 | | 2). The prev/next link fields use borrowed references. |
672 | | |
673 | | 3). For a full cache, the pure python version rotates the location of the |
674 | | root entry so that it never has to move individual links and it can |
675 | | limit updates to just the key and result fields. However, in the C |
676 | | version, links are temporarily removed while the cache dict updates are |
677 | | occurring. Afterwards, they are appended or prepended back into the |
678 | | doubly-linked lists. |
679 | | |
680 | | 4) In the Python version, the _HashSeq class is used to prevent __hash__ |
681 | | from being called more than once. In the C version, the "known hash" |
682 | | variants of dictionary calls as used to the same effect. |
683 | | |
684 | | */ |
685 | | |
686 | | |
687 | | /* this object is used delimit args and keywords in the cache keys */ |
688 | | static PyObject *kwd_mark = NULL; |
689 | | |
690 | | struct lru_list_elem; |
691 | | struct lru_cache_object; |
692 | | |
693 | | typedef struct lru_list_elem { |
694 | | PyObject_HEAD |
695 | | struct lru_list_elem *prev, *next; /* borrowed links */ |
696 | | Py_hash_t hash; |
697 | | PyObject *key, *result; |
698 | | } lru_list_elem; |
699 | | |
700 | | static void |
701 | | lru_list_elem_dealloc(lru_list_elem *link) |
702 | 0 | { |
703 | 0 | Py_XDECREF(link->key); |
704 | 0 | Py_XDECREF(link->result); |
705 | 0 | PyObject_Del(link); |
706 | 0 | } |
707 | | |
708 | | static PyTypeObject lru_list_elem_type = { |
709 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
710 | | "functools._lru_list_elem", /* tp_name */ |
711 | | sizeof(lru_list_elem), /* tp_basicsize */ |
712 | | 0, /* tp_itemsize */ |
713 | | /* methods */ |
714 | | (destructor)lru_list_elem_dealloc, /* tp_dealloc */ |
715 | | 0, /* tp_vectorcall_offset */ |
716 | | 0, /* tp_getattr */ |
717 | | 0, /* tp_setattr */ |
718 | | 0, /* tp_as_async */ |
719 | | 0, /* tp_repr */ |
720 | | 0, /* tp_as_number */ |
721 | | 0, /* tp_as_sequence */ |
722 | | 0, /* tp_as_mapping */ |
723 | | 0, /* tp_hash */ |
724 | | 0, /* tp_call */ |
725 | | 0, /* tp_str */ |
726 | | 0, /* tp_getattro */ |
727 | | 0, /* tp_setattro */ |
728 | | 0, /* tp_as_buffer */ |
729 | | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
730 | | }; |
731 | | |
732 | | |
733 | | typedef PyObject *(*lru_cache_ternaryfunc)(struct lru_cache_object *, PyObject *, PyObject *); |
734 | | |
735 | | typedef struct lru_cache_object { |
736 | | lru_list_elem root; /* includes PyObject_HEAD */ |
737 | | lru_cache_ternaryfunc wrapper; |
738 | | int typed; |
739 | | PyObject *cache; |
740 | | Py_ssize_t hits; |
741 | | PyObject *func; |
742 | | Py_ssize_t maxsize; |
743 | | Py_ssize_t misses; |
744 | | PyObject *cache_info_type; |
745 | | PyObject *dict; |
746 | | } lru_cache_object; |
747 | | |
748 | | static PyTypeObject lru_cache_type; |
749 | | |
750 | | static PyObject * |
751 | | lru_cache_make_key(PyObject *args, PyObject *kwds, int typed) |
752 | 0 | { |
753 | 0 | PyObject *key, *keyword, *value; |
754 | 0 | Py_ssize_t key_size, pos, key_pos, kwds_size; |
755 | |
|
756 | 0 | kwds_size = kwds ? PyDict_GET_SIZE(kwds) : 0; |
757 | | |
758 | | /* short path, key will match args anyway, which is a tuple */ |
759 | 0 | if (!typed && !kwds_size) { |
760 | 0 | if (PyTuple_GET_SIZE(args) == 1) { |
761 | 0 | key = PyTuple_GET_ITEM(args, 0); |
762 | 0 | if (PyUnicode_CheckExact(key) || PyLong_CheckExact(key)) { |
763 | | /* For common scalar keys, save space by |
764 | | dropping the enclosing args tuple */ |
765 | 0 | Py_INCREF(key); |
766 | 0 | return key; |
767 | 0 | } |
768 | 0 | } |
769 | 0 | Py_INCREF(args); |
770 | 0 | return args; |
771 | 0 | } |
772 | | |
773 | 0 | key_size = PyTuple_GET_SIZE(args); |
774 | 0 | if (kwds_size) |
775 | 0 | key_size += kwds_size * 2 + 1; |
776 | 0 | if (typed) |
777 | 0 | key_size += PyTuple_GET_SIZE(args) + kwds_size; |
778 | |
|
779 | 0 | key = PyTuple_New(key_size); |
780 | 0 | if (key == NULL) |
781 | 0 | return NULL; |
782 | | |
783 | 0 | key_pos = 0; |
784 | 0 | for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) { |
785 | 0 | PyObject *item = PyTuple_GET_ITEM(args, pos); |
786 | 0 | Py_INCREF(item); |
787 | 0 | PyTuple_SET_ITEM(key, key_pos++, item); |
788 | 0 | } |
789 | 0 | if (kwds_size) { |
790 | 0 | Py_INCREF(kwd_mark); |
791 | 0 | PyTuple_SET_ITEM(key, key_pos++, kwd_mark); |
792 | 0 | for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) { |
793 | 0 | Py_INCREF(keyword); |
794 | 0 | PyTuple_SET_ITEM(key, key_pos++, keyword); |
795 | 0 | Py_INCREF(value); |
796 | 0 | PyTuple_SET_ITEM(key, key_pos++, value); |
797 | 0 | } |
798 | 0 | assert(key_pos == PyTuple_GET_SIZE(args) + kwds_size * 2 + 1); |
799 | 0 | } |
800 | 0 | if (typed) { |
801 | 0 | for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) { |
802 | 0 | PyObject *item = (PyObject *)Py_TYPE(PyTuple_GET_ITEM(args, pos)); |
803 | 0 | Py_INCREF(item); |
804 | 0 | PyTuple_SET_ITEM(key, key_pos++, item); |
805 | 0 | } |
806 | 0 | if (kwds_size) { |
807 | 0 | for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) { |
808 | 0 | PyObject *item = (PyObject *)Py_TYPE(value); |
809 | 0 | Py_INCREF(item); |
810 | 0 | PyTuple_SET_ITEM(key, key_pos++, item); |
811 | 0 | } |
812 | 0 | } |
813 | 0 | } |
814 | 0 | assert(key_pos == key_size); |
815 | 0 | return key; |
816 | 0 | } |
817 | | |
818 | | static PyObject * |
819 | | uncached_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds) |
820 | 0 | { |
821 | 0 | PyObject *result; |
822 | |
|
823 | 0 | self->misses++; |
824 | 0 | result = PyObject_Call(self->func, args, kwds); |
825 | 0 | if (!result) |
826 | 0 | return NULL; |
827 | 0 | return result; |
828 | 0 | } |
829 | | |
830 | | static PyObject * |
831 | | infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds) |
832 | 0 | { |
833 | 0 | PyObject *result; |
834 | 0 | Py_hash_t hash; |
835 | 0 | PyObject *key = lru_cache_make_key(args, kwds, self->typed); |
836 | 0 | if (!key) |
837 | 0 | return NULL; |
838 | 0 | hash = PyObject_Hash(key); |
839 | 0 | if (hash == -1) { |
840 | 0 | Py_DECREF(key); |
841 | 0 | return NULL; |
842 | 0 | } |
843 | 0 | result = _PyDict_GetItem_KnownHash(self->cache, key, hash); |
844 | 0 | if (result) { |
845 | 0 | Py_INCREF(result); |
846 | 0 | self->hits++; |
847 | 0 | Py_DECREF(key); |
848 | 0 | return result; |
849 | 0 | } |
850 | 0 | if (PyErr_Occurred()) { |
851 | 0 | Py_DECREF(key); |
852 | 0 | return NULL; |
853 | 0 | } |
854 | 0 | self->misses++; |
855 | 0 | result = PyObject_Call(self->func, args, kwds); |
856 | 0 | if (!result) { |
857 | 0 | Py_DECREF(key); |
858 | 0 | return NULL; |
859 | 0 | } |
860 | 0 | if (_PyDict_SetItem_KnownHash(self->cache, key, result, hash) < 0) { |
861 | 0 | Py_DECREF(result); |
862 | 0 | Py_DECREF(key); |
863 | 0 | return NULL; |
864 | 0 | } |
865 | 0 | Py_DECREF(key); |
866 | 0 | return result; |
867 | 0 | } |
868 | | |
869 | | static void |
870 | | lru_cache_extract_link(lru_list_elem *link) |
871 | 0 | { |
872 | 0 | lru_list_elem *link_prev = link->prev; |
873 | 0 | lru_list_elem *link_next = link->next; |
874 | 0 | link_prev->next = link->next; |
875 | 0 | link_next->prev = link->prev; |
876 | 0 | } |
877 | | |
878 | | static void |
879 | | lru_cache_append_link(lru_cache_object *self, lru_list_elem *link) |
880 | 0 | { |
881 | 0 | lru_list_elem *root = &self->root; |
882 | 0 | lru_list_elem *last = root->prev; |
883 | 0 | last->next = root->prev = link; |
884 | 0 | link->prev = last; |
885 | 0 | link->next = root; |
886 | 0 | } |
887 | | |
888 | | static void |
889 | | lru_cache_prepend_link(lru_cache_object *self, lru_list_elem *link) |
890 | 0 | { |
891 | 0 | lru_list_elem *root = &self->root; |
892 | 0 | lru_list_elem *first = root->next; |
893 | 0 | first->prev = root->next = link; |
894 | 0 | link->prev = root; |
895 | 0 | link->next = first; |
896 | 0 | } |
897 | | |
898 | | /* General note on reentrancy: |
899 | | |
900 | | There are four dictionary calls in the bounded_lru_cache_wrapper(): |
901 | | 1) The initial check for a cache match. 2) The post user-function |
902 | | check for a cache match. 3) The deletion of the oldest entry. |
903 | | 4) The addition of the newest entry. |
904 | | |
905 | | In all four calls, we have a known hash which lets use avoid a call |
906 | | to __hash__(). That leaves only __eq__ as a possible source of a |
907 | | reentrant call. |
908 | | |
909 | | The __eq__ method call is always made for a cache hit (dict access #1). |
910 | | Accordingly, we have make sure not modify the cache state prior to |
911 | | this call. |
912 | | |
913 | | The __eq__ method call is never made for the deletion (dict access #3) |
914 | | because it is an identity match. |
915 | | |
916 | | For the other two accesses (#2 and #4), calls to __eq__ only occur |
917 | | when some other entry happens to have an exactly matching hash (all |
918 | | 64-bits). Though rare, this can happen, so we have to make sure to |
919 | | either call it at the top of its code path before any cache |
920 | | state modifications (dict access #2) or be prepared to restore |
921 | | invariants at the end of the code path (dict access #4). |
922 | | |
923 | | Another possible source of reentrancy is a decref which can trigger |
924 | | arbitrary code execution. To make the code easier to reason about, |
925 | | the decrefs are deferred to the end of the each possible code path |
926 | | so that we know the cache is a consistent state. |
927 | | */ |
928 | | |
929 | | static PyObject * |
930 | | bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds) |
931 | 0 | { |
932 | 0 | lru_list_elem *link; |
933 | 0 | PyObject *key, *result, *testresult; |
934 | 0 | Py_hash_t hash; |
935 | |
|
936 | 0 | key = lru_cache_make_key(args, kwds, self->typed); |
937 | 0 | if (!key) |
938 | 0 | return NULL; |
939 | 0 | hash = PyObject_Hash(key); |
940 | 0 | if (hash == -1) { |
941 | 0 | Py_DECREF(key); |
942 | 0 | return NULL; |
943 | 0 | } |
944 | 0 | link = (lru_list_elem *)_PyDict_GetItem_KnownHash(self->cache, key, hash); |
945 | 0 | if (link != NULL) { |
946 | 0 | lru_cache_extract_link(link); |
947 | 0 | lru_cache_append_link(self, link); |
948 | 0 | result = link->result; |
949 | 0 | self->hits++; |
950 | 0 | Py_INCREF(result); |
951 | 0 | Py_DECREF(key); |
952 | 0 | return result; |
953 | 0 | } |
954 | 0 | if (PyErr_Occurred()) { |
955 | 0 | Py_DECREF(key); |
956 | 0 | return NULL; |
957 | 0 | } |
958 | 0 | self->misses++; |
959 | 0 | result = PyObject_Call(self->func, args, kwds); |
960 | 0 | if (!result) { |
961 | 0 | Py_DECREF(key); |
962 | 0 | return NULL; |
963 | 0 | } |
964 | 0 | testresult = _PyDict_GetItem_KnownHash(self->cache, key, hash); |
965 | 0 | if (testresult != NULL) { |
966 | | /* Getting here means that this same key was added to the cache |
967 | | during the PyObject_Call(). Since the link update is already |
968 | | done, we need only return the computed result. */ |
969 | 0 | Py_DECREF(key); |
970 | 0 | return result; |
971 | 0 | } |
972 | 0 | if (PyErr_Occurred()) { |
973 | | /* This is an unusual case since this same lookup |
974 | | did not previously trigger an error during lookup. |
975 | | Treat it the same as an error in user function |
976 | | and return with the error set. */ |
977 | 0 | Py_DECREF(key); |
978 | 0 | Py_DECREF(result); |
979 | 0 | return NULL; |
980 | 0 | } |
981 | | /* This is the normal case. The new key wasn't found before |
982 | | user function call and it is still not there. So we |
983 | | proceed normally and update the cache with the new result. */ |
984 | | |
985 | 0 | assert(self->maxsize > 0); |
986 | 0 | if (PyDict_GET_SIZE(self->cache) < self->maxsize || |
987 | 0 | self->root.next == &self->root) |
988 | 0 | { |
989 | | /* Cache is not full, so put the result in a new link */ |
990 | 0 | link = (lru_list_elem *)PyObject_New(lru_list_elem, |
991 | 0 | &lru_list_elem_type); |
992 | 0 | if (link == NULL) { |
993 | 0 | Py_DECREF(key); |
994 | 0 | Py_DECREF(result); |
995 | 0 | return NULL; |
996 | 0 | } |
997 | | |
998 | 0 | link->hash = hash; |
999 | 0 | link->key = key; |
1000 | 0 | link->result = result; |
1001 | | /* What is really needed here is a SetItem variant with a "no clobber" |
1002 | | option. If the __eq__ call triggers a reentrant call that adds |
1003 | | this same key, then this setitem call will update the cache dict |
1004 | | with this new link, leaving the old link as an orphan (i.e. not |
1005 | | having a cache dict entry that refers to it). */ |
1006 | 0 | if (_PyDict_SetItem_KnownHash(self->cache, key, (PyObject *)link, |
1007 | 0 | hash) < 0) { |
1008 | 0 | Py_DECREF(link); |
1009 | 0 | return NULL; |
1010 | 0 | } |
1011 | 0 | lru_cache_append_link(self, link); |
1012 | 0 | Py_INCREF(result); /* for return */ |
1013 | 0 | return result; |
1014 | 0 | } |
1015 | | /* Since the cache is full, we need to evict an old key and add |
1016 | | a new key. Rather than free the old link and allocate a new |
1017 | | one, we reuse the link for the new key and result and move it |
1018 | | to front of the cache to mark it as recently used. |
1019 | | |
1020 | | We try to assure all code paths (including errors) leave all |
1021 | | of the links in place. Either the link is successfully |
1022 | | updated and moved or it is restored to its old position. |
1023 | | However if an unrecoverable error is found, it doesn't |
1024 | | make sense to reinsert the link, so we leave it out |
1025 | | and the cache will no longer register as full. |
1026 | | */ |
1027 | 0 | PyObject *oldkey, *oldresult, *popresult; |
1028 | | |
1029 | | /* Extract the oldest item. */ |
1030 | 0 | assert(self->root.next != &self->root); |
1031 | 0 | link = self->root.next; |
1032 | 0 | lru_cache_extract_link(link); |
1033 | | /* Remove it from the cache. |
1034 | | The cache dict holds one reference to the link. |
1035 | | We created one other reference when the link was created. |
1036 | | The linked list only has borrowed references. */ |
1037 | 0 | popresult = _PyDict_Pop_KnownHash(self->cache, link->key, |
1038 | 0 | link->hash, Py_None); |
1039 | 0 | if (popresult == Py_None) { |
1040 | | /* Getting here means that the user function call or another |
1041 | | thread has already removed the old key from the dictionary. |
1042 | | This link is now an orphan. Since we don't want to leave the |
1043 | | cache in an inconsistent state, we don't restore the link. */ |
1044 | 0 | Py_DECREF(popresult); |
1045 | 0 | Py_DECREF(link); |
1046 | 0 | Py_DECREF(key); |
1047 | 0 | return result; |
1048 | 0 | } |
1049 | 0 | if (popresult == NULL) { |
1050 | | /* An error arose while trying to remove the oldest key (the one |
1051 | | being evicted) from the cache. We restore the link to its |
1052 | | original position as the oldest link. Then we allow the |
1053 | | error propagate upward; treating it the same as an error |
1054 | | arising in the user function. */ |
1055 | 0 | lru_cache_prepend_link(self, link); |
1056 | 0 | Py_DECREF(key); |
1057 | 0 | Py_DECREF(result); |
1058 | 0 | return NULL; |
1059 | 0 | } |
1060 | | /* Keep a reference to the old key and old result to prevent their |
1061 | | ref counts from going to zero during the update. That will |
1062 | | prevent potentially arbitrary object clean-up code (i.e. __del__) |
1063 | | from running while we're still adjusting the links. */ |
1064 | 0 | oldkey = link->key; |
1065 | 0 | oldresult = link->result; |
1066 | |
|
1067 | 0 | link->hash = hash; |
1068 | 0 | link->key = key; |
1069 | 0 | link->result = result; |
1070 | | /* Note: The link is being added to the cache dict without the |
1071 | | prev and next fields set to valid values. We have to wait |
1072 | | for successful insertion in the cache dict before adding the |
1073 | | link to the linked list. Otherwise, the potentially reentrant |
1074 | | __eq__ call could cause the then orphan link to be visited. */ |
1075 | 0 | if (_PyDict_SetItem_KnownHash(self->cache, key, (PyObject *)link, |
1076 | 0 | hash) < 0) { |
1077 | | /* Somehow the cache dict update failed. We no longer can |
1078 | | restore the old link. Let the error propagate upward and |
1079 | | leave the cache short one link. */ |
1080 | 0 | Py_DECREF(popresult); |
1081 | 0 | Py_DECREF(link); |
1082 | 0 | Py_DECREF(oldkey); |
1083 | 0 | Py_DECREF(oldresult); |
1084 | 0 | return NULL; |
1085 | 0 | } |
1086 | 0 | lru_cache_append_link(self, link); |
1087 | 0 | Py_INCREF(result); /* for return */ |
1088 | 0 | Py_DECREF(popresult); |
1089 | 0 | Py_DECREF(oldkey); |
1090 | 0 | Py_DECREF(oldresult); |
1091 | 0 | return result; |
1092 | 0 | } |
1093 | | |
1094 | | static PyObject * |
1095 | | lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
1096 | 1 | { |
1097 | 1 | PyObject *func, *maxsize_O, *cache_info_type, *cachedict; |
1098 | 1 | int typed; |
1099 | 1 | lru_cache_object *obj; |
1100 | 1 | Py_ssize_t maxsize; |
1101 | 1 | PyObject *(*wrapper)(lru_cache_object *, PyObject *, PyObject *); |
1102 | 1 | static char *keywords[] = {"user_function", "maxsize", "typed", |
1103 | 1 | "cache_info_type", NULL}; |
1104 | | |
1105 | 1 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OOpO:lru_cache", keywords, |
1106 | 1 | &func, &maxsize_O, &typed, |
1107 | 1 | &cache_info_type)) { |
1108 | 0 | return NULL; |
1109 | 0 | } |
1110 | | |
1111 | 1 | if (!PyCallable_Check(func)) { |
1112 | 0 | PyErr_SetString(PyExc_TypeError, |
1113 | 0 | "the first argument must be callable"); |
1114 | 0 | return NULL; |
1115 | 0 | } |
1116 | | |
1117 | | /* select the caching function, and make/inc maxsize_O */ |
1118 | 1 | if (maxsize_O == Py_None) { |
1119 | 0 | wrapper = infinite_lru_cache_wrapper; |
1120 | | /* use this only to initialize lru_cache_object attribute maxsize */ |
1121 | 0 | maxsize = -1; |
1122 | 1 | } else if (PyIndex_Check(maxsize_O)) { |
1123 | 1 | maxsize = PyNumber_AsSsize_t(maxsize_O, PyExc_OverflowError); |
1124 | 1 | if (maxsize == -1 && PyErr_Occurred()) |
1125 | 0 | return NULL; |
1126 | 1 | if (maxsize < 0) { |
1127 | 0 | maxsize = 0; |
1128 | 0 | } |
1129 | 1 | if (maxsize == 0) |
1130 | 0 | wrapper = uncached_lru_cache_wrapper; |
1131 | 1 | else |
1132 | 1 | wrapper = bounded_lru_cache_wrapper; |
1133 | 1 | } else { |
1134 | 0 | PyErr_SetString(PyExc_TypeError, "maxsize should be integer or None"); |
1135 | 0 | return NULL; |
1136 | 0 | } |
1137 | | |
1138 | 1 | if (!(cachedict = PyDict_New())) |
1139 | 0 | return NULL; |
1140 | | |
1141 | 1 | obj = (lru_cache_object *)type->tp_alloc(type, 0); |
1142 | 1 | if (obj == NULL) { |
1143 | 0 | Py_DECREF(cachedict); |
1144 | 0 | return NULL; |
1145 | 0 | } |
1146 | | |
1147 | 1 | obj->root.prev = &obj->root; |
1148 | 1 | obj->root.next = &obj->root; |
1149 | 1 | obj->wrapper = wrapper; |
1150 | 1 | obj->typed = typed; |
1151 | 1 | obj->cache = cachedict; |
1152 | 1 | Py_INCREF(func); |
1153 | 1 | obj->func = func; |
1154 | 1 | obj->misses = obj->hits = 0; |
1155 | 1 | obj->maxsize = maxsize; |
1156 | 1 | Py_INCREF(cache_info_type); |
1157 | 1 | obj->cache_info_type = cache_info_type; |
1158 | 1 | return (PyObject *)obj; |
1159 | 1 | } |
1160 | | |
1161 | | static lru_list_elem * |
1162 | | lru_cache_unlink_list(lru_cache_object *self) |
1163 | 0 | { |
1164 | 0 | lru_list_elem *root = &self->root; |
1165 | 0 | lru_list_elem *link = root->next; |
1166 | 0 | if (link == root) |
1167 | 0 | return NULL; |
1168 | 0 | root->prev->next = NULL; |
1169 | 0 | root->next = root->prev = root; |
1170 | 0 | return link; |
1171 | 0 | } |
1172 | | |
1173 | | static void |
1174 | | lru_cache_clear_list(lru_list_elem *link) |
1175 | 0 | { |
1176 | 0 | while (link != NULL) { |
1177 | 0 | lru_list_elem *next = link->next; |
1178 | 0 | Py_DECREF(link); |
1179 | 0 | link = next; |
1180 | 0 | } |
1181 | 0 | } |
1182 | | |
1183 | | static void |
1184 | | lru_cache_dealloc(lru_cache_object *obj) |
1185 | 0 | { |
1186 | 0 | lru_list_elem *list; |
1187 | | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
1188 | 0 | PyObject_GC_UnTrack(obj); |
1189 | |
|
1190 | 0 | list = lru_cache_unlink_list(obj); |
1191 | 0 | Py_XDECREF(obj->cache); |
1192 | 0 | Py_XDECREF(obj->func); |
1193 | 0 | Py_XDECREF(obj->cache_info_type); |
1194 | 0 | Py_XDECREF(obj->dict); |
1195 | 0 | lru_cache_clear_list(list); |
1196 | 0 | Py_TYPE(obj)->tp_free(obj); |
1197 | 0 | } |
1198 | | |
1199 | | static PyObject * |
1200 | | lru_cache_call(lru_cache_object *self, PyObject *args, PyObject *kwds) |
1201 | 0 | { |
1202 | 0 | return self->wrapper(self, args, kwds); |
1203 | 0 | } |
1204 | | |
1205 | | static PyObject * |
1206 | | lru_cache_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
1207 | 0 | { |
1208 | 0 | if (obj == Py_None || obj == NULL) { |
1209 | 0 | Py_INCREF(self); |
1210 | 0 | return self; |
1211 | 0 | } |
1212 | 0 | return PyMethod_New(self, obj); |
1213 | 0 | } |
1214 | | |
1215 | | static PyObject * |
1216 | | lru_cache_cache_info(lru_cache_object *self, PyObject *unused) |
1217 | 0 | { |
1218 | 0 | if (self->maxsize == -1) { |
1219 | 0 | return PyObject_CallFunction(self->cache_info_type, "nnOn", |
1220 | 0 | self->hits, self->misses, Py_None, |
1221 | 0 | PyDict_GET_SIZE(self->cache)); |
1222 | 0 | } |
1223 | 0 | return PyObject_CallFunction(self->cache_info_type, "nnnn", |
1224 | 0 | self->hits, self->misses, self->maxsize, |
1225 | 0 | PyDict_GET_SIZE(self->cache)); |
1226 | 0 | } |
1227 | | |
1228 | | static PyObject * |
1229 | | lru_cache_cache_clear(lru_cache_object *self, PyObject *unused) |
1230 | 0 | { |
1231 | 0 | lru_list_elem *list = lru_cache_unlink_list(self); |
1232 | 0 | self->hits = self->misses = 0; |
1233 | 0 | PyDict_Clear(self->cache); |
1234 | 0 | lru_cache_clear_list(list); |
1235 | 0 | Py_RETURN_NONE; |
1236 | 0 | } |
1237 | | |
1238 | | static PyObject * |
1239 | | lru_cache_reduce(PyObject *self, PyObject *unused) |
1240 | 0 | { |
1241 | 0 | return PyObject_GetAttrString(self, "__qualname__"); |
1242 | 0 | } |
1243 | | |
1244 | | static PyObject * |
1245 | | lru_cache_copy(PyObject *self, PyObject *unused) |
1246 | 0 | { |
1247 | 0 | Py_INCREF(self); |
1248 | 0 | return self; |
1249 | 0 | } |
1250 | | |
1251 | | static PyObject * |
1252 | | lru_cache_deepcopy(PyObject *self, PyObject *unused) |
1253 | 0 | { |
1254 | 0 | Py_INCREF(self); |
1255 | 0 | return self; |
1256 | 0 | } |
1257 | | |
1258 | | static int |
1259 | | lru_cache_tp_traverse(lru_cache_object *self, visitproc visit, void *arg) |
1260 | 2 | { |
1261 | 2 | lru_list_elem *link = self->root.next; |
1262 | 2 | while (link != &self->root) { |
1263 | 0 | lru_list_elem *next = link->next; |
1264 | 0 | Py_VISIT(link->key); |
1265 | 0 | Py_VISIT(link->result); |
1266 | 0 | link = next; |
1267 | 0 | } |
1268 | 2 | Py_VISIT(self->func); |
1269 | 2 | Py_VISIT(self->cache); |
1270 | 2 | Py_VISIT(self->cache_info_type); |
1271 | 2 | Py_VISIT(self->dict); |
1272 | 2 | return 0; |
1273 | 2 | } |
1274 | | |
1275 | | static int |
1276 | | lru_cache_tp_clear(lru_cache_object *self) |
1277 | 0 | { |
1278 | 0 | lru_list_elem *list = lru_cache_unlink_list(self); |
1279 | 0 | Py_CLEAR(self->func); |
1280 | 0 | Py_CLEAR(self->cache); |
1281 | 0 | Py_CLEAR(self->cache_info_type); |
1282 | 0 | Py_CLEAR(self->dict); |
1283 | 0 | lru_cache_clear_list(list); |
1284 | 0 | return 0; |
1285 | 0 | } |
1286 | | |
1287 | | |
1288 | | PyDoc_STRVAR(lru_cache_doc, |
1289 | | "Create a cached callable that wraps another function.\n\ |
1290 | | \n\ |
1291 | | user_function: the function being cached\n\ |
1292 | | \n\ |
1293 | | maxsize: 0 for no caching\n\ |
1294 | | None for unlimited cache size\n\ |
1295 | | n for a bounded cache\n\ |
1296 | | \n\ |
1297 | | typed: False cache f(3) and f(3.0) as identical calls\n\ |
1298 | | True cache f(3) and f(3.0) as distinct calls\n\ |
1299 | | \n\ |
1300 | | cache_info_type: namedtuple class with the fields:\n\ |
1301 | | hits misses currsize maxsize\n" |
1302 | | ); |
1303 | | |
1304 | | static PyMethodDef lru_cache_methods[] = { |
1305 | | {"cache_info", (PyCFunction)lru_cache_cache_info, METH_NOARGS}, |
1306 | | {"cache_clear", (PyCFunction)lru_cache_cache_clear, METH_NOARGS}, |
1307 | | {"__reduce__", (PyCFunction)lru_cache_reduce, METH_NOARGS}, |
1308 | | {"__copy__", (PyCFunction)lru_cache_copy, METH_VARARGS}, |
1309 | | {"__deepcopy__", (PyCFunction)lru_cache_deepcopy, METH_VARARGS}, |
1310 | | {NULL} |
1311 | | }; |
1312 | | |
1313 | | static PyGetSetDef lru_cache_getsetlist[] = { |
1314 | | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
1315 | | {NULL} |
1316 | | }; |
1317 | | |
1318 | | static PyTypeObject lru_cache_type = { |
1319 | | PyVarObject_HEAD_INIT(NULL, 0) |
1320 | | "functools._lru_cache_wrapper", /* tp_name */ |
1321 | | sizeof(lru_cache_object), /* tp_basicsize */ |
1322 | | 0, /* tp_itemsize */ |
1323 | | /* methods */ |
1324 | | (destructor)lru_cache_dealloc, /* tp_dealloc */ |
1325 | | 0, /* tp_vectorcall_offset */ |
1326 | | 0, /* tp_getattr */ |
1327 | | 0, /* tp_setattr */ |
1328 | | 0, /* tp_as_async */ |
1329 | | 0, /* tp_repr */ |
1330 | | 0, /* tp_as_number */ |
1331 | | 0, /* tp_as_sequence */ |
1332 | | 0, /* tp_as_mapping */ |
1333 | | 0, /* tp_hash */ |
1334 | | (ternaryfunc)lru_cache_call, /* tp_call */ |
1335 | | 0, /* tp_str */ |
1336 | | 0, /* tp_getattro */ |
1337 | | 0, /* tp_setattro */ |
1338 | | 0, /* tp_as_buffer */ |
1339 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
1340 | | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_METHOD_DESCRIPTOR, |
1341 | | /* tp_flags */ |
1342 | | lru_cache_doc, /* tp_doc */ |
1343 | | (traverseproc)lru_cache_tp_traverse,/* tp_traverse */ |
1344 | | (inquiry)lru_cache_tp_clear, /* tp_clear */ |
1345 | | 0, /* tp_richcompare */ |
1346 | | 0, /* tp_weaklistoffset */ |
1347 | | 0, /* tp_iter */ |
1348 | | 0, /* tp_iternext */ |
1349 | | lru_cache_methods, /* tp_methods */ |
1350 | | 0, /* tp_members */ |
1351 | | lru_cache_getsetlist, /* tp_getset */ |
1352 | | 0, /* tp_base */ |
1353 | | 0, /* tp_dict */ |
1354 | | lru_cache_descr_get, /* tp_descr_get */ |
1355 | | 0, /* tp_descr_set */ |
1356 | | offsetof(lru_cache_object, dict), /* tp_dictoffset */ |
1357 | | 0, /* tp_init */ |
1358 | | 0, /* tp_alloc */ |
1359 | | lru_cache_new, /* tp_new */ |
1360 | | }; |
1361 | | |
1362 | | /* module level code ********************************************************/ |
1363 | | |
1364 | | PyDoc_STRVAR(module_doc, |
1365 | | "Tools that operate on functions."); |
1366 | | |
1367 | | static PyMethodDef module_methods[] = { |
1368 | | {"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc}, |
1369 | | {"cmp_to_key", (PyCFunction)(void(*)(void))functools_cmp_to_key, |
1370 | | METH_VARARGS | METH_KEYWORDS, functools_cmp_to_key_doc}, |
1371 | | {NULL, NULL} /* sentinel */ |
1372 | | }; |
1373 | | |
1374 | | static void |
1375 | | module_free(void *m) |
1376 | 0 | { |
1377 | 0 | Py_CLEAR(kwd_mark); |
1378 | 0 | } |
1379 | | |
1380 | | static struct PyModuleDef _functoolsmodule = { |
1381 | | PyModuleDef_HEAD_INIT, |
1382 | | "_functools", |
1383 | | module_doc, |
1384 | | -1, |
1385 | | module_methods, |
1386 | | NULL, |
1387 | | NULL, |
1388 | | NULL, |
1389 | | module_free, |
1390 | | }; |
1391 | | |
1392 | | PyMODINIT_FUNC |
1393 | | PyInit__functools(void) |
1394 | 1 | { |
1395 | 1 | int i; |
1396 | 1 | PyObject *m; |
1397 | 1 | const char *name; |
1398 | 1 | PyTypeObject *typelist[] = { |
1399 | 1 | &partial_type, |
1400 | 1 | &lru_cache_type, |
1401 | 1 | NULL |
1402 | 1 | }; |
1403 | | |
1404 | 1 | m = PyModule_Create(&_functoolsmodule); |
1405 | 1 | if (m == NULL) |
1406 | 0 | return NULL; |
1407 | | |
1408 | 1 | kwd_mark = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type); |
1409 | 1 | if (!kwd_mark) { |
1410 | 0 | Py_DECREF(m); |
1411 | 0 | return NULL; |
1412 | 0 | } |
1413 | | |
1414 | 3 | for (i=0 ; typelist[i] != NULL ; i++) { |
1415 | 2 | if (PyType_Ready(typelist[i]) < 0) { |
1416 | 0 | Py_DECREF(m); |
1417 | 0 | return NULL; |
1418 | 0 | } |
1419 | 2 | name = _PyType_Name(typelist[i]); |
1420 | 2 | Py_INCREF(typelist[i]); |
1421 | 2 | PyModule_AddObject(m, name, (PyObject *)typelist[i]); |
1422 | 2 | } |
1423 | 1 | return m; |
1424 | 1 | } |