chore: Change numpy dtype from_args call sig to const ref (#3878)

* Change numpy from_args call signature to avoid copy

* Reorder ctors

* Rename arg

* Fix unnecessary move

* Fix clang-tidy and Add a few missing moves to memory_view pytype
This commit is contained in:
Aaron Gokaslan 2022-04-18 11:11:24 -04:00 committed by GitHub
parent fbcde3f0af
commit 1c636f4dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 8 deletions

View File

@ -547,9 +547,11 @@ public:
.ptr();
}
explicit dtype(const std::string &format) : dtype(from_args(pybind11::str(format))) {}
explicit dtype(const pybind11::str &format) : dtype(from_args(format)) {}
explicit dtype(const char *format) : dtype(from_args(pybind11::str(format))) {}
explicit dtype(const std::string &format) : dtype(pybind11::str(format)) {}
explicit dtype(const char *format) : dtype(pybind11::str(format)) {}
dtype(list names, list formats, list offsets, ssize_t itemsize) {
dict args;
@ -557,7 +559,7 @@ public:
args["formats"] = std::move(formats);
args["offsets"] = std::move(offsets);
args["itemsize"] = pybind11::int_(itemsize);
m_ptr = from_args(std::move(args)).release().ptr();
m_ptr = from_args(args).release().ptr();
}
explicit dtype(int typenum)
@ -568,7 +570,7 @@ public:
}
/// This is essentially the same as calling numpy.dtype(args) in Python.
static dtype from_args(object args) {
static dtype from_args(const object &args) {
PyObject *ptr = nullptr;
if ((detail::npy_api::get().PyArray_DescrConverter_(args.ptr(), &ptr) == 0) || !ptr) {
throw error_already_set();

View File

@ -1914,8 +1914,8 @@ public:
return memoryview::from_buffer(reinterpret_cast<void *>(ptr),
sizeof(T),
format_descriptor<T>::value,
shape,
strides,
std::move(shape),
std::move(strides),
readonly);
}
@ -1923,7 +1923,8 @@ public:
static memoryview from_buffer(const T *ptr,
detail::any_container<ssize_t> shape,
detail::any_container<ssize_t> strides) {
return memoryview::from_buffer(const_cast<T *>(ptr), shape, strides, true);
return memoryview::from_buffer(
const_cast<T *>(ptr), std::move(shape), std::move(strides), true);
}
/** \rst

View File

@ -610,5 +610,5 @@ TEST_SUBMODULE(numpy_dtypes, m) {
[]() { PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); });
// test_str_leak
m.def("dtype_wrapper", [](py::object d) { return py::dtype::from_args(std::move(d)); });
m.def("dtype_wrapper", [](const py::object &d) { return py::dtype::from_args(d); });
}