Repository navigation
Keep the simulator hooks of a VUnit object when another one is created - #1234
Merged
Merged
Conversation
The simulator hooks registered by packages were kept in a module global registry, which every new VUnit object cleared. A run script creating a second VUnit object after adding a package therefore lost the hooks of the first one: its tests ran without the flags and environment the package registered, for example without the library search path of a native library, and failed with errors like GHDL's "cannot load VHPIDIRECT shared library". Every VUnit object now has a SimulatorHooks registry of its own, which the package context registers into and the VUnit object gives to the simulator interface it creates. The vsim based interfaces ask for the hooks of their persistent process when it is started, since the interface gets the hooks after it has been created. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The simulator hooks that VUnit packages register (#1221) were kept in a module-global registry, and every new
VUnitobject cleared it. A run script that creates a secondVUnitobject afteradd_package(...)therefore loses the hooks of the first one. Its tests then run without the flags and environment the package registered, even though the package's setup has run and its files exist.This was found with vunit-python-bridge. Its GHDL
run_envhook puts the folder of its native library onLD_LIBRARY_PATH. A run script that created anotherVUnitobject made every test fail with:NVC similarly loses
--load=..., and ModelSim/Questa loses itsprocess_flagsandrun_env. The docs already say the hooks "apply only to the project in which they are registered", so this makes the code do what the docs describe.Changes
vunit/sim_if/hooks.py: aSimulatorHooksclass holds the hooks of one VUnit object (register,get_flags,get_run_env), replacing the global_HOOKS,register_hooksandclear_hooks. The module functionsget_flags(simulator_interface, kind)andget_run_env(simulator_interface, env)keep their signatures. They use the registry the interface was given, or none for an interface not created by a VUnit object. The simulator interfaces' call sites are unchanged.VUnitobject: it creates its ownSimulatorHooksinstead of clearing the global registry, and attaches it to the simulator interface it creates (simulator_if.simulator_hooks).PackageContext.register_simulator_hooks: now a method with the same keyword arguments, registering into the registry of the context's VUnit object.VsimSimulatorMixinused to readprocess_flagsandrun_envin__init__, before the VUnit object can attach its registry. It now reads them when the persistentvsimprocess is started, through a weak reference so the closure still doesn't keep the interface alive. Without this, persistent ModelSim/Questa and Riviera-PRO processes would have lost their hooks.vunit_packages.rstnow says that creating another VUnit object neither removes a project's hooks nor gives them to the new object.Tests
tests/unit/test_sim_if_hooks.py: the tests use aSimulatorHooksinstance per test instead of the global registry. New tests check that:The persistent-vsim test attaches the hooks after constructing the interface, so it covers the deferred lookup.
tests/unit/test_builtins.py: the mocked VUnit object gets a registry.Locally on Windows:
pytest tests/unit(593 passed) andpytest tests/lint(pycodestyle, pylint, mypy, license) pass, and black is clean.End to end: vunit-python-bridge's feature tests on AlmaLinux 9.6 with Python 3.11 and GHDL 6.0.0 (mcode), with an extra unused
VUnit.from_args(args)afteradd_packagein the run script:cannot load VHPIDIRECT shared libraryWithout the extra object, both pass 108 of 108.
🤖 Generated with Claude Code