From e7e1edff662fecf25d85aa4383520e6b1704ad86 Mon Sep 17 00:00:00 2001 From: Stephen McDowell Date: Mon, 7 Nov 2022 09:21:59 -0500 Subject: [PATCH] [tmp] test in CI equivalent of https://github.com/RobotLocomotion/pybind11/pull/60 --- .../pybind11/patches/python-3.11.patch | 60 +++++++++++++++++++ tools/workspace/pybind11/repository.bzl | 3 + 2 files changed, 63 insertions(+) create mode 100644 tools/workspace/pybind11/patches/python-3.11.patch diff --git a/tools/workspace/pybind11/patches/python-3.11.patch b/tools/workspace/pybind11/patches/python-3.11.patch new file mode 100644 index 000000000000..db358ed7534c --- /dev/null +++ b/tools/workspace/pybind11/patches/python-3.11.patch @@ -0,0 +1,60 @@ +--- include/pybind11/detail/type_caster_base.h ++++ include/pybind11/detail/type_caster_base.h +@@ -524,9 +524,10 @@ PYBIND11_NOINLINE std::string error_string() { + trace = trace->tb_next; + + PyFrameObject *frame = trace->tb_frame; ++ Py_XINCREF(frame); + errorString += "\n\nAt:\n"; + while (frame) { +-#if PY_VERSION_HEX >= 0x03090000 ++#if PY_VERSION_HEX >= 0x030900B1 + PyCodeObject *f_code = PyFrame_GetCode(frame); + #else + PyCodeObject *f_code = frame->f_code; +@@ -537,8 +538,15 @@ PYBIND11_NOINLINE std::string error_string() { + " " + handle(f_code->co_filename).cast() + + "(" + std::to_string(lineno) + "): " + + handle(f_code->co_name).cast() + "\n"; +- frame = frame->f_back; + Py_DECREF(f_code); ++#if PY_VERSION_HEX >= 0x030900B1 ++ auto *b_frame = PyFrame_GetBack(frame); ++#else ++ auto *b_frame = frame->f_back; ++ Py_XINCREF(b_frame); ++#endif ++ Py_DECREF(frame); ++ frame = b_frame; + } + } + #endif +--- include/pybind11/pybind11.h ++++ include/pybind11/pybind11.h +@@ -2776,9 +2776,7 @@ 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) && PY_VERSION_HEX < 0x030B0000 +- // TODO: Remove PyPy workaround for Python 3.11. +- // Current API fails on 3.11 since co_varnames can be null. ++#if !defined(PYPY_VERSION) + #if PY_VERSION_HEX >= 0x03090000 + PyFrameObject *frame = PyThreadState_GetFrame(PyThreadState_Get()); + if (frame != nullptr) { +@@ -2786,10 +2784,11 @@ inline function get_type_override(const void *this_ptr, const type_info *this_ty + // f_code is guaranteed to not be NULL + if ((std::string) str(f_code->co_name) == name && f_code->co_argcount > 0) { + PyObject* locals = PyEval_GetLocals(); +- if (locals != nullptr && f_code->co_varnames != nullptr) { +- PyObject *self_caller = dict_getitem( +- locals, PyTuple_GET_ITEM(f_code->co_varnames, 0) +- ); ++ if (locals != nullptr) { ++ PyObject *co_varnames = PyObject_GetAttrString((PyObject *) f_code, "co_varnames"); ++ PyObject *self_arg = PyTuple_GET_ITEM(co_varnames, 0); ++ Py_DECREF(co_varnames); ++ PyObject *self_caller = dict_getitem(locals, self_arg); + if (self_caller == self.ptr()) { + Py_DECREF(f_code); + Py_DECREF(frame); diff --git a/tools/workspace/pybind11/repository.bzl b/tools/workspace/pybind11/repository.bzl index 6f3a4011be8f..a3fd78b498a9 100644 --- a/tools/workspace/pybind11/repository.bzl +++ b/tools/workspace/pybind11/repository.bzl @@ -24,6 +24,9 @@ def pybind11_repository( sha256 = _SHA256, build_file = ":package.BUILD.bazel", mirrors = mirrors, + patches = [ + ":patches/python-3.11.patch", + ], ) def generate_pybind11_version_py_file(name):