Skip to content

Multiple const members of struct breaks PYBIND11_NUMPY_DTYPE #2147

Description

@rprechelt

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

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions