8.5.20. pymodaq_utils.safe_pickle module

Read legacy pickled content without being able to run code

PyMoDAQ used to store some data (strings in .h5 files, dock layouts) with pickle, which can run arbitrary code when loaded. New files do not use pickle anymore, this module only exists so that legacy files can still be read: only plain data (str, bytes, numbers, tuples, lists, dicts, sets…) is rebuilt, any attempt to reference another object is refused.

exception pymodaq_utils.safe_pickle.LegacyFormatWarning[source]

Bases: UserWarning

Emitted when a file is read in a legacy format that should be migrated

exception pymodaq_utils.safe_pickle.UnsafePickleError[source]

Bases: UnpicklingError

Raised when legacy content tries to build something else than plain data

pymodaq_utils.safe_pickle.restricted_load(file, extra_allowed=())[source]

Equivalent of pickle.load restricted to plain data, raises UnsafePickleError otherwise

Return type:

Any

pymodaq_utils.safe_pickle.restricted_loads(data, extra_allowed=())[source]

Equivalent of pickle.loads restricted to plain data, raises UnsafePickleError otherwise

Parameters:
  • data (bytes)

  • extra_allowed (iterable of (module name, object name)) – Other harmless objects that the content may refer to

Return type:

Any

pymodaq_utils.safe_pickle.warn_legacy_format(what)[source]

Tell the user that what is read uses a legacy format and how to migrate it

Python shows a given warning once, so the user is not flooded when a file holds many legacy values