Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/functional/builtins/codegen/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,3 +720,34 @@ def foo(bar: {i_typ}) -> {o_typ}:
input_val = decimal_to_int(input_val)
with tx_failed():
c3.foo(input_val)


@pytest.mark.parametrize(
"val",
[
"a000",
"0880",
"deadbeef",
"cafebabe",
"0123456789abcdef",
# test cases that would trigger sign extension
"80",
"8000",
"800000",
"80000000",
"8000000000000000",
"ff",
"ffff",
"ffffffff",
"ffffffffffffffff",
],
)
def test_convert_bytes_literal_int(get_contract, val):
expected = int(val, 16)
source = f"""
@external
def test() -> uint256:
return convert(x'{val}', uint256)
"""
c = get_contract(source)
assert c.test() == expected
15 changes: 15 additions & 0 deletions tests/functional/builtins/folding/test_len.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,18 @@ def foo(a: Bytes[1024]) -> uint256:
new_node = old_node.get_folded_value()

assert contract.foo(value) == new_node.value


def test_len_hexbytes(get_contract):
source = """
@external
def foo(a: Bytes[1024]) -> uint256:
return len(a)
"""

contract = get_contract(source)
vyper_ast = parse_and_fold("len(x'0000')")
old_node = vyper_ast.body[0].value
new_node = old_node.get_folded_value()

assert contract.foo("0x0000") == new_node.value
7 changes: 4 additions & 3 deletions vyper/builtins/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ def _literal_int(expr, arg_typ, out_typ):
# TODO: possible to reuse machinery from expr.py?
if isinstance(expr, vy_ast.Hex):
val = int(expr.value, 16)
elif isinstance(expr, vy_ast.Bytes):
elif isinstance(expr, (vy_ast.Bytes, vy_ast.HexBytes)):
val = int.from_bytes(expr.value, "big")
elif isinstance(expr, (vy_ast.Int, vy_ast.Decimal, vy_ast.NameConstant)):
val = expr.value
else: # pragma: no cover
raise CompilerPanic("unreachable")

if isinstance(expr, (vy_ast.Hex, vy_ast.Bytes)) and out_typ.is_signed:
if isinstance(expr, (vy_ast.Hex, vy_ast.Bytes, vy_ast.HexBytes)) and out_typ.is_signed:
val = _signextend(expr, val, arg_typ)

lo, hi = out_typ.int_bounds
Expand All @@ -244,6 +244,7 @@ def _literal_decimal(expr, arg_typ, out_typ):
val = decimal.Decimal(int(expr.value, 16))
else:
val = decimal.Decimal(expr.value) # should work for Int, Decimal
assert isinstance(expr.value, int)
val *= DECIMAL_DIVISOR

# sanity check type checker did its job
Expand All @@ -252,7 +253,7 @@ def _literal_decimal(expr, arg_typ, out_typ):
val = int(val)

# apply sign extension, if expected
if isinstance(expr, (vy_ast.Hex, vy_ast.Bytes)) and out_typ.is_signed:
if isinstance(expr, vy_ast.Hex) and out_typ.is_signed:
val = _signextend(expr, val, arg_typ)

lo, hi = out_typ.int_bounds
Expand Down
2 changes: 1 addition & 1 deletion vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ class Len(BuiltinFunctionT):
def _try_fold(self, node):
validate_call_args(node, 1)
arg = node.args[0].get_folded_value()
if isinstance(arg, (vy_ast.Str, vy_ast.Bytes)):
if isinstance(arg, (vy_ast.Str, vy_ast.Bytes, vy_ast.HexBytes)):
length = len(arg.value)
elif isinstance(arg, vy_ast.Hex):
length = len(arg.bytes_value)
Expand Down