From f6726fdc3ec09d40fb53eef7ce9b6b5a05762548 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 16 Sep 2021 20:20:54 +0200 Subject: [PATCH] [3.2.x] Refs #32074 -- Fixed find_module()/find_loader() warnings on Python 3.10+. Backport of f1bcaa9be8227dce89a320ce1ca37e1df7c80d03 from main. --- django/utils/version.py | 1 + tests/utils_tests/test_module_loading.py | 43 ++++++++++++++---------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/django/utils/version.py b/django/utils/version.py index b0f395be3f..74c327525e 100644 --- a/django/utils/version.py +++ b/django/utils/version.py @@ -14,6 +14,7 @@ PY36 = sys.version_info >= (3, 6) PY37 = sys.version_info >= (3, 7) PY38 = sys.version_info >= (3, 8) PY39 = sys.version_info >= (3, 9) +PY310 = sys.version_info >= (3, 10) def get_version(version=None): diff --git a/tests/utils_tests/test_module_loading.py b/tests/utils_tests/test_module_loading.py index ac54fd6b8e..773c0a2735 100644 --- a/tests/utils_tests/test_module_loading.py +++ b/tests/utils_tests/test_module_loading.py @@ -9,6 +9,7 @@ from django.test.utils import extend_sys_path from django.utils.module_loading import ( autodiscover_modules, import_string, module_has_submodule, ) +from django.utils.version import PY310 class DefaultLoader(unittest.TestCase): @@ -184,32 +185,38 @@ class AutodiscoverModulesTestCase(SimpleTestCase): self.assertEqual(site._registry, {'lorem': 'ipsum'}) -class TestFinder: - def __init__(self, *args, **kwargs): - self.importer = zipimporter(*args, **kwargs) +if PY310: + class TestFinder: + def __init__(self, *args, **kwargs): + self.importer = zipimporter(*args, **kwargs) - def find_module(self, path): - importer = self.importer.find_module(path) - if importer is None: - return - return TestLoader(importer) + def find_spec(self, path, target=None): + return self.importer.find_spec(path, target) +else: + class TestFinder: + def __init__(self, *args, **kwargs): + self.importer = zipimporter(*args, **kwargs) + def find_module(self, path): + importer = self.importer.find_module(path) + if importer is None: + return + return TestLoader(importer) -class TestLoader: - def __init__(self, importer): - self.importer = importer + class TestLoader: + def __init__(self, importer): + self.importer = importer - def load_module(self, name): - mod = self.importer.load_module(name) - mod.__loader__ = self - return mod + def load_module(self, name): + mod = self.importer.load_module(name) + mod.__loader__ = self + return mod class CustomLoader(EggLoader): """The Custom Loader test is exactly the same as the EggLoader, but - it uses a custom defined Loader and Finder that is intentionally - split into two classes. Although the EggLoader combines both functions - into one class, this isn't required. + it uses a custom defined Loader class. Although the EggLoader combines both + functions into one class, this isn't required. """ def setUp(self): super().setUp()