uqfoundation/dill

Q: does dill verify the object can be serialized before writing to a file?

Closed this issue · 1 comments

Q: does dill verify the object can be serialized before writing to a file? (dump operation)

Apologies if this isn't the right place, but couldn't find the answer anywhere else.

I am implementing a 'verification step' before writing to file because it happened several times that dill complains about not being able to serialize the object

TypeError: cannot pickle '_hashlib.HASH' object

and leaves a broken file that previously had valid data on it.

Error when trying to load it again:
EOFError: Ran out of input

I am also doing a 'backup file before overwriting' just in case.

Just in case, I am writing with

    with open(file_name, "wb") as output_file:
        pickle.dump(data, output_file)

automatically, no. However, there are several methods in dill that you can use to verify if something is going to pickle or not.

Python 3.9.16 (main, Dec  7 2022, 02:40:44) 
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> import hashlib
>>> h = hashlib.md5()
>>> dill.pickles(h)
False
>>> dill.detect.baditems(h)
[<md5 _hashlib.HASH object @ 0x10b860410>]

... most of dill.detect is investigating what causes serialization failure, so you could find a method that you like and run that ahead of writing to a file.

Feel free to reopen this if you feel I've not answered your question sufficiently.