Skip to content

Add Python dict support using VUnit dict_t - #11

Merged
ru551n merged 7 commits into
VUnit:mainfrom
ru551n:python-dict-support
Oct 16, 2026
Merged

ru551n merged 7 commits into
VUnit:mainfrom
ru551n:python-dict-support

Conversation

@ru551n

@ru551n ru551n commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Requires VUnit 5.0.0.dev15 (vunit_hdl>=5.0.0.dev15), the first release with get_key and get_value_type of dict_pkg (VUnit/vunit#1229).

Motivation

Python code naturally exchanges configuration and results as dicts. VUnit's dict_t (string keys, mixed-type values) is the VHDL counterpart, but the bridge could not pass one in either direction.

API

  • eval_dict / call_dict return a new dict_t (aliased as eval / call; no ambiguity with the existing eval/call overloads). The caller owns and deallocates it. Values: int -> integer, float -> real, str -> string, bool -> boolean, list of int -> integer_vector_ptr_t ref, NumPy array of integers -> integer_array_t ref (bit width and signedness as for eval_integer_array: from the dtype, or kept from an array VHDL passed in), nested dict -> nested dict_t ref. None, out-of-range ints, non-str keys and other values are errors; after a reported failure an empty dict is returned. Python converts and checks the whole dict, then VHDL fetches each value over the typed results of the bridge (binary integers, reals and arrays; a nested dict by its handle), so there is no text parsing.
  • arg(dict_t) / kwarg(kw, dict_t) write a Python literal {"key": value, ...} using only the public dict_pkg API (num_keys, get_key, get_value_type, getters). Values: integer, real, string, boolean, std_ulogic, integer_vector, real_vector, integer_vector_ptr_t, integer_array_t (becomes a NumPy array), nested dict. Other types fail like other unconvertible arguments. An integer_array_t value is transferred like an integer_array_t argument.

Python side: KIND_DICT and _dict_result in runtime.py serialise the dict into the existing byte-result path (no native changes). The generated python_pkg.vhd is regenerated from the template.

Tests

New cases in tests/tb_python_pkg_bridge.vhd: every value type and nested dicts, exact reals, error cases, arg/kwarg, a round trip VHDL dict -> Python -> VHDL, an unconvertible value, and a group & with a dict kwarg. Full suite: 113 of 113 pass on NVC and on GHDL, pytest tests/test_python_bridge.py 85 passed.

Notes: tests do not deallocate multi-key dicts because new_dict after deallocate of a grown dict fails in VUnit's dict_pkg (recycled pool arrays are longer than requested).

🤖 Generated with Claude Code

@ru551n

ru551n commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author

Added a test that deallocates returned multi-key dicts. It needs the new_dict fix now included in VUnit/vunit#1229.

ru551n and others added 4 commits October 16, 2026 14:11
eval_dict/call_dict (aliased eval/call) return a new dict_t from a Python
dict with str keys: int, float, str, bool, list of int and nested dict
values. arg/kwarg accept a dict_t and write it as a Python dict literal.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Needs the new_dict fix of VUnit/vunit#1229.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The dict_t support needs get_key and get_value_type of dict_pkg, which
VUnit 5.0.0.dev15 is the first release to have (VUnit/vunit#1229).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ru551n
ru551n force-pushed the python-dict-support branch from d13aff3 to 9b6b9c5 Compare October 16, 2026 12:12
VUnit's dict_pkg stores integer_array_t refs, so the bridge converts them
in both directions. In a dict_t argument the array is transferred like an
integer_array_t argument and becomes a NumPy array. A NumPy array in a
returned dict becomes an integer_array_t with the word size of an
eval_integer_array result: from the dtype, or that of the array VHDL
passed in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread docs/types.rst Outdated
Comment thread docs/types.rst Outdated
Comment thread src/vunit_python_bridge/hdl/src/python_pkg.vhd Outdated
Which simulators the Python bridge supports is documented for the whole
package.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A dict result was one string of text records that VHDL parsed, with reals
written as an exact mantissa and exponent and arrays as comma separated
integers. Python now converts the dict and keeps its entries, and VHDL
fetches each value with the eval of its type: integer, real, boolean,
string, integer_vector_ptr_t and integer_array_t over their binary results,
a nested dict by its handle. The text parsing functions are gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ru551n
ru551n merged commit 743373c into VUnit:main Oct 16, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant