pybind / pybind/pybind11

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

Open
#5,116 0 comments 0 reactions 0 assignees View on GitHub

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
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.