Skip to content

Commit 2387863

Browse files
Lisa CarrierLisa Carrier
authored andcommitted
Uses swap_item for globals().
1 parent 135539f commit 2387863

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

Lib/test/audit-tests.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import sys
1111
import unittest.mock
12+
from test.support import swap_item
1213

1314

1415
class TestHook:
@@ -726,18 +727,14 @@ def test_builtin__import__():
726727
def test_import_statement():
727728
import importlib # noqa: F401
728729
# 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
741738

742739
actual = [a for e, a in hook.seen if e == "import"]
743740
# Import statement ordering is different because the package is

0 commit comments

Comments
 (0)