From: whitequark Date: Thu, 27 Aug 2020 08:33:48 +0000 (+0000) Subject: sim.pysim: in write_vcd(), close files if an exception is raised. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=d9daa8abc61ccff70eeaceeebca5afd39d2aae06;p=nmigen.git 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. --- 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)