Skip to content

Commit

Permalink
refactor: Fix clang-tidy warnings in Py_utils. (#95)
Browse files Browse the repository at this point in the history
Co-authored-by: kirkrodrigues <[email protected]>
  • Loading branch information
LinZhihao-723 and kirkrodrigues authored Dec 2, 2024
1 parent cec9730 commit 2ae024c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
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"
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)
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

0 comments on commit 2ae024c

Please sign in to comment.