Portable mode: first draft (Brace yourselves)#491
Draft
leogama wants to merge 1 commit intouqfoundation:masterfrom
Draft
Portable mode: first draft (Brace yourselves)#491leogama wants to merge 1 commit intouqfoundation:masterfrom
leogama wants to merge 1 commit intouqfoundation:masterfrom
Conversation
Contributor
Author
|
Example of testing the portable mode (Python 3.8+): import dill
def square(x):
return x*x
with open('portable.pkl', 'wb') as file:
dill.dump(square, file, portable=True)In a different session: # If dill is in PYTHONPATH (just works if dill was not already imported):
import importlib, os, sys
if os.path.exists(os.path.join('dill', '__init__.py')):
sys.path.remove('')
while (spec := importlib.util.find_spec("dill")) is not None:
sys.path.remove(os.path.dirname(os.path.dirname(spec.origin)))
import pickle
with open('portable.pkl', 'rb') as file:
square = pickle.load(file)
print(square(17))
import dill
print(*vars(dill)) |
fd26449 to
f8a3459
Compare
197de98 to
74793e8
Compare
Contributor
Author
|
Travis CI failures:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This draft is a proof of concept. It is the first complete version of a "portable mode" implementation.
The pickle streams generated by it have the following design:
-> Bootstrap header that contains:
--> Op-codes to create
dillanddill._dillempty modules--> Minimal logic to check if a
dillmodule can be imported and its version--> The "payload", a compressed pickle stream that is loaded conditionally and contains:
---> A first batch of bootstrapped constructors:
_create_code,_create_function,_import_moduleand_reverse_typemap---> A second batch of constructors loaded using the constructors from the first batch, with its global vars, etc.
-> Body: the standard pickle stream generated by
Pickler.dumpDesign considerations:
Pickler.dump, directly to the file-like object, and have to leave the unpickling stack empty when loaded.cPicklein the future, the header can't use memoization as the memo mapping ofcPickleis private (it can just be copied afterwards).This is what drove me to use an internal pickle stream (the "payload") instead of putting everything directly in the header:
dill._dillcan be implemented as unpickling the payload or an empty pickle streamNote 1: The code annotation is incomplete and outdated. I may complete it in the next days.
Note 2: Don't mind the changes made for testing purposes, they were just a quick and dirty way to test the portable mode with a variety of objects.
PS: It's a monster, I know.