From b9b901279a6c508924188fede121e18bcd3d587a Mon Sep 17 00:00:00 2001 From: Felix Thaler Date: Fri, 9 Apr 2021 13:08:36 +0200 Subject: [PATCH] Fix integer format comparison --- .../storage/adapter/python_sid_adapter.hpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/include/gridtools/storage/adapter/python_sid_adapter.hpp b/include/gridtools/storage/adapter/python_sid_adapter.hpp index 47c70e814..1c3b996be 100644 --- a/include/gridtools/storage/adapter/python_sid_adapter.hpp +++ b/include/gridtools/storage/adapter/python_sid_adapter.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -85,10 +86,23 @@ namespace gridtools { if (info.itemsize != sizeof(T)) throw std::domain_error("buffer has incorrect itemsize: " + std::to_string(info.itemsize) + "; expected " + std::to_string(sizeof(T))); + using format_desc_t = pybind11::format_descriptor>; - if (info.format != format_desc_t::format()) + auto expected_format = format_desc_t::format(); + assert(info.format.size() == 1 && expected_format.size() == 1); + const char *int_formats = "bBhHiIlLqQnN"; + const char *int_char = std::strchr(int_formats, info.format[0]); + const char *expected_int_char = std::strchr(int_formats, expected_format[0]); + if (int_char && expected_int_char) { + // just check upper/lower case for integer formats which indicates signedness (itemsize already checked) + // direct format comparison in not enough, for details see + // https://github.com/pybind/pybind11/issues/1806 and https://github.com/pybind/pybind11/issues/1908 + if (std::islower(*int_char) != std::islower(*expected_int_char)) + throw std::domain_error("incompatible integer formats: " + info.format + " and " + expected_format); + } else if (info.format != expected_format) { throw std::domain_error( - "buffer has incorrect format: " + info.format + "; expected " + format_desc_t::format()); + "buffer has incorrect format: " + info.format + "; expected " + expected_format); + } return {std::move(info)}; }