Skip to content

Commit 4f587cb

Browse files
committed
Get class and staticmethod from builtins
1 parent 40e3df9 commit 4f587cb

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

src/wrapt/_wrappers.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,8 @@ static int WraptFunctionWrapper_init(WraptFunctionWrapperObject *self,
26422642
static PyObject *classmethod_str = NULL;
26432643
static PyObject *staticmethod_str = NULL;
26442644
static PyObject *function_str = NULL;
2645+
static PyObject *classmethod_type = NULL;
2646+
static PyObject *staticmethod_type = NULL;
26452647

26462648
int result = 0;
26472649

@@ -2664,16 +2666,34 @@ static int WraptFunctionWrapper_init(WraptFunctionWrapperObject *self,
26642666
function_str = PyUnicode_InternFromString("function");
26652667
}
26662668

2667-
/* XXX TODO
2668-
if (PyObject_IsInstance(wrapped, (PyObject *)&PyClassMethod_Type)) {
2669+
if (!classmethod_type) {
2670+
PyObject *builtins = PyImport_ImportModule("builtins");
2671+
classmethod_type = PyObject_GetAttrString(builtins, "classmethod");
2672+
if (!staticmethod_type) {
2673+
staticmethod_type = PyObject_GetAttrString(builtins, "staticmethod");
2674+
}
2675+
Py_DECREF(builtins);
2676+
if ((!classmethod_type) || (!staticmethod_type)) {
2677+
return -1;
2678+
}
2679+
}
2680+
2681+
if (!staticmethod_type) {
2682+
PyObject *builtins = PyImport_ImportModule("builtins");
2683+
staticmethod_type = PyObject_GetAttrString(builtins, "staticmethod");
2684+
Py_DECREF(builtins);
2685+
if (!staticmethod_type) {
2686+
return -1;
2687+
}
2688+
}
2689+
2690+
if (PyObject_IsInstance(wrapped, classmethod_type)) {
26692691
binding = classmethod_str;
26702692
}
2671-
else if (PyObject_IsInstance(wrapped, (PyObject *)&PyStaticMethod_Type)) {
2693+
else if (PyObject_IsInstance(wrapped, staticmethod_type)) {
26722694
binding = staticmethod_str;
26732695
}
2674-
else
2675-
*/
2676-
if ((instance = PyObject_GetAttrString(wrapped, "__self__")) != 0) {
2696+
else if ((instance = PyObject_GetAttrString(wrapped, "__self__")) != 0) {
26772697
if (PyObject_IsInstance(instance, (PyObject *)&PyType_Type)) {
26782698
binding = classmethod_str;
26792699
}

0 commit comments

Comments
 (0)