Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,7 @@ def _sanity_check(name, package, level):
_ERR_MSG_PREFIX = 'No module named '

def _find_and_load_unlocked(name, import_):
sys.audit("import", name, None, sys.path, sys.meta_path, sys.path_hooks)
path = None
parent = name.rpartition('.')[0]
parent_spec = None
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,26 @@ def hook(event, args):
assertEqual(event_script_path, tmp_file.name)
assertEqual(remote_event_script_path, tmp_file.name)

def test_import_module():
import importlib

with TestHook() as hook:
importlib.import_module("importlib") # already imported, won't get logged
importlib.import_module("email") # standard library module
importlib.import_module("pythoninfo") # random module
importlib.import_module(".test_importlib.abc", "test") # relative import

actual = [a[0] for e, a in hook.seen if e == "import"]
assertSequenceEqual(
[
"email",
"pythoninfo",
"test.test_importlib.abc",
"test.test_importlib"
],
actual,
)

if __name__ == "__main__":
from test.support import suppress_msvcrt_asserts

Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,5 +331,8 @@ def test_sys_remote_exec(self):
if returncode:
self.fail(stderr)

def test_import_module(self):
self.do_test("test_import_module")

if __name__ == "__main__":
unittest.main()
9 changes: 0 additions & 9 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -3694,15 +3694,6 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
Py_XDECREF(sys_path);
return NULL;
}
if (_PySys_Audit(tstate, "import", "OOOOO",
abs_name, Py_None, sys_path ? sys_path : Py_None,
sys_meta_path ? sys_meta_path : Py_None,
sys_path_hooks ? sys_path_hooks : Py_None) < 0) {
Py_XDECREF(sys_path_hooks);
Py_XDECREF(sys_meta_path);
Py_XDECREF(sys_path);
return NULL;
}
Py_XDECREF(sys_path_hooks);
Py_XDECREF(sys_meta_path);
Py_XDECREF(sys_path);
Expand Down
Loading