hdl.rec: unbreak hasattr(rec, ...).
authorwhitequark <whitequark@whitequark.org>
Mon, 3 Jun 2019 07:16:09 +0000 (07:16 +0000)
committerwhitequark <whitequark@whitequark.org>
Mon, 3 Jun 2019 07:43:31 +0000 (07:43 +0000)
hasattr() requires that AttributeError be raised. Change __getitem__
to raise AttributeError, too, since it is fundamentally just sugar
for getattr().

nmigen/hdl/rec.py
nmigen/test/test_hdl_rec.py

index 7abd6ff85cbe9f43464044a841ed63e0c245f3d6..26b26f7ca451acc1431e138defbb50efaeb7536f 100644 (file)
@@ -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)
 
index ba1600c5443cfd393096701636409f10c2795dec..fc1959fc1d913109c480442fb9f01a641cd877cf 100644 (file)
@@ -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