Replacement for reload().
(m)
| 264 | modules_reloading = {} |
| 265 | |
| 266 | def deep_reload_hook(m): |
| 267 | """Replacement for reload().""" |
| 268 | # Hardcode this one as it would raise a NotImplementedError from the |
| 269 | # bowels of Python and screw up the import machinery after. |
| 270 | # unlike other imports the `exclude` list already in place is not enough. |
| 271 | |
| 272 | if m is types: |
| 273 | return m |
| 274 | if not isinstance(m, ModuleType): |
| 275 | raise TypeError("reload() argument must be module") |
| 276 | |
| 277 | name = m.__name__ |
| 278 | |
| 279 | if name not in sys.modules: |
| 280 | raise ImportError("reload(): module %.200s not in sys.modules" % name) |
| 281 | |
| 282 | global modules_reloading |
| 283 | try: |
| 284 | return modules_reloading[name] |
| 285 | except: |
| 286 | modules_reloading[name] = m |
| 287 | |
| 288 | dot = name.rfind('.') |
| 289 | if dot < 0: |
| 290 | subname = name |
| 291 | path = None |
| 292 | else: |
| 293 | try: |
| 294 | parent = sys.modules[name[:dot]] |
| 295 | except KeyError: |
| 296 | modules_reloading.clear() |
| 297 | raise ImportError("reload(): parent %.200s not in sys.modules" % name[:dot]) |
| 298 | subname = name[dot+1:] |
| 299 | path = getattr(parent, "__path__", None) |
| 300 | |
| 301 | try: |
| 302 | # This appears to be necessary on Python 3, because imp.find_module() |
| 303 | # tries to import standard libraries (like io) itself, and we don't |
| 304 | # want them to be processed by our deep_import_hook. |
| 305 | with replace_import_hook(original_import): |
| 306 | fp, filename, stuff = imp.find_module(subname, path) |
| 307 | finally: |
| 308 | modules_reloading.clear() |
| 309 | |
| 310 | try: |
| 311 | newm = imp.load_module(name, fp, filename, stuff) |
| 312 | except: |
| 313 | # load_module probably removed name from modules because of |
| 314 | # the error. Put back the original module object. |
| 315 | sys.modules[name] = m |
| 316 | raise |
| 317 | finally: |
| 318 | if fp: fp.close() |
| 319 | |
| 320 | modules_reloading.clear() |
| 321 | return newm |
| 322 | |
| 323 | # Save the original hooks |
no test coverage detected