From 9399d7cacb373f3548d5106351fdacd6c9cc1404 Mon Sep 17 00:00:00 2001 From: Graham Dumpleton Date: Sat, 5 Mar 2022 15:07:54 +1100 Subject: [PATCH] Clean up C version of instance check and add change note. --- docs/changes.rst | 6 ++++++ src/wrapt/_wrappers.c | 9 +++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/docs/changes.rst b/docs/changes.rst index 1dd17741..06946e60 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -12,6 +12,12 @@ Version 1.14.0 The replacement function is exposed as `wrapt.formatargspec()` if need it for your own code. +* When using a decorator on a class, `isinstance()` checks wouldn't previously + work as expected and you had to manually use `Type.__wrapped__` to access + the real type when doing instance checks. The `__instancecheck__` hook is now + implemented such that you don't have to use `Type.__wrapped__` instead of + `Type` as last argument to `isinstance()`. + **New Features** * Binary wheels provided on PyPi for `aarch64` Linux systems and macOS diff --git a/src/wrapt/_wrappers.c b/src/wrapt/_wrappers.c index 575bdaa8..67c5d5e1 100644 --- a/src/wrapt/_wrappers.c +++ b/src/wrapt/_wrappers.c @@ -2551,21 +2551,18 @@ static PyObject *WraptFunctionWrapperBase_instancecheck( PyObject *result = NULL; int check = 0; + if (!self->object_proxy.wrapped) { PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized"); return NULL; } - object = PyObject_GetAttrString(self, "__wrapped__"); - if (!object) { - PyErr_Clear(); - } + check = PyObject_IsInstance(instance, self->object_proxy.wrapped); - check = PyObject_IsInstance(instance, object); - Py_XDECREF(object); if (check < 0) { return NULL; } + result = check ? Py_True : Py_False; Py_INCREF(result);