Skip to content

Commit 79a69bd

Browse files
committed
Enable ruff flake8-errmsg checks and apply fixes. This moves exception error messages to an err variable, to make tracebacks more readable:
Before (error message is partially repeated): ``` >>> pyrtl.Simulation() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/lauj/venv/pyrtlnet/lib/python3.12/site-packages/pyrtl/simulation.py", line 126, in __init__ tracer = SimulationTrace() ^^^^^^^^^^^^^^^^^ File "/home/lauj/venv/pyrtlnet/lib/python3.12/site-packages/pyrtl/simulation.py", line 1490, in __init__ raise PyrtlError("There needs to be at least one named non-constant wire " pyrtl.pyrtlexceptions.PyrtlError: There needs to be at least one named non-constant wire for simulation to be useful ``` After (only one copy of the error message): ``` >>> pyrtl.Simulation() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/lauj/projects/pyrtl/pyrtl/simulation.py", line 143, in __init__ tracer = SimulationTrace() ^^^^^^^^^^^^^^^^^ File "/home/lauj/projects/pyrtl/pyrtl/simulation.py", line 1582, in __init__ raise PyrtlError(msg) pyrtl.pyrtlexceptions.PyrtlError: There needs to be at least one named non-constant wire for simulation to be useful ```
1 parent 9a7a4eb commit 79a69bd

29 files changed

Lines changed: 899 additions & 666 deletions

examples/example2-counter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def ripple_add(a, b, carry_in=0):
5252
# interesting features of PyRTL can be seen here: WireVectors can be indexed
5353
# like lists, with [0] accessing the least significant bit and [1:] being an
5454
# example of the use of Python slicing syntax. While you can add two lists
55-
# together in Python, a WireVector + Wirevector means "make an adder", so to
55+
# together in Python, a WireVector + WireVector means "make an adder", so to
5656
# concatenate the bits of two vectors one needs to use "concat". Finally,
5757
# if we look at "carry_in" it seems to have a default value of the integer "0" but
5858
# is a WireVector at other times. Python supports polymorphism throughout

examples/example5-introspection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ def __getattr__(self, name):
2626
try:
2727
return self._pipeline_register_map[self._current_stage_num][name]
2828
except KeyError as exc:
29-
raise pyrtl.PyrtlError(
29+
msg = (
3030
f'error, no pipeline register "{name}" defined for stage '
3131
f"{self._current_stage_num}"
32-
) from exc
32+
)
33+
raise pyrtl.PyrtlError(msg) from exc
3334

3435
def __setattr__(self, name, value):
3536
if name.startswith("_"):

ipynb-examples/example2-counter.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"source": [
120120
"#### A couple interesting features of PyRTL can be seen here:\n",
121121
"* WireVectors can be indexed like lists, with [0] accessing the least significant bit and [1:] being an example of the use of Python slicing syntax.\n",
122-
"* While you can add two lists together in python a WireVector + Wirevector means \"make an adder\" so to concatenate the bits of two vectors one need to use \"concat\".\n",
122+
"* While you can add two lists together in python a WireVector + WireVector means \"make an adder\" so to concatenate the bits of two vectors one need to use \"concat\".\n",
123123
"* If we look at \"cin\" it seems to have a default value of the integer \"0\" but is a WireVector at other times.Python supports polymorphism throughout and PyRTL will cast integers and some other types to WireVectors when it can."
124124
]
125125
},

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ select = [
6969
"B",
7070
# flake8-comprehensions
7171
"C4",
72+
# flake8-errmsg
73+
"EM",
7274
# flake8-future-annotations
7375
"FA",
7476
# flake8-return

pyrtl/analysis.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def stdcell_estimate(net):
8585
return multiplier_stdcell_estimate(len(net.args[0]))
8686
if net.op in "m@":
8787
return 0 # memories handled elsewhere
88-
raise PyrtlInternalError(
89-
"Unable to estimate the following net "
90-
f"due to unimplemented op :\n{str(net)}"
88+
msg = (
89+
"Unable to estimate the following net due to unimplemented op :\n{str(net)}"
9190
)
91+
raise PyrtlInternalError(msg)
9292

9393
block = working_block(block)
9494

@@ -407,10 +407,12 @@ def extract_area_delay_from_yosys_output(yosys_output):
407407
print("---------------------------------------------", file=sys.stderr)
408408
print(str(exc.output).replace("\\n", "\n"), file=sys.stderr)
409409
print("---------------------------------------------", file=sys.stderr)
410-
raise PyrtlError("Yosys call failed") from exc
410+
msg = "Yosys call failed"
411+
raise PyrtlError(msg) from exc
411412
except OSError as exc:
412413
print("Error with call to yosys...", file=sys.stderr)
413-
raise PyrtlError("Call to yosys failed (not installed or on path?)") from exc
414+
msg = "Call to yosys failed (not installed or on path?)"
415+
raise PyrtlError(msg) from exc
414416
finally:
415417
if leave_in_dir is None:
416418
os.remove(temp_path)

pyrtl/compilesim.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ def inspect(self, w: str) -> int:
155155
pass
156156
else:
157157
if not vals:
158-
raise PyrtlError("No context available. Please run a simulation step")
158+
msg = "No context available. Please run a simulation step"
159+
raise PyrtlError(msg)
159160
return vals[-1]
160-
raise PyrtlError(
161-
"CompiledSimulation does not support inspecting internal WireVectors"
162-
)
161+
msg = "CompiledSimulation does not support inspecting internal WireVectors"
162+
raise PyrtlError(msg)
163163

164164
def step(self, provided_inputs: dict[str, int] = None, inputs=None):
165165
if provided_inputs is None:
@@ -186,9 +186,8 @@ def step_multiple(
186186
if provided_inputs is None:
187187
provided_inputs = {}
188188
if not nsteps and len(provided_inputs) == 0:
189-
raise PyrtlError(
190-
"need to supply either input values or a number of steps to simulate"
191-
)
189+
msg = "need to supply either input values or a number of steps to simulate"
190+
raise PyrtlError(msg)
192191

193192
if len(provided_inputs) > 0:
194193
longest = sorted(
@@ -197,26 +196,30 @@ def step_multiple(
197196
msteps = len(longest[1])
198197
if nsteps:
199198
if nsteps > msteps:
200-
raise PyrtlError(
201-
"nsteps is specified but is greater than the "
202-
"number of values supplied for each input"
199+
msg = (
200+
"nsteps is specified but is greater than the number of values "
201+
"supplied for each input"
203202
)
203+
raise PyrtlError(msg)
204204
else:
205205
nsteps = msteps
206206

207207
if nsteps < 1:
208-
raise PyrtlError("must simulate at least one step")
208+
msg = "must simulate at least one step"
209+
raise PyrtlError(msg)
209210

210211
if list(filter(lambda value: len(value) < nsteps, provided_inputs.values())):
211-
raise PyrtlError(
212+
msg = (
212213
"must supply a value for each provided wire for each step of simulation"
213214
)
215+
raise PyrtlError(msg)
214216

215217
if list(filter(lambda value: len(value) < nsteps, expected_outputs.values())):
216-
raise PyrtlError(
217-
"any expected outputs must have a supplied value "
218-
"each step of simulation"
218+
msg = (
219+
"any expected outputs must have a supplied value each step of "
220+
"simulation"
219221
)
222+
raise PyrtlError(msg)
220223

221224
failed = []
222225
for i in range(nsteps):
@@ -302,7 +305,8 @@ def run(self, inputs: list[dict[str, int]]):
302305
start, count = self._inputpos[rname]
303306
buf, sz = ibuf, self._ibufsz
304307
else:
305-
raise PyrtlInternalError("Untraceable wire in tracer")
308+
msg = "Untraceable wire in tracer"
309+
raise PyrtlInternalError(msg)
306310
res = []
307311
for _step in range(steps):
308312
val = 0
@@ -793,9 +797,11 @@ def _create_code(self, write):
793797
mems = {net.op_param[1] for net in self.block.logic_subset("m@")}
794798
for key in self._memmap:
795799
if key not in mems:
796-
raise PyrtlError("unrecognized MemBlock in memory_value_map")
800+
msg = "unrecognized MemBlock in memory_value_map"
801+
raise PyrtlError(msg)
797802
if isinstance(key, RomBlock):
798-
raise PyrtlError("RomBlock in memory_value_map")
803+
msg = "RomBlock in memory_value_map"
804+
raise PyrtlError(msg)
799805
self._declare_mem_helpers(write)
800806
roms = {mem for mem in mems if isinstance(mem, RomBlock)}
801807
self._declare_roms(write, roms)

pyrtl/conditional.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ def _push_condition(predicate):
9292
_check_under_condition()
9393
_depth += 1
9494
if predicate is not otherwise and len(predicate) > 1:
95-
raise PyrtlError(
96-
"all predicates for conditional assignments must be wirevectors of len 1"
97-
)
95+
msg = "all predicates for conditional assignments must be wirevectors of len 1"
96+
raise PyrtlError(msg)
9897
_conditions_list_stack[-1].append(predicate)
9998
_conditions_list_stack.append([])
10099

@@ -126,18 +125,21 @@ def _build_read_port(mem, addr):
126125

127126
def _check_no_nesting():
128127
if _depth != 0:
129-
raise PyrtlError("no nesting of conditional assignments allowed")
128+
msg = "no nesting of conditional assignments allowed"
129+
raise PyrtlError(msg)
130130

131131

132132
def _check_under_condition():
133133
if not currently_under_condition():
134-
raise PyrtlError('conditional assignment "|=" only valid under a condition')
134+
msg = 'conditional assignment "|=" only valid under a condition'
135+
raise PyrtlError(msg)
135136

136137

137138
def _check_and_add_pred_set(lhs, pred_set):
138139
for test_set in _conflicts_map.setdefault(lhs, []):
139140
if _pred_sets_are_in_conflict(pred_set, test_set):
140-
raise PyrtlError(f"conflicting conditions for {lhs}")
141+
msg = f"conflicting conditions for {lhs}"
142+
raise PyrtlError(msg)
141143
_conflicts_map[lhs].append(pred_set)
142144

143145

@@ -188,7 +190,8 @@ def _finalize(defaults):
188190
else:
189191
result = 0 # default for wire is "0"
190192
else:
191-
raise PyrtlInternalError("unknown assignment in finalize")
193+
msg = "unknown assignment in finalize"
194+
raise PyrtlInternalError(msg)
192195
predlist = _predicate_map[lhs]
193196
for p, rhs in predlist:
194197
result = select(p, truecase=rhs, falsecase=result)
@@ -237,9 +240,11 @@ def between_otherwise_and_current(predlist):
237240
pred_set.add((predicate, False))
238241

239242
if select is None:
240-
raise PyrtlError("problem with conditional assignment")
243+
msg = "problem with conditional assignment"
244+
raise PyrtlError(msg)
241245
if len(select) != 1:
242-
raise PyrtlInternalError("conditional predicate with length greater than 1")
246+
msg = "conditional predicate with length greater than 1"
247+
raise PyrtlInternalError(msg)
243248

244249
return select, pred_set
245250

0 commit comments

Comments
 (0)