@@ -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;
@@ -562,6 +560,13 @@ class dtype : public object {
562560 m_ptr = from_args (std::move (args)).release ().ptr ();
563561 }
564562
563+ explicit dtype (int typenum)
564+ : object (detail::npy_api::get ().PyArray_DescrFromType_ (typenum), stolen_t {}) {
565+ if (m_ptr == nullptr ) {
566+ throw error_already_set ();
567+ }
568+ }
569+
565570 // / This is essentially the same as calling numpy.dtype(args) in Python.
566571 static dtype from_args (object args) {
567572 PyObject *ptr = nullptr ;
@@ -596,6 +601,23 @@ class dtype : public object {
596601 return detail::array_descriptor_proxy (m_ptr)->type ;
597602 }
598603
604+ // / type number of dtype.
605+ ssize_t num () const {
606+ // Note: The signature, `dtype::num` follows the naming of NumPy's public
607+ // Python API (i.e., ``dtype.num``), rather than its internal
608+ // C API (``PyArray_Descr::type_num``).
609+ return detail::array_descriptor_proxy (m_ptr)->type_num ;
610+ }
611+
612+ // / Single character for byteorder
613+ char byteorder () const { return detail::array_descriptor_proxy (m_ptr)->byteorder ; }
614+
615+ // / Alignment of the data type
616+ int alignment () const { return detail::array_descriptor_proxy (m_ptr)->alignment ; }
617+
618+ // / Flags for the array descriptor
619+ char flags () const { return detail::array_descriptor_proxy (m_ptr)->flags ; }
620+
599621private:
600622 static object _dtype_from_pep3118 () {
601623 static PyObject *obj = module_::import (" numpy.core._internal" )
@@ -614,7 +636,7 @@ class dtype : public object {
614636 }
615637
616638 struct field_descr {
617- PYBIND11_STR_TYPE name;
639+ pybind11::str name;
618640 object format;
619641 pybind11::int_ offset;
620642 };
@@ -629,7 +651,7 @@ class dtype : public object {
629651 continue ;
630652 }
631653 field_descriptors.push_back (
632- {(PYBIND11_STR_TYPE ) name, format.strip_padding (format.itemsize ()), offset});
654+ {(pybind11::str ) name, format.strip_padding (format.itemsize ()), offset});
633655 }
634656
635657 std::sort (field_descriptors.begin (),
@@ -1335,7 +1357,7 @@ PYBIND11_NOINLINE void register_structured_dtype(any_container<field_descriptor>
13351357 pybind11_fail (std::string (" NumPy: unsupported field dtype: `" ) + field.name + " ` @ "
13361358 + tinfo.name ());
13371359 }
1338- names.append (PYBIND11_STR_TYPE (field.name ));
1360+ names.append (pybind11::str (field.name ));
13391361 formats.append (field.descr );
13401362 offsets.append (pybind11::int_ (field.offset ));
13411363 }
0 commit comments