Skip to content

Commit

Permalink
Merge pull request #259 from pymtl/pp482-error-msg-slicing-non-bits
Browse files Browse the repository at this point in the history
Use better error message when slicing a Bitstruct signal
  • Loading branch information
ptpan authored Dec 6, 2023
2 parents 9ee08a5 + 0edc9a7 commit 14d2282
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pymtl3/dsl/Connectable.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ def __setitem__( s, idx, v ):

def __getitem__( s, idx ):
if not issubclass( s._dsl.Type, Bits ):
raise InvalidConnectionError( "We don't allow slicing on non-Bits signals." )
host = s.get_host_component()
name = s.get_field_name()
type_name = s._dsl.Type.__name__
raise InvalidConnectionError( f"Slicing on Bitstruct signal ({name} of {type_name} in {str(host)}) is not allowed!" )

# Turn index into a slice
if isinstance( idx, int ):
Expand Down
24 changes: 23 additions & 1 deletion pymtl3/dsl/test/DataStruct_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
Author : Shunning Jiang
Date : Apr 16, 2018
"""
from pymtl3.datatypes import Bits16, Bits32, bitstruct
from pymtl3.datatypes import Bits16, Bits32, Bits64, bitstruct
from pymtl3.dsl.ComponentLevel1 import update
from pymtl3.dsl.ComponentLevel2 import update_ff
from pymtl3.dsl.ComponentLevel3 import ComponentLevel3, connect
from pymtl3.dsl.Connectable import InPort, OutPort, Wire
from pymtl3.dsl.errors import (
InvalidConnectionError,
MultiWriterError,
NoWriterError,
UpdateFFBlockWriteError,
Expand Down Expand Up @@ -581,3 +582,24 @@ def ffs():
print("{} is thrown\n{}".format( e.__class__.__name__, e ))
return
raise Exception("Should've thrown UpdateFFNonTopLevelSignalError.")

def test_slicing_on_non_bits_error_msg():

class Bitstruct2Bits( ComponentLevel3 ):
def construct( s ):
s.pt_bitstruct = InPort ( SomeMsg )
s.pt_bits = OutPort( Bits64 )
@update
def upblk():
s.pt_bits @= s.pt_bitstruct[0:64]

Check warning on line 594 in pymtl3/dsl/test/DataStruct_test.py

View check run for this annotation

Codecov / codecov/patch

pymtl3/dsl/test/DataStruct_test.py#L594

Added line #L594 was not covered by tests

m = Bitstruct2Bits()
try:
m.elaborate()
except InvalidConnectionError as e:
err_msg = str(e)
print("{} is thrown\n{}".format( e.__class__.__name__, err_msg ))
assert "SomeMsg" in err_msg
assert "pt_bitstruct" in err_msg
return
raise Exception("Should've thrown InvalidConnectionError.")

Check warning on line 605 in pymtl3/dsl/test/DataStruct_test.py

View check run for this annotation

Codecov / codecov/patch

pymtl3/dsl/test/DataStruct_test.py#L605

Added line #L605 was not covered by tests

0 comments on commit 14d2282

Please sign in to comment.