Skip to content

Commit 3829b76

Browse files
authored
chore: simplify numpy dtype ctor (#3869)
* Simplify numpy dtype ctor * Simplify c_str ctor * Remove compat macro for str in numpy
1 parent 45164c1 commit 3829b76

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

include/pybind11/numpy.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -540,18 +540,16 @@ class dtype : public object {
540540
PYBIND11_OBJECT_DEFAULT(dtype, object, detail::npy_api::get().PyArrayDescr_Check_);
541541

542542
explicit dtype(const buffer_info &info) {
543-
dtype descr(_dtype_from_pep3118()(PYBIND11_STR_TYPE(info.format)));
543+
dtype descr(_dtype_from_pep3118()(pybind11::str(info.format)));
544544
// If info.itemsize == 0, use the value calculated from the format string
545545
m_ptr = descr.strip_padding(info.itemsize != 0 ? info.itemsize : descr.itemsize())
546546
.release()
547547
.ptr();
548548
}
549549

550-
explicit dtype(const std::string &format) {
551-
m_ptr = from_args(pybind11::str(format)).release().ptr();
552-
}
550+
explicit dtype(const std::string &format) : dtype(from_args(pybind11::str(format))) {}
553551

554-
explicit dtype(const char *format) : dtype(std::string(format)) {}
552+
explicit dtype(const char *format) : dtype(from_args(pybind11::str(format))) {}
555553

556554
dtype(list names, list formats, list offsets, ssize_t itemsize) {
557555
dict args;
@@ -638,7 +636,7 @@ class dtype : public object {
638636
}
639637

640638
struct field_descr {
641-
PYBIND11_STR_TYPE name;
639+
pybind11::str name;
642640
object format;
643641
pybind11::int_ offset;
644642
};
@@ -653,7 +651,7 @@ class dtype : public object {
653651
continue;
654652
}
655653
field_descriptors.push_back(
656-
{(PYBIND11_STR_TYPE) name, format.strip_padding(format.itemsize()), offset});
654+
{(pybind11::str) name, format.strip_padding(format.itemsize()), offset});
657655
}
658656

659657
std::sort(field_descriptors.begin(),
@@ -1359,7 +1357,7 @@ PYBIND11_NOINLINE void register_structured_dtype(any_container<field_descriptor>
13591357
pybind11_fail(std::string("NumPy: unsupported field dtype: `") + field.name + "` @ "
13601358
+ tinfo.name());
13611359
}
1362-
names.append(PYBIND11_STR_TYPE(field.name));
1360+
names.append(pybind11::str(field.name));
13631361
formats.append(field.descr);
13641362
offsets.append(pybind11::int_(field.offset));
13651363
}

0 commit comments

Comments
 (0)