Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.4k
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 all commits
a9c7bc0b875bee678893f46706963241aeb5ffd268d3cdd6b682613f22c996d6744e912509478fd29fd1a6868a2bef8cc5584e07b9ec71f22134259cd3f0bb19e1a3ad12e5c69e28fa4ca81976File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -297,4 +297,6 @@ def load_tests(loader, tests, pattern): | ||
| if __name__ == "__main__": | ||
| # set __name__ to match doctest expectations | ||
| __name__ = "test.test_metaclass" | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,7 +3,9 @@ | ||
| Nick Mathewson | ||
| ''' | ||
| import importlib.machinery | ||
| import sys | ||
| from contextlib import contextmanager | ||
| from textwrap import dedent | ||
| from types import FunctionType, MethodType, BuiltinFunctionType | ||
| import pyclbr | ||
| @@ -22,6 +24,29 @@ | ||
| # is imperfect (as designed), testModule is called with a set of | ||
| # members to ignore. | ||
| @contextmanager | ||
| def temporary_main_spec(): | ||
| """ | ||
| A context manager that temporarily sets the `__spec__` attribute | ||
| of the `__main__` module if it's missing. | ||
| """ | ||
| main_mod = sys.modules.get("__main__") | ||
| if main_mod is None: | ||
| yield # Do nothing if __main__ is not present | ||
| return | ||
| original_spec = getattr(main_mod, "__spec__", None) | ||
| if original_spec is None: | ||
| main_mod.__spec__ = importlib.machinery.ModuleSpec( | ||
| name="__main__", loader=None, origin="built-in" | ||
| ) | ||
| try: | ||
| yield | ||
| finally: | ||
| main_mod.__spec__ = original_spec | ||
| class PyclbrTest(TestCase): | ||
| def assertListEq(self, l1, l2, ignore): | ||
| @@ -145,8 +170,9 @@ def test_easy(self): | ||
| self.checkModule('pyclbr') | ||
| # XXX: Metaclasses are not supported | ||
| # self.checkModule('ast') | ||
| self.checkModule('doctest', ignore=("TestResults", "_SpoofOut", | ||
| "DocTestCase", '_DocTestSuite')) | ||
| with temporary_main_spec(): | ||
| self.checkModule('doctest', ignore=("TestResults", "_SpoofOut", | ||
| "DocTestCase", '_DocTestSuite')) | ||
| self.checkModule('difflib', ignore=("Match",)) | ||
| def test_cases(self): | ||
| @@ -223,12 +249,13 @@ 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals'), | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ) | ||
| cm('pydoc', ignore=('input', 'output',)) # properties | ||
| with temporary_main_spec(): | ||
| cm( | ||
| 'pdb', | ||
| # pyclbr does not handle elegantly `typing` or properties | ||
| ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals'), | ||
| ) | ||
| cm('pydoc', ignore=('input', 'output',)) # properties | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # Tests for modules inside packages | ||
| cm('email.parser') | ||
| 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 @@ | ||
| Tests in :file:`Lib/test` can now be correctly executed as standalone scripts. |
Uh oh!
There was an error while loading. Please reload this page.