From 4c14b5de1cf372e963a4378fc6cb2bca36d24eb8 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sat, 4 May 2013 13:56:58 -0400 Subject: #17115,17116: Have modules initialize the __package__ and __loader__ attributes to None. The long-term goal is for people to be able to rely on these attributes existing and checking for None to see if they have been set. Since import itself sets these attributes when a loader does not the only instances when the attributes are None are from someone overloading __import__() and not using a loader or someone creating a module from scratch. This patch also unifies module initialization. Before you could have different attributes with default values depending on how the module object was created. Now the only way to not get the same default set of attributes is to circumvent initialization by calling ModuleType.__new__() directly. --- Lib/test/test_importlib/test_api.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'Lib/test/test_importlib/test_api.py') diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py index ac88b2bf9c..297d6cbef6 100644 --- a/Lib/test/test_importlib/test_api.py +++ b/Lib/test/test_importlib/test_api.py @@ -197,14 +197,12 @@ class StartupTests(unittest.TestCase): # Issue #17098: all modules should have __loader__ defined. for name, module in sys.modules.items(): if isinstance(module, types.ModuleType): - if name in sys.builtin_module_names: - self.assertIn(module.__loader__, - (importlib.machinery.BuiltinImporter, - importlib._bootstrap.BuiltinImporter)) - elif imp.is_frozen(name): - self.assertIn(module.__loader__, - (importlib.machinery.FrozenImporter, - importlib._bootstrap.FrozenImporter)) + self.assertTrue(hasattr(module, '__loader__'), + '{!r} lacks a __loader__ attribute'.format(name)) + if importlib.machinery.BuiltinImporter.find_module(name): + self.assertIsNot(module.__loader__, None) + elif importlib.machinery.FrozenImporter.find_module(name): + self.assertIsNot(module.__loader__, None) if __name__ == '__main__': -- cgit v1.2.1