chore: simplify numpy dtype ctor (#3869)

* Simplify numpy dtype ctor

* Simplify c_str ctor

* Remove compat macro for str in numpy
This commit is contained in:
Aaron Gokaslan 2022-04-14 17:12:44 -04:00 committed by GitHub
parent 45164c1fcb
commit 3829b7624c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 8 deletions

View File

@ -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<field_descriptor>
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));
}