Load the given module, and insert it into the parent scope, and also the original importing scope.
()
| 19 | """ |
| 20 | |
| 21 | def _loadModule(): |
| 22 | """ Load the given module, and insert it into the parent |
| 23 | scope, and also the original importing scope. |
| 24 | """ |
| 25 | |
| 26 | mod = sys.modules.get(name, None) |
| 27 | if mod is None or not isinstance(mod, types.ModuleType): |
| 28 | try: |
| 29 | file = open(pathname, 'U') |
| 30 | except: |
| 31 | file = None |
| 32 | |
| 33 | try: |
| 34 | mod = imp.load_module(name, file, pathname, desc) |
| 35 | finally: |
| 36 | if file is not None: |
| 37 | file.close() |
| 38 | |
| 39 | sys.modules[name] = mod |
| 40 | |
| 41 | scope[name] = mod |
| 42 | |
| 43 | frame = sys._getframe(2) |
| 44 | global_scope = frame.f_globals |
| 45 | local_scope = frame.f_locals |
| 46 | |
| 47 | # check to see if this module exists for any part of the name |
| 48 | # we are importing, e.g. if you are importing foo.bar.baz, |
| 49 | # look for foo.bar.baz, bar.baz, and baz. |
| 50 | moduleParts = name.split('.') |
| 51 | names = [ '.'.join(moduleParts[-x:]) for x in range(len(moduleParts)) ] |
| 52 | for modulePart in names: |
| 53 | if modulePart in local_scope: |
| 54 | if local_scope[modulePart].__class__.__name__ == 'ModuleProxy': |
| 55 | # FIXME: this makes me cringe, but I haven't figured out a |
| 56 | # better way to ensure that the module proxy we're |
| 57 | # looking at is actually a proxy for this module |
| 58 | if pathname in repr(local_scope[modulePart]): |
| 59 | local_scope[modulePart] = mod |
| 60 | if modulePart in global_scope: |
| 61 | if global_scope[modulePart].__class__.__name__ == 'ModuleProxy': |
| 62 | if pathname in repr(global_scope[modulePart]): |
| 63 | global_scope[modulePart] = mod |
| 64 | |
| 65 | return mod |
| 66 | |
| 67 | class ModuleProxy(object): |
| 68 | __slots__ = [] |