Refs #33107 -- Optimized cached_import() helper.
This commit is contained in:
parent
1953dd02b6
commit
a3185a6701
|
@ -6,14 +6,14 @@ from importlib.util import find_spec as importlib_find
|
||||||
|
|
||||||
|
|
||||||
def cached_import(module_path, class_name):
|
def cached_import(module_path, class_name):
|
||||||
modules = sys.modules
|
# Check whether module is loaded and fully initialized.
|
||||||
if module_path not in modules or (
|
if not (
|
||||||
# Module is not fully initialized.
|
(module := sys.modules.get(module_path)) and
|
||||||
getattr(modules[module_path], '__spec__', None) is not None and
|
(spec := getattr(module, '__spec__', None)) and
|
||||||
getattr(modules[module_path].__spec__, '_initializing', False) is True
|
getattr(spec, '_initializing', False) is False
|
||||||
):
|
):
|
||||||
import_module(module_path)
|
module = import_module(module_path)
|
||||||
return getattr(modules[module_path], class_name)
|
return getattr(module, class_name)
|
||||||
|
|
||||||
|
|
||||||
def import_string(dotted_path):
|
def import_string(dotted_path):
|
||||||
|
|
Loading…
Reference in New Issue