pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.08k stars 2.05k forks source link

[BUG]: py::object.get_pointer() should use reinterpret_cast #5116

Open juanjosegarciaripoll opened 2 months ago

juanjosegarciaripoll commented 2 months ago

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.12.0

Problem description

The code for get_pointer() reads

T *result = static_cast<T *>(PyCapsule_GetPointer(m_ptr, name));

This makes it impossible to use get_pointer<f>() where f is a function type. I believe the right code should use reinterpret_cast. MSVC does not complain, but GCC breaks my code because of this.

Reproducible example code

//// Usual pybind headers and definitions

template <class f>
static void load_wrapper(py::dict &__pyx_capi__, const char *name,
                         f *&pointer) {
  py::capsule wrapper = __pyx_capi__[name];
  pointer = wrapper.get_pointer<f>();
}

double (*ddot_ptr)(int *n, double *zx, int *incx, double *zy, int *incy);

void load_scipy_wrappers() {
    auto cython_blas = py::module_::import("scipy.linalg.cython_blas");
    py::dict __pyx_capi__ = cython_blas.attr("__pyx_capi__");
    load_wrapper(__pyx_capi__, "ddot", ddot_ptr);
 }

Is this a regression? Put the last known working version here if it is.

Not a regression