Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if a file has been created by pickle?

Tags:

Is there any way of checking if a file has been created by pickle? I could just catch exceptions thrown by pickle.load but there is no specific "not a pickle file" exception.

like image 370
Erik Avatar asked Dec 18 '12 19:12

Erik


2 Answers

Pickle files don't have a header, so there's no standard way of identifying them short of trying to unpickle one and seeing if any exceptions are raised while doing so.

You could define your own enhanced protocol that included some kind of header by subclassing the Pickler() and Unpickler() classes in the pickle module. However this can't be done with the much faster cPickle module because, in it, they're factory functions, which can't be subclassed [1].

A more flexible approach would be define your own independent classes that used corresponding Pickler() and Unpickler() instances from either one of these modules in its implementation.

Update

The last byte of all pickle files should be the pickle.STOP opcode, so while there isn't a header, there is effectively a very minimal trailer which would be a relatively simple thing to check.

Depending on your exact usage, you might be able to get away with supplementing that with something more elaborate (and longer than one byte), since any data past the STOP opcode in a pickled object's representation is ignored [2].

[1]  Footnote [2] in the Python 2 documentation.
[2]  Documentation forpickle.loads(), which also applies to pickle.load()since it's currently implemented in terms of the former.
like image 114
martineau Avatar answered Nov 17 '22 04:11

martineau


There is no sure way other than to try to unpickle it, and catch exceptions.

like image 24
Ned Batchelder Avatar answered Nov 17 '22 06:11

Ned Batchelder