-
Notifications
You must be signed in to change notification settings - Fork 1
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
Allow for custom unpickler in load(s)_compressed #69
Conversation
This is useful to restrict possible imports or to allow unpickling when required module or function names have been refactored.
(benchmark 5591280767 / attempt 1)
|
@@ -13,6 +13,10 @@ def __init__(self): | |||
self.open = open | |||
self.compress = lambda data: data | |||
|
|||
@staticmethod |
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.
Was this missing and untested?
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.
Yes, it seems that loads_compressed
was benchmarked (with lzma compression), but untested prior to my introduction of test_loads_compressed_custom_unpickler
.
Without compression it failed due to the missing decompress
method in _NoCompression
.
@@ -129,29 +135,39 @@ def load_compressed( | |||
set to the compression method and other key-value pairs which are forwarded | |||
to open() of the compression library. | |||
Inspired by the pandas.to_csv interface. | |||
:param unpickler_class: custom unpickler class derived from pickle.Unpickler. |
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.
Hm, I wonder if we should pass a callable load(file) -> unpickled_obj
here instead of a class, and users may pass load=lambda file: pickle.Unpickler(file).load()
.
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.
As shown in the tests, a custom unpickling scheme will be defined via a class derived from pickle.Unpickler
. Hence, I find it the most straightforward to pass this class directly, rather than taking the function detour.
But I'm open to other solutions as well. What do you think, @pavelzw?
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.
I think since we expect users to always provide some kind of Unpickler
, this approach is fine.
Otherwise, everytime you want to specify custom pickling behavior, you would need to explicitly pass a lambda function lambda file: CustomUnpickler(file).load()
.
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.
Thanks @StefanSorgQC!
@@ -129,29 +135,39 @@ def load_compressed( | |||
set to the compression method and other key-value pairs which are forwarded | |||
to open() of the compression library. | |||
Inspired by the pandas.to_csv interface. | |||
:param unpickler_class: custom unpickler class derived from pickle.Unpickler. |
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.
I think since we expect users to always provide some kind of Unpickler
, this approach is fine.
Otherwise, everytime you want to specify custom pickling behavior, you would need to explicitly pass a lambda function lambda file: CustomUnpickler(file).load()
.
It seems that the tests are failing for lightgbm 4.0 :/ #72 |
This is useful to restrict possible imports or to allow unpickling when required module or function names have been refactored.