lib.data: improve diagnostics for field access on array layout view.

Fixes #837.
This commit is contained in:
Catherine 2023-07-18 14:03:17 +00:00
parent ea36c80663
commit 385b10d743
2 changed files with 18 additions and 0 deletions

View file

@ -662,6 +662,10 @@ class View(ValueCastable):
If ``ShapeCastable.__call__`` does not return a value or a value-castable object.
"""
if isinstance(self.__layout, ArrayLayout):
if not isinstance(key, (int, Value, ValueCastable)):
raise TypeError("Views with array layout may only be indexed with an integer "
"or a value, not {!r}"
.format(key))
shape = self.__layout.elem_shape
value = self.__target.word_select(key, Shape.cast(self.__layout.elem_shape).width)
else:
@ -697,6 +701,9 @@ class View(ValueCastable):
If the layout does not define a field called ``name``, or if ``name`` starts with
an underscore.
"""
if isinstance(self.__layout, ArrayLayout):
raise AttributeError("View of {!r} with an array layout does not have fields"
.format(self.__target))
try:
item = self[name]
except KeyError:

View file

@ -611,6 +611,17 @@ class ViewTestCase(FHDLTestCase):
r"and may only be accessed by indexing$"):
Signal(StructLayout({"_c": signed(1)}))._c
def test_bug_837_array_layout_getitem_str(self):
with self.assertRaisesRegex(TypeError,
r"^Views with array layout may only be indexed with an integer or a value, "
r"not 'reset'$"):
Signal(ArrayLayout(unsigned(1), 1), reset=[0])["reset"]
def test_bug_837_array_layout_getattr(self):
with self.assertRaisesRegex(AttributeError,
r"^View of \(sig \$signal\) with an array layout does not have fields$"):
Signal(ArrayLayout(unsigned(1), 1), reset=[0]).reset
class StructTestCase(FHDLTestCase):
def test_construct(self):