From 073d3afc5f26e9925e807ee4687aaaeb43fc6196 Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 3 Jun 2019 07:16:09 +0000 Subject: [PATCH] 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(). --- nmigen/hdl/rec.py | 4 ++-- nmigen/test/test_hdl_rec.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) 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 -- 2.30.2