From d9daa8abc61ccff70eeaceeebca5afd39d2aae06 Mon Sep 17 00:00:00 2001 From: whitequark Date: Thu, 27 Aug 2020 08:33:48 +0000 Subject: [PATCH] sim.pysim: in write_vcd(), close files if an exception is raised. This also avoids leaving the waveform writer list in an inconsistent state after an exception. --- nmigen/sim/pysim.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/nmigen/sim/pysim.py b/nmigen/sim/pysim.py index 5f30816..847a5a1 100644 --- a/nmigen/sim/pysim.py +++ b/nmigen/sim/pysim.py @@ -402,10 +402,16 @@ class Simulator: Signals to display traces for. """ if self._state.timeline.now != 0.0: + for file in (vcd_file, gtkw_file): + if hasattr(file, "close"): + file.close() raise ValueError("Cannot start writing waveforms after advancing simulation time") + waveform_writer = _VCDWaveformWriter(self._fragment, vcd_file=vcd_file, gtkw_file=gtkw_file, traces=traces) - self._waveform_writers.append(waveform_writer) - yield - waveform_writer.close(self._state.timeline.now) - self._waveform_writers.remove(waveform_writer) + try: + self._waveform_writers.append(waveform_writer) + yield + finally: + waveform_writer.close(self._state.timeline.now) + self._waveform_writers.remove(waveform_writer) -- 2.30.2