From aa5dd0c2ffa6e0a92bd3a85b6fc48ffcea55d6ac Mon Sep 17 00:00:00 2001 From: Sebastian Abrahamsson Date: Thu, 19 Nov 2026 14:26:25 +0100 Subject: [PATCH] Make relative PYTHONPATH entries absolute at setup VUnit starts vsim (Riviera-PRO, Questa/ModelSim) in the test output directory, so the embedded interpreter resolved relative entries there and could not import classes of Python objects found through them. Co-Authored-By: Claude Opus 5.5 --- docs/file_paths.rst | 5 ++++- src/vunit_python_bridge/bridge.py | 13 +++++++++++++ tests/test_python_bridge.py | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/file_paths.rst b/docs/file_paths.rst index bc9157e..230ea3f 100644 --- a/docs/file_paths.rst +++ b/docs/file_paths.rst @@ -66,7 +66,10 @@ run script does. Without any setup it finds: Python code anywhere else is found once its directory is on ``PYTHONPATH``, set in the run script before ``vu.main()``: the simulations inherit the -environment of VUnit (see the recipes below). A verification component used by +environment of VUnit (see the recipes below). Relative entries of a +``PYTHONPATH`` set before ``add_package`` are made absolute against the current +directory, since some simulators run in the test output directory; set later +ones as absolute paths. A verification component used by many testbenches can so keep its model in a directory of its own and name its class, for example ``"my_vc_model.Model"`` for a :ref:`Python object `. Installing the model as a package, for example with diff --git a/src/vunit_python_bridge/bridge.py b/src/vunit_python_bridge/bridge.py index 1a6661b..a1829c5 100644 --- a/src/vunit_python_bridge/bridge.py +++ b/src/vunit_python_bridge/bridge.py @@ -118,6 +118,7 @@ def setup( front_end = "fli" if is_fli else "vhpi" if is_vhpi else None library_file = _prepare_library(Path(simulator_prefix) if front_end else None, front_end) run_script_dir = str(Path.cwd() if run_script_path is None else Path(run_script_path).resolve().parent) + _make_pythonpath_absolute() # The configuration belongs to the project, the library may be shared. The simulator # hooks tell the library where the configuration is. config_file = root / CONFIG_FILE_NAME @@ -208,6 +209,18 @@ def _check_attributes(name: str, interface, hooks) -> None: ) +def _make_pythonpath_absolute() -> None: + """ + Resolve relative PYTHONPATH entries against the current directory, as python does. VUnit + starts vsim (Questa/ModelSim, Riviera-PRO) in the test output directory, where they would + resolve differently. + """ + # Not covered: a relative PYTHONPATH set after add_package, which needs the cwd in the config + value = os.environ.get("PYTHONPATH") + if value: + os.environ["PYTHONPATH"] = os.pathsep.join(os.path.abspath(entry) for entry in value.split(os.pathsep)) + + def _prepare_library(simulator_prefix: Optional[Path], front_end: Optional[str]) -> Path: """ Prepare the library in the library directory, failing with a pointer to the variable diff --git a/tests/test_python_bridge.py b/tests/test_python_bridge.py index 7947b81..c218843 100644 --- a/tests/test_python_bridge.py +++ b/tests/test_python_bridge.py @@ -1223,6 +1223,25 @@ def _config_keys(text): return dict(line.split("=", 1) for line in text.splitlines()) +class TestPythonPath(unittest.TestCase): + """ + _make_pythonpath_absolute: vsim runs in the test output directory, not in the current one + """ + + def test_relative_entries_resolved_against_current_directory(self): + value = os.pathsep.join(["models", "", os.path.abspath("/abs")]) + with mock.patch.dict(os.environ, {"PYTHONPATH": value}): + bridge_setup._make_pythonpath_absolute() # pylint: disable=protected-access + entries = os.environ["PYTHONPATH"].split(os.pathsep) + self.assertEqual(entries, [os.path.abspath("models"), os.getcwd(), os.path.abspath("/abs")]) + + def test_unset_stays_unset(self): + with mock.patch.dict(os.environ): + os.environ.pop("PYTHONPATH", None) + bridge_setup._make_pythonpath_absolute() # pylint: disable=protected-access + self.assertNotIn("PYTHONPATH", os.environ) + + class TestConfigFile(unittest.TestCase): """ _config_text / _write_if_changed