Refs #32074 -- Fixed find_module()/find_loader() warnings on Python 3.10+.

This commit is contained in:
Mariusz Felisiak 2021-09-16 20:20:54 +02:00 committed by GitHub
parent 306607d5b9
commit f1bcaa9be8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 18 deletions

View File

@ -9,6 +9,7 @@ from django.test.utils import extend_sys_path
from django.utils.module_loading import ( from django.utils.module_loading import (
autodiscover_modules, import_string, module_has_submodule, autodiscover_modules, import_string, module_has_submodule,
) )
from django.utils.version import PY310
class DefaultLoader(unittest.TestCase): class DefaultLoader(unittest.TestCase):
@ -184,32 +185,38 @@ class AutodiscoverModulesTestCase(SimpleTestCase):
self.assertEqual(site._registry, {'lorem': 'ipsum'}) self.assertEqual(site._registry, {'lorem': 'ipsum'})
class TestFinder: if PY310:
def __init__(self, *args, **kwargs): class TestFinder:
self.importer = zipimporter(*args, **kwargs) def __init__(self, *args, **kwargs):
self.importer = zipimporter(*args, **kwargs)
def find_module(self, path): def find_spec(self, path, target=None):
importer = self.importer.find_module(path) return self.importer.find_spec(path, target)
if importer is None: else:
return class TestFinder:
return TestLoader(importer) 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: class TestLoader:
def __init__(self, importer): def __init__(self, importer):
self.importer = importer self.importer = importer
def load_module(self, name): def load_module(self, name):
mod = self.importer.load_module(name) mod = self.importer.load_module(name)
mod.__loader__ = self mod.__loader__ = self
return mod return mod
class CustomLoader(EggLoader): class CustomLoader(EggLoader):
"""The Custom Loader test is exactly the same as the EggLoader, but """The Custom Loader test is exactly the same as the EggLoader, but
it uses a custom defined Loader and Finder that is intentionally it uses a custom defined Loader class. Although the EggLoader combines both
split into two classes. Although the EggLoader combines both functions functions into one class, this isn't required.
into one class, this isn't required.
""" """
def setUp(self): def setUp(self):
super().setUp() super().setUp()