Skip to content

Commit

Permalink
python|examples: Minor linter and formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
taminob committed Mar 28, 2024
1 parent 2b1d62a commit a985561
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 19 deletions.
1 change: 1 addition & 0 deletions examples/python_plugin/python_plugin_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <exception>
#include <filesystem>
#include <iostream>
#include <string>
#include <thread>
#include <tuple>
#include <utility>
Expand Down
1 change: 1 addition & 0 deletions include/python/python_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PythonObject {

// TODO: use explicit template instantiations instead?
std::optional<int> asInt();
// NOLINTNEXTLINE(google-runtime-int)
std::optional<long long> asLongLong();
std::optional<double> asDouble();
std::optional<bool> asBool();
Expand Down
4 changes: 4 additions & 0 deletions include/python/python_tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class PythonTuple {
template <typename T, typename... Args>
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);
Expand All @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion src/python_exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
#include "detail/compatibility_utils.h"
#include "python/python_object.h"

#include <cassert>
#include <optional>
#include <string>

#include <Python.h>
#define PY_SSIZE_T_CLEAN
#include <Python.h> // NOLINT(misc-include-cleaner)
#include <abstract.h>
#include <import.h>
#include <listobject.h>
#include <object.h>
#include <pyerrors.h>
#include <pytypedefs.h>

namespace ppplugin {
Expand Down
3 changes: 2 additions & 1 deletion src/python_guard.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "python/python_guard.h"
#include "python/python_forward_defs.h"

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <Python.h> // NOLINT(misc-include-cleaner)
#include <ceval.h>

namespace ppplugin {
Expand Down
39 changes: 33 additions & 6 deletions src/python_interpreter.cpp
Original file line number Diff line number Diff line change
@@ -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 <cassert>
#include <cstdio>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <tuple>
#include <vector>

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <Python.h> // NOLINT(misc-include-cleaner)
#include <abstract.h>
#include <bytesobject.h>
#include <ceval.h>
#include <compile.h>
#include <cpython/initconfig.h>
#include <dictobject.h>
#include <import.h>
#include <listobject.h>
#include <moduleobject.h>
#include <object.h>
#include <pylifecycle.h>
#include <pystate.h>
#include <pythonrun.h>
#include <unicodeobject.h>

namespace {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Expand Down Expand Up @@ -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);
} }
{
Expand Down Expand Up @@ -93,15 +114,21 @@ PythonInterpreter::PythonInterpreter()

std::optional<LoadError> PythonInterpreter::load(const std::string& file_name)
{
std::unique_ptr<FILE, void (*)(FILE*)> file { std::fopen(file_name.c_str(), "r+"), [](FILE* file) { std::fclose(file); } };
const std::unique_ptr<FILE, void (*)(FILE*)> 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) {
Expand Down
12 changes: 11 additions & 1 deletion src/python_object.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#include "python/python_object.h"

#include <optional>
#include <string>

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <Python.h> // NOLINT(misc-include-cleaner)
#include <boolobject.h>
#include <bytesobject.h>
#include <floatobject.h>
#include <longobject.h>
#include <object.h>
#include <pytypedefs.h>
#include <unicodeobject.h>

namespace ppplugin {
PythonObject::PythonObject()
Expand Down Expand Up @@ -34,6 +43,7 @@ std::optional<int> PythonObject::asInt()
return std::nullopt;
}

// NOLINTNEXTLINE(google-runtime-int)
std::optional<long long> PythonObject::asLongLong()
{
int overflow {};
Expand Down
10 changes: 1 addition & 9 deletions src/python_plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
#include "python/plugin.h"

#include "detail/compatibility_utils.h"
#include "errors.h"
#include "expected.h"
#include "python/plugin.h"

#include <filesystem>

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <pyerrors.h>
#include <pylifecycle.h>
#include <pytypedefs.h>

namespace ppplugin {
Expected<PythonPlugin, LoadError> PythonPlugin::load(const std::filesystem::path& python_script_path)
{
Expand Down
17 changes: 16 additions & 1 deletion src/python_tuple.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
#include "python/python_tuple.h"
#include "python/python_forward_defs.h"

#include <cassert>
#include <cstddef>
#include <string>
#include <string_view>

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <Python.h> // NOLINT(misc-include-cleaner)
#include <boolobject.h>
#include <floatobject.h>
#include <longobject.h>
#include <object.h>
#include <pyport.h>
#include <tupleobject.h>
#include <unicodeobject.h>

namespace ppplugin {

Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit a985561

Please sign in to comment.