[BUG]: py::object.get_pointer() should use reinterpret_cast
Open
Nobody has claimed this yet.
triage
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the implementation of py::object.get_pointer() and the code path using PyCapsule_GetPointer. Reproduce the function-pointer example with GCC, then verify that the function-pointer case compiles and existing capsule behavior remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100