Skip to content

Commit 135539f

Browse files
Lisa CarrierLisa Carrier
authored andcommitted
Fixes rel imports, test whole tuple, fixes args.
1 parent 9bc4407 commit 135539f

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

Lib/importlib/_bootstrap.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,15 +1306,15 @@ def _sanity_check(name, package, level):
13061306
_ERR_MSG_PREFIX = 'No module named '
13071307

13081308
def _find_and_load_unlocked(name, import_):
1309+
path = None
13091310
sys.audit(
13101311
"import",
13111312
name,
1312-
None,
1313-
getattr(sys, "path"),
1314-
getattr(sys, "meta_path"),
1315-
getattr(sys, "path_hooks")
1313+
path,
1314+
getattr(sys, "path", None),
1315+
getattr(sys, "meta_path", None),
1316+
getattr(sys, "path_hooks", None)
13161317
)
1317-
path = None
13181318
parent = name.rpartition('.')[0]
13191319
parent_spec = None
13201320
if parent:

Lib/test/audit-tests.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import contextlib
99
import os
1010
import sys
11+
import unittest.mock
1112

1213

1314
class TestHook:
@@ -683,16 +684,16 @@ def test_import_module():
683684
importlib.import_module("test.audit_test_data.submodule2") # absolute import
684685
importlib.import_module("_testcapi") # extension module
685686

686-
actual = [a[0] for e, a in hook.seen if e == "import"]
687+
actual = [a for e, a in hook.seen if e == "import"]
687688
assertSequenceEqual(
688689
[
689-
"email",
690-
"pythoninfo",
691-
"test.audit_test_data.submodule",
692-
"test.audit_test_data",
693-
"test.audit_test_data.submodule2",
694-
"_testcapi",
695-
"_testcapi",
690+
("email", None, sys.path, sys.meta_path, sys.path_hooks),
691+
("pythoninfo", None, sys.path, sys.meta_path, sys.path_hooks),
692+
("test.audit_test_data.submodule", None, sys.path, sys.meta_path, sys.path_hooks),
693+
("test.audit_test_data", None, sys.path, sys.meta_path, sys.path_hooks),
694+
("test.audit_test_data.submodule2", None, sys.path, sys.meta_path, sys.path_hooks),
695+
("_testcapi", None, sys.path, sys.meta_path, sys.path_hooks),
696+
("_testcapi", unittest.mock.ANY, None, None, None)
696697
],
697698
actual,
698699
)
@@ -704,47 +705,52 @@ def test_builtin__import__():
704705
__import__("importlib")
705706
__import__("email")
706707
__import__("pythoninfo")
707-
__import__("test.audit_test_data.submodule", fromlist=["audit_test_data"])
708+
__import__("audit_test_data.submodule", level=1, globals={"__package__": "test"})
708709
__import__("test.audit_test_data.submodule2")
709710
__import__("_testcapi")
710711

711-
actual = [a[0] for e, a in hook.seen if e == "import"]
712+
actual = [a for e, a in hook.seen if e == "import"]
712713
assertSequenceEqual(
713714
[
714-
"email",
715-
"pythoninfo",
716-
"test.audit_test_data.submodule",
717-
"test.audit_test_data",
718-
"test.audit_test_data.submodule2",
719-
"_testcapi",
720-
"_testcapi",
715+
("email", None, sys.path, sys.meta_path, sys.path_hooks),
716+
("pythoninfo", None, sys.path, sys.meta_path, sys.path_hooks),
717+
("test.audit_test_data.submodule", None, sys.path, sys.meta_path, sys.path_hooks),
718+
("test.audit_test_data", None, sys.path, sys.meta_path, sys.path_hooks),
719+
("test.audit_test_data.submodule2", None, sys.path, sys.meta_path, sys.path_hooks),
720+
("_testcapi", None, sys.path, sys.meta_path, sys.path_hooks),
721+
("_testcapi", unittest.mock.ANY, None, None, None)
721722
],
722723
actual,
723724
)
724725

725726
def test_import_statement():
726727
import importlib # noqa: F401
728+
# Set __package__ so relative imports work
729+
old_package = globals().get("__package__", None)
730+
globals()["__package__"] = "test"
727731

728732
with TestHook() as hook:
729733
import importlib # noqa: F401
730734
import email # noqa: F401
731735
import pythoninfo # noqa: F401
732-
from test.audit_test_data import submodule # noqa: F401
736+
from .audit_test_data import submodule # noqa: F401
733737
import test.audit_test_data.submodule2 # noqa: F401
734738
import _testcapi # noqa: F401
735739

736-
actual = [a[0] for e, a in hook.seen if e == "import"]
740+
globals()["__package__"] = old_package
741+
742+
actual = [a for e, a in hook.seen if e == "import"]
737743
# Import statement ordering is different because the package is
738744
# loaded first and then the submodule
739745
assertSequenceEqual(
740746
[
741-
"email",
742-
"pythoninfo",
743-
"test.audit_test_data",
744-
"test.audit_test_data.submodule",
745-
"test.audit_test_data.submodule2",
746-
"_testcapi",
747-
"_testcapi",
747+
("email", None, sys.path, sys.meta_path, sys.path_hooks),
748+
("pythoninfo", None, sys.path, sys.meta_path, sys.path_hooks),
749+
("test.audit_test_data", None, sys.path, sys.meta_path, sys.path_hooks),
750+
("test.audit_test_data.submodule", None, sys.path, sys.meta_path, sys.path_hooks),
751+
("test.audit_test_data.submodule2", None, sys.path, sys.meta_path, sys.path_hooks),
752+
("_testcapi", None, sys.path, sys.meta_path, sys.path_hooks),
753+
("_testcapi", unittest.mock.ANY, None, None, None)
748754
],
749755
actual,
750756
)

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,7 @@ TESTSUBDIRS= idlelib/idle_test \
25902590
test/test_ast \
25912591
test/test_ast/data \
25922592
test/archivetestdata \
2593+
test/audit_test_data \
25932594
test/audiodata \
25942595
test/certdata \
25952596
test/certdata/capath \

0 commit comments

Comments
 (0)