m = import_submodule(mod, subname, fullname)
(mod, subname, fullname)
| 162 | found_now = {} |
| 163 | |
| 164 | def import_submodule(mod, subname, fullname): |
| 165 | """m = import_submodule(mod, subname, fullname)""" |
| 166 | # Require: |
| 167 | # if mod == None: subname == fullname |
| 168 | # else: mod.__name__ + "." + subname == fullname |
| 169 | |
| 170 | global found_now |
| 171 | if fullname in found_now and fullname in sys.modules: |
| 172 | m = sys.modules[fullname] |
| 173 | else: |
| 174 | print('Reloading', fullname) |
| 175 | found_now[fullname] = 1 |
| 176 | oldm = sys.modules.get(fullname, None) |
| 177 | |
| 178 | if mod is None: |
| 179 | path = None |
| 180 | elif hasattr(mod, '__path__'): |
| 181 | path = mod.__path__ |
| 182 | else: |
| 183 | return None |
| 184 | |
| 185 | try: |
| 186 | # This appears to be necessary on Python 3, because imp.find_module() |
| 187 | # tries to import standard libraries (like io) itself, and we don't |
| 188 | # want them to be processed by our deep_import_hook. |
| 189 | with replace_import_hook(original_import): |
| 190 | fp, filename, stuff = imp.find_module(subname, path) |
| 191 | except ImportError: |
| 192 | return None |
| 193 | |
| 194 | try: |
| 195 | m = imp.load_module(fullname, fp, filename, stuff) |
| 196 | except: |
| 197 | # load_module probably removed name from modules because of |
| 198 | # the error. Put back the original module object. |
| 199 | if oldm: |
| 200 | sys.modules[fullname] = oldm |
| 201 | raise |
| 202 | finally: |
| 203 | if fp: fp.close() |
| 204 | |
| 205 | add_submodule(mod, m, fullname, subname) |
| 206 | |
| 207 | return m |
| 208 | |
| 209 | def add_submodule(mod, submod, fullname, subname): |
| 210 | """mod.{subname} = submod""" |
no test coverage detected