Skip to content

Commit

Permalink
Merge pull request #471 from jasonrhaas/python-moderization-2
Browse files Browse the repository at this point in the history
Python modernization 2
  • Loading branch information
carver authored Nov 29, 2017
2 parents 7b14f83 + 4c52f79 commit 29085d0
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 50 deletions.
3 changes: 1 addition & 2 deletions tests/core/contracts/test_concise_contract.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from unittest.mock import Mock

from eth_utils import (
decode_hex,
Expand All @@ -10,8 +11,6 @@
ConciseMethod,
)

from unittest.mock import Mock


@pytest.fixture()
def EMPTY_ADDR():
Expand Down
11 changes: 8 additions & 3 deletions tests/core/eth-module/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def test_eth_account_recover_message(acct):
b'\x0cu0\x84\xe5\xa8)\x02\x192L\x1a:\x86\xd4\x06M\xed-\x15\x97\x9b\x1e\xa7\x90sJ\xaa,\xea\xaf\xc1"\x9c\xa4S\x81\x06\x81\x9f\xd3\xa5P\x9d\xd3\x83\xe8\xfeKs\x1chp3\x95V\xa5\xc0o\xeb\x9c\xf30\xbb\x00', # noqa: E501
# test signature bytes with chain-naive v (27 in this case)
b'\x0cu0\x84\xe5\xa8)\x02\x192L\x1a:\x86\xd4\x06M\xed-\x15\x97\x9b\x1e\xa7\x90sJ\xaa,\xea\xaf\xc1"\x9c\xa4S\x81\x06\x81\x9f\xd3\xa5P\x9d\xd3\x83\xe8\xfeKs\x1chp3\x95V\xa5\xc0o\xeb\x9c\xf30\xbb\x1b', # noqa: E501
]
],
ids=['test_sig_bytes_standard_v', 'test_sig_bytes_chain_naive_v']
)
def test_eth_account_recover_signature_bytes(acct, signature_bytes):
msg_hash = b'\xbb\r\x8a\xba\x9f\xf7\xa1<N,s{i\x81\x86r\x83{\xba\x9f\xe2\x1d\xaa\xdd\xb3\xd6\x01\xda\x00\xb7)\xa1' # noqa: E501
Expand Down Expand Up @@ -201,7 +202,8 @@ def test_eth_account_recover_vrs_standard_v(acct):
'Hello World',
HexBytes('0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2'),
),
]
],
ids=['message_to_sign', 'hexstr_as_text', 'hello_world']
)
def test_eth_account_hash_message_text(acct, message, expected):
assert acct.hashMessage(text=message) == expected
Expand All @@ -218,7 +220,8 @@ def test_eth_account_hash_message_text(acct, message, expected):
'0x29d9f7d6a1d1e62152f314f04e6bd4300ad56fd72102b6b83702869a089f470c',
HexBytes('0xe709159ef0e6323c705786fc50e47a8143812e9f82f429e585034777c7bf530b'),
),
]
],
ids=['hexbytes_1', 'hexbytes_2']
)
def test_eth_account_hash_message_hexstr(acct, message, expected):
assert acct.hashMessage(hexstr=message) == expected
Expand All @@ -238,6 +241,7 @@ def test_eth_account_hash_message_hexstr(acct, message, expected):
HexBytes('0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c'), # noqa: E501
),
),
ids=['account_1']
)
def test_eth_account_sign(acct, message, key, expected_bytes, expected_hash, v, r, s, signature):
signed = acct.sign(message_text=message, private_key=key)
Expand Down Expand Up @@ -272,6 +276,7 @@ def test_eth_account_sign(acct, message, key, expected_bytes, expected_hash, v,
37,
),
),
ids=['account_1']
)
def test_eth_account_sign_transaction(acct, txn, private_key, expected_raw_tx, tx_hash, r, s, v):
signed = acct.signTransaction(txn, private_key)
Expand Down
1 change: 0 additions & 1 deletion tests/core/eth-module/test_eth_contract.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest

from unittest.mock import Mock


Expand Down
3 changes: 1 addition & 2 deletions tests/core/middleware/test_stalecheck.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import pytest
from unittest.mock import Mock, patch

from web3.middleware import make_stalecheck_middleware
from web3.middleware.stalecheck import (
Expand All @@ -8,8 +9,6 @@
)
from web3.utils.datastructures import AttributeDict

from unittest.mock import Mock, patch


@pytest.fixture
def now():
Expand Down
22 changes: 11 additions & 11 deletions tests/core/web3-module/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
(256, b'\x01\x00'),
(True, b'\x01'),
(False, b'\x00'),
)
),
)
def test_to_bytes_primitive(val, expected):
assert Web3.toBytes(val) == expected
Expand All @@ -36,7 +36,7 @@ def test_to_bytes_primitive(val, expected):
('0x100', b'\x01\x00'),
('0x0000', b'\x00\x00'),
('0000', b'\x00\x00'),
)
),
)
def test_to_bytes_hexstr(val, expected):
assert Web3.toBytes(hexstr=val) == expected
Expand All @@ -47,7 +47,7 @@ def test_to_bytes_hexstr(val, expected):
(
('cowmö', b'cowm\xc3\xb6'),
('', b''),
)
),
)
def test_to_bytes_text(val, expected):
assert Web3.toBytes(text=val) == expected
Expand All @@ -66,7 +66,7 @@ def test_to_text_identity():
('0x636f776dc3b6', 'cowmö'),
(0x636f776dc3b6, 'cowmö'),
('0xa', '\n'),
)
),
)
def test_to_text(val, expected):
assert Web3.toText(val) == expected
Expand All @@ -79,7 +79,7 @@ def test_to_text(val, expected):
('0xa', '\n'),
('0x636f776dc3b6', 'cowmö'),
('636f776dc3b6', 'cowmö'),
)
),
)
def test_to_text_hexstr(val, expected):
assert Web3.toText(hexstr=val) == expected
Expand All @@ -98,7 +98,7 @@ def test_to_text_hexstr(val, expected):
('-1', TypeError),
('0x0', TypeError),
('0x1', TypeError),
)
),
)
def test_to_int(val, expected):
if isinstance(expected, type):
Expand All @@ -118,7 +118,7 @@ def test_to_int(val, expected):
('0x1', ValueError),
('1.1', ValueError),
('a', ValueError),
)
),
)
def test_to_int_text(val, expected):
if isinstance(expected, type):
Expand All @@ -139,7 +139,7 @@ def test_to_int_text(val, expected):
('1', 1),
('01', 1),
('10', 16),
)
),
)
def test_to_int_hexstr(val, expected):
assert Web3.toInt(hexstr=val) == expected
Expand All @@ -162,7 +162,7 @@ def test_to_int_hexstr(val, expected):
(0x0F, '0xf'),
(False, '0x0'),
(True, '0x1'),
)
),
)
def test_to_hex(val, expected):
assert Web3.toHex(val) == expected
Expand All @@ -173,7 +173,7 @@ def test_to_hex(val, expected):
(
('', '0x'),
('cowmö', '0x636f776dc3b6'),
)
),
)
def test_to_hex_text(val, expected):
assert Web3.toHex(text=val) == expected
Expand All @@ -188,7 +188,7 @@ def test_to_hex_text(val, expected):
('0x10', '0x10'),
('0xF', '0xf'),
('F', '0xf'),
)
),
)
def test_to_hex_cleanup_only(val, expected):
assert Web3.toHex(hexstr=val) == expected
10 changes: 5 additions & 5 deletions tests/core/web3-module/test_sha3.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[
('cowmö', HexBytes('0x0f355f04c0a06eebac1d219b34c598f85a1169badee164be8a30345944885fe8')),
('', HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')),
]
],
)
def test_sha3_text(message, digest):
assert Web3.sha3(text=message) == digest
Expand All @@ -36,7 +36,7 @@ def test_sha3_text(message, digest):
'',
HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
),
]
],
)
def test_sha3_hexstr(hexstr, digest):
assert Web3.sha3(hexstr=hexstr) == digest
Expand All @@ -48,7 +48,7 @@ def test_sha3_hexstr(hexstr, digest):
('0x0', TypeError),
('', TypeError),
(-1, ValueError),
]
],
)
def test_sha3_primitive_invalid(primitive, exception):
with pytest.raises(exception):
Expand All @@ -66,7 +66,7 @@ def test_sha3_primitive_invalid(primitive, exception):
b'',
HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
),
]
],
)
def test_sha3_primitive(primitive, digest):
assert Web3.sha3(primitive) == digest
Expand All @@ -78,7 +78,7 @@ def test_sha3_primitive(primitive, digest):
{'text': ''},
{'hexstr': '0x'},
{'text': '', 'hexstr': '0x'},
]
],
)
def test_sha3_raise_if_primitive_and(kwargs):
# must not set more than one input
Expand Down
5 changes: 0 additions & 5 deletions web3/account.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from collections import (
Mapping,
)
Expand Down Expand Up @@ -135,8 +134,6 @@ def setKeyBackend(self, backend):
def sign(self, message=None, private_key=None, message_hexstr=None, message_text=None):
'''
@param private_key in bytes, str, or int.
In Python 2, a bytes, unicode or str object will be interpreted as hexstr
In Python 3, only a str object will be interpreted as hexstr
'''
msg_bytes = to_bytes(message, hexstr=message_hexstr, text=message_text)
msg_hash = self.hashMessage(msg_bytes)
Expand All @@ -156,8 +153,6 @@ def sign(self, message=None, private_key=None, message_hexstr=None, message_text
def signTransaction(self, transaction_dict, private_key):
'''
@param private_key in bytes, str, or int.
In Python 2, a bytes, unicode or str object will be interpreted as hexstr
In Python 3, only a str object will be interpreted as hexstr
'''
assert isinstance(transaction_dict, Mapping)

Expand Down
1 change: 0 additions & 1 deletion web3/middleware/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import absolute_import
import functools

from .abi import ( # noqa: F401
Expand Down
5 changes: 0 additions & 5 deletions web3/providers/eth_tester/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
from __future__ import (
absolute_import,
unicode_literals,
)

import operator
import random
import sys
Expand Down
17 changes: 3 additions & 14 deletions web3/utils/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ def to_hex(value=None, hexstr=None, text=None):
return to_hex(text=value)

if is_integer(value):
# python2 longs end up with an `L` hanging off the end of their hexidecimal
# representation.
return hex(value).rstrip('L')
return hex(value)

raise TypeError(
"Unsupported type: '{0}'. Must be one of Boolean, Dictionary, String, "
Expand Down Expand Up @@ -193,7 +191,6 @@ def to_bytes(primitive=None, hexstr=None, text=None):
elif is_integer(primitive):
return to_bytes(hexstr=to_hex(primitive))
elif hexstr is not None:
hexstr = hexstr.rstrip('L') # handle longs in Python 2
if len(hexstr) % 2:
hexstr = '0x0' + remove_0x_prefix(hexstr)
return decode_hex(hexstr)
Expand All @@ -203,10 +200,6 @@ def to_bytes(primitive=None, hexstr=None, text=None):


def to_text(primitive=None, hexstr=None, text=None):
if bytes is str:
# must be able to tell the difference between bytes and a hexstr
raise NotImplementedError("This method only works in Python 3+.")

assert_one_val(primitive, hexstr=hexstr, text=text)

if hexstr is not None:
Expand All @@ -230,9 +223,7 @@ def text_if_str(to_type, text_or_primitive):
@param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text),
eg~ to_bytes, to_text, to_hex, to_int, etc
@param hexstr_or_primitive in bytes, str, or int. (or unicode in py2)
In Python 2, only a unicode object will be interpreted as unicode text
In Python 3, only a str object will be interpreted as interpreted as unicode text
@param hexstr_or_primitive in bytes, str, or int.
'''
if isinstance(text_or_primitive, str):
(primitive, text) = (None, text_or_primitive)
Expand All @@ -248,9 +239,7 @@ def hexstr_if_str(to_type, hexstr_or_primitive):
@param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text),
eg~ to_bytes, to_text, to_hex, to_int, etc
@param text_or_primitive in bytes, str, or int. (or unicode in py2)
In Python 2, a bytes, unicode or str object will be interpreted as hexstr
In Python 3, only a str object will be interpreted as hexstr
@param text_or_primitive in bytes, str, or int.
'''
if isinstance(hexstr_or_primitive, str):
(primitive, hexstr) = (None, hexstr_or_primitive)
Expand Down
1 change: 0 additions & 1 deletion web3/utils/signing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from web3.utils.encoding import (
to_bytes,
to_int,
Expand Down

0 comments on commit 29085d0

Please sign in to comment.