-
Notifications
You must be signed in to change notification settings - Fork 114
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
Simplify our unpickling logic and add tests for Python 2 pickles #2160
Open
poodlewars
wants to merge
1
commit into
master
Choose a base branch
from
metadata-pickling-fixes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1050,11 +1050,11 @@ def _ext_hook(self, code, data): | |
# If stored in Python2 we want to use raw while unpacking. | ||
# https://github.com/msgpack/msgpack-python/blob/master/msgpack/_unpacker.pyx#L230 | ||
data = unpackb(data, raw=True) | ||
return Pickler.read(data, pickled_in_python2=True) | ||
return Pickler.read(data) | ||
|
||
if code == MsgPackSerialization.PY_PICKLE_3: | ||
data = unpackb(data, raw=False) | ||
return Pickler.read(data, pickled_in_python2=False) | ||
return Pickler.read(data) | ||
|
||
return ExtType(code, data) | ||
|
||
|
@@ -1070,21 +1070,8 @@ def _msgpack_unpackb(self, buff, raw=False): | |
|
||
class Pickler(object): | ||
@staticmethod | ||
def read(data, pickled_in_python2=False): | ||
if isinstance(data, str): | ||
return pickle.loads(data.encode("ascii"), encoding="bytes") | ||
elif isinstance(data, str): | ||
if not pickled_in_python2: | ||
# Use the default encoding for python2 pickled objects similar to what's being done for PY2. | ||
return pickle.loads(data, encoding="bytes") | ||
|
||
try: | ||
# This tries normal pickle.loads first then falls back to special Pandas unpickling. Pandas unpickling | ||
# handles Pandas 1 vs Pandas 2 API breaks better. | ||
return pd.read_pickle(io.BytesIO(data)) | ||
except UnicodeDecodeError as exc: | ||
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.
|
||
log.debug("Failed decoding with ascii, using latin-1.") | ||
return pickle.loads(data, encoding="latin-1") | ||
def read(data): | ||
return pd.read_pickle(io.BytesIO(data)) | ||
|
||
@staticmethod | ||
def write(obj): | ||
|
This file contains 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
40 changes: 40 additions & 0 deletions
40
python/tests/unit/arcticdb/version_store/pickles_generation/python2_pickles.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""How some of the Python 2 pickles in test_normalization.py were created. | ||
|
||
Executed from a Python 2 env with msgpack 0.6.2 | ||
""" | ||
from email import errors # arbitrary module with some custom types to pickle | ||
import pickle | ||
import msgpack | ||
import sys | ||
|
||
major_version = sys.version[0] | ||
|
||
|
||
def custom_pack(obj): | ||
# 102 is our extension code for pickled in Python 2 | ||
return msgpack.ExtType(102, msgpack.packb(pickle.dumps(obj))) | ||
|
||
|
||
def msgpack_packb(obj): | ||
return msgpack.packb(obj, use_bin_type=True, strict_types=True, default=custom_pack) | ||
|
||
|
||
obj = errors.BoundaryError("bananas") | ||
title = "py" + major_version + "_obj.bin" | ||
with open(title, "wb") as f: | ||
msgpack.dump(obj, f, default=custom_pack) | ||
|
||
obj = {"dict_key": errors.BoundaryError("bananas")} | ||
title = "py" + major_version + "_dict.bin" | ||
with open(title, "wb") as f: | ||
msgpack.dump(obj, f, default=custom_pack) | ||
|
||
obj = "my_string" | ||
title = "py" + major_version + "_str.bin" | ||
with open(title, "wb") as f: | ||
msgpack.dump(obj, f, default=custom_pack) | ||
|
||
obj = b"my_bytes" | ||
title = "py" + major_version + "_str_bytes.bin" | ||
with open(title, "wb") as f: | ||
msgpack.dump(obj, f, default=custom_pack) |
This file contains 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
Oops, something went wrong.
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.
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.
This else case was clearly wrong #2156 (comment)