|
37 | 37 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
38 | 38 | # SOFTWARE.
|
39 | 39 |
|
| 40 | +import sys |
| 41 | + |
40 | 42 |
|
41 | 43 | @builtin
|
42 |
| -def import_current_as_named_module(module_name, globals): |
| 44 | +def import_current_as_named_module(name, owner_globals=None): |
43 | 45 | """
|
44 | 46 | load a builtin anonymous module which does not have a Truffle land builtin module counter part
|
45 | 47 |
|
46 |
| - :param module_name: the module name to load as |
47 |
| - :param globals: the module globals (to update) |
48 |
| - :return: None |
| 48 | + :param name: the module name to load as |
| 49 | + :param owner_globals: the module globals (to update) |
| 50 | + :return: the loaded module |
49 | 51 | """
|
50 |
| - import sys |
51 |
| - module = sys.modules.get(module_name, None) |
| 52 | + if owner_globals is None: |
| 53 | + owner_globals = {} |
| 54 | + |
| 55 | + module = sys.modules.get(name, None) |
52 | 56 | if not module:
|
53 |
| - module = type(sys)(module_name) |
54 |
| - sys.modules[module_name] = module |
55 |
| - new_globals = dict(**globals) |
| 57 | + module = type(sys)(name) |
| 58 | + sys.modules[name] = module |
| 59 | + new_globals = dict(**owner_globals) |
56 | 60 | new_globals.update(**module.__dict__)
|
57 | 61 | module.__dict__.update(**new_globals)
|
| 62 | + return module |
58 | 63 |
|
59 | 64 |
|
60 | 65 | @builtin
|
61 |
| -def lazy_attribute_loading_from_module(attributes, module_name, globals): |
| 66 | +def lazy_attributes_from_delegate(delegate_name, attributes, owner_module): |
62 | 67 | """
|
63 | 68 | used to lazily load attributes defined in another module via the __getattr__ mechanism.
|
64 | 69 | This will only cache the attributes in the caller module.
|
65 | 70 |
|
| 71 | + :param delegate_name: the delegate module |
66 | 72 | :param attributes: a list of attributes names to be loaded lazily from the delagate module
|
67 |
| - :param module_name: the delegate module |
68 |
| - :param globals: the module globals (to update) |
| 73 | + :param owner_module: the owner module (where this is called from) |
69 | 74 | :return:
|
70 | 75 | """
|
71 |
| - def __getattr__(name): |
72 |
| - if name in attributes: |
73 |
| - delegate_module = __import__(module_name) |
74 |
| - globals.update(delegate_module.__dict__) |
75 |
| - globals['__all__'] = attributes |
76 |
| - if '__getattr__' in globals: |
77 |
| - del globals['__getattr__'] |
78 |
| - return getattr(delegate_module, name) |
79 |
| - raise AttributeError("module '{}' does not have '{}' attribute".format(module_name, name)) |
| 76 | + attributes.append('__all__') |
| 77 | + |
| 78 | + def __getattr__(attr): |
| 79 | + if attr in attributes: |
| 80 | + delegate_module = __import__(delegate_name) |
| 81 | + |
| 82 | + new_globals = dict(**delegate_module.__dict__) |
| 83 | + new_globals.update(**owner_module.__dict__) |
| 84 | + owner_module.__dict__.update(**new_globals) |
| 85 | + |
| 86 | + if '__getattr__' in owner_module.__dict__: |
| 87 | + del owner_module.__dict__['__getattr__'] |
80 | 88 |
|
81 |
| - globals['__getattr__'] = __getattr__ |
82 |
| - globals['__all__'] = ['__getattr__'] |
| 89 | + return getattr(delegate_module, attr) |
| 90 | + raise AttributeError("module '{}' does not have '{}' attribute".format(delegate_name, attr)) |
| 91 | + |
| 92 | + owner_module.__dict__['__getattr__'] = __getattr__ |
| 93 | + |
| 94 | + |
| 95 | +@builtin |
| 96 | +def import_current_as_named_module_with_delegate(module_name, delegate_name, attributes=None, owner_globals=None): |
| 97 | + owner_module = import_current_as_named_module(module_name, owner_globals=owner_globals) |
| 98 | + if attributes: |
| 99 | + lazy_attributes_from_delegate(delegate_name, attributes, owner_module) |
0 commit comments