Skip to content

Commit 1c636f4

Browse files
authored
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
1 parent fbcde3f commit 1c636f4

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

include/pybind11/numpy.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,17 +547,19 @@ class dtype : public object {
547547
.ptr();
548548
}
549549

550-
explicit dtype(const std::string &format) : dtype(from_args(pybind11::str(format))) {}
550+
explicit dtype(const pybind11::str &format) : dtype(from_args(format)) {}
551551

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

554556
dtype(list names, list formats, list offsets, ssize_t itemsize) {
555557
dict args;
556558
args["names"] = std::move(names);
557559
args["formats"] = std::move(formats);
558560
args["offsets"] = std::move(offsets);
559561
args["itemsize"] = pybind11::int_(itemsize);
560-
m_ptr = from_args(std::move(args)).release().ptr();
562+
m_ptr = from_args(args).release().ptr();
561563
}
562564

563565
explicit dtype(int typenum)
@@ -568,7 +570,7 @@ class dtype : public object {
568570
}
569571

570572
/// This is essentially the same as calling numpy.dtype(args) in Python.
571-
static dtype from_args(object args) {
573+
static dtype from_args(const object &args) {
572574
PyObject *ptr = nullptr;
573575
if ((detail::npy_api::get().PyArray_DescrConverter_(args.ptr(), &ptr) == 0) || !ptr) {
574576
throw error_already_set();

include/pybind11/pytypes.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,16 +1914,17 @@ class memoryview : public object {
19141914
return memoryview::from_buffer(reinterpret_cast<void *>(ptr),
19151915
sizeof(T),
19161916
format_descriptor<T>::value,
1917-
shape,
1918-
strides,
1917+
std::move(shape),
1918+
std::move(strides),
19191919
readonly);
19201920
}
19211921

19221922
template <typename T>
19231923
static memoryview from_buffer(const T *ptr,
19241924
detail::any_container<ssize_t> shape,
19251925
detail::any_container<ssize_t> strides) {
1926-
return memoryview::from_buffer(const_cast<T *>(ptr), shape, strides, true);
1926+
return memoryview::from_buffer(
1927+
const_cast<T *>(ptr), std::move(shape), std::move(strides), true);
19271928
}
19281929

19291930
/** \rst

tests/test_numpy_dtypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,5 +610,5 @@ TEST_SUBMODULE(numpy_dtypes, m) {
610610
[]() { PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); });
611611

612612
// test_str_leak
613-
m.def("dtype_wrapper", [](py::object d) { return py::dtype::from_args(std::move(d)); });
613+
m.def("dtype_wrapper", [](const py::object &d) { return py::dtype::from_args(d); });
614614
}

0 commit comments

Comments
 (0)