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
    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__
  • __setstate__
  • 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