From 3829b7624c644c2691a5675130acb12a55d30934 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Thu, 14 Apr 2022 17:12:44 -0400 Subject: [PATCH] chore: simplify numpy dtype ctor (#3869) * Simplify numpy dtype ctor * Simplify c_str ctor * Remove compat macro for str in numpy --- include/pybind11/numpy.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index d71f08907..d06ddfa11 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -540,18 +540,16 @@ public: PYBIND11_OBJECT_DEFAULT(dtype, object, detail::npy_api::get().PyArrayDescr_Check_); explicit dtype(const buffer_info &info) { - dtype descr(_dtype_from_pep3118()(PYBIND11_STR_TYPE(info.format))); + dtype descr(_dtype_from_pep3118()(pybind11::str(info.format))); // If info.itemsize == 0, use the value calculated from the format string m_ptr = descr.strip_padding(info.itemsize != 0 ? info.itemsize : descr.itemsize()) .release() .ptr(); } - explicit dtype(const std::string &format) { - m_ptr = from_args(pybind11::str(format)).release().ptr(); - } + explicit dtype(const std::string &format) : dtype(from_args(pybind11::str(format))) {} - explicit dtype(const char *format) : dtype(std::string(format)) {} + explicit dtype(const char *format) : dtype(from_args(pybind11::str(format))) {} dtype(list names, list formats, list offsets, ssize_t itemsize) { dict args; @@ -638,7 +636,7 @@ private: } struct field_descr { - PYBIND11_STR_TYPE name; + pybind11::str name; object format; pybind11::int_ offset; }; @@ -653,7 +651,7 @@ private: continue; } field_descriptors.push_back( - {(PYBIND11_STR_TYPE) name, format.strip_padding(format.itemsize()), offset}); + {(pybind11::str) name, format.strip_padding(format.itemsize()), offset}); } std::sort(field_descriptors.begin(), @@ -1359,7 +1357,7 @@ PYBIND11_NOINLINE void register_structured_dtype(any_container pybind11_fail(std::string("NumPy: unsupported field dtype: `") + field.name + "` @ " + tinfo.name()); } - names.append(PYBIND11_STR_TYPE(field.name)); + names.append(pybind11::str(field.name)); formats.append(field.descr); offsets.append(pybind11::int_(field.offset)); }