Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Fix clang-tidy warnings in Py_utils. #95

Merged
merged 8 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lint-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ tasks:
cmds:
- task: ":utils:clang-tidy"
vars:
FLAGS: "--config-file={{.ROOT_DIR}}/.clang-tidy -p {{.CLP_FFI_PY_COMPILE_COMMANDS_DB}}"
FLAGS: >-
--config-file "{{.CLP_FFI_PY_CPP_SRC_DIR}}/.clang-tidy"
-p "{{.CLP_FFI_PY_COMPILE_COMMANDS_DB}}"
SRC_PATHS:
ref: ".G_CPP_LINT_DIRS"
kirkrodrigues marked this conversation as resolved.
Show resolved Hide resolved
VENV_DIR: "{{.G_LINT_VENV_DIR}}"
Expand Down
6 changes: 6 additions & 0 deletions src/clp_ffi_py/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
InheritParentConfig: true

CheckOptions:
# Variable naming rules
# Allow PyObject global variables to start with `Py_`
readability-identifier-naming.GlobalVariableIgnoredRegexp: "Py(_[a-z0-9]+)+"
49 changes: 31 additions & 18 deletions src/clp_ffi_py/Py_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@

#include "Py_utils.hpp"

#include <string>
#include <string_view>

#include <clp/ir/types.hpp>

#include <clp_ffi_py/PyObjectCast.hpp>
#include <clp_ffi_py/PyObjectUtils.hpp>
#include <clp_ffi_py/utils.hpp>

namespace clp_ffi_py {
namespace {
constexpr char const* const cPyFuncNameGetFormattedTimestamp{"get_formatted_timestamp"};
PyObjectStaticPtr<PyObject> Py_func_get_formatted_timestamp{nullptr};
constexpr std::string_view cPyFuncNameGetFormattedTimestamp{"get_formatted_timestamp"};
constexpr std::string_view cPyFuncNameGetTimezoneFromTimezoneId{"get_timezone_from_timezone_id"};
constexpr std::string_view cPyFuncNameSerializeDictToMsgpack{"serialize_dict_to_msgpack"};
constexpr std::string_view cPyFuncNameParseJsonStr{"parse_json_str"};

constexpr char const* const cPyFuncNameGetTimezoneFromTimezoneId{"get_timezone_from_timezone_id"};
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
LinZhihao-723 marked this conversation as resolved.
Show resolved Hide resolved
PyObjectStaticPtr<PyObject> Py_func_get_formatted_timestamp{nullptr};
PyObjectStaticPtr<PyObject> Py_func_get_timezone_from_timezone_id{nullptr};

constexpr std::string_view cPyFuncNameSerializeDictToMsgpack{"serialize_dict_to_msgpack"};
PyObjectStaticPtr<PyObject> Py_func_serialize_dict_to_msgpack{nullptr};

constexpr std::string_view cPyFuncNameParseJsonStr{"parse_json_str"};
PyObjectStaticPtr<PyObject> Py_func_parse_json_str{nullptr};

// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)

/**
* Wrapper of PyObject_CallObject.
* @param func PyObject that points to the calling function.
Expand All @@ -39,28 +44,34 @@ auto py_utils_init() -> bool {
return false;
}

Py_func_get_timezone_from_timezone_id.reset(
PyObject_GetAttrString(py_utils, cPyFuncNameGetTimezoneFromTimezoneId)
);
Py_func_get_timezone_from_timezone_id.reset(PyObject_GetAttrString(
py_utils,
get_c_str_from_constexpr_string_view(cPyFuncNameGetTimezoneFromTimezoneId)
));
if (nullptr == Py_func_get_timezone_from_timezone_id.get()) {
return false;
}

Py_func_get_formatted_timestamp.reset(
PyObject_GetAttrString(py_utils, cPyFuncNameGetFormattedTimestamp)
);
Py_func_get_formatted_timestamp.reset(PyObject_GetAttrString(
py_utils,
get_c_str_from_constexpr_string_view(cPyFuncNameGetFormattedTimestamp)
));
if (nullptr == Py_func_get_formatted_timestamp.get()) {
return false;
}

Py_func_serialize_dict_to_msgpack.reset(
PyObject_GetAttrString(py_utils, cPyFuncNameSerializeDictToMsgpack.data())
);
Py_func_serialize_dict_to_msgpack.reset(PyObject_GetAttrString(
py_utils,
get_c_str_from_constexpr_string_view(cPyFuncNameSerializeDictToMsgpack)
));
if (nullptr == Py_func_serialize_dict_to_msgpack.get()) {
return false;
}

Py_func_parse_json_str.reset(PyObject_GetAttrString(py_utils, cPyFuncNameParseJsonStr.data()));
Py_func_parse_json_str.reset(PyObject_GetAttrString(
py_utils,
get_c_str_from_constexpr_string_view(cPyFuncNameParseJsonStr)
));
if (nullptr == Py_func_parse_json_str.get()) {
return false;
}
Expand Down Expand Up @@ -112,7 +123,9 @@ auto py_utils_serialize_dict_to_msgpack(PyDictObject* py_dict) -> PyBytesObject*
}

auto py_utils_parse_json_str(std::string_view json_str) -> PyObject* {
PyObjectPtr<PyObject> const func_args_ptr{Py_BuildValue("(s)", json_str.data())};
PyObjectPtr<PyObject> const func_args_ptr{
Py_BuildValue("(s#)", json_str.data(), static_cast<Py_ssize_t>(json_str.size()))
};
auto* func_args{func_args_ptr.get()};
if (nullptr == func_args) {
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/Py_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <string>
#include <string_view>

#include <clp/ffi/encoding_methods.hpp>
#include <clp/ir/types.hpp>

namespace clp_ffi_py {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/Python.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <bytesobject.h>
#include <dictobject.h>
#include <floatobject.h>
#include <longobject.h>
#include <import.h>
#include <longobject.h>
#include <memoryobject.h>
#include <methodobject.h>
Expand Down
10 changes: 10 additions & 0 deletions src/clp_ffi_py/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <iostream>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>

Expand Down Expand Up @@ -85,6 +86,15 @@ auto handle_traceable_exception(clp::TraceableException& exception) noexcept ->
template <typename T>
[[maybe_unused]] constexpr bool cAlwaysFalse{false};

/**
* @param sv
* @return The underlying C-string of the given constexpr string view.
*/
[[nodiscard]] consteval auto get_c_str_from_constexpr_string_view(std::string_view const& sv
) -> char const* {
return sv.data();
}

template <typename int_type>
auto parse_py_int(PyObject* py_int, int_type& val) -> bool {
if (false == static_cast<bool>(PyLong_Check(py_int))) {
Expand Down
Loading