Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,8 +1306,15 @@ def _sanity_check(name, package, level):
_ERR_MSG_PREFIX = 'No module named '

def _find_and_load_unlocked(name, import_):
sys.audit(
"import",
name,
None,
getattr(sys, "path"),
getattr(sys, "meta_path"),
getattr(sys, "path_hooks")
)
path = None
sys.audit("import", name, path, sys.path, sys.meta_path, sys.path_hooks)
parent = name.rpartition('.')[0]
parent_spec = None
if parent:
Expand Down
63 changes: 60 additions & 3 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,15 +679,72 @@ def test_import_module():
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
importlib.import_module(".audit_test_data.submodule", "test") # relative import
importlib.import_module("test.audit_test_data.submodule2") # absolute import
importlib.import_module("_testcapi") # extension module

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

def test_builtin__import__():
import importlib # noqa: F401

with TestHook() as hook:
__import__("importlib")
__import__("email")
__import__("pythoninfo")
__import__("test.audit_test_data.submodule", fromlist=["audit_test_data"])
__import__("test.audit_test_data.submodule2")
__import__("_testcapi")

actual = [a[0] for e, a in hook.seen if e == "import"]
assertSequenceEqual(
[
"email",
"pythoninfo",
"test.audit_test_data.submodule",
"test.audit_test_data",
"test.audit_test_data.submodule2",
"_testcapi",
"_testcapi",
],
actual,
)

def test_import_statement():
import importlib # noqa: F401

with TestHook() as hook:
import importlib # noqa: F401
import email # noqa: F401
import pythoninfo # noqa: F401
from test.audit_test_data import submodule # noqa: F401
import test.audit_test_data.submodule2 # noqa: F401
import _testcapi # noqa: F401

actual = [a[0] for e, a in hook.seen if e == "import"]
# Import statement ordering is different because the package is
# loaded first and then the submodule
assertSequenceEqual(
[
"email",
"pythoninfo",
"test.audit_test_data",
"test.audit_test_data.submodule",
"test.audit_test_data.submodule2",
"_testcapi",
"_testcapi",
],
actual,
)
Expand Down
Empty file.
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,11 @@ def test_sys_remote_exec(self):
def test_import_module(self):
self.do_test("test_import_module")

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

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

if __name__ == "__main__":
unittest.main()
18 changes: 0 additions & 18 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -3681,24 +3681,6 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)

PyTime_t t1 = 0, accumulated_copy = accumulated;

PyObject *sys_path, *sys_meta_path, *sys_path_hooks;
if (PySys_GetOptionalAttrString("path", &sys_path) < 0) {
return NULL;
}
if (PySys_GetOptionalAttrString("meta_path", &sys_meta_path) < 0) {
Py_XDECREF(sys_path);
return NULL;
}
if (PySys_GetOptionalAttrString("path_hooks", &sys_path_hooks) < 0) {
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);


/* XOptions is initialized after first some imports.
* So we can't have negative cache before completed initialization.
* Anyway, importlib._find_and_load is much slower than
Expand Down
Loading