pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

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

Open juanjosegarciaripoll opened this issue 9 months ago • 0 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [X] Consider asking first in the Gitter chat room or in a Discussion.

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

juanjosegarciaripoll avatar Apr 30 '24 13:04 juanjosegarciaripoll