|
9 | 9 | import os |
10 | 10 | import sys |
11 | 11 | import unittest.mock |
| 12 | +from test.support import swap_item |
12 | 13 |
|
13 | 14 |
|
14 | 15 | class TestHook: |
@@ -726,18 +727,14 @@ def test_builtin__import__(): |
726 | 727 | def test_import_statement(): |
727 | 728 | import importlib # noqa: F401 |
728 | 729 | # Set __package__ so relative imports work |
729 | | - old_package = globals().get("__package__", None) |
730 | | - globals()["__package__"] = "test" |
731 | | - |
732 | | - with TestHook() as hook: |
733 | | - import importlib # noqa: F401 |
734 | | - import email # noqa: F401 |
735 | | - import pythoninfo # noqa: F401 |
736 | | - from .audit_test_data import submodule # noqa: F401 |
737 | | - import test.audit_test_data.submodule2 # noqa: F401 |
738 | | - import _testcapi # noqa: F401 |
739 | | - |
740 | | - globals()["__package__"] = old_package |
| 730 | + with swap_item(globals(), "__package__", "test"): |
| 731 | + with TestHook() as hook: |
| 732 | + import importlib # noqa: F401 |
| 733 | + import email # noqa: F401 |
| 734 | + import pythoninfo # noqa: F401 |
| 735 | + from .audit_test_data import submodule # noqa: F401 |
| 736 | + import test.audit_test_data.submodule2 # noqa: F401 |
| 737 | + import _testcapi # noqa: F401 |
741 | 738 |
|
742 | 739 | actual = [a for e, a in hook.seen if e == "import"] |
743 | 740 | # Import statement ordering is different because the package is |
|
0 commit comments