-
Notifications
You must be signed in to change notification settings - Fork 10
WIP: custom serialization #155
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,6 +5,9 @@ | |||||||||||||||||||
# LICENSE file in the root directory of this source tree. | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
import importlib | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
class Test: | ||||||||||||||||||||
def __init__(self, *args, **kwargs): | ||||||||||||||||||||
self._args = args | ||||||||||||||||||||
|
@@ -25,6 +28,23 @@ def __init__(self, op, correctness_tests, performance_tests): | |||||||||||||||||||
self.correctness_tests = correctness_tests | ||||||||||||||||||||
self.performance_tests = performance_tests | ||||||||||||||||||||
|
||||||||||||||||||||
def __getstate__(self): | ||||||||||||||||||||
# Custom serialization to handle callable op | ||||||||||||||||||||
state = self.__dict__.copy() | ||||||||||||||||||||
if callable(state.get("op")): | ||||||||||||||||||||
op = state.pop("op") | ||||||||||||||||||||
state["op_name"] = op.__name__ | ||||||||||||||||||||
state["op_module"] = op.__module__ | ||||||||||||||||||||
return state | ||||||||||||||||||||
|
||||||||||||||||||||
def __setstate__(self, state): | ||||||||||||||||||||
if "op_name" in state and "op_module" in state: | ||||||||||||||||||||
op_name = state.pop("op_name") | ||||||||||||||||||||
op_module = state.pop("op_module") | ||||||||||||||||||||
module = importlib.import_module(op_module) | ||||||||||||||||||||
state["op"] = getattr(module, op_name) | ||||||||||||||||||||
Comment on lines
+44
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The deserialization process doesn't handle cases where the module cannot be imported or the attribute doesn't exist in the module. This could cause ImportError or AttributeError during deserialization, making the object unusable.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
self.__dict__.update(state) | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
class TestSuite: | ||||||||||||||||||||
def __init__(self, name, optests): | ||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code assumes that callable objects always have
__name__
and__module__
attributes, but some callable objects like lambda functions, functools.partial objects, or custom callable classes may not have these attributes or may have unexpected values. This could cause AttributeError during serialization.Copilot uses AI. Check for mistakes.