Debian Codegraph @

tests/cloudpickle_testpkg/_cloudpickle_testpkg/mod.py

raw
1 import sys
2 import types
3
4
5 # #354: To emulate package capabilities while being a single file, an extension
6 # module (for instance a mod.so file) can dynamically create a module object
7 # (most likely using the *package_name*.*parent_module_name*.*submodule_name*
8 # naming convention to be compatible with ``import`` semantics). Internally,
9 # it will use the Python/C API ``PyImport_AddModule(submodule_qualified_name)``
10 # utility, which creates a module object and adds it inside ``sys.modules``. A
11 # module created this way IS a dynamic module. However, because the ``import``
12 # machinery automatically imports the parent package/module of a submodule
13 # before importing the submodule itself, and importing the parent
14 # package/module creates and append the submodule to sys.modules (sys.modules
15 # acts as a cache for the import machinery), this submodule, albeit dynamic, is
16 # importable. To detect this, we need to recursively check the parent modules
17 # of this submodule to see if the parent module is importable. If yes, we
18 # reasonably assume that the submodule named using the aforementioned
19 # hierarchised convention has been created during the import of its parent
20 # module. The following lines emulate such a behavior without being a compiled
21 # extension module.
22
23 submodule_name = "_cloudpickle_testpkg.mod.dynamic_submodule"
24 dynamic_submodule = types.ModuleType(submodule_name)
25
26 # This line allows the dynamic_module to be imported using either one of:
27 # - ``from _cloudpickle_testpkg.mod import dynamic_submodule``
28 # - ``import _cloudpickle_testpkg.mod.dynamic_submodule``
29 sys.modules[submodule_name] = dynamic_submodule
30 # Both lines will make importlib try to get the module from sys.modules after
31 # importing the parent module, before trying getattr(mod, 'dynamic_submodule'),
32 # so this dynamic module could be binded to another name. This behavior is
33 # demonstrated with `dynamic_submodule_two`
34
35 submodule_name_two = "_cloudpickle_testpkg.mod.dynamic_submodule_two"
36 # Notice the inconsistent name binding, breaking attribute lookup-based import
37 # attempts.
38 another_submodule = types.ModuleType(submodule_name_two)
39 sys.modules[submodule_name_two] = another_submodule
40
41
42 # In this third case, the module is not added to sys.modules, and can only be
43 # imported using attribute lookup-based imports.
44 submodule_three = types.ModuleType("_cloudpickle_testpkg.mod.dynamic_submodule_three")
45 code = """
46 def f(x):
47 return x
48 """
49
50 exec(code, vars(submodule_three))
51
52 # What about a dynamic submodule inside a dynamic submodule inside an
53 # importable module?
54 subsubmodule_name = "_cloudpickle_testpkg.mod.dynamic_submodule.dynamic_subsubmodule"
55 dynamic_subsubmodule = types.ModuleType(subsubmodule_name)
56 dynamic_submodule.dynamic_subsubmodule = dynamic_subsubmodule
57 sys.modules[subsubmodule_name] = dynamic_subsubmodule
58
59
60 def module_function():
61 return "hello from a module!"
62
63
64 global_variable = "some global variable"
65
66
67 def module_function_with_global():
68 global global_variable
69 return global_variable