Skip to content
Snippets Groups Projects
Commit 7768b4ea authored by Vincenzo Eduardo Padulano's avatar Vincenzo Eduardo Padulano
Browse files

[PyROOT] Add __reduce__ method to ROOTFacade to help serialization tools

parent a0ce07bf
No related branches found
No related tags found
No related merge requests found
import importlib
import types
import sys
import os
......@@ -75,6 +76,17 @@ def _create_rdf_experimental_distributed_module(parent):
return experimental
def _subimport(name):
# type: (str) -> types.ModuleType
"""
Import and return the Python module with the input name.
Helper function for the __reduce__ method of the ROOTFacade class.
"""
return importlib.import_module(name)
class ROOTFacade(types.ModuleType):
"""Facade class for ROOT module"""
......@@ -253,13 +265,41 @@ class ROOTFacade(types.ModuleType):
logons = [
os.path.join(str(self.TROOT.GetEtcDir()), 'system' + name),
os.path.expanduser(os.path.join('~', name))
]
]
if logons[-1] != os.path.join(os.getcwd(), name):
logons.append(name)
for rootlogon in logons:
if os.path.exists(rootlogon):
self.TApplication.ExecuteFile(rootlogon)
def __reduce__(self):
# type: () -> types.ModuleType
"""
Reduction function of the ROOT facade to customize the (pickle)
serialization step.
Defines the ingredients needed for a correct serialization of the
facade, that is a function that imports a Python module and the name of
that module, which corresponds to this facade's __name__ attribute. This
method helps serialization tools like `cloudpickle`, especially used in
distributed environments, that always need to include information about
the ROOT module in the serialization step. For example, the following
snippet would not work without this method::
import ROOT
import cloudpickle
def foo():
return ROOT.TH1F()
cloudpickle.loads(cloudpickle.dumps(foo))
In particular, it would raise::
TypeError: cannot pickle 'ROOTFacade' object
"""
return _subimport, (self.__name__,)
# Inject version as __version__ property in ROOT module
@property
def __version__(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment