Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not guess if a string is already hex encoded #216

Merged
merged 3 commits into from
Aug 2, 2017
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
4 changes: 3 additions & 1 deletion tests/contracts/test_normalization_of_return_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
'0xbb9bc244d798123fde783fcc1c72d3bb8c189413',
],
),
)
),
ids=['nullbyte', 'soloaddr', 'addrlist']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming this is the fix for pytest 3.2, I think I'd rather just pin the dependency to pytest<3.2.0 with a comment linking to the issue in the pytest repo.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it fixes the pytest issue, but it also makes the pytest output somewhat more readable:

tests/contracts/test_normalization_of_return_types.py::test_normalizing_return_values[nullbyte] PASSED
tests/contracts/test_normalization_of_return_types.py::test_normalizing_return_values[soloaddr] PASSED
tests/contracts/test_normalization_of_return_types.py::test_normalizing_return_values[addrlist] PASSED

But I'm only mildly in favor of this approach. If you are more worried about consistency between tests than test output readability for this test, I'm happy to go the version pinning route.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm convinced. feel free to leave this in.


)
def test_normalizing_return_values(data_type, data_value, expected_value):
actual_value = normalize_return_type(data_type, data_value)
Expand Down
5 changes: 5 additions & 0 deletions tests/utilities/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from web3.utils.encoding import (
from_decimal,
to_decimal,
to_hex,
)


Expand Down Expand Up @@ -40,3 +41,7 @@ def test_conversion_rount_trip(value):
intermediate_value = from_decimal(value)
result_value = to_decimal(intermediate_value)
assert result_value == value, "Expected: {0!r}, Result: {1!r}, Intermediate: {2!r}".format(value, result_value, intermediate_value)

def test_bytes_that_start_with_0x():
sneaky_bytes = b'0x\xde\xad'
assert to_hex(sneaky_bytes) == '0x3078dead'
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ envlist=

[flake8]
max-line-length= 100
exclude= tests/*,venv
exclude= tests,venv,.tox,docs,build

[testenv]
usedevelop=True
Expand Down
7 changes: 1 addition & 6 deletions web3/utils/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ def to_hex(value):
return encode_hex(json.dumps(value, sort_keys=True))

if is_string(value):
if is_prefixed(value, '-0x'):
return from_decimal(value)
elif is_0x_prefixed(value):
return value
else:
return encode_hex(value)
return encode_hex(value)

if is_integer(value):
return from_decimal(value)
Expand Down
2 changes: 1 addition & 1 deletion web3/utils/exception_py2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def raise_from(my_exception, other_exception):
raise my_exception, None, sys.exc_info()[2] # noqa: W602, E999
raise my_exception, None, sys.exc_info()[2] # noqa: ignore=W602, E999