From f6bd00131e687aedf2719ad31e84b097562ca5f2 Mon Sep 17 00:00:00 2001 From: Thomas Khyn Date: Fri, 9 Jun 2017 06:34:20 +1200 Subject: [PATCH] Fixed #28241 -- Allowed module_has_submodule()'s module_name arg to be a dotted path. --- django/utils/module_loading.py | 9 ++++++++- .../utils_tests/test_module/child_module/__init__.py | 0 .../test_module/child_module/grandchild_module.py | 1 + tests/utils_tests/test_module_loading.py | 12 ++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/utils_tests/test_module/child_module/__init__.py create mode 100644 tests/utils_tests/test_module/child_module/grandchild_module.py diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py index 5e5fa0e69e..413c6c2540 100644 --- a/django/utils/module_loading.py +++ b/django/utils/module_loading.py @@ -70,7 +70,14 @@ def module_has_submodule(package, module_name): return False full_module_name = package_name + '.' + module_name - return importlib_find(full_module_name, package_path) is not None + try: + return importlib_find(full_module_name, package_path) is not None + except (ImportError, AttributeError): + # When module_name is an invalid dotted path, Python raises ImportError + # (or ModuleNotFoundError in Python 3.6+). AttributeError may be raised + # if the penultimate part of the path is not a package. + # (http://bugs.python.org/issue30436) + return False def module_dir(module): diff --git a/tests/utils_tests/test_module/child_module/__init__.py b/tests/utils_tests/test_module/child_module/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/utils_tests/test_module/child_module/grandchild_module.py b/tests/utils_tests/test_module/child_module/grandchild_module.py new file mode 100644 index 0000000000..3375c07f56 --- /dev/null +++ b/tests/utils_tests/test_module/child_module/grandchild_module.py @@ -0,0 +1 @@ +content = 'Grandchild Module' diff --git a/tests/utils_tests/test_module_loading.py b/tests/utils_tests/test_module_loading.py index 5362c2b06b..c114d84d88 100644 --- a/tests/utils_tests/test_module_loading.py +++ b/tests/utils_tests/test_module_loading.py @@ -48,6 +48,18 @@ class DefaultLoader(unittest.TestCase): with self.assertRaises(ImportError): import_module('utils_tests.test_no_submodule.anything') + def test_has_sumbodule_with_dotted_path(self): + """Nested module existence can be tested.""" + test_module = import_module('utils_tests.test_module') + # A grandchild that exists. + self.assertIs(module_has_submodule(test_module, 'child_module.grandchild_module'), True) + # A grandchild that doesn't exist. + self.assertIs(module_has_submodule(test_module, 'child_module.no_such_module'), False) + # A grandchild whose parent doesn't exist. + self.assertIs(module_has_submodule(test_module, 'no_such_module.grandchild_module'), False) + # A grandchild whose parent is not a package. + self.assertIs(module_has_submodule(test_module, 'good_module.no_such_module'), False) + class EggLoader(unittest.TestCase): def setUp(self):