Skip to content

Commit

Permalink
Fixing the static py ptrs with the trivial destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Oct 26, 2023
1 parent 1b8d10e commit ee3e3f4
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 18 deletions.
21 changes: 21 additions & 0 deletions src/clp_ffi_py/PyObjectUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,33 @@ class PyObjectDeleter {
void operator()(PyObjectType* ptr) { Py_XDECREF(reinterpret_cast<PyObject*>(ptr)); }
};

/**
* An empty deleter that has an empty implementation to ensure object trivial
* destruction.
* @tparam PyObjectType
*/
template <typename PyObjectType>
class PyObjectTrivialDeleter {
public:
void operator()(PyObjectType* ptr) {}
};

/**
* A type of smart pointer that maintains a reference to a Python object for the
* duration of its lifetime.
* @tparam PyObjectType
*/
template <typename PyObjectType>
using PyObjectPtr = std::unique_ptr<PyObjectType, PyObjectDeleter<PyObjectType>>;

/**
* A type of smart pointer that holds a static Python object raw pointer. For
* static/global variables, the destructor will be executed after the Python
* interpreter exits. Therefore, the destructor is set to empty to avoid unsafe
* memory operations.
* @tparam PyObjectType
*/
template <typename PyObjectType>
using PyObjectStaticPtr = std::unique_ptr<PyObjectType, PyObjectTrivialDeleter<PyObjectType>>;
} // namespace clp_ffi_py
#endif // CLP_FFI_PY_PY_OBJECT_PTR_HPP
4 changes: 2 additions & 2 deletions src/clp_ffi_py/Py_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
namespace clp_ffi_py {
namespace {
constexpr char const* const cPyFuncNameGetFormattedTimestamp{"get_formatted_timestamp"};
PyObjectPtr<PyObject> Py_func_get_formatted_timestamp;
PyObjectStaticPtr<PyObject> Py_func_get_formatted_timestamp;

constexpr char const* const cPyFuncNameGetTimezoneFromTimezoneId{"get_timezone_from_timezone_id"};
PyObjectPtr<PyObject> Py_func_get_timezone_from_timezone_id;
PyObjectStaticPtr<PyObject> Py_func_get_timezone_from_timezone_id;

/**
* Wrapper of PyObject_CallObject.
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ PyType_Spec PyDecoder_type_spec{
};
} // namespace

PyObjectPtr<PyTypeObject> PyDecoder::m_py_type{nullptr};
PyObjectStaticPtr<PyTypeObject> PyDecoder::m_py_type{nullptr};

auto PyDecoder::module_level_init(PyObject* py_module) -> bool {
static_assert(std::is_trivially_destructible<PyDecoder>());
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyDecoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PyDecoder {
private:
PyObject_HEAD;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
};
} // namespace clp_ffi_py::ir::native

Expand Down
4 changes: 2 additions & 2 deletions src/clp_ffi_py/ir/native/PyDecoderBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ auto PyDecoderBuffer::test_streaming(uint32_t seed) -> PyObject* {
);
}

PyObjectPtr<PyTypeObject> PyDecoderBuffer::m_py_type{nullptr};
PyObjectPtr<PyObject> PyDecoderBuffer::m_py_incomplete_stream_error{nullptr};
PyObjectStaticPtr<PyTypeObject> PyDecoderBuffer::m_py_type{nullptr};
PyObjectStaticPtr<PyObject> PyDecoderBuffer::m_py_incomplete_stream_error{nullptr};

auto PyDecoderBuffer::get_py_type() -> PyTypeObject* {
return m_py_type.get();
Expand Down
4 changes: 2 additions & 2 deletions src/clp_ffi_py/ir/native/PyDecoderBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ class PyDecoderBuffer {
size_t m_num_decoded_message;
bool m_py_buffer_protocol_enabled;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectPtr<PyObject> m_py_incomplete_stream_error;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyObject> m_py_incomplete_stream_error;
};
} // namespace clp_ffi_py::ir::native
#endif
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyFourByteEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ PyType_Spec PyFourByteEncoder_type_spec{
};
} // namespace

PyObjectPtr<PyTypeObject> PyFourByteEncoder::m_py_type{nullptr};
PyObjectStaticPtr<PyTypeObject> PyFourByteEncoder::m_py_type{nullptr};

auto PyFourByteEncoder::module_level_init(PyObject* py_module) -> bool {
static_assert(std::is_trivially_destructible<PyFourByteEncoder>());
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyFourByteEncoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PyFourByteEncoder {
private:
PyObject_HEAD;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
};
} // namespace clp_ffi_py::ir::native
#endif
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyLogEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ auto PyLogEvent::init(
return true;
}

PyObjectPtr<PyTypeObject> PyLogEvent::m_py_type{nullptr};
PyObjectStaticPtr<PyTypeObject> PyLogEvent::m_py_type{nullptr};

auto PyLogEvent::get_py_type() -> PyTypeObject* {
return m_py_type.get();
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyLogEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class PyLogEvent {
LogEvent* m_log_event;
PyMetadata* m_py_metadata;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
};
} // namespace clp_ffi_py::ir::native
#endif // CLP_FFI_PY_PY_LOG_EVENT_HPP
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ auto PyMetadata::init_py_timezone() -> bool {
return true;
}

PyObjectPtr<PyTypeObject> PyMetadata::m_py_type{nullptr};
PyObjectStaticPtr<PyTypeObject> PyMetadata::m_py_type{nullptr};

auto PyMetadata::get_py_type() -> PyTypeObject* {
return m_py_type.get();
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyMetadata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class PyMetadata {
Metadata* m_metadata;
PyObject* m_py_timezone;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
};
} // namespace clp_ffi_py::ir::native
#endif // CLP_FFI_PY_PY_METADATA_HPP
4 changes: 2 additions & 2 deletions src/clp_ffi_py/ir/native/PyQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,8 @@ auto PyQuery::init(
return true;
}

PyObjectPtr<PyTypeObject> PyQuery::m_py_type{nullptr};
PyObjectPtr<PyObject> PyQuery::m_py_wildcard_query_type{nullptr};
PyObjectStaticPtr<PyTypeObject> PyQuery::m_py_type{nullptr};
PyObjectStaticPtr<PyObject> PyQuery::m_py_wildcard_query_type{nullptr};

auto PyQuery::get_py_type() -> PyTypeObject* {
return m_py_type.get();
Expand Down
4 changes: 2 additions & 2 deletions src/clp_ffi_py/ir/native/PyQuery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class PyQuery {
PyObject_HEAD;
Query* m_query;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectPtr<PyObject> m_py_wildcard_query_type;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyObject> m_py_wildcard_query_type;
};
} // namespace clp_ffi_py::ir::native
#endif

0 comments on commit ee3e3f4

Please sign in to comment.