pymor.core.pickle
¶
This module contains methods for object serialization.
Instead of importing serialization functions from Python’s
pickle
module directly, you should use the dump
, dumps
,
load
, loads
functions defined here. In particular, these
methods will use dumps_function
to serialize
function objects which cannot be pickled by Python’s standard
methods. Note, however, pickling such methods should be avoided
since the implementation of dumps_function
uses non-portable
implementation details of CPython to achieve its goals.
Module Contents¶
- class pymor.core.pickle.PyMORPickler[source]¶
Bases:
pickle.Pickler
This takes a binary file for writing a pickle data stream.
The optional protocol argument tells the pickler to use the given protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default protocol is 4. It was introduced in Python 3.4, and is incompatible with previous versions.
Specifying a negative protocol version selects the highest protocol version supported. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.
The file argument must have a write() method that accepts a single bytes argument. It can thus be a file object opened for binary writing, an io.BytesIO instance, or any other custom object that meets this interface.
If fix_imports is True and protocol is less than 3, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2.
If buffer_callback is None (the default), buffer views are serialized into file as part of the pickle stream.
If buffer_callback is not None, then it can be called any number of times with a buffer view. If the callback returns a false value (such as None), the given buffer is out-of-band; otherwise the buffer is serialized in-band, i.e. inside the pickle stream.
It is an error if buffer_callback is not None and protocol is None or smaller than 5.
Methods
- class pymor.core.pickle.PyMORUnpickler[source]¶
Bases:
pickle.Unpickler
This takes a binary file for reading a pickle data stream.
The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object’s representation are ignored.
The argument file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return bytes. Thus file can be a binary file object opened for reading, an io.BytesIO object, or any other custom object that meets this interface.
Optional keyword arguments are fix_imports, encoding and errors, which are used to control compatibility support for pickle stream generated by Python 2. If fix_imports is True, pickle will try to map the old Python 2 names to the new names used in Python 3. The encoding and errors tell pickle how to decode 8-bit string instances pickled by Python 2; these default to ‘ASCII’ and ‘strict’, respectively. The encoding can be ‘bytes’ to read these 8-bit string instances as bytes objects.
Methods
- pymor.core.pickle.dumps_function(function)[source]¶
Tries hard to pickle a function object:
The function’s code object is serialized using the
marshal
module.For all global names used in the function’s code object the corresponding object in the function’s global namespace is pickled. In case this object is a module, the modules __package__ name is pickled.
All default arguments are pickled.
All objects in the function’s closure are pickled.
Note that also this is heavily implementation specific and will probably only work with CPython. If possible, avoid using this method.
- pymor.core.pickle.loads_function(s)[source]¶
Restores a function serialized with
dumps_function
.