From fbcde3f0af59c5ae26561a774046623e1cc8a7c9 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 18 Apr 2022 11:09:45 -0400 Subject: [PATCH 1/3] chore: enable clang-tidy check modernize-use-nullptr (#3881) * Enable clang-tidy check modernize-use-nullptr * Sort clang-tidy * Sorted again --- .clang-tidy | 5 +++-- include/pybind11/numpy.h | 4 ++-- include/pybind11/pybind11.h | 2 +- include/pybind11/pytypes.h | 6 +++--- tests/cross_module_gil_utils.cpp | 6 +++--- tests/test_numpy_dtypes.cpp | 4 ++-- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index d01ca352c..e82443c4c 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -25,11 +25,12 @@ modernize-replace-random-shuffle, modernize-shrink-to-fit, modernize-use-auto, modernize-use-bool-literals, +modernize-use-default-member-init, modernize-use-equals-default, modernize-use-equals-delete, -modernize-use-default-member-init, -modernize-use-noexcept, modernize-use-emplace, +modernize-use-noexcept, +modernize-use-nullptr, modernize-use-override, modernize-use-using, *performance*, diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index d06ddfa11..335b78ff1 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -263,7 +263,7 @@ private: static npy_api lookup() { module_ m = module_::import("numpy.core.multiarray"); auto c = m.attr("_ARRAY_API"); - void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), NULL); + void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), nullptr); npy_api api; #define DECL_NPY_API(Func) api.Func##_ = (decltype(api.Func##_)) api_ptr[API_##Func]; DECL_NPY_API(PyArray_GetNDArrayCFeatureVersion); @@ -1549,7 +1549,7 @@ public: void *data() const { return p_ptr; } private: - char *p_ptr{0}; + char *p_ptr{nullptr}; container_type m_strides; }; diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 97275e40a..ce05b6e4e 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -2489,7 +2489,7 @@ public: exception(handle scope, const char *name, handle base = PyExc_Exception) { std::string full_name = scope.attr("__name__").cast() + std::string(".") + name; - m_ptr = PyErr_NewException(const_cast(full_name.c_str()), base.ptr(), NULL); + m_ptr = PyErr_NewException(const_cast(full_name.c_str()), base.ptr(), nullptr); if (hasattr(scope, "__dict__") && scope.attr("__dict__").contains(name)) { pybind11_fail("Error during initialization: multiple incompatible " "definitions with name \"" diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 7d52774c8..26d9025c5 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -600,13 +600,13 @@ inline handle get_function(handle value) { inline PyObject *dict_getitemstring(PyObject *v, const char *key) { PyObject *kv = nullptr, *rv = nullptr; kv = PyUnicode_FromString(key); - if (kv == NULL) { + if (kv == nullptr) { throw error_already_set(); } rv = PyDict_GetItemWithError(v, kv); Py_DECREF(kv); - if (rv == NULL && PyErr_Occurred()) { + if (rv == nullptr && PyErr_Occurred()) { throw error_already_set(); } return rv; @@ -614,7 +614,7 @@ inline PyObject *dict_getitemstring(PyObject *v, const char *key) { inline PyObject *dict_getitem(PyObject *v, PyObject *key) { PyObject *rv = PyDict_GetItemWithError(v, key); - if (rv == NULL && PyErr_Occurred()) { + if (rv == nullptr && PyErr_Occurred()) { throw error_already_set(); } return rv; diff --git a/tests/cross_module_gil_utils.cpp b/tests/cross_module_gil_utils.cpp index 2e64da052..1436c35d6 100644 --- a/tests/cross_module_gil_utils.cpp +++ b/tests/cross_module_gil_utils.cpp @@ -25,8 +25,8 @@ void gil_acquire() { py::gil_scoped_acquire gil; } constexpr char kModuleName[] = "cross_module_gil_utils"; -struct PyModuleDef moduledef - = {PyModuleDef_HEAD_INIT, kModuleName, NULL, 0, NULL, NULL, NULL, NULL, NULL}; +struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, kModuleName, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr}; } // namespace @@ -34,7 +34,7 @@ extern "C" PYBIND11_EXPORT PyObject *PyInit_cross_module_gil_utils() { PyObject *m = PyModule_Create(&moduledef); - if (m != NULL) { + if (m != nullptr) { static_assert(sizeof(&gil_acquire) == sizeof(void *), "Function pointer must have the same size as void*"); PyModule_AddObject( diff --git a/tests/test_numpy_dtypes.cpp b/tests/test_numpy_dtypes.cpp index 7de36f2fe..38f2afa07 100644 --- a/tests/test_numpy_dtypes.cpp +++ b/tests/test_numpy_dtypes.cpp @@ -289,8 +289,8 @@ py::list test_dtype_ctors() { dict["itemsize"] = py::int_(20); list.append(py::dtype::from_args(dict)); list.append(py::dtype(names, formats, offsets, 20)); - list.append(py::dtype(py::buffer_info((void *) 0, sizeof(unsigned int), "I", 1))); - list.append(py::dtype(py::buffer_info((void *) 0, 0, "T{i:a:f:b:}", 1))); + list.append(py::dtype(py::buffer_info((void *) nullptr, sizeof(unsigned int), "I", 1))); + list.append(py::dtype(py::buffer_info((void *) nullptr, 0, "T{i:a:f:b:}", 1))); list.append(py::dtype(py::detail::npy_api::NPY_DOUBLE_)); return list; } From 1c636f4dce8414d458fee3a00df72ed769b7f79a Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 18 Apr 2022 11:11:24 -0400 Subject: [PATCH 2/3] 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 --- include/pybind11/numpy.h | 10 ++++++---- include/pybind11/pytypes.h | 7 ++++--- tests/test_numpy_dtypes.cpp | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 335b78ff1..2713e13a8 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -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(); diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 26d9025c5..324fa932f 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1914,8 +1914,8 @@ public: return memoryview::from_buffer(reinterpret_cast(ptr), sizeof(T), format_descriptor::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 shape, detail::any_container strides) { - return memoryview::from_buffer(const_cast(ptr), shape, strides, true); + return memoryview::from_buffer( + const_cast(ptr), std::move(shape), std::move(strides), true); } /** \rst diff --git a/tests/test_numpy_dtypes.cpp b/tests/test_numpy_dtypes.cpp index 38f2afa07..c25bc042b 100644 --- a/tests/test_numpy_dtypes.cpp +++ b/tests/test_numpy_dtypes.cpp @@ -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); }); } From e8e229fa0b486118bf91321b923f8158055c053c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 18 Apr 2022 17:44:01 -0400 Subject: [PATCH 3/3] [pre-commit.ci] pre-commit autoupdate (#3885) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/hadialqattan/pycln: v1.2.5 → v1.3.1](https://github.com/hadialqattan/pycln/compare/v1.2.5...v1.3.1) - [github.com/pre-commit/mirrors-clang-format: v13.0.1 → v14.0.1](https://github.com/pre-commit/mirrors-clang-format/compare/v13.0.1...v14.0.1) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9cfcef21f..099f0dc26 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -71,7 +71,7 @@ repos: # Autoremoves unused imports - repo: https://github.com/hadialqattan/pycln - rev: "v1.2.5" + rev: "v1.3.1" hooks: - id: pycln stages: [manual] @@ -164,7 +164,7 @@ repos: # Clang format the codebase automatically - repo: https://github.com/pre-commit/mirrors-clang-format - rev: "v13.0.1" + rev: "v14.0.1" hooks: - id: clang-format types_or: [c++, c, cuda]