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

feat: Add Python class Serializer to serialize log events into CLP key-value pair IR format. #83

Merged
merged 60 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
6f20e57
Add template to start
LinZhihao-723 Oct 31, 2024
b8cc1ed
Merge branch 'oss_main' into kv_log_event
LinZhihao-723 Nov 2, 2024
28eed53
Add msgpack
LinZhihao-723 Nov 2, 2024
45767a1
Refactoring cmake
LinZhihao-723 Nov 2, 2024
ead58b8
Fix
LinZhihao-723 Nov 2, 2024
3844595
Add gsl
LinZhihao-723 Nov 2, 2024
467c030
Implement PyKeyValuePairLogEvent
LinZhihao-723 Nov 3, 2024
273aed6
Add unit tests
LinZhihao-723 Nov 3, 2024
8d0f782
Fix msgpack configuration
LinZhihao-723 Nov 3, 2024
5662c86
Apply rabit's comments
LinZhihao-723 Nov 4, 2024
800e733
Fix comment
LinZhihao-723 Nov 4, 2024
a21bc2e
Add comment
LinZhihao-723 Nov 4, 2024
04112b5
Reordering...
LinZhihao-723 Nov 4, 2024
389b79f
Reordering...
LinZhihao-723 Nov 4, 2024
8b41d80
Fix init issue...
LinZhihao-723 Nov 4, 2024
15fcb41
Implement PySerializer
LinZhihao-723 Nov 4, 2024
941a26a
Add desctructor
LinZhihao-723 Nov 4, 2024
b562160
Update stub file
LinZhihao-723 Nov 5, 2024
b42a6a3
Add finalize for testing...
LinZhihao-723 Nov 5, 2024
32db0ff
Remove cerr
LinZhihao-723 Nov 5, 2024
ebcc461
Add support for context manager
LinZhihao-723 Nov 5, 2024
76f98e3
Add comment
LinZhihao-723 Nov 5, 2024
5507570
Add simple unit tests
LinZhihao-723 Nov 5, 2024
cc5a421
refactor
LinZhihao-723 Nov 5, 2024
2706260
Add unit tests for unclosed serializer
LinZhihao-723 Nov 5, 2024
003f19d
Fix dangling reference
LinZhihao-723 Nov 5, 2024
98c95e2
Apply rabit's comment
LinZhihao-723 Nov 5, 2024
f65ef2a
Merge branch 'kv_log_event' into py_serializer
LinZhihao-723 Nov 5, 2024
942c0ce
Forgot to free self in dealloc...
LinZhihao-723 Nov 5, 2024
9a1a26a
Bug fixing...
LinZhihao-723 Nov 6, 2024
94f76ec
Fix the reference bug
LinZhihao-723 Nov 6, 2024
b1d0b53
Update to release
LinZhihao-723 Nov 6, 2024
c72acb1
Fix ruff
LinZhihao-723 Nov 6, 2024
43180e9
Add destructor
LinZhihao-723 Nov 6, 2024
1639255
Add big five
LinZhihao-723 Nov 6, 2024
802608f
Run black
LinZhihao-723 Nov 6, 2024
1aef311
Merge branch 'kv_log_event' into py_serializer
LinZhihao-723 Nov 6, 2024
99fb924
Merge branch 'oss-main' into kv_log_event
LinZhihao-723 Nov 6, 2024
42f6695
Merge branch 'kv_log_event' into py_serializer
LinZhihao-723 Nov 6, 2024
cad3888
Apply code review comments
LinZhihao-723 Nov 6, 2024
7d83cba
Fix...
LinZhihao-723 Nov 7, 2024
118cce4
Fix issue
LinZhihao-723 Nov 7, 2024
3a0c811
Merge branch 'kv_log_event' into py_serializer
LinZhihao-723 Nov 7, 2024
369395e
Apply code review comments
LinZhihao-723 Nov 7, 2024
5fbe65b
Merge branch 'kv_log_event' into py_serializer
LinZhihao-723 Nov 7, 2024
1474c96
fix
LinZhihao-723 Nov 7, 2024
a8f1a16
Merge branch 'kv_log_event' into py_serializer
LinZhihao-723 Nov 7, 2024
770827d
Merge with kv_log_event's changes
LinZhihao-723 Nov 7, 2024
555af3f
Merge branch 'oss-main' into py_serializer
LinZhihao-723 Nov 9, 2024
a1086c4
Fix leak
LinZhihao-723 Nov 9, 2024
3168399
Merge oss-main
LinZhihao-723 Nov 19, 2024
2eb113b
Merge beta branch
LinZhihao-723 Nov 19, 2024
68e088d
Apply new review suggestions from last PR
LinZhihao-723 Nov 19, 2024
ea64e8a
Apply code review comments from last PR
LinZhihao-723 Nov 19, 2024
c4e36fb
Remove unused unit test inputs
LinZhihao-723 Nov 19, 2024
8a72410
Merge guard changes
LinZhihao-723 Nov 19, 2024
1b59e17
Apply suggestions from code review
LinZhihao-723 Nov 21, 2024
836c1dd
Apply code review comments
LinZhihao-723 Nov 22, 2024
c11c975
Apply code review comments
LinZhihao-723 Nov 22, 2024
1bb026b
Use n to parse Py_ssize_t
LinZhihao-723 Nov 22, 2024
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ set(CLP_FFI_PY_LIB_IR_SOURCES
${CLP_FFI_PY_LIB_SRC_DIR}/ir/native/PyMetadata.hpp
${CLP_FFI_PY_LIB_SRC_DIR}/ir/native/PyQuery.cpp
${CLP_FFI_PY_LIB_SRC_DIR}/ir/native/PyQuery.hpp
${CLP_FFI_PY_LIB_SRC_DIR}/ir/native/PySerializer.cpp
${CLP_FFI_PY_LIB_SRC_DIR}/ir/native/PySerializer.hpp
${CLP_FFI_PY_LIB_SRC_DIR}/ir/native/Query.cpp
${CLP_FFI_PY_LIB_SRC_DIR}/ir/native/Query.hpp
${CLP_FFI_PY_LIB_SRC_DIR}/ir/native/serialization_methods.cpp
Expand Down
1 change: 1 addition & 0 deletions clp_ffi_py/ir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"Metadata", # native
"Query", # native
"QueryBuilder", # query_builder
"Serializer", # native
"ClpIrFileReader", # readers
"ClpIrStreamReader", # readers
]
19 changes: 18 additions & 1 deletion clp_ffi_py/ir/native.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

