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 2 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
69 changes: 41 additions & 28 deletions src/clp_ffi_py/Py_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@

#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 char const* const cPyFuncNameGetTimezoneFromTimezoneId{"get_timezone_from_timezone_id"};
PyObjectStaticPtr<PyObject> Py_func_get_timezone_from_timezone_id{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"};
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};

// 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};
LinZhihao-723 marked this conversation as resolved.
Show resolved Hide resolved
kirkrodrigues marked this conversation as resolved.
Show resolved Hide resolved
PyObjectStaticPtr<PyObject> py_func_get_timezone_from_timezone_id{nullptr};
PyObjectStaticPtr<PyObject> py_func_serialize_dict_to_msgpack{nullptr};
PyObjectStaticPtr<PyObject> py_func_parse_json_str{nullptr};

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

/**
* Wrapper of PyObject_CallObject.
Expand All @@ -39,29 +44,35 @@ auto py_utils_init() -> bool {
return false;
}

Py_func_get_timezone_from_timezone_id.reset(
PyObject_GetAttrString(py_utils, cPyFuncNameGetTimezoneFromTimezoneId)
);
if (nullptr == Py_func_get_timezone_from_timezone_id.get()) {
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)
);
if (nullptr == Py_func_get_formatted_timestamp.get()) {
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())
);
if (nullptr == Py_func_serialize_dict_to_msgpack.get()) {
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()));
if (nullptr == Py_func_parse_json_str.get()) {
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 All @@ -75,7 +86,7 @@ auto py_utils_get_formatted_timestamp(clp::ir::epoch_time_ms_t timestamp, PyObje
if (nullptr == func_args) {
return nullptr;
}
return py_utils_function_call_wrapper(Py_func_get_formatted_timestamp.get(), func_args);
return py_utils_function_call_wrapper(py_func_get_formatted_timestamp.get(), func_args);
}

auto py_utils_get_timezone_from_timezone_id(std::string const& timezone_id) -> PyObject* {
Expand All @@ -84,7 +95,7 @@ auto py_utils_get_timezone_from_timezone_id(std::string const& timezone_id) -> P
if (nullptr == func_args) {
return nullptr;
}
return py_utils_function_call_wrapper(Py_func_get_timezone_from_timezone_id.get(), func_args);
return py_utils_function_call_wrapper(py_func_get_timezone_from_timezone_id.get(), func_args);
}

auto py_utils_serialize_dict_to_msgpack(PyDictObject* py_dict) -> PyBytesObject* {
Expand All @@ -95,7 +106,7 @@ auto py_utils_serialize_dict_to_msgpack(PyDictObject* py_dict) -> PyBytesObject*
if (nullptr == func_args) {
return nullptr;
}
auto* result{py_utils_function_call_wrapper(Py_func_serialize_dict_to_msgpack.get(), func_args)
auto* result{py_utils_function_call_wrapper(py_func_serialize_dict_to_msgpack.get(), func_args)
};
if (nullptr == result) {
return nullptr;
Expand All @@ -112,11 +123,13 @@ 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;
}
return py_utils_function_call_wrapper(Py_func_parse_json_str.get(), func_args);
return py_utils_function_call_wrapper(py_func_parse_json_str.get(), func_args);
}
} // namespace clp_ffi_py
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
1 change: 1 addition & 0 deletions src/clp_ffi_py/Python.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <abstract.h>
#include <bytesobject.h>
#include <dictobject.h>
#include <import.h>
#include <longobject.h>
#include <memoryobject.h>
#include <methodobject.h>
Expand Down
11 changes: 11 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,16 @@ auto handle_traceable_exception(clp::TraceableException& exception) noexcept ->
template <typename T>
[[maybe_unused]] constexpr bool cAlwaysFalse{false};

/**
* A consteval to get the C-string from a constexpr string view.
LinZhihao-723 marked this conversation as resolved.
Show resolved Hide resolved
* @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