Reload the module and return it. The module must have been successfully imported before.
(module)
| 92 | |
| 93 | |
| 94 | def reload(module): |
| 95 | """Reload the module and return it. |
| 96 | |
| 97 | The module must have been successfully imported before. |
| 98 | |
| 99 | """ |
| 100 | try: |
| 101 | name = module.__spec__.name |
| 102 | except AttributeError: |
| 103 | try: |
| 104 | name = module.__name__ |
| 105 | except AttributeError: |
| 106 | raise TypeError("reload() argument must be a module") from None |
| 107 | |
| 108 | if sys.modules.get(name) is not module: |
| 109 | raise ImportError(f"module {name} not in sys.modules", name=name) |
| 110 | if name in _RELOADING: |
| 111 | return _RELOADING[name] |
| 112 | _RELOADING[name] = module |
| 113 | try: |
| 114 | parent_name = name.rpartition('.')[0] |
| 115 | if parent_name: |
| 116 | try: |
| 117 | parent = sys.modules[parent_name] |
| 118 | except KeyError: |
| 119 | raise ImportError(f"parent {parent_name!r} not in sys.modules", |
| 120 | name=parent_name) from None |
| 121 | else: |
| 122 | pkgpath = parent.__path__ |
| 123 | else: |
| 124 | pkgpath = None |
| 125 | target = module |
| 126 | spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target) |
| 127 | if spec is None: |
| 128 | raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name) |
| 129 | _bootstrap._exec(spec, module) |
| 130 | # The module may have replaced itself in sys.modules! |
| 131 | return sys.modules[name] |
| 132 | finally: |
| 133 | try: |
| 134 | del _RELOADING[name] |
| 135 | except KeyError: |
| 136 | pass |
nothing calls this directly
no test coverage detected
searching dependent graphs…