-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Python modernization 2 #471
Changes from 2 commits
4fadc90
b8dd981
71feb6c
78ee0f4
77e6df5
0d43fa4
3b9eb40
deaf5bd
4958b1a
4c52f79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
) | ||
import json | ||
import os | ||
import sys | ||
|
||
from cytoolz import ( | ||
compose, | ||
|
@@ -36,8 +35,7 @@ | |
hexstr_if_str, | ||
text_if_str, | ||
to_bytes, | ||
to_int, | ||
to_hex, | ||
to_int | ||
) | ||
from web3.utils.signing import ( | ||
LocalAccount, | ||
|
@@ -60,14 +58,11 @@ class Account(object): | |
def create(self, extra_entropy=''): | ||
extra_key_bytes = text_if_str(to_bytes, extra_entropy) | ||
key_bytes = keccak(os.urandom(32) + extra_key_bytes) | ||
if sys.version_info.major < 3: | ||
key_bytes = to_hex(key_bytes) | ||
return self.privateKeyToAccount(key_bytes) | ||
|
||
@staticmethod | ||
def decrypt(keyfile_json, password): | ||
if isinstance(keyfile_json, str) or ( | ||
sys.version_info.major < 3 and isinstance(keyfile_json, unicode)): # noqa: 821 | ||
if isinstance(keyfile_json, str): # noqa: 821 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @techtonik that was already in the code so I just left it there. I'm not sure what the comment is referencing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just FYI, these noqa comments are a way of telling The list of warnings are here: http://flake8.pycqa.org/en/3.4.1/user/error-codes.html#error-violation-codes 821 is usage of an undefined name, which is usually pretty bad, but in this case it was just because Now that |
||
keyfile = json.loads(keyfile_json) | ||
elif is_dict(keyfile_json): | ||
keyfile = keyfile_json | ||
|
@@ -123,8 +118,6 @@ def recoverTransaction(self, serialized_transaction): | |
txn_bytes = HexBytes(serialized_transaction) | ||
txn = Transaction.from_bytes(txn_bytes) | ||
msg_hash = hash_of_signed_transaction(txn) | ||
if sys.version_info.major < 3: | ||
msg_hash = to_hex(msg_hash) | ||
return self.recover(msg_hash, vrs=vrs_from(txn)) | ||
|
||
def setKeyBackend(self, backend): | ||
|
@@ -133,7 +126,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) | ||
|
@@ -153,7 +145,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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
# String encodings and numeric representations | ||
import json | ||
import re | ||
import sys | ||
|
||
import rlp | ||
from rlp.sedes import big_endian_int | ||
|
@@ -73,7 +72,7 @@ def hex_encode_abi_type(abi_type, value, force_size=None): | |
elif is_address_type(abi_type): | ||
return pad_hex(value, data_size) | ||
elif is_bytes_type(abi_type): | ||
if sys.version_info.major >= 3 and is_bytes(value): | ||
if is_bytes(value): | ||
return encode_hex(value) | ||
else: | ||
return value | ||
|
@@ -194,7 +193,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
if len(hexstr) % 2: | ||
hexstr = '0x0' + remove_0x_prefix(hexstr) | ||
return decode_hex(hexstr) | ||
|
@@ -231,16 +229,10 @@ 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. | ||
In Python 3, only a str object will be interpreted as unicode text | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is all Python 3 now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess its safe to delete all the "python 3" comments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
''' | ||
if sys.version_info.major < 3: | ||
if isinstance(text_or_primitive, unicode): # noqa: F821 | ||
(primitive, text) = (None, text_or_primitive) | ||
else: | ||
(primitive, text) = (text_or_primitive, None) | ||
elif isinstance(text_or_primitive, str): | ||
if isinstance(text_or_primitive, str): | ||
(primitive, text) = (None, text_or_primitive) | ||
else: | ||
(primitive, text) = (text_or_primitive, None) | ||
|
@@ -254,13 +246,10 @@ 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 | ||
@param text_or_primitive in bytes, str, or int. | ||
In Python 3, only a str object will be interpreted as hexstr | ||
''' | ||
if isinstance(hexstr_or_primitive, str) or ( | ||
sys.version_info.major < 3 and isinstance(hexstr_or_primitive, unicode) # noqa: F821 | ||
): | ||
if isinstance(hexstr_or_primitive, str): # noqa: F821 | ||
(primitive, hexstr) = (None, hexstr_or_primitive) | ||
if remove_0x_prefix(hexstr) and not is_hex(hexstr): | ||
raise ValueError( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,5 @@ | ||
import sys | ||
|
||
|
||
if sys.version_info.major == 2: | ||
from .six_py2 import ( | ||
urlparse, | ||
urlunparse, | ||
Generator, | ||
) | ||
else: | ||
from .six_py3 import ( # noqa: #401 | ||
urlparse, | ||
urlunparse, | ||
Generator, | ||
) | ||
from .six_py3 import ( # noqa: #401 | ||
urlparse, | ||
urlunparse, | ||
Generator, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