Skip to content

Commit 5bb2dd5

Browse files
committed
apply petr's comment
1 parent 3c36c69 commit 5bb2dd5

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

Modules/_functoolsmodule.c

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ typedef struct _functools_state {
2828
/* this object is used delimit args and keywords in the cache keys */
2929
PyObject *kwd_mark;
3030
PyTypeObject *partial_type;
31-
PyTypeObject *lru_cache_type;
3231
PyTypeObject *keyobject_type;
3332
PyTypeObject *lru_list_elem_type;
3433
} _functools_state;
@@ -67,15 +66,13 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
6766
return NULL;
6867
}
6968

70-
_functools_state *state = get_functools_state_by_type(type);
71-
if (state == NULL) {
72-
return NULL;
73-
}
74-
7569
pargs = pkw = NULL;
7670
func = PyTuple_GET_ITEM(args, 0);
77-
if (Py_IS_TYPE(func, state->partial_type)
78-
&& type == state->partial_type) {
71+
if (Py_TYPE(func)->tp_new == partial_new) {
72+
// The type of "func" might not be exactly the same type object
73+
// as "type", but if it is called using partial_new, it must have the
74+
// same memory layout (fn, args and kw members).
75+
// We can use its underlying function directly and merge the arguments.
7976
partialobject *part = (partialobject *)func;
8077
if (part->dict == NULL) {
8178
pargs = part->args;
@@ -555,19 +552,15 @@ keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds)
555552
{
556553
PyObject *object;
557554
keyobject *result;
558-
_functools_state *state;
559555
static char *kwargs[] = {"obj", NULL};
560556

561557
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:K", kwargs, &object))
562558
return NULL;
563559

564-
state = get_functools_state_by_type(Py_TYPE(ko));
565-
if (state == NULL) {
560+
result = PyObject_New(keyobject, Py_TYPE(ko));
561+
if (result == NULL) {
566562
return NULL;
567563
}
568-
result = PyObject_New(keyobject, state->keyobject_type);
569-
if (!result)
570-
return NULL;
571564
Py_INCREF(ko->cmp);
572565
result->cmp = ko->cmp;
573566
Py_INCREF(object);
@@ -584,13 +577,8 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
584577
PyObject *compare;
585578
PyObject *answer;
586579
PyObject* stack[2];
587-
_functools_state *state;
588580

589-
state = get_functools_state_by_type(Py_TYPE(ko));
590-
if (state == NULL) {
591-
return NULL;
592-
}
593-
if (!Py_IS_TYPE(other, state->keyobject_type)) {
581+
if (!Py_IS_TYPE(other, Py_TYPE(ko))) {
594582
PyErr_Format(PyExc_TypeError, "other argument must be K instance");
595583
return NULL;
596584
}
@@ -1437,12 +1425,13 @@ _functools_exec(PyObject *module)
14371425
return -1;
14381426
}
14391427

1440-
state->lru_cache_type = (PyTypeObject *)PyType_FromModuleAndSpec(module,
1428+
PyObject *lru_cache_type = PyType_FromModuleAndSpec(module,
14411429
&lru_cache_type_spec, NULL);
1442-
if (state->lru_cache_type == NULL) {
1430+
if (lru_cache_type == NULL) {
14431431
return -1;
14441432
}
1445-
if (PyModule_AddType(module, state->lru_cache_type) < 0) {
1433+
if (PyModule_AddType(module, (PyTypeObject *)lru_cache_type) < 0) {
1434+
Py_DECREF(lru_cache_type);
14461435
return -1;
14471436
}
14481437

@@ -1473,7 +1462,6 @@ _functools_traverse(PyObject *module, visitproc visit, void *arg)
14731462
_functools_state *state = get_functools_state(module);
14741463
Py_VISIT(state->kwd_mark);
14751464
Py_VISIT(state->partial_type);
1476-
Py_VISIT(state->lru_cache_type);
14771465
Py_VISIT(state->keyobject_type);
14781466
Py_VISIT(state->lru_list_elem_type);
14791467
return 0;
@@ -1485,7 +1473,6 @@ _functools_clear(PyObject *module)
14851473
_functools_state *state = get_functools_state(module);
14861474
Py_CLEAR(state->kwd_mark);
14871475
Py_CLEAR(state->partial_type);
1488-
Py_CLEAR(state->lru_cache_type);
14891476
Py_CLEAR(state->keyobject_type);
14901477
Py_CLEAR(state->lru_list_elem_type);
14911478
return 0;

0 commit comments

Comments
 (0)