GH-139686: Revert "gh-139686: Make reloading a lazy module no-op (GH-… · python/cpython@dfeefbe

GitHub

GitHub CopilotWrite better code with AI | MCP RegistryIntegrate external tools | ActionsAutomate any workflow | CodespacesInstant dev environments | IssuesPlan and track work | Code ReviewManage code changes | Code QualityEnforce quality at merge | Why GitHub | Marketplace | View all features | Enterprises | Small and medium teams | Startups | View all use cases | View all industries | View all solutions | AI | Software Development | DevOps | Security | View all topics | Customer stories | Events & webinars | Ebooks & reports | Business insights | Trust center | Partners | View all resources

@@ -10,9 +10,6 @@

1010fromtest.supportimportthreading_helper

1111fromtest.test_importlibimportutilastest_util

121213-# Make sure sys.modules[util] is in sync with the import.

14-# That is needed as other tests may reload util.

15-sys.modules['importlib.util'] =util

16131714classCollectInit:

1815@@ -195,7 +192,7 @@ def test_lazy_self_referential_modules(self):

195192sys.modules['json'] =module

196193loader.exec_module(module)

197194198-# Trigger load with attribute lookup, ensure expected behavior.

195+# Trigger load with attribute lookup, ensure expected behavior

199196test_load=module.loads('{}')

200197self.assertEqual(test_load, {})

201198@@ -227,26 +224,6 @@ def __delattr__(self, name):

227224withself.assertRaises(AttributeError):

228225delmodule.CONSTANT

229226230-deftest_reload(self):

231-# Reloading a lazy module that hasn't been materialized is a no-op.

232-module=self.new_module()

233-sys.modules[TestingImporter.module_name] =module

234-235-# Change the source code to add a new attribute.

236-TestingImporter.source_code='attr = 42\nnew_attr = 123\n__name__ = {!r}'.format(TestingImporter.mutated_name)

237-self.assertIsInstance(module, util._LazyModule)

238-239-# Reload the module (should be a no-op since not materialized).

240-reloaded=importlib.reload(module)

241-self.assertIs(reloaded, module)

242-self.assertIsInstance(module, util._LazyModule)

243-244-# Access the new attribute (should trigger materialization, and new_attr should exist).

245-self.assertEqual(module.attr, 42)

246-self.assertNotIsInstance(module, util._LazyModule)

247-self.assertTrue(hasattr(module, 'new_attr'))

248-self.assertEqual(module.new_attr, 123)

249-250227251228if__name__=='__main__':

252229unittest.main()