Make the "Pointer types ... are not supported" message Eigen-specific, as suggested by @Lalaland. Move to new pybind11/eigen/common.h header.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2023-05-18 13:44:22 -07:00
parent ddb625e69d
commit 03dafde5cb
6 changed files with 20 additions and 12 deletions

View File

@ -126,6 +126,7 @@ set(PYBIND11_HEADERS
include/pybind11/complex.h include/pybind11/complex.h
include/pybind11/options.h include/pybind11/options.h
include/pybind11/eigen.h include/pybind11/eigen.h
include/pybind11/eigen/common.h
include/pybind11/eigen/matrix.h include/pybind11/eigen/matrix.h
include/pybind11/eigen/tensor.h include/pybind11/eigen/tensor.h
include/pybind11/embed.h include/pybind11/embed.h

View File

@ -1034,11 +1034,6 @@ struct format_descriptor<
static std::string format() { return std::string(1, c); } static std::string format() { return std::string(1, c); }
}; };
// Common message for `static_assert()`s, which are useful to easily preempt much less obvious
// errors in code that does not support `format_descriptor<PyObject *>`.
#define PYBIND11_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED \
"Pointer types (in particular `PyObject *`) are not supported."
PYBIND11_NAMESPACE_BEGIN(detail) PYBIND11_NAMESPACE_BEGIN(detail)
// Returns the index of the given type in the type char array below, and in the list in numpy.h // Returns the index of the given type in the type char array below, and in the list in numpy.h
// The order here is: bool; 8 ints ((signed,unsigned)x(8,16,32,64)bits); float,double,long double; // The order here is: bool; 8 ints ((signed,unsigned)x(8,16,32,64)bits); float,double,long double;

View File

@ -0,0 +1,9 @@
// Copyright (c) 2023 The pybind Community.
#pragma once
// Common message for `static_assert()`s, which are useful to easily
// preempt much less obvious errors.
#define PYBIND11_EIGEN_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED \
"Pointer types (in particular `PyObject *`) are not supported as scalar types for Eigen " \
"types."

View File

@ -10,6 +10,7 @@
#pragma once #pragma once
#include "../numpy.h" #include "../numpy.h"
#include "common.h"
/* HINT: To suppress warnings originating from the Eigen headers, use -isystem. /* HINT: To suppress warnings originating from the Eigen headers, use -isystem.
See also: See also:
@ -288,7 +289,7 @@ template <typename Type>
struct type_caster<Type, enable_if_t<is_eigen_dense_plain<Type>::value>> { struct type_caster<Type, enable_if_t<is_eigen_dense_plain<Type>::value>> {
using Scalar = typename Type::Scalar; using Scalar = typename Type::Scalar;
static_assert(!std::is_pointer<Scalar>::value, static_assert(!std::is_pointer<Scalar>::value,
PYBIND11_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED); PYBIND11_EIGEN_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED);
using props = EigenProps<Type>; using props = EigenProps<Type>;
bool load(handle src, bool convert) { bool load(handle src, bool convert) {
@ -408,7 +409,7 @@ private:
template <typename MapType> template <typename MapType>
struct eigen_map_caster { struct eigen_map_caster {
static_assert(!std::is_pointer<typename MapType::Scalar>::value, static_assert(!std::is_pointer<typename MapType::Scalar>::value,
PYBIND11_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED); PYBIND11_EIGEN_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED);
private: private:
using props = EigenProps<MapType>; using props = EigenProps<MapType>;
@ -463,7 +464,7 @@ private:
using props = EigenProps<Type>; using props = EigenProps<Type>;
using Scalar = typename props::Scalar; using Scalar = typename props::Scalar;
static_assert(!std::is_pointer<Scalar>::value, static_assert(!std::is_pointer<Scalar>::value,
PYBIND11_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED); PYBIND11_EIGEN_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED);
using MapType = Eigen::Map<PlainObjectType, 0, StrideType>; using MapType = Eigen::Map<PlainObjectType, 0, StrideType>;
using Array using Array
= array_t<Scalar, = array_t<Scalar,
@ -612,7 +613,7 @@ private:
template <typename Type> template <typename Type>
struct type_caster<Type, enable_if_t<is_eigen_other<Type>::value>> { struct type_caster<Type, enable_if_t<is_eigen_other<Type>::value>> {
static_assert(!std::is_pointer<typename Type::Scalar>::value, static_assert(!std::is_pointer<typename Type::Scalar>::value,
PYBIND11_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED); PYBIND11_EIGEN_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED);
protected: protected:
using Matrix using Matrix
@ -643,7 +644,7 @@ template <typename Type>
struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> { struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
using Scalar = typename Type::Scalar; using Scalar = typename Type::Scalar;
static_assert(!std::is_pointer<Scalar>::value, static_assert(!std::is_pointer<Scalar>::value,
PYBIND11_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED); PYBIND11_EIGEN_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED);
using StorageIndex = remove_reference_t<decltype(*std::declval<Type>().outerIndexPtr())>; using StorageIndex = remove_reference_t<decltype(*std::declval<Type>().outerIndexPtr())>;
using Index = typename Type::Index; using Index = typename Type::Index;
static constexpr bool rowMajor = Type::IsRowMajor; static constexpr bool rowMajor = Type::IsRowMajor;

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include "../numpy.h" #include "../numpy.h"
#include "common.h"
#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
static_assert(__GNUC__ > 5, "Eigen Tensor support in pybind11 requires GCC > 5.0"); static_assert(__GNUC__ > 5, "Eigen Tensor support in pybind11 requires GCC > 5.0");
@ -165,7 +166,7 @@ PYBIND11_WARNING_POP
template <typename Type> template <typename Type>
struct type_caster<Type, typename eigen_tensor_helper<Type>::ValidType> { struct type_caster<Type, typename eigen_tensor_helper<Type>::ValidType> {
static_assert(!std::is_pointer<typename Type::Scalar>::value, static_assert(!std::is_pointer<typename Type::Scalar>::value,
PYBIND11_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED); PYBIND11_EIGEN_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED);
using Helper = eigen_tensor_helper<Type>; using Helper = eigen_tensor_helper<Type>;
static constexpr auto temp_name = get_tensor_descriptor<Type, false>::value; static constexpr auto temp_name = get_tensor_descriptor<Type, false>::value;
PYBIND11_TYPE_CASTER(Type, temp_name); PYBIND11_TYPE_CASTER(Type, temp_name);
@ -362,7 +363,7 @@ template <typename Type, int Options>
struct type_caster<Eigen::TensorMap<Type, Options>, struct type_caster<Eigen::TensorMap<Type, Options>,
typename eigen_tensor_helper<remove_cv_t<Type>>::ValidType> { typename eigen_tensor_helper<remove_cv_t<Type>>::ValidType> {
static_assert(!std::is_pointer<typename Type::Scalar>::value, static_assert(!std::is_pointer<typename Type::Scalar>::value,
PYBIND11_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED); PYBIND11_EIGEN_MESSAGE_POINTER_TYPES_ARE_NOT_SUPPORTED);
using MapType = Eigen::TensorMap<Type, Options>; using MapType = Eigen::TensorMap<Type, Options>;
using Helper = eigen_tensor_helper<remove_cv_t<Type>>; using Helper = eigen_tensor_helper<remove_cv_t<Type>>;

View File

@ -57,6 +57,7 @@ detail_headers = {
} }
eigen_headers = { eigen_headers = {
"include/pybind11/eigen/common.h",
"include/pybind11/eigen/matrix.h", "include/pybind11/eigen/matrix.h",
"include/pybind11/eigen/tensor.h", "include/pybind11/eigen/tensor.h",
} }