Skip to content

Commit

Permalink
fix: use PyFrame_GetCode on Python 3.9+
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Oct 15, 2021
1 parent 061751d commit e7aee30
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,28 @@ inline function get_type_override(const void *this_ptr, const type_info *this_ty
/* Don't call dispatch code if invoked from overridden function.
Unfortunately this doesn't work on PyPy. */
#if !defined(PYPY_VERSION)

#if PY_VERSION_HEX >= 0x03090000
PyFrameObject *frame = PyThreadState_GetFrame(PyThreadState_Get());
if (frame != nullptr) {
PyCodeObject *code = PyFrame_GetCode(frame);
if ((std::string) str(code->co_name) == name && code->co_argcount > 0) {
PyObject* locals = PyEval_GetLocals();
if (locals != nullptr) {
PyObject *self_caller = dict_getitem(
locals, PyTuple_GET_ITEM(code->co_varnames, 0)
);
if (self_caller == self.ptr()) {
Py_DECREF(code);
Py_DECREF(frame);
return function();
}
}
}
Py_DECREF(code);
Py_DECREF(frame);
}
#else
PyFrameObject *frame = PyThreadState_Get()->frame;
if (frame != nullptr && (std::string) str(frame->f_code->co_name) == name
&& frame->f_code->co_argcount > 0) {
Expand All @@ -2335,6 +2357,8 @@ inline function get_type_override(const void *this_ptr, const type_info *this_ty
if (self_caller == self.ptr())
return function();
}
#endif

#else
/* PyPy currently doesn't provide a detailed cpyext emulation of
frame objects, so we have to emulate this using Python. This
Expand Down

0 comments on commit e7aee30

Please sign in to comment.