From 84c590c41d63ce7b36f15fc6f60fe6c21baaaad8 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 31 Mar 2023 17:45:36 -0700 Subject: [PATCH 1/4] GH-102700: allow built-in modules to be submodules --- Lib/importlib/_bootstrap.py | 2 - .../test_importlib/builtin/test_finder.py | 51 +++++++------------ 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index bebe7e15cbce67..22fa2469964ab3 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -887,8 +887,6 @@ class BuiltinImporter: @classmethod def find_spec(cls, fullname, path=None, target=None): - if path is not None: - return None if _imp.is_builtin(fullname): return spec_from_loader(fullname, cls, origin=cls._ORIGIN) else: diff --git a/Lib/test/test_importlib/builtin/test_finder.py b/Lib/test/test_importlib/builtin/test_finder.py index a4869e07b9c0c2..fd7c10e520fc73 100644 --- a/Lib/test/test_importlib/builtin/test_finder.py +++ b/Lib/test/test_importlib/builtin/test_finder.py @@ -1,13 +1,13 @@ from test.test_importlib import abc, util -machinery = util.import_importlib('importlib.machinery') +machinery = util.import_importlib("importlib.machinery") import sys import unittest import warnings -@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') +@unittest.skipIf(util.BUILTINS.good_name is None, "no reasonable builtin module") class FindSpecTests(abc.FinderTests): """Test find_spec() for built-in modules.""" @@ -17,7 +17,7 @@ def test_module(self): with util.uncache(util.BUILTINS.good_name): found = self.machinery.BuiltinImporter.find_spec(util.BUILTINS.good_name) self.assertTrue(found) - self.assertEqual(found.origin, 'built-in') + self.assertEqual(found.origin, "built-in") # Built-in modules cannot be a package. test_package = None @@ -32,25 +32,18 @@ def test_module(self): test_package_over_module = None def test_failure(self): - name = 'importlib' + name = "importlib" assert name not in sys.builtin_module_names spec = self.machinery.BuiltinImporter.find_spec(name) self.assertIsNone(spec) - def test_ignore_path(self): - # The value for 'path' should always trigger a failed import. - with util.uncache(util.BUILTINS.good_name): - spec = self.machinery.BuiltinImporter.find_spec(util.BUILTINS.good_name, - ['pkg']) - self.assertIsNone(spec) - -(Frozen_FindSpecTests, - Source_FindSpecTests - ) = util.test_both(FindSpecTests, machinery=machinery) +(Frozen_FindSpecTests, Source_FindSpecTests) = util.test_both( + FindSpecTests, machinery=machinery +) -@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') +@unittest.skipIf(util.BUILTINS.good_name is None, "no reasonable builtin module") class FinderTests(abc.FinderTests): """Test find_module() for built-in modules.""" @@ -60,9 +53,11 @@ def test_module(self): with util.uncache(util.BUILTINS.good_name): with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) - found = self.machinery.BuiltinImporter.find_module(util.BUILTINS.good_name) + found = self.machinery.BuiltinImporter.find_module( + util.BUILTINS.good_name + ) self.assertTrue(found) - self.assertTrue(hasattr(found, 'load_module')) + self.assertTrue(hasattr(found, "load_module")) # Built-in modules cannot be a package. test_package = test_package_in_package = test_package_over_module = None @@ -71,27 +66,17 @@ def test_module(self): test_module_in_package = None def test_failure(self): - assert 'importlib' not in sys.builtin_module_names + assert "importlib" not in sys.builtin_module_names with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) - loader = self.machinery.BuiltinImporter.find_module('importlib') + loader = self.machinery.BuiltinImporter.find_module("importlib") self.assertIsNone(loader) - def test_ignore_path(self): - # The value for 'path' should always trigger a failed import. - with util.uncache(util.BUILTINS.good_name): - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - loader = self.machinery.BuiltinImporter.find_module( - util.BUILTINS.good_name, - ['pkg']) - self.assertIsNone(loader) - -(Frozen_FinderTests, - Source_FinderTests - ) = util.test_both(FinderTests, machinery=machinery) +(Frozen_FinderTests, Source_FinderTests) = util.test_both( + FinderTests, machinery=machinery +) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() From 197422baf4576e2ec32ca8ddf0cabc15b4d14ac4 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 31 Mar 2023 17:46:42 -0700 Subject: [PATCH 2/4] Add a news entry --- .../2023-04-01-00-46-31.gh-issue-102700.493NB4.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-04-01-00-46-31.gh-issue-102700.493NB4.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-04-01-00-46-31.gh-issue-102700.493NB4.rst b/Misc/NEWS.d/next/Core and Builtins/2023-04-01-00-46-31.gh-issue-102700.493NB4.rst new file mode 100644 index 00000000000000..f6ad23f79c41da --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-04-01-00-46-31.gh-issue-102700.493NB4.rst @@ -0,0 +1 @@ +All built-in modules to be submodules. From 5d5b934975bd6cc767d113b819252a36068cff0e Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Tue, 4 Apr 2023 15:44:48 -0700 Subject: [PATCH 3/4] Revert changes made by Black --- .../test_importlib/builtin/test_finder.py | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/Lib/test/test_importlib/builtin/test_finder.py b/Lib/test/test_importlib/builtin/test_finder.py index fd7c10e520fc73..81dc5a3699d952 100644 --- a/Lib/test/test_importlib/builtin/test_finder.py +++ b/Lib/test/test_importlib/builtin/test_finder.py @@ -1,13 +1,13 @@ from test.test_importlib import abc, util -machinery = util.import_importlib("importlib.machinery") +machinery = util.import_importlib('importlib.machinery') import sys import unittest import warnings -@unittest.skipIf(util.BUILTINS.good_name is None, "no reasonable builtin module") +@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') class FindSpecTests(abc.FinderTests): """Test find_spec() for built-in modules.""" @@ -17,7 +17,7 @@ def test_module(self): with util.uncache(util.BUILTINS.good_name): found = self.machinery.BuiltinImporter.find_spec(util.BUILTINS.good_name) self.assertTrue(found) - self.assertEqual(found.origin, "built-in") + self.assertEqual(found.origin, 'built-in') # Built-in modules cannot be a package. test_package = None @@ -32,18 +32,18 @@ def test_module(self): test_package_over_module = None def test_failure(self): - name = "importlib" + name = 'importlib' assert name not in sys.builtin_module_names spec = self.machinery.BuiltinImporter.find_spec(name) self.assertIsNone(spec) -(Frozen_FindSpecTests, Source_FindSpecTests) = util.test_both( - FindSpecTests, machinery=machinery -) +(Frozen_FindSpecTests, + Source_FindSpecTests + ) = util.test_both(FindSpecTests, machinery=machinery) -@unittest.skipIf(util.BUILTINS.good_name is None, "no reasonable builtin module") +@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') class FinderTests(abc.FinderTests): """Test find_module() for built-in modules.""" @@ -53,11 +53,9 @@ def test_module(self): with util.uncache(util.BUILTINS.good_name): with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) - found = self.machinery.BuiltinImporter.find_module( - util.BUILTINS.good_name - ) + found = self.machinery.BuiltinImporter.find_module(util.BUILTINS.good_name) self.assertTrue(found) - self.assertTrue(hasattr(found, "load_module")) + self.assertTrue(hasattr(found, 'load_module')) # Built-in modules cannot be a package. test_package = test_package_in_package = test_package_over_module = None @@ -66,17 +64,17 @@ def test_module(self): test_module_in_package = None def test_failure(self): - assert "importlib" not in sys.builtin_module_names + assert 'importlib' not in sys.builtin_module_names with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) - loader = self.machinery.BuiltinImporter.find_module("importlib") + loader = self.machinery.BuiltinImporter.find_module('importlib') self.assertIsNone(loader) -(Frozen_FinderTests, Source_FinderTests) = util.test_both( - FinderTests, machinery=machinery -) +(Frozen_FinderTests, + Source_FinderTests + ) = util.test_both(FinderTests, machinery=machinery) -if __name__ == "__main__": +if __name__ == '__main__': unittest.main() From 7bb418fe73fe6d580021e08762757a9332545c87 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Thu, 6 Apr 2023 12:55:03 -0700 Subject: [PATCH 4/4] Update news entry to be more descriptive --- .../2023-04-01-00-46-31.gh-issue-102700.493NB4.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-04-01-00-46-31.gh-issue-102700.493NB4.rst b/Misc/NEWS.d/next/Core and Builtins/2023-04-01-00-46-31.gh-issue-102700.493NB4.rst index f6ad23f79c41da..46951486e4f9c9 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2023-04-01-00-46-31.gh-issue-102700.493NB4.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2023-04-01-00-46-31.gh-issue-102700.493NB4.rst @@ -1 +1 @@ -All built-in modules to be submodules. +Allow built-in modules to be submodules. This allows submodules to be statically linked into a CPython binary.