From a985561b98b14432794e38bbed233153028bae8b Mon Sep 17 00:00:00 2001 From: Tamino Bauknecht Date: Thu, 28 Mar 2024 00:48:04 +0100 Subject: [PATCH] python|examples: Minor linter and formatting fixes --- .../python_plugin/python_plugin_manager.cpp | 1 + include/python/python_object.h | 1 + include/python/python_tuple.h | 4 ++ src/python_exception.cpp | 9 ++++- src/python_guard.cpp | 3 +- src/python_interpreter.cpp | 39 ++++++++++++++++--- src/python_object.cpp | 12 +++++- src/python_plugin.cpp | 10 +---- src/python_tuple.cpp | 17 +++++++- 9 files changed, 77 insertions(+), 19 deletions(-) diff --git a/examples/python_plugin/python_plugin_manager.cpp b/examples/python_plugin/python_plugin_manager.cpp index 9874601..6d79f72 100644 --- a/examples/python_plugin/python_plugin_manager.cpp +++ b/examples/python_plugin/python_plugin_manager.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/include/python/python_object.h b/include/python/python_object.h index 808770b..c8a06ed 100644 --- a/include/python/python_object.h +++ b/include/python/python_object.h @@ -38,6 +38,7 @@ class PythonObject { // TODO: use explicit template instantiations instead? std::optional asInt(); + // NOLINTNEXTLINE(google-runtime-int) std::optional asLongLong(); std::optional asDouble(); std::optional asBool(); diff --git a/include/python/python_tuple.h b/include/python/python_tuple.h index 2756ac8..2461b1c 100644 --- a/include/python/python_tuple.h +++ b/include/python/python_tuple.h @@ -25,6 +25,8 @@ class PythonTuple { template void fillTuple(int start_index, T&& arg, Args&&... args); + // NOLINTBEGIN(bugprone-easily-swappable-parameters) + // NOLINTBEGIN(google-runtime-int) void setTupleItem(int index, double value); void setTupleItem(int index, unsigned int value); void setTupleItem(int index, int value); @@ -37,6 +39,8 @@ class PythonTuple { void setTupleItem(int index, const std::string& value); void setTupleItem(int index, bool value); void setTupleItem(int index, std::nullptr_t); + // NOLINTEND(google-runtime-int) + // NOLINTEND(bugprone-easily-swappable-parameters) // TODO: also make adding function (via function pointer) possible? private: diff --git a/src/python_exception.cpp b/src/python_exception.cpp index b919a13..275ea53 100644 --- a/src/python_exception.cpp +++ b/src/python_exception.cpp @@ -2,10 +2,17 @@ #include "detail/compatibility_utils.h" #include "python/python_object.h" +#include #include +#include -#include +#define PY_SSIZE_T_CLEAN +#include // NOLINT(misc-include-cleaner) +#include #include +#include +#include +#include #include namespace ppplugin { diff --git a/src/python_guard.cpp b/src/python_guard.cpp index 06c3d25..5094d2c 100644 --- a/src/python_guard.cpp +++ b/src/python_guard.cpp @@ -1,7 +1,8 @@ #include "python/python_guard.h" +#include "python/python_forward_defs.h" #define PY_SSIZE_T_CLEAN -#include +#include // NOLINT(misc-include-cleaner) #include namespace ppplugin { diff --git a/src/python_interpreter.cpp b/src/python_interpreter.cpp index b18690d..c4f36a4 100644 --- a/src/python_interpreter.cpp +++ b/src/python_interpreter.cpp @@ -1,14 +1,35 @@ #include "python/python_interpreter.h" +#include "errors.h" #include "python/python_exception.h" +#include "python/python_forward_defs.h" #include "python/python_guard.h" +#include "python/python_object.h" +#include +#include +#include #include +#include #include +#include #include #define PY_SSIZE_T_CLEAN -#include +#include // NOLINT(misc-include-cleaner) +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include namespace { // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) @@ -46,11 +67,11 @@ PythonInterpreter::PythonInterpreter() // TODO: handle this failure return; } - PythonGuard python_guard { state() }; + PythonGuard const python_guard { state() }; Py_DECREF(main_module); } } , state_ { nullptr, [](auto* state) { - PythonGuard python_guard { state }; + PythonGuard const python_guard { state }; Py_EndInterpreter(state); } } { @@ -93,15 +114,21 @@ PythonInterpreter::PythonInterpreter() std::optional PythonInterpreter::load(const std::string& file_name) { - std::unique_ptr file { std::fopen(file_name.c_str(), "r+"), [](FILE* file) { std::fclose(file); } }; + const std::unique_ptr file { + std::fopen(file_name.c_str(), "r+"), + [](FILE* file) { + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) + std::ignore = std::fclose(file); + } + }; if (!file) { return LoadError::fileNotReadable; } - PythonGuard python_guard { state() }; + PythonGuard const python_guard { state() }; auto* globals = PyModule_GetDict(mainModule()); assert(globals); auto* locals = globals; - int start { Py_file_input }; + int const start { Py_file_input }; auto* result = PyRun_File(file.get(), file_name.c_str(), start, globals, locals); Py_DECREF(globals); if (result != nullptr) { diff --git a/src/python_object.cpp b/src/python_object.cpp index d72cdfc..9b90a9d 100644 --- a/src/python_object.cpp +++ b/src/python_object.cpp @@ -1,8 +1,17 @@ #include "python/python_object.h" +#include +#include + #define PY_SSIZE_T_CLEAN -#include +#include // NOLINT(misc-include-cleaner) +#include +#include +#include +#include +#include #include +#include namespace ppplugin { PythonObject::PythonObject() @@ -34,6 +43,7 @@ std::optional PythonObject::asInt() return std::nullopt; } +// NOLINTNEXTLINE(google-runtime-int) std::optional PythonObject::asLongLong() { int overflow {}; diff --git a/src/python_plugin.cpp b/src/python_plugin.cpp index f610c50..d64bf54 100644 --- a/src/python_plugin.cpp +++ b/src/python_plugin.cpp @@ -1,17 +1,9 @@ -#include "python/plugin.h" - -#include "detail/compatibility_utils.h" #include "errors.h" #include "expected.h" +#include "python/plugin.h" #include -#define PY_SSIZE_T_CLEAN -#include -#include -#include -#include - namespace ppplugin { Expected PythonPlugin::load(const std::filesystem::path& python_script_path) { diff --git a/src/python_tuple.cpp b/src/python_tuple.cpp index 2d960e1..af0b42c 100644 --- a/src/python_tuple.cpp +++ b/src/python_tuple.cpp @@ -1,9 +1,20 @@ #include "python/python_tuple.h" +#include "python/python_forward_defs.h" + +#include +#include +#include +#include #define PY_SSIZE_T_CLEAN -#include +#include // NOLINT(misc-include-cleaner) +#include +#include +#include #include +#include #include +#include namespace ppplugin { @@ -18,6 +29,8 @@ PyObject* PythonTuple::initTuple(int size) return new_tuple; } +// NOLINTBEGIN(bugprone-easily-swappable-parameters) +// NOLINTBEGIN(google-runtime-int) void PythonTuple::setTupleItem(int index, double value) { auto* py_value = PyFloat_FromDouble(value); @@ -62,6 +75,8 @@ void PythonTuple::setTupleItem(int index, long long value) assert(py_value); assert(PyTuple_SetItem(object(), index, py_value) == 0); } +// NOLINTEND(google-runtime-int) +// NOLINTEND(bugprone-easily-swappable-parameters) void PythonTuple::setTupleItem(int index, const char* value) {