Safe unpickling of utf8 and latin1 encodings in Python3 #49
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 allows Python2 pickle files to have strings encoded as either utf8 or ascii (latin1).
The only disadvantage is that it uses the pure-python
pickle
implementation, rather than the faster C-based_pickle
. In practice, though, this doesn't seem much slower.I thought that I should be able to avoid doing this by setting the
errors
argument onpickle.load
. This gets passed tocodecs.decode
, and controls what happens if an error happens when trying to decode a string. However, whatever I set as the value oferrors
, I always get this exception:ValueError: Failed to encode latin1 string when unpickling a Numpy array. pickle.load(a, encoding='latin1') is assumed.
I even tried defining my own error handler with
codecs.register_error
, but I can't seem to get around that exception.Anyway, should we go ahead with this? It seems like a bit of a hack, but it does let things work nicely with pickle files that use either encoding.