GrahamDumpleton / wrapt

A Python module for decorators, wrappers and monkey patching.
BSD 2-Clause "Simplified" License
2.04k stars 230 forks source link

Add support for isinstance() checks. #201 #202

Closed hongweipeng closed 2 years ago

GrahamDumpleton commented 2 years ago

Going to close and reopen this to try and force ability to run tests via GitHub actions. Option not showing.

GrahamDumpleton commented 2 years ago

Didn't pay attention close enough when I merged. After the fact I simplified the C code to:

static PyObject *WraptFunctionWrapperBase_instancecheck(
        WraptFunctionWrapperObject *self, PyObject *instance)
{
    PyObject *object = NULL;
    PyObject *result = NULL;

    int check = 0;

    if (!self->object_proxy.wrapped) {
        PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized");
        return NULL;
    }

    check = PyObject_IsInstance(instance, self->object_proxy.wrapped);

    if (check < 0) {
        return NULL;
    }

    result = check ? Py_True : Py_False;

    Py_INCREF(result);
    return result;
}