Brine provides a way to wrap function objects so that they may be pickled.
To truly pickle a function we need to be able to duplicate its code and its closures. By default, pickle will simply store the function’s name, and then attempt to associate that with a function when unpickling. This of course fails when the function is a lambda or not otherwise defined at the top level.
In order to mark a function, method, or partial for storage, use the brine function to create a wrapper. Later, after pickling and unpickling the wrapper, call unbrine to get a new copy of the original function.
Loading this module has the side effect of registering a pickle handler for the CellType and CodeType types. This should be of low impact, as the only place these types are used is within function instances, and are typically unexposed.
author: | Christopher O’Brien <obriencj@gmail.com> |
---|---|
license: | LGPL v.3 |
Wrap an object so that it may be pickled. Behavior by type of value is as follows:
This function provides neither caching nor preservation of uniqueness – if the same function is in a list multiple times, each will be wrapped individually and as a result will be duplicated when unbrined.
In cases where uniqueness needs to be preserved, use a Barrel instead.
Also, if a brined function refers to other functions in its closure cells, they will not be brined. Use a Barrel for those cases as well.
Parameters: | value : object
|
---|---|
Returns: | wrapped : object
|
Unwrap a value previously wrapped with the brine function. Behavior by type of value is as follows:
Parameters: | value : object
with_globals : dict or None
|
---|---|
Returns: | unwrapped : object
|
Abstract base class for brine wrappers. Defines the interface required for brine.
Wrap value so that it may be pickled.
Parameters: | value : object
|
---|
Special method used by a pickle.Pickler to serialize an instance of this class.
Returns: | data : tuple
|
---|
Bases: brine.BrinedObject
Wraps a function so that it may be pickled. For the most part you’ll want to use the brine and unbrine functions from this module rather than instantiating or accessing this class directly
Bases: brine.BrinedObject
Wraps a bound method so that it can be pickled. By default pickle refuses to operate on bound instance method object. This wrapper will still require that the object instance supports pickling, which in turn requires that the class be defined at the top level. As with the BrineFunction class, it is better to use the brine and unbrine functions from this module rather than to create an instance of this class directly.
Bases: brine.BrinedObject
Wrap a functools.partial instance that references a function or method that is otherwise unsupported by pickle.