Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out / print with which version of the protocol a pickle file has been generated

Tags:

python

pickle

In some cases, when I load an existing pickle file, and after that dump it again, the size is almost halved.

I wonder why, and the first suspect is the protocol version. Can I somehow find out with which protocol version a file was pickled?

like image 364
Jacques de Hooge Avatar asked Nov 22 '18 12:11

Jacques de Hooge


People also ask

What is protocol in pickle?

Protocol version 1 is an old binary format which is also compatible with earlier versions of Python. Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of new-style classes. Refer to PEP 307 for information about improvements brought by protocol 2.

How do I view the contents of a pickle file?

We can use the pandas library to read a pickle file in Python. The pandas module has a read_pickle() method that can be used to read a pickle file.

What is .pickle file?

Pickle can be used to serialize Python object structures, which refers to the process of converting an object in the memory to a byte stream that can be stored as a binary file on disk. When we load it back to a Python program, this binary file can be de-serialized back to a Python object.

How do I extract a pickle file?

To retrieve pickled data, the steps are quite simple. You have to use pickle. load() function to do that. The primary argument of pickle load function is the file object that you get by opening the file in read-binary (rb) mode.


1 Answers

There may be a more elegant way but to get down to the metal you can use pickletools:

import pickle
import pickletools
s = pickle.dumps('Test')
proto_op = next(pickletools.genops(s))
assert proto_op[0].name == 'PROTO'
proto_ver = proto_op[1]

To figure out the version required to decode this, you'll need to maximum protocol version of each opcode:

proto_ver = max(op[0].proto for op in pickletools.genops(s))
like image 186
user2722968 Avatar answered Sep 20 '22 12:09

user2722968