-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-131290: ensure that test files can be executed as standalone scripts #131371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
a9c7bc0
b875bee
678893f
4670696
3241aeb
5ffd268
d3cdd6b
682613f
22c996d
6744e91
2509478
fd29fd1
a6868a2
bef8cc5
584e07b
9ec71f2
2134259
cd3f0bb
19e1a3a
d12e5c6
9e28fa4
ca81976
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,6 +142,12 @@ def defined_in(item, module): | |
| self.assertHaskey(dict, name, ignore) | ||
|
|
||
| def test_easy(self): | ||
| import importlib.util | ||
| if getattr(sys.modules["__main__"], "__spec__", None) is None: | ||
| sys.modules["__main__"].__spec__ = importlib.machinery.ModuleSpec( | ||
| name="__main__", loader=None, origin="built-in" | ||
| ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not very happy with this specific hack. Would it be possible to have some contextmanager decorator that we could apply on the test method so that the module's spec is only changed for the duration of the test? |
||
|
|
||
| self.checkModule('pyclbr') | ||
| # XXX: Metaclasses are not supported | ||
| # self.checkModule('ast') | ||
|
|
@@ -214,6 +220,18 @@ def compare(parent1, children1, parent2, children2): | |
|
|
||
| compare(None, actual, None, expected) | ||
|
|
||
| def test_pdb_module(self): | ||
| import importlib.util | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a top-level import, no need for a local one (we don't really need fast start up time for tests) |
||
| if getattr(sys.modules["__main__"], "__spec__", None) is None: | ||
| sys.modules["__main__"].__spec__ = importlib.machinery.ModuleSpec( | ||
| name="__main__", loader=None, origin="builtin" | ||
| ) | ||
|
|
||
| self.checkModule( | ||
| 'pdb', | ||
| ignore=('_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'Pdb'), | ||
| ) | ||
|
|
||
| def test_others(self): | ||
| cm = self.checkModule | ||
|
|
||
|
|
@@ -223,11 +241,6 @@ def test_others(self): | |
| with warnings.catch_warnings(): | ||
| warnings.simplefilter('ignore', DeprecationWarning) | ||
| cm('sre_parse', ignore=('dump', 'groups', 'pos')) # from sre_constants import *; property | ||
| cm( | ||
| 'pdb', | ||
| # pyclbr does not handle elegantly `typing` or properties | ||
|
picnixz marked this conversation as resolved.
|
||
| ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals'), | ||
|
picnixz marked this conversation as resolved.
|
||
| ) | ||
| cm('pydoc', ignore=('input', 'output',)) # properties | ||
|
|
||
| # Tests for modules inside packages | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2546,4 +2546,5 @@ def test_test_result_get_state(self): | |
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| setup.setup_process() | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Several tests are modified when called directly via ./python Lib/test/... | ||
|
MaximGit1 marked this conversation as resolved.
Outdated
|
||
|
picnixz marked this conversation as resolved.
Outdated
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Tests in :file:`Lib/test` can now be correctly executed as standalone scripts. | ||
|
|
||
| Several tests were modified to correctly handle execution when called directly via `./python Lib/test/...`, ensuring that the `__spec__` attribute is properly set in certain modules like `__main__`. |
Uh oh!
There was an error while loading. Please reload this page.