Add dtype(names, offsets, formats, itemsize) ctor

This commit is contained in:
Ivan Smirnov 2016-07-24 17:51:35 +01:00
parent fc5620afa6
commit d77bc8c343
1 changed files with 17 additions and 12 deletions

View File

@ -125,6 +125,15 @@ public:
m_ptr = from_args(pybind11::str(format)).release().ptr(); m_ptr = from_args(pybind11::str(format)).release().ptr();
} }
dtype(list names, list formats, list offsets, size_t itemsize) {
dict args;
args["names"] = names;
args["formats"] = formats;
args["offsets"] = offsets;
args["itemsize"] = int_(itemsize);
m_ptr = from_args(args).release().ptr();
}
static dtype from_args(object args) { static dtype from_args(object args) {
// This is essentially the same as calling np.dtype() constructor in Python // This is essentially the same as calling np.dtype() constructor in Python
PyObject *ptr = nullptr; PyObject *ptr = nullptr;
@ -184,12 +193,11 @@ private:
list names, formats, offsets; list names, formats, offsets;
for (auto& descr : field_descriptors) { for (auto& descr : field_descriptors) {
names.append(descr.name); formats.append(descr.format); offsets.append(descr.offset); names.append(descr.name);
formats.append(descr.format);
offsets.append(descr.offset);
} }
auto args = dict(); return dtype(names, formats, offsets, itemsize());
args["names"] = names; args["formats"] = formats; args["offsets"] = offsets;
args["itemsize"] = (int_) itemsize();
return dtype::from_args(args);
} }
}; };
@ -324,7 +332,7 @@ DECL_FMT(std::complex<double>, NPY_CDOUBLE_, "complex128");
static PYBIND11_DESCR name() { return _("S") + _<N>(); } \ static PYBIND11_DESCR name() { return _("S") + _<N>(); } \
static pybind11::dtype dtype() { \ static pybind11::dtype dtype() { \
PYBIND11_DESCR fmt = _("S") + _<N>(); \ PYBIND11_DESCR fmt = _("S") + _<N>(); \
return pybind11::dtype::from_args(pybind11::str(fmt.text())); \ return pybind11::dtype(fmt.text()); \
} \ } \
static const char *format() { PYBIND11_DESCR s = _<N>() + _("s"); return s.text(); } static const char *format() { PYBIND11_DESCR s = _<N>() + _("s"); return s.text(); }
template <size_t N> struct npy_format_descriptor<char[N]> { DECL_CHAR_FMT }; template <size_t N> struct npy_format_descriptor<char[N]> { DECL_CHAR_FMT };
@ -356,18 +364,15 @@ struct npy_format_descriptor<T, typename std::enable_if<is_pod_struct<T>::value>
} }
static void register_dtype(std::initializer_list<field_descriptor> fields) { static void register_dtype(std::initializer_list<field_descriptor> fields) {
auto args = dict(); list names, formats, offsets;
list names { }, offsets { }, formats { };
for (auto field : fields) { for (auto field : fields) {
if (!field.descr) if (!field.descr)
pybind11_fail("NumPy: unsupported field dtype"); pybind11_fail("NumPy: unsupported field dtype");
names.append(str(field.name)); names.append(str(field.name));
offsets.append(int_(field.offset));
formats.append(field.descr); formats.append(field.descr);
offsets.append(int_(field.offset));
} }
args["names"] = names; args["offsets"] = offsets; args["formats"] = formats; dtype_() = pybind11::dtype(names, formats, offsets, sizeof(T)).release().ptr();
args["itemsize"] = int_(sizeof(T));
dtype_() = pybind11::dtype::from_args(args).release().ptr();
// There is an existing bug in NumPy (as of v1.11): trailing bytes are // There is an existing bug in NumPy (as of v1.11): trailing bytes are
// not encoded explicitly into the format string. This will supposedly // not encoded explicitly into the format string. This will supposedly