diff options
| author | Nick Pope <nick@nickpope.me.uk> | 2021-10-04 06:47:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-04 07:47:49 +0200 |
| commit | a3185a670169eb67f1ce5c774fe7af555f80163b (patch) | |
| tree | db70627b1c64df0c428058d0da1e2d72cc9eb85b /django/utils/module_loading.py | |
| parent | 1953dd02b6719ee89fddbc3098d86968d79f3cd7 (diff) | |
Refs #33107 -- Optimized cached_import() helper.
Diffstat (limited to 'django/utils/module_loading.py')
| -rw-r--r-- | django/utils/module_loading.py | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py index d42b9a5f22..bf099cba96 100644 --- a/django/utils/module_loading.py +++ b/django/utils/module_loading.py @@ -6,14 +6,14 @@ from importlib.util import find_spec as importlib_find def cached_import(module_path, class_name): - modules = sys.modules - if module_path not in modules or ( - # Module is not fully initialized. - getattr(modules[module_path], '__spec__', None) is not None and - getattr(modules[module_path].__spec__, '_initializing', False) is True + # Check whether module is loaded and fully initialized. + if not ( + (module := sys.modules.get(module_path)) and + (spec := getattr(module, '__spec__', None)) and + getattr(spec, '_initializing', False) is False ): - import_module(module_path) - return getattr(modules[module_path], class_name) + module = import_module(module_path) + return getattr(module, class_name) def import_string(dotted_path): |
