Signal.__getitem__: raise TypeError and IndexError when appropriate

This commit is contained in:
Robert Jordens 2014-08-17 14:56:31 -06:00 committed by Sebastien Bourdeauducq
parent b3d69913cd
commit 6036fffef2

View file

@ -81,17 +81,20 @@ class Value(HUID):
def __getitem__(self, key):
from migen.fhdl.bitcontainer import flen
n = flen(self)
if isinstance(key, int):
if key >= n:
raise IndexError
if key < 0:
key += flen(self)
key += n
return _Slice(self, key, key+1)
elif isinstance(key, slice):
start, stop, step = key.indices(flen(self))
start, stop, step = key.indices(n)
if step != 1:
return Cat(self[i] for i in range(start, stop, step))
return _Slice(self, start, stop)
else:
raise KeyError
raise TypeError
def eq(self, r):
"""Assignment