Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion docs/file_paths.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
<python_bridge:objects>`. Installing the model as a package, for example with
Expand Down
13 changes: 13 additions & 0 deletions src/vunit_python_bridge/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/test_python_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading