utils.serialization

Utilities for safely pickling exceptions.

Module Contents

Classes

UnpickleableExceptionWrapper(self,exc_module,exc_cls_name,exc_args,text=None) Wraps unpickleable exceptions.

Functions

subclass_exception(name,parent,module) Create new exception class.
find_pickleable_exception(exc,loads=None,dumps=None) Find first pickleable exception base class.
itermro(cls,stop)
create_exception_cls(name,module,parent=None) Dynamically create an exception class.
ensure_serializable(items,encoder) Ensure items will serialize.
get_pickleable_exception(exc) Make sure exception is pickleable.
get_pickleable_etype(cls,loads=None,dumps=None) Get pickleable exception type.
get_pickled_exception(exc) Reverse of get_pickleable_exception().
b64encode(s)
b64decode(s)
strtobool(term,table=dict) Convert common terms for true/false to bool.
_datetime_to_json(dt)
jsonify(obj,builtin_types=tuple,key=None,keyfilter=None,unknown_type_filter=None) Transform object making it suitable for json serialization.
raise_with_context(exc)
raise_with_context(exc)
subclass_exception(name, parent, module)

Create new exception class.

find_pickleable_exception(exc, loads=None, dumps=None)

Find first pickleable exception base class.

With an exception instance, iterate over its super classes (by MRO) and find the first super exception that’s pickleable. It does not go below Exception (i.e., it skips Exception, BaseException and object). If that happens you should use UnpickleableException instead.

Arguments:
exc (BaseException): An exception instance. loads: decoder to use. dumps: encoder to use
Returns:
Exception: Nearest pickleable parent exception class
(except Exception and parents), or if the exception is pickleable it will return None.
itermro(cls, stop)
create_exception_cls(name, module, parent=None)

Dynamically create an exception class.

ensure_serializable(items, encoder)

Ensure items will serialize.

For a given list of arbitrary objects, return the object or a string representation, safe for serialization.

Arguments:
items (Iterable[Any]): Objects to serialize. encoder (Callable): Callable function to serialize with.
class UnpickleableExceptionWrapper(exc_module, exc_cls_name, exc_args, text=None)

Wraps unpickleable exceptions.

Arguments:
exc_module (str): See exc_module. exc_cls_name (str): See exc_cls_name. exc_args (Tuple[Any, …]): See exc_args.
Example:
>>> def pickle_it(raising_function):
...     try:
...         raising_function()
...     except Exception as e:
...         exc = UnpickleableExceptionWrapper(
...             e.__class__.__module__,
...             e.__class__.__name__,
...             e.args,
...         )
...         pickle.dumps(exc)  # Works fine.
__init__(exc_module, exc_cls_name, exc_args, text=None)
restore()
__str__()
from_exception(exc)
get_pickleable_exception(exc)

Make sure exception is pickleable.

get_pickleable_etype(cls, loads=None, dumps=None)

Get pickleable exception type.

get_pickled_exception(exc)

Reverse of get_pickleable_exception().

b64encode(s)
b64decode(s)
strtobool(term, table=dict)

Convert common terms for true/false to bool.

Examples (true/false/yes/no/on/off/1/0).

_datetime_to_json(dt)
jsonify(obj, builtin_types=tuple, key=None, keyfilter=None, unknown_type_filter=None)

Transform object making it suitable for json serialization.

raise_with_context(exc)
raise_with_context(exc)