from datetime import tzinfo
from typing import Any, Dict, IO, List, Optional
from types import TracebackType
from typing import Any, Dict, IO, List, Optional, Type

from clp_ffi_py.wildcard_query import WildcardQuery

Expand Down Expand Up @@ -86,4 +89,18 @@ class KeyValuePairLogEvent:
def __init__(self, dictionary: Dict[Any, Any]): ...
def to_dict(self) -> Dict[Any, Any]: ...

class Serializer:
def __init__(self, output_stream: IO[bytes], buffer_size_limit: int = 65536): ...
def __enter__(self) -> Serializer: ...
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None: ...
def serialize_log_event_from_msgpack_map(self, msgpack_map: bytes) -> int: ...
def get_num_bytes_serialized(self) -> int: ...
def flush(self) -> None: ...
def close(self) -> None: ...

class IncompleteStreamError(Exception): ...
2 changes: 2 additions & 0 deletions src/clp_ffi_py/PyObjectCast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class PyKeyValuePairLogEvent;
class PyLogEvent;
class PyMetadata;
class PyQuery;
class PySerializer;
} // namespace ir::native

CLP_FFI_PY_MARK_AS_PYOBJECT(ir::native::PyDeserializerBuffer);
Expand All @@ -126,6 +127,7 @@ CLP_FFI_PY_MARK_AS_PYOBJECT(ir::native::PyKeyValuePairLogEvent);
CLP_FFI_PY_MARK_AS_PYOBJECT(ir::native::PyLogEvent);
CLP_FFI_PY_MARK_AS_PYOBJECT(ir::native::PyMetadata);
CLP_FFI_PY_MARK_AS_PYOBJECT(ir::native::PyQuery);
CLP_FFI_PY_MARK_AS_PYOBJECT(ir::native::PySerializer);
CLP_FFI_PY_MARK_AS_PYOBJECT(PyBytesObject);
CLP_FFI_PY_MARK_AS_PYOBJECT(PyDictObject);
CLP_FFI_PY_MARK_AS_PYOBJECT(PyTypeObject);
Expand Down
28 changes: 28 additions & 0 deletions src/clp_ffi_py/PyObjectUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <memory>

#include <clp_ffi_py/PyExceptionContext.hpp>

namespace clp_ffi_py {
/**
* A specialized deleter for PyObjectPtr which decrements the pointed PyObject reference count when
Expand Down Expand Up @@ -47,6 +49,32 @@ using PyObjectPtr = std::unique_ptr<PyObjectType, PyObjectDeleter<PyObjectType>>
*/
template <typename PyObjectType>
using PyObjectStaticPtr = std::unique_ptr<PyObjectType, PyObjectTrivialDeleter<PyObjectType>>;

/**
* A guard class for Python exceptions. In certain CPython methods, such as `tp_finalize`,
* the exception state must remain unchanged throughout execution. This class saves the current
* exception state upon initialization and restores it upon destruction, ensuring the exception
* status is preserved.
* Docs: https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_finalize
*/
class PyErrGuard {
public:
// Constructor
PyErrGuard() = default;
haiqi96 marked this conversation as resolved.
Show resolved Hide resolved

// Destructor
~PyErrGuard() { m_exception_context.restore(); }

// Delete copy/move constructor and assignment
PyErrGuard(PyErrGuard const&) = delete;
PyErrGuard(PyErrGuard&&) = delete;
auto operator=(PyErrGuard const&) -> PyErrGuard& = delete;
auto operator=(PyErrGuard&&) -> PyErrGuard& = delete;

private:
// Variables
PyExceptionContext m_exception_context;
};
} // namespace clp_ffi_py

#endif // CLP_FFI_PY_PY_OBJECT_UTILS_HPP
4 changes: 4 additions & 0 deletions src/clp_ffi_py/Python.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
#ifdef CLP_FFI_PY_ENABLE_LINTING
// Inform IWYU of the headers that we use that are exported by Python.h
// IWYU pragma: begin_exports
#include <abstract.h>
#include <bytesobject.h>
#include <dictobject.h>
#include <longobject.h>
#include <memoryobject.h>
#include <methodobject.h>
#include <modsupport.h>
#include <object.h>
#include <objimpl.h>
#include <pyerrors.h>
#include <pymacro.h>
#include <pyport.h>
#include <typeslots.h>
// IWYU pragma: end_exports
#endif
Expand Down
Loading
Loading