Skip to content

Throw an exception if the user tries to render a trace without stepping the simulation#472

Merged
fdxmw merged 2 commits into
UCSBarchlab:developmentfrom
gaborszita:sim-trace-error-no-step
May 26, 2026
Merged

Throw an exception if the user tries to render a trace without stepping the simulation#472
fdxmw merged 2 commits into
UCSBarchlab:developmentfrom
gaborszita:sim-trace-error-no-step

Conversation

@gaborszita
Copy link
Copy Markdown
Contributor

When the user tries to render a trace without stepping the simulation, PyRTL gives this weird error:

gabi@Gabors-MacBook-Pro PyRTL % python3 test.py
Traceback (most recent call last):
  File "/Users/gabi/Documents/PyRTL/test.py", line 8, in <module>
    sim.tracer.render_trace()
    ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/gabi/Documents/PyRTL/pyrtl/simulation.py", line 1787, in render_trace
    self.render_trace_to_text(
    ~~~~~~~~~~~~~~~~~~~~~~~~~^
        trace_list=trace_list,
        ^^^^^^^^^^^^^^^^^^^^^^
    ...<5 lines>...
        segment_size=segment_size,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/gabi/Documents/PyRTL/pyrtl/simulation.py", line 1876, in render_trace_to_text
    current_symbol_len = max(
        len(
    ...<4 lines>...
        for v in trace
    )
ValueError: max() iterable argument is empty

test.py:

import pyrtl

a = pyrtl.Register(name="test_reg", bitwidth=3)
a.next <<= a+1

sim = pyrtl.Simulation()

sim.tracer.render_trace()

Output after this PR:

gabi@Gabors-MacBook-Pro PyRTL % python3 test.py
Traceback (most recent call last):
  File "/Users/gabi/Documents/PyRTL/test.py", line 8, in <module>
    sim.tracer.render_trace()
    ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/gabi/Documents/PyRTL/pyrtl/simulation.py", line 1760, in render_trace
    raise PyrtlError("You need to step the simulation at least once to render a trace.")
pyrtl.pyrtlexceptions.PyrtlError: You need to step the simulation at least once to render a trace.

I was working on pyrtlnet and it took me around 10 minutes to figure out why I'm getting this error so I decided to open this little PR 😅

@gaborszita
Copy link
Copy Markdown
Contributor Author

Seems a CompileSim test is failing, here's why: render_trace_to_text throws an exception if we are not tracing any signals and a there is a check for this exception in the CompileSim tests. However, __len__ does the same check, but it just throws the exception with a different message, and since with this change __len__ is now called before render_trace_to_text, __len__'s exception is thrown:

image

The two error messages are basically the same thing just described differently, so there is no logic error in the code. There are multiple ways to solve this problem, the simplest is just to copy the error message from render_trace_to_text to __len__. @fdxmw what do you suggest we do?

@fdxmw
Copy link
Copy Markdown
Member

fdxmw commented Sep 8, 2025

Thank you for improving this!

The two error messages are basically the same thing just described differently, so there is no logic error in the code. There are multiple ways to solve this problem, the simplest is just to copy the error message from render_trace_to_text to len. @fdxmw what do you suggest we do?

This is why I'm generally not a fan of tests that check error messages :) These tests are too sensitive, and often fail when the code's behavior is still correct. I'd be okay with just removing the assertEqual that checks the error message; I think it's sufficient to just check that PyrtlError was thrown.

Please also run ruff check and ruff format on your change. Some minor readability improvements will be needed before we can merge it. In the future, please run tox to test your changes before you open a pull request; tox will also run ruff for you.

In particular, try not to rely on GitHub's pull request actions to run tests for you, because opening a pull request triggers notifications for people watching the repository, which creates unnecessary noise.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 26, 2026

Codecov Report

❌ Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.9%. Comparing base (97f29c0) to head (ee9e9ff).

Files with missing lines Patch % Lines
pyrtl/simulation.py 33.4% 2 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##           development    #472     +/-   ##
=============================================
- Coverage         91.9%   91.9%   -0.0%     
=============================================
  Files               30      30             
  Lines             7440    7443      +3     
=============================================
+ Hits              6836    6837      +1     
- Misses             604     606      +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fdxmw fdxmw force-pushed the sim-trace-error-no-step branch from ad114ad to ee9e9ff Compare May 26, 2026 23:54
@fdxmw fdxmw merged commit 96bac4b into UCSBarchlab:development May 26, 2026
4 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.

2 participants