diff options
| author | Claude Paroz <claude@2xlibre.net> | 2016-12-01 11:38:01 +0100 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2017-01-18 16:21:28 +0100 |
| commit | c716fe87821df00f9f03ecc761c914d1682591a2 (patch) | |
| tree | 0436706cdb190acbc76fb5fcf6d66f16e09fafa3 /django/utils/module_loading.py | |
| parent | e63d98b7beb16d1410168a2315cbe04c43c9c80d (diff) | |
Refs #23919 -- Removed six.PY2/PY3 usage
Thanks Tim Graham for the review.
Diffstat (limited to 'django/utils/module_loading.py')
| -rw-r--r-- | django/utils/module_loading.py | 92 |
1 files changed, 11 insertions, 81 deletions
diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py index 9dcbc926b9..ed2b8da635 100644 --- a/django/utils/module_loading.py +++ b/django/utils/module_loading.py @@ -2,6 +2,7 @@ import copy import os import sys from importlib import import_module +from importlib.util import find_spec as importlib_find from django.utils import six @@ -63,88 +64,17 @@ def autodiscover_modules(*args, **kwargs): raise -if six.PY3: - from importlib.util import find_spec as importlib_find - - def module_has_submodule(package, module_name): - """See if 'module' is in 'package'.""" - try: - package_name = package.__name__ - package_path = package.__path__ - except AttributeError: - # package isn't a package. - return False - - full_module_name = package_name + '.' + module_name - return importlib_find(full_module_name, package_path) is not None - -else: - import imp +def module_has_submodule(package, module_name): + """See if 'module' is in 'package'.""" + try: + package_name = package.__name__ + package_path = package.__path__ + except AttributeError: + # package isn't a package. + return False - def module_has_submodule(package, module_name): - """See if 'module' is in 'package'.""" - name = ".".join([package.__name__, module_name]) - try: - # None indicates a cached miss; see mark_miss() in Python/import.c. - return sys.modules[name] is not None - except KeyError: - pass - try: - package_path = package.__path__ # No __path__, then not a package. - except AttributeError: - # Since the remainder of this function assumes that we're dealing with - # a package (module with a __path__), so if it's not, then bail here. - return False - for finder in sys.meta_path: - if finder.find_module(name, package_path): - return True - for entry in package_path: - try: - # Try the cached finder. - finder = sys.path_importer_cache[entry] - if finder is None: - # Implicit import machinery should be used. - try: - file_, _, _ = imp.find_module(module_name, [entry]) - if file_: - file_.close() - return True - except ImportError: - continue - # Else see if the finder knows of a loader. - elif finder.find_module(name): - return True - else: - continue - except KeyError: - # No cached finder, so try and make one. - for hook in sys.path_hooks: - try: - finder = hook(entry) - # XXX Could cache in sys.path_importer_cache - if finder.find_module(name): - return True - else: - # Once a finder is found, stop the search. - break - except ImportError: - # Continue the search for a finder. - continue - else: - # No finder found. - # Try the implicit import machinery if searching a directory. - if os.path.isdir(entry): - try: - file_, _, _ = imp.find_module(module_name, [entry]) - if file_: - file_.close() - return True - except ImportError: - pass - # XXX Could insert None or NullImporter - else: - # Exhausted the search, so the module cannot be found. - return False + full_module_name = package_name + '.' + module_name + return importlib_find(full_module_name, package_path) is not None def module_dir(module): |
