diff --git a/hexbytes/_utils.py b/hexbytes/_utils.py index bc99e14..f4e3388 100644 --- a/hexbytes/_utils.py +++ b/hexbytes/_utils.py @@ -43,7 +43,7 @@ def hexstr_to_bytes(hexstr: str) -> bytes: try: ascii_hex = padded_hex.encode("ascii") - except UnicodeDecodeError as err: + except UnicodeEncodeError as err: raise ValueError( f"hex string {padded_hex} may only contain [0-9a-fA-F] characters" ) from err diff --git a/tests/core/test_hexbytes.py b/tests/core/test_hexbytes.py index 36cf1f6..80d0cb4 100644 --- a/tests/core/test_hexbytes.py +++ b/tests/core/test_hexbytes.py @@ -73,6 +73,13 @@ def test_invalid_integer_inputs(integer): assert str(integer) in message +def test_invalid_non_ascii_hexstr(): + with pytest.raises(ValueError) as exc_info: + HexBytes("0x0é") + + assert "may only contain [0-9a-fA-F] characters" in str(exc_info.value) + + @given(st.integers(min_value=0)) def test_integer_inputs(integer): wrapped = HexBytes(integer)