Skip to content

Commit 9bc4407

Browse files
Lisa CarrierLisa Carrier
authored andcommitted
Adds more tests and cleans up import.c.
1 parent 5207807 commit 9bc4407

File tree

7 files changed

+74
-22
lines changed

7 files changed

+74
-22
lines changed

Lib/importlib/_bootstrap.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,8 +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+
sys.audit(
1310+
"import",
1311+
name,
1312+
None,
1313+
getattr(sys, "path"),
1314+
getattr(sys, "meta_path"),
1315+
getattr(sys, "path_hooks")
1316+
)
13091317
path = None
1310-
sys.audit("import", name, path, sys.path, sys.meta_path, sys.path_hooks)
13111318
parent = name.rpartition('.')[0]
13121319
parent_spec = None
13131320
if parent:

Lib/test/audit-tests.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,15 +679,72 @@ def test_import_module():
679679
importlib.import_module("importlib") # already imported, won't get logged
680680
importlib.import_module("email") # standard library module
681681
importlib.import_module("pythoninfo") # random module
682-
importlib.import_module(".test_importlib.abc", "test") # relative import
682+
importlib.import_module(".audit_test_data.submodule", "test") # relative import
683+
importlib.import_module("test.audit_test_data.submodule2") # absolute import
684+
importlib.import_module("_testcapi") # extension module
683685

684686
actual = [a[0] for e, a in hook.seen if e == "import"]
685687
assertSequenceEqual(
686688
[
687689
"email",
688690
"pythoninfo",
689-
"test.test_importlib.abc",
690-
"test.test_importlib"
691+
"test.audit_test_data.submodule",
692+
"test.audit_test_data",
693+
"test.audit_test_data.submodule2",
694+
"_testcapi",
695+
"_testcapi",
696+
],
697+
actual,
698+
)
699+
700+
def test_builtin__import__():
701+
import importlib # noqa: F401
702+
703+
with TestHook() as hook:
704+
__import__("importlib")
705+
__import__("email")
706+
__import__("pythoninfo")
707+
__import__("test.audit_test_data.submodule", fromlist=["audit_test_data"])
708+
__import__("test.audit_test_data.submodule2")
709+
__import__("_testcapi")
710+
711+
actual = [a[0] for e, a in hook.seen if e == "import"]
712+
assertSequenceEqual(
713+
[
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",
721+
],
722+
actual,
723+
)
724+
725+
def test_import_statement():
726+
import importlib # noqa: F401
727+
728+
with TestHook() as hook:
729+
import importlib # noqa: F401
730+
import email # noqa: F401
731+
import pythoninfo # noqa: F401
732+
from test.audit_test_data import submodule # noqa: F401
733+
import test.audit_test_data.submodule2 # noqa: F401
734+
import _testcapi # noqa: F401
735+
736+
actual = [a[0] for e, a in hook.seen if e == "import"]
737+
# Import statement ordering is different because the package is
738+
# loaded first and then the submodule
739+
assertSequenceEqual(
740+
[
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",
691748
],
692749
actual,
693750
)

Lib/test/audit_test_data/__init__.py

Whitespace-only changes.

Lib/test/audit_test_data/submodule.py

Whitespace-only changes.

Lib/test/audit_test_data/submodule2.py

Whitespace-only changes.

Lib/test/test_audit.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,5 +334,11 @@ def test_sys_remote_exec(self):
334334
def test_import_module(self):
335335
self.do_test("test_import_module")
336336

337+
def test_builtin__import__(self):
338+
self.do_test("test_builtin__import__")
339+
340+
def test_import_statement(self):
341+
self.do_test("test_import_statement")
342+
337343
if __name__ == "__main__":
338344
unittest.main()

Python/import.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3681,24 +3681,6 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
36813681

36823682
PyTime_t t1 = 0, accumulated_copy = accumulated;
36833683

3684-
PyObject *sys_path, *sys_meta_path, *sys_path_hooks;
3685-
if (PySys_GetOptionalAttrString("path", &sys_path) < 0) {
3686-
return NULL;
3687-
}
3688-
if (PySys_GetOptionalAttrString("meta_path", &sys_meta_path) < 0) {
3689-
Py_XDECREF(sys_path);
3690-
return NULL;
3691-
}
3692-
if (PySys_GetOptionalAttrString("path_hooks", &sys_path_hooks) < 0) {
3693-
Py_XDECREF(sys_meta_path);
3694-
Py_XDECREF(sys_path);
3695-
return NULL;
3696-
}
3697-
Py_XDECREF(sys_path_hooks);
3698-
Py_XDECREF(sys_meta_path);
3699-
Py_XDECREF(sys_path);
3700-
3701-
37023684
/* XOptions is initialized after first some imports.
37033685
* So we can't have negative cache before completed initialization.
37043686
* Anyway, importlib._find_and_load is much slower than

0 commit comments

Comments
 (0)