Signal.__getitem__: raise TypeError and IndexError when appropriate
authorRobert Jordens <jordens@gmail.com>
Sun, 17 Aug 2014 20:56:31 +0000 (14:56 -0600)
committerSebastien Bourdeauducq <sb@m-labs.hk>
Mon, 18 Aug 2014 03:01:56 +0000 (11:01 +0800)
migen/fhdl/structure.py

index 3edc2860867e8950e9cd042687bba38acd01e30e..ba84dd85b85935d7d254b7e05e9dd29472564bf2 100644 (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