This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch tvm-further-cleanup-python-tests-followup
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit bc0ef2a3d879fe5f807e39a0378ad6c6ecf5ccc6
Author: Tianqi Chen <[email protected]>
AuthorDate: Sun Jul 5 16:27:10 2026 +0000

    [CI] Locate request hook from test root
    
    Pass the exact test-local hook path from pytest and Sphinx callers so 
wheel-installed tvm.testing code does not infer a nonexistent checkout from 
site-packages. Keep exact-file loading cached and cover the installed-package 
layout explicitly.
---
 docs/conf.py                                      |  2 +-
 python/tvm/testing/utils.py                       | 37 +++--------------------
 tests/python/conftest.py                          |  3 +-
 tests/python/testing/test_tvm_testing_features.py | 29 ++++++++++++++++++
 4 files changed, 36 insertions(+), 35 deletions(-)

diff --git a/docs/conf.py b/docs/conf.py
index 24f43e1eb6..c226de1c40 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -215,7 +215,7 @@ def rst2md(text, gallery_conf, target_dir, heading_levels, 
real_func):
 
 
 def install_request_hook(gallery_conf, fname):
-    testing.utils.install_request_hook(depth=3)
+    testing.utils.install_request_hook(tvm_path.resolve() / "tests" / "python" 
/ "request_hook.py")
 
 
 INSTALL_TVM_DEV = """\
diff --git a/python/tvm/testing/utils.py b/python/tvm/testing/utils.py
index 86cfb66dc7..f41f06dc07 100644
--- a/python/tvm/testing/utils.py
+++ b/python/tvm/testing/utils.py
@@ -864,46 +864,17 @@ def is_ampere_or_newer():
     return major >= 8 and minor != 9
 
 
-def install_request_hook(depth: int) -> None:
-    """Add a wrapper around urllib.request for CI tests"""
+def install_request_hook(hook_script: Path) -> None:
+    """Add a wrapper around urllib.request for CI tests."""
     if not IS_IN_CI:
         return
 
-    # 
https://sphinx-gallery.github.io/stable/faq.html#why-is-file-not-defined-what-can-i-use
-    base = None
-    msg = ""
-    try:
-        base = __file__
-        msg += f"found file {__file__}\n"
-    except NameError:
-        msg += "no file\n"
-
-    if base is None:
-        hook_script_dir = Path.cwd().resolve()
-        msg += "used path.cwd()\n"
-    else:
-        hook_script_dir = Path(base).resolve().parent
-        msg += "used base()\n"
-
-    msg += f"using depth {depth}\n"
-    if depth <= 0:
-        raise ValueError(f"depth less than 1 not supported, found: {depth}")
-
-    # Go up the parent directories
-    while depth > 0:
-        msg += f"[depth={depth}] dir={hook_script_dir}\n"
-        hook_script_dir = hook_script_dir.parent
-        depth -= 1
-
-    # Ensure the specified dir is valid
-    hook_script_dir = hook_script_dir / "tests" / "python"
-    hook_script = hook_script_dir / "request_hook.py"
+    hook_script = Path(hook_script).resolve()
     if not hook_script.is_file():
-        raise RuntimeError(f"File {hook_script} does not exist:\n{msg}")
+        raise RuntimeError(f"Request hook {hook_script} does not exist")
 
     # Load the exact hook file without exposing the test root as an import 
path.
     # Cache its initializer because Sphinx invokes this once per gallery 
example.
-    hook_script = hook_script.resolve()
     try:
         init = _REQUEST_HOOK_INITIALIZERS[hook_script]
     except KeyError:
diff --git a/tests/python/conftest.py b/tests/python/conftest.py
index 9046f9d083..b3a7dc8378 100644
--- a/tests/python/conftest.py
+++ b/tests/python/conftest.py
@@ -17,6 +17,7 @@
 """Configure pytest for TVM's Python test suite."""
 
 import os
+from pathlib import Path
 
 
 def pytest_sessionstart():
@@ -25,4 +26,4 @@ def pytest_sessionstart():
             install_request_hook,  # pylint: disable=import-outside-toplevel
         )
 
-        install_request_hook(3)
+        install_request_hook(Path(__file__).with_name("request_hook.py"))
diff --git a/tests/python/testing/test_tvm_testing_features.py 
b/tests/python/testing/test_tvm_testing_features.py
index a3eaab3656..d66fd2841b 100644
--- a/tests/python/testing/test_tvm_testing_features.py
+++ b/tests/python/testing/test_tvm_testing_features.py
@@ -73,6 +73,35 @@ def test_fixture_cache_reuses_setup_and_returns_copies():
     assert setup_calls == [1, 2]
 
 
+def test_request_hook_uses_explicit_path(monkeypatch, tmp_path):
+    hook_script = tmp_path / "request_hook.py"
+    hook_script.touch()
+    hook_script = hook_script.resolve()
+    loads = []
+    initializations = []
+
+    def load_hook(path):
+        loads.append(path)
+        return {"init": lambda: initializations.append(path)}
+
+    monkeypatch.setattr(tvm.testing.utils, "IS_IN_CI", True)
+    monkeypatch.setattr(
+        tvm.testing.utils,
+        "__file__",
+        "/installed/site-packages/tvm/testing/utils.py",
+    )
+    monkeypatch.setattr(tvm.testing.utils.runpy, "run_path", load_hook)
+
+    try:
+        tvm.testing.utils.install_request_hook(hook_script)
+        tvm.testing.utils.install_request_hook(hook_script)
+    finally:
+        tvm.testing.utils._REQUEST_HOOK_INITIALIZERS.pop(hook_script, None)
+
+    assert loads == [str(hook_script)]
+    assert initializations == [str(hook_script), str(hook_script)]
+
+
 class TestBrokenFixture:
     # Tests that use a fixture that throws an exception fail, and are
     # marked as setup failures.  The tests themselves are never run.

Reply via email to