Removes six From Project (#1660)

* Replaces `six` urllib with python 3's urllib
* Replaces `six`'s int2byte method with native `bytes` class
* Removes edge case testing for python2 vs python3
* Removes useless object inheritance
* Uses native io.BytesIO instead of six.BytesIO
* Removes six from dependencies
This commit is contained in:
Oleg Silkin 2018-11-22 22:24:21 -06:00 committed by Lex Berezhny
parent 0c999083cc
commit b3fde9d78d
9 changed files with 15 additions and 39 deletions

View file

@ -1,6 +1,6 @@
import asyncio import asyncio
import logging import logging
from six.moves.urllib import parse as urlparse from urllib import parse as urlparse
import json import json
import inspect import inspect
import signal import signal

View file

@ -1,4 +1,3 @@
import six
import struct import struct
import binascii import binascii
from torba.client.hash import double_sha256 from torba.client.hash import double_sha256
@ -17,7 +16,7 @@ def get_hash_for_outpoint(txhash, nout, height_of_last_takeover):
# noinspection PyPep8 # noinspection PyPep8
def verify_proof(proof, rootHash, name): def verify_proof(proof, root_hash, name):
previous_computed_hash = None previous_computed_hash = None
reverse_computed_name = '' reverse_computed_name = ''
verified_value = False verified_value = False
@ -32,7 +31,7 @@ def verify_proof(proof, rootHash, name):
if previous_child_character >= child['character']: if previous_child_character >= child['character']:
raise InvalidProofError("children not in increasing order") raise InvalidProofError("children not in increasing order")
previous_child_character = child['character'] previous_child_character = child['character']
to_hash += six.int2byte(child['character']) to_hash += bytes((child['character'],))
if 'nodeHash' in child: if 'nodeHash' in child:
if len(child['nodeHash']) != 64: if len(child['nodeHash']) != 64:
raise InvalidProofError("invalid child nodeHash") raise InvalidProofError("invalid child nodeHash")
@ -70,7 +69,7 @@ def verify_proof(proof, rootHash, name):
previous_computed_hash = double_sha256(to_hash) previous_computed_hash = double_sha256(to_hash)
if previous_computed_hash != binascii.unhexlify(rootHash)[::-1]: if previous_computed_hash != binascii.unhexlify(root_hash)[::-1]:
raise InvalidProofError("computed hash does not match roothash") raise InvalidProofError("computed hash does not match roothash")
if 'txhash' in proof and 'nOut' in proof: if 'txhash' in proof and 'nOut' in proof:
if not verified_value: if not verified_value:

View file

@ -1,4 +1,3 @@
import six
import lbrynet.schema import lbrynet.schema
from lbrynet.schema.base import b58encode, b58decode, validate_b58_checksum from lbrynet.schema.base import b58encode, b58decode, validate_b58_checksum
from lbrynet.schema.hashing import double_sha256, hash160 from lbrynet.schema.hashing import double_sha256, hash160
@ -12,10 +11,7 @@ def validate_address_length(addr_bytes):
def validate_address_prefix(addr_bytes): def validate_address_prefix(addr_bytes):
if six.PY3: prefix = addr_bytes[0]
prefix = addr_bytes[0]
else:
prefix = ord(addr_bytes[0])
if prefix not in ADDRESS_PREFIXES[lbrynet.schema.BLOCKCHAIN_NAME].values(): if prefix not in ADDRESS_PREFIXES[lbrynet.schema.BLOCKCHAIN_NAME].values():
raise InvalidAddress("Invalid address prefix: %.2X" % prefix) raise InvalidAddress("Invalid address prefix: %.2X" % prefix)

View file

@ -1,4 +1,3 @@
import six
from lbrynet.schema.schema import ADDRESS_CHECKSUM_LENGTH from lbrynet.schema.schema import ADDRESS_CHECKSUM_LENGTH
from lbrynet.schema.hashing import double_sha256 from lbrynet.schema.hashing import double_sha256
from lbrynet.schema.error import InvalidAddress from lbrynet.schema.error import InvalidAddress
@ -7,20 +6,6 @@ from lbrynet.schema.error import InvalidAddress
alphabet = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' alphabet = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
if six.PY2:
iseq, bseq, buffer = (
lambda s: map(ord, s),
lambda s: ''.join(map(chr, s)),
lambda s: s,
)
elif six.PY3:
iseq, bseq, buffer = (
lambda s: s,
bytes,
lambda s: s.buffer,
)
def scrub_input(v): def scrub_input(v):
if isinstance(v, str) and not isinstance(v, bytes): if isinstance(v, str) and not isinstance(v, bytes):
v = v.encode('ascii') v = v.encode('ascii')
@ -48,7 +33,7 @@ def b58encode(v):
nPad -= len(v) nPad -= len(v)
p, acc = 1, 0 p, acc = 1, 0
for c in iseq(reversed(v)): for c in reversed(v):
acc += p * c acc += p * c
p = p << 8 p = p << 8
@ -84,7 +69,7 @@ def b58decode(v):
acc, mod = divmod(acc, 256) acc, mod = divmod(acc, 256)
result.append(mod) result.append(mod)
return (b'\0' * (origlen - newlen) + bseq(reversed(result))) return b'\0' * (origlen - newlen) + bytes(reversed(result))
def validate_b58_checksum(addr_bytes): def validate_b58_checksum(addr_bytes):

View file

@ -1,7 +1,6 @@
import json import json
import binascii import binascii
from google.protobuf import json_format # pylint: disable=no-name-in-module from google.protobuf import json_format # pylint: disable=no-name-in-module
import six
from lbrynet.schema.error import DecodeError, InvalidAddress from lbrynet.schema.error import DecodeError, InvalidAddress
from lbrynet.schema.legacy.migrate import migrate as schema_migrator from lbrynet.schema.legacy.migrate import migrate as schema_migrator
@ -48,15 +47,15 @@ def smart_decode(claim_value):
if claim_value[0] in ['{', ord('{')]: if claim_value[0] in ['{', ord('{')]:
try: try:
if six.PY3 and isinstance(claim_value, six.binary_type): if isinstance(claim_value, bytes):
claim_value = claim_value.decode() claim_value = claim_value.decode()
decoded_json = json.loads(claim_value) decoded_json = json.loads(claim_value)
return migrate_json_claim_value(decoded_json) return migrate_json_claim_value(decoded_json)
except (ValueError, TypeError): except (ValueError, TypeError):
pass pass
try: try:
if six.PY3 and isinstance(claim_value, six.text_type): if isinstance(claim_value, str):
claim_value = claim_value.encode() claim_value = claim_value.encode()
return ClaimDict.deserialize(claim_value) return ClaimDict.deserialize(claim_value)
except (DecodeError, InvalidAddress, KeyError, TypeError): except (DecodeError, InvalidAddress, KeyError, TypeError):
raise DecodeError(claim_value) raise DecodeError(claim_value)

View file

@ -1,9 +1,8 @@
import six
import hashlib import hashlib
def sha256(x): def sha256(x):
if isinstance(x, six.text_type): if isinstance(x, str):
x = x.encode('utf-8') x = x.encode('utf-8')
return hashlib.sha256(x).digest() return hashlib.sha256(x).digest()
@ -13,7 +12,7 @@ def double_sha256(x):
def ripemd160(x): def ripemd160(x):
if isinstance(x, six.text_type): if isinstance(x, str):
x = x.encode('utf-8') x = x.encode('utf-8')
md = hashlib.new('ripemd160') md = hashlib.new('ripemd160')
md.update(x) md.update(x)

View file

@ -1,5 +1,4 @@
from string import hexdigits from string import hexdigits
import six
import ecdsa import ecdsa
import hashlib import hashlib
import binascii import binascii
@ -20,13 +19,13 @@ from lbrynet.schema.schema import NIST256p, NIST384p, SECP256k1, ECDSA_CURVES, C
def validate_claim_id(claim_id): def validate_claim_id(claim_id):
if not len(claim_id) == 40: if not len(claim_id) == 40:
raise Exception("Incorrect claimid length: %i" % len(claim_id)) raise Exception("Incorrect claimid length: %i" % len(claim_id))
if isinstance(claim_id, six.binary_type): if isinstance(claim_id, bytes):
claim_id = claim_id.decode('utf-8') claim_id = claim_id.decode('utf-8')
if set(claim_id).difference(hexdigits): if set(claim_id).difference(hexdigits):
raise Exception("Claim id is not hex encoded") raise Exception("Claim id is not hex encoded")
class Validator(object): class Validator:
CURVE_NAME = None CURVE_NAME = None
HASHFUNC = hashlib.sha256 HASHFUNC = hashlib.sha256

View file

@ -43,7 +43,6 @@ setup(
'treq', 'treq',
'docopt', 'docopt',
'colorama==0.3.7', 'colorama==0.3.7',
'six'
], ],
extras_require={ extras_require={
'test': ( 'test': (

View file

@ -8,7 +8,7 @@ from tests.mocks import mock_conf_settings
from cryptography.hazmat.primitives.ciphers.algorithms import AES from cryptography.hazmat.primitives.ciphers.algorithms import AES
import random import random
import string import string
from six import BytesIO from io import BytesIO
import os import os
AES_BLOCK_SIZE_BYTES = int(AES.block_size / 8) AES_BLOCK_SIZE_BYTES = int(AES.block_size / 8)