Pickle
Pickle Process
Pickle, convert an object to a byte stream
Unpickle, construct the object from the byte stream
Unpickle process is not safe. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling
non-Python programs may not be able to reconstruct pickled Python objects
cPickle has been removed in Python 3
Picklable Types
- None, True, and False
- integers, long integers, floating point numbers, complex numbers
- normal and Unicode strings
- tuples, lists, sets, and dictionaries containing only picklable objects
- functions defined at the top level of a module
- built-in functions defined at the top level of a module
- classes that are defined at the top level of a module
- instances of such classes whose __dict__ or the result of calling __getstate__() is picklable (see section The pickle protocol for details).
Protocal
0, ASCII protocol
1, old binary format
2, python 2.3
3, python 3.0
4, python 3.4
5, python 3.8
-1, highest available protocol
The higher the protocol used, the more recent the version of Python needed to read the pickle produced
print(pickle.HIGHEST_PROTOCOL) # highest protocol
print(pickle.DEFAULT_PROTOCOL) # default protocol
# write pickled object to file
import pickle
f = open('temp.pkl', 'wb')
l = list(range(10))
pickle.dump(l, f) # default protocol
# pickle.dump(l, f, 5)
f.close();
# read pickled object from file and unpickle
f = open('temp.pkl', 'rb')
l = pickle.load(f)
print(l)
f.close()
l = list(range(10))
# write pickled object to bytes
s = pickle.dumps(l, 2)
print(s)
# read pickled object from bytes and unpickle
t = pickle.loads(s)
print(t)
Pickle Classes
f = open('temp.pkl', 'wb')
l = list(range(10))
# pickle
p_write = pickle.Pickler(f)
p_write.dump(l)
f.close()
f = open('temp.pkl', 'rb')
p_read = pickle.Unpickler(f)
# unpickle
print(p_read.load())
f.close()
Define Picklable Class
__getstate__
- if the class defines the method __getstate__(), it is called and then return state is pickled as the contents for the instance
- if there is no __getstate__() method, the instance's __dict__ is pickled
__setstate__
- if the class also defines the method __setstate__(), it is called with the unpickled state
- if there is no __setstate__() method, the pickled state must be a dictionary and its items are assigned to the new instance's dictionary
class Vehicle(object):
def __init__(self, brand):
self.__brand = brand
def __str__(self):
return "Info: %s" % self.__dict__
def __getstate__(self):
print('Call __getstate__ for pickling ...')
state = self.__dict__.copy()
return state
def __setstate__(self, state):
print('Call __setstate__ for unpickling ...')
self.__dict__.update(state)
# pickle object
f = open('temp.pkl', 'wb')
pickle.dump(v, f) # call __getstate__()
f.close()
# unpickle object
f = open('temp.pkl', 'rb')
l = pickle.load(f) # call __setstate__()
print(l) # call __str__()
f.close()
Reference