Skip to content

Commit

Permalink
Add function to determine if module is dynamic
Browse files Browse the repository at this point in the history
_is_dynamic_module returns True if the specified module is in the set
of modules that should be considered dynamic for pickling purposes. If
submodules is True, then the module's parent modules will be searched
to see if they were registered as dynamic.
  • Loading branch information
kinghuang committed Jul 8, 2020
1 parent 0858ab2 commit 79b82df
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ def unregister_dynamic_module(module):
_CUSTOM_DYNAMIC_MODULES_BY_NAME.remove(module_name)


def _is_dynamic_module(module, submodules=True):
module_name = module.__name__ if inspect.ismodule(module) else module
if module_name in _CUSTOM_DYNAMIC_MODULES_BY_NAME:
return True
if submodules:
while True:
parent_name = module_name.rsplit(".", 1)[0]
if parent_name == module_name:
break
if parent_name in _CUSTOM_DYNAMIC_MODULES_BY_NAME:
return True
module_name = parent_name
return False


def _whichmodule(obj, name):
"""Find the module an object belongs to.
Expand Down

0 comments on commit 79b82df

Please sign in to comment.