|
8 | 8 | import contextlib
|
9 | 9 | import os
|
10 | 10 | import sys
|
| 11 | +import unittest.mock |
| 12 | +from test.support import swap_item |
11 | 13 |
|
12 | 14 |
|
13 | 15 | class TestHook:
|
@@ -672,6 +674,84 @@ def hook(event, args):
|
672 | 674 | assertEqual(event_script_path, tmp_file.name)
|
673 | 675 | assertEqual(remote_event_script_path, tmp_file.name)
|
674 | 676 |
|
| 677 | +def test_import_module(): |
| 678 | + import importlib |
| 679 | + |
| 680 | + with TestHook() as hook: |
| 681 | + importlib.import_module("importlib") # already imported, won't get logged |
| 682 | + importlib.import_module("email") # standard library module |
| 683 | + importlib.import_module("pythoninfo") # random module |
| 684 | + importlib.import_module(".audit_test_data.submodule", "test") # relative import |
| 685 | + importlib.import_module("test.audit_test_data.submodule2") # absolute import |
| 686 | + importlib.import_module("_testcapi") # extension module |
| 687 | + |
| 688 | + actual = [a for e, a in hook.seen if e == "import"] |
| 689 | + assertSequenceEqual( |
| 690 | + [ |
| 691 | + ("email", None, sys.path, sys.meta_path, sys.path_hooks), |
| 692 | + ("pythoninfo", None, sys.path, sys.meta_path, sys.path_hooks), |
| 693 | + ("test.audit_test_data.submodule", None, sys.path, sys.meta_path, sys.path_hooks), |
| 694 | + ("test.audit_test_data", None, sys.path, sys.meta_path, sys.path_hooks), |
| 695 | + ("test.audit_test_data.submodule2", None, sys.path, sys.meta_path, sys.path_hooks), |
| 696 | + ("_testcapi", None, sys.path, sys.meta_path, sys.path_hooks), |
| 697 | + ("_testcapi", unittest.mock.ANY, None, None, None) |
| 698 | + ], |
| 699 | + actual, |
| 700 | + ) |
| 701 | + |
| 702 | +def test_builtin__import__(): |
| 703 | + import importlib # noqa: F401 |
| 704 | + |
| 705 | + with TestHook() as hook: |
| 706 | + __import__("importlib") |
| 707 | + __import__("email") |
| 708 | + __import__("pythoninfo") |
| 709 | + __import__("audit_test_data.submodule", level=1, globals={"__package__": "test"}) |
| 710 | + __import__("test.audit_test_data.submodule2") |
| 711 | + __import__("_testcapi") |
| 712 | + |
| 713 | + actual = [a for e, a in hook.seen if e == "import"] |
| 714 | + assertSequenceEqual( |
| 715 | + [ |
| 716 | + ("email", None, sys.path, sys.meta_path, sys.path_hooks), |
| 717 | + ("pythoninfo", None, sys.path, sys.meta_path, sys.path_hooks), |
| 718 | + ("test.audit_test_data.submodule", None, sys.path, sys.meta_path, sys.path_hooks), |
| 719 | + ("test.audit_test_data", None, sys.path, sys.meta_path, sys.path_hooks), |
| 720 | + ("test.audit_test_data.submodule2", None, sys.path, sys.meta_path, sys.path_hooks), |
| 721 | + ("_testcapi", None, sys.path, sys.meta_path, sys.path_hooks), |
| 722 | + ("_testcapi", unittest.mock.ANY, None, None, None) |
| 723 | + ], |
| 724 | + actual, |
| 725 | + ) |
| 726 | + |
| 727 | +def test_import_statement(): |
| 728 | + import importlib # noqa: F401 |
| 729 | + # Set __package__ so relative imports work |
| 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 |
| 738 | + |
| 739 | + actual = [a for e, a in hook.seen if e == "import"] |
| 740 | + # Import statement ordering is different because the package is |
| 741 | + # loaded first and then the submodule |
| 742 | + assertSequenceEqual( |
| 743 | + [ |
| 744 | + ("email", None, sys.path, sys.meta_path, sys.path_hooks), |
| 745 | + ("pythoninfo", None, sys.path, sys.meta_path, sys.path_hooks), |
| 746 | + ("test.audit_test_data", None, sys.path, sys.meta_path, sys.path_hooks), |
| 747 | + ("test.audit_test_data.submodule", None, sys.path, sys.meta_path, sys.path_hooks), |
| 748 | + ("test.audit_test_data.submodule2", None, sys.path, sys.meta_path, sys.path_hooks), |
| 749 | + ("_testcapi", None, sys.path, sys.meta_path, sys.path_hooks), |
| 750 | + ("_testcapi", unittest.mock.ANY, None, None, None) |
| 751 | + ], |
| 752 | + actual, |
| 753 | + ) |
| 754 | + |
675 | 755 | if __name__ == "__main__":
|
676 | 756 | from test.support import suppress_msvcrt_asserts
|
677 | 757 |
|
|
0 commit comments