pybind / pybind/pybind11

Multiple const members of struct breaks PYBIND11_NUMPY_DTYPE

Open
#2,147 1 comment 0 reactions 1 assignee View on GitHub

@YannickJadoul is already working on this.

Since Jul 26, 2020.

bug
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Issue description

Attempting to PYBIND11_NUMPY_DTYPE on a struct containing more than one const double gives a ImportError: NumPy: invalid buffer descriptor! error on import. A class containing a single const double runs correctly. If you change the struct to contain two const double's or a const double and a regular double, it fails on import.

Reproducible example code

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

// a struct with one enum and a double
struct StructType1 {

  const double x;

  StructType1(const double x_) : x(x_) {};
  
};


// a struct with one enum and two doubles
struct StructType2 {

  const double x;
  const double y;

  StructType2(const double x_,
              const double y_) : x(x_), y(y_) {};


};


PYBIND11_MODULE(mystruct, m) {

  // wrap our class with one double
  py::class_<StructType1>(m, "StructType1")
    .def(py::init<const double>());

  // and wrap our class with two doubles
  py::class_<StructType2>(m, "StructType2")
    .def(py::init<const double, const double>());

  // this line is fine
  PYBIND11_NUMPY_DTYPE(StructType1, x);

  // including the below line gives a
  // ImportError: NumPy: invalid buffer descriptor!
  // PYBIND11_NUMPY_DTYPE(StructType2, x, y);

}

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.