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

后端 未结 2 1020
没有蜡笔的小新
没有蜡笔的小新 2021-02-18 18:37

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

相关标签:
2条回答
  • 2021-02-18 18:52

    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.
    0 讨论(0)
  • 2021-02-18 18:52

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

    0 讨论(0)
提交回复
热议问题