From 3c07d8d52cef639dad501e86522db804796e0d6e Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 1 Jan 2019 03:39:12 +0000 Subject: [PATCH] hdl.rec: include record name in error message. --- nmigen/hdl/rec.py | 8 ++++++-- nmigen/test/test_hdl_rec.py | 11 ++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/nmigen/hdl/rec.py b/nmigen/hdl/rec.py index f291087..ef2fcb6 100644 --- a/nmigen/hdl/rec.py +++ b/nmigen/hdl/rec.py @@ -93,8 +93,12 @@ class Record(Value): try: return self.fields[name] except KeyError: - raise NameError("Record does not have a field '{}'. Did you mean one of: {}?" - .format(name, ", ".join(self.fields))) from None + if self.name is None: + reference = "Unnamed record" + else: + reference = "Record '{}'".format(self.name) + raise NameError("{} does not have a field '{}'. Did you mean one of: {}?" + .format(reference, name, ", ".join(self.fields))) from None def shape(self): return sum(len(f) for f in self.fields.values()), False diff --git a/nmigen/test/test_hdl_rec.py b/nmigen/test/test_hdl_rec.py index dd5cb7b..3f68247 100644 --- a/nmigen/test/test_hdl_rec.py +++ b/nmigen/test/test_hdl_rec.py @@ -85,5 +85,14 @@ class RecordTestCase(FHDLTestCase): ("ack", 1), ]) with self.assertRaises(NameError, - msg="Record does not have a field 'en'. Did you mean one of: stb, ack?"): + msg="Record 'r' does not have a field 'en'. Did you mean one of: stb, ack?"): + r.en + + def test_wrong_field_unnamed(self): + r = [Record([ + ("stb", 1), + ("ack", 1), + ])][0] + with self.assertRaises(NameError, + msg="Unnamed record does not have a field 'en'. Did you mean one of: stb, ack?"): r.en -- 2.30.2