From: whitequark Date: Mon, 3 Jun 2019 07:16:09 +0000 (+0000) Subject: hdl.rec: unbreak hasattr(rec, ...). X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=073d3afc5f26e9925e807ee4687aaaeb43fc6196;p=nmigen.git hdl.rec: unbreak hasattr(rec, ...). hasattr() requires that AttributeError be raised. Change __getitem__ to raise AttributeError, too, since it is fundamentally just sugar for getattr(). --- diff --git a/nmigen/hdl/rec.py b/nmigen/hdl/rec.py index 7abd6ff..26b26f7 100644 --- a/nmigen/hdl/rec.py +++ b/nmigen/hdl/rec.py @@ -109,8 +109,8 @@ class Record(Value): reference = "Unnamed record" else: reference = "Record '{}'".format(self.name) - raise NameError("{} does not have a field '{}'. Did you mean one of: {}?" - .format(reference, item, ", ".join(self.fields))) from None + raise AttributeError("{} does not have a field '{}'. Did you mean one of: {}?" + .format(reference, item, ", ".join(self.fields))) from None else: return super().__getitem__(item) diff --git a/nmigen/test/test_hdl_rec.py b/nmigen/test/test_hdl_rec.py index ba1600c..fc1959f 100644 --- a/nmigen/test/test_hdl_rec.py +++ b/nmigen/test/test_hdl_rec.py @@ -71,6 +71,9 @@ class RecordTestCase(FHDLTestCase): self.assertEqual(r.stb.name, "r__stb") self.assertEqual(r["stb"].name, "r__stb") + self.assertTrue(hasattr(r, "stb")) + self.assertFalse(hasattr(r, "xxx")) + def test_unnamed(self): r = [Record([ ("stb", 1) @@ -93,7 +96,10 @@ class RecordTestCase(FHDLTestCase): ("stb", 1), ("ack", 1), ]) - with self.assertRaises(NameError, + with self.assertRaises(AttributeError, + msg="Record 'r' does not have a field 'en'. Did you mean one of: stb, ack?"): + r["en"] + with self.assertRaises(AttributeError, msg="Record 'r' does not have a field 'en'. Did you mean one of: stb, ack?"): r.en @@ -102,7 +108,7 @@ class RecordTestCase(FHDLTestCase): ("stb", 1), ("ack", 1), ])][0] - with self.assertRaises(NameError, + with self.assertRaises(AttributeError, msg="Unnamed record does not have a field 'en'. Did you mean one of: stb, ack?"): r.en