Skip to content

Commit

Permalink
add tearDown and clear module cache
Browse files Browse the repository at this point in the history
  • Loading branch information
sunmy2019 committed May 6, 2023
1 parent 1630345 commit ae45649
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions Lib/test/test_importlib/extension/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ def setUp(self):
self.loader = self.machinery.ExtensionFileLoader(
self.name, self.spec.origin)

def tearDown(self):
try:
del sys.modules[self.name]
except KeyError:
pass

def load_module(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
Expand Down Expand Up @@ -194,6 +200,12 @@ def setUp(self):
self.loader = self.machinery.ExtensionFileLoader(
self.name, self.spec.origin)

def tearDown(self):
try:
del sys.modules[self.name]
except KeyError:
pass

def load_module(self):
# Load the module from the test extension.
with warnings.catch_warnings():
Expand Down Expand Up @@ -262,15 +274,16 @@ def test_reload(self):

def test_try_registration(self):
# Assert that the PyState_{Find,Add,Remove}Module C API doesn't work.
module = self.load_module()
with self.subTest('PyState_FindModule'):
self.assertEqual(module.call_state_registration_func(0), None)
with self.subTest('PyState_AddModule'):
with self.assertRaises(SystemError):
module.call_state_registration_func(1)
with self.subTest('PyState_RemoveModule'):
with self.assertRaises(SystemError):
module.call_state_registration_func(2)
with util.uncache(self.name):
module = self.load_module()
with self.subTest('PyState_FindModule'):
self.assertEqual(module.call_state_registration_func(0), None)
with self.subTest('PyState_AddModule'):
with self.assertRaises(SystemError):
module.call_state_registration_func(1)
with self.subTest('PyState_RemoveModule'):
with self.assertRaises(SystemError):
module.call_state_registration_func(2)

def test_load_submodule(self):
# Test loading a simulated submodule.
Expand Down

0 comments on commit ae45649

Please sign in to comment.