From 6c0b71d731b27e66a349e599eacff1398b08e45b Mon Sep 17 00:00:00 2001 From: Catherine Date: Thu, 27 Jun 2024 15:03:31 +0000 Subject: [PATCH] [WIP] [PoC] lib.enum: forward property accesses from enum view to base enum. --- amaranth/lib/enum.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/amaranth/lib/enum.py b/amaranth/lib/enum.py index d6463c3bd..5a228aebf 100644 --- a/amaranth/lib/enum.py +++ b/amaranth/lib/enum.py @@ -1,8 +1,9 @@ +import builtins import enum as py_enum import warnings import operator -from ..hdl import Value, ValueCastable, Shape, ShapeCastable, Const, SyntaxWarning, Format +from ..hdl import Value, ValueCastable, Shape, ShapeCastable, Const, SyntaxWarning, Format, Array __all__ = py_enum.__all__ + ["EnumView", "FlagView"] @@ -309,6 +310,12 @@ def __ne__(self, other): raise TypeError("an EnumView can only be compared to value or other EnumView of the same enum type") return self.target != other.target + def __getattr__(self, name): + if (attr := getattr(self.enum, name, None)) and isinstance(attr, builtins.property): + return Array(attr.fget(self.enum(n)) + for n in range(max(e.value for e in self.enum) + 1))[self.target] + raise AttributeError(f"{self!r} has no attribute {name!r}") + def __repr__(self): return f"{type(self).__qualname__}({self.enum.__qualname__}, {self.target!r})"