Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
GH-102700: allow built-in modules to be submodules
  • Loading branch information
brettcannon committed Apr 1, 2023
commit 84c590c41d63ce7b36f15fc6f60fe6c21baaaad8
2 changes: 0 additions & 2 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
51 changes: 18 additions & 33 deletions Lib/test/test_importlib/builtin/test_finder.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from test.test_importlib import abc, util

machinery = util.import_importlib('importlib.machinery')
machinery = util.import_importlib("importlib.machinery")
Comment thread
brettcannon marked this conversation as resolved.
Outdated

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."""
Expand All @@ -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
Expand All @@ -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."""
Expand All @@ -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
Expand All @@ -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()