forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure `__spec__.cached` (at minimum) can be used.
- Loading branch information
1 parent
9ffbad4
commit 7064cd5
Showing
11 changed files
with
130 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Tests for helper functions used by import.c .""" | ||
|
||
from importlib import _bootstrap_external, machinery | ||
import os.path | ||
import unittest | ||
|
||
from .. import util | ||
|
||
|
||
class FixUpModuleTests: | ||
|
||
def test_no_loader_but_spec(self): | ||
loader = object() | ||
name = "hello" | ||
path = "hello.py" | ||
spec = machinery.ModuleSpec(name, loader) | ||
ns = {"__spec__": spec} | ||
_bootstrap_external._fix_up_module(ns, name, path) | ||
|
||
expected = {"__spec__": spec, "__loader__": loader, "__file__": path, | ||
"__cached__": None} | ||
self.assertEqual(ns, expected) | ||
|
||
def test_no_loader_no_spec_but_sourceless(self): | ||
name = "hello" | ||
path = "hello.py" | ||
ns = {} | ||
_bootstrap_external._fix_up_module(ns, name, path, path) | ||
|
||
expected = {"__file__": path, "__cached__": path} | ||
|
||
for key, val in expected.items(): | ||
with self.subTest(f"{key}: {val}"): | ||
self.assertEqual(ns[key], val) | ||
|
||
spec = ns["__spec__"] | ||
self.assertIsInstance(spec, machinery.ModuleSpec) | ||
self.assertEqual(spec.name, name) | ||
self.assertEqual(spec.origin, os.path.abspath(path)) | ||
self.assertEqual(spec.cached, os.path.abspath(path)) | ||
self.assertIsInstance(spec.loader, machinery.SourcelessFileLoader) | ||
self.assertEqual(spec.loader.name, name) | ||
self.assertEqual(spec.loader.path, path) | ||
self.assertEqual(spec.loader, ns["__loader__"]) | ||
|
||
def test_no_loader_no_spec_but_source(self): | ||
name = "hello" | ||
path = "hello.py" | ||
ns = {} | ||
_bootstrap_external._fix_up_module(ns, name, path) | ||
|
||
expected = {"__file__": path, "__cached__": None} | ||
|
||
for key, val in expected.items(): | ||
with self.subTest(f"{key}: {val}"): | ||
self.assertEqual(ns[key], val) | ||
|
||
spec = ns["__spec__"] | ||
self.assertIsInstance(spec, machinery.ModuleSpec) | ||
self.assertEqual(spec.name, name) | ||
self.assertEqual(spec.origin, os.path.abspath(path)) | ||
self.assertIsInstance(spec.loader, machinery.SourceFileLoader) | ||
self.assertEqual(spec.loader.name, name) | ||
self.assertEqual(spec.loader.path, path) | ||
self.assertEqual(spec.loader, ns["__loader__"]) | ||
|
||
|
||
FrozenFixUpModuleTests, SourceFixUpModuleTests = util.test_both(FixUpModuleTests) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2022-10-06-17-59-22.gh-issue-65961.SXlQnI.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Do not rely solely on ``__cached__`` on modules; code will also support | ||
``__spec__.cached``. |