From b3fde9d78db23c2f9bf40ade64a5b95136203fce Mon Sep 17 00:00:00 2001 From: Oleg Silkin Date: Thu, 22 Nov 2018 22:24:21 -0600 Subject: [PATCH] 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 --- lbrynet/extras/daemon/auth/server.py | 2 +- lbrynet/extras/wallet/claim_proofs.py | 7 +++---- lbrynet/schema/address.py | 6 +----- lbrynet/schema/base.py | 19 ++----------------- lbrynet/schema/decode.py | 7 +++---- lbrynet/schema/hashing.py | 5 ++--- lbrynet/schema/validator.py | 5 ++--- setup.py | 1 - tests/unit/cryptstream/test_cryptblob.py | 2 +- 9 files changed, 15 insertions(+), 39 deletions(-) diff --git a/lbrynet/extras/daemon/auth/server.py b/lbrynet/extras/daemon/auth/server.py index 8678c4e79..e275b09bb 100644 --- a/lbrynet/extras/daemon/auth/server.py +++ b/lbrynet/extras/daemon/auth/server.py @@ -1,6 +1,6 @@ import asyncio import logging -from six.moves.urllib import parse as urlparse +from urllib import parse as urlparse import json import inspect import signal diff --git a/lbrynet/extras/wallet/claim_proofs.py b/lbrynet/extras/wallet/claim_proofs.py index 77beafa16..dd1a2a29b 100644 --- a/lbrynet/extras/wallet/claim_proofs.py +++ b/lbrynet/extras/wallet/claim_proofs.py @@ -1,4 +1,3 @@ -import six import struct import binascii from torba.client.hash import double_sha256 @@ -17,7 +16,7 @@ def get_hash_for_outpoint(txhash, nout, height_of_last_takeover): # noinspection PyPep8 -def verify_proof(proof, rootHash, name): +def verify_proof(proof, root_hash, name): previous_computed_hash = None reverse_computed_name = '' verified_value = False @@ -32,7 +31,7 @@ def verify_proof(proof, rootHash, name): if previous_child_character >= child['character']: raise InvalidProofError("children not in increasing order") previous_child_character = child['character'] - to_hash += six.int2byte(child['character']) + to_hash += bytes((child['character'],)) if 'nodeHash' in child: if len(child['nodeHash']) != 64: raise InvalidProofError("invalid child nodeHash") @@ -70,7 +69,7 @@ def verify_proof(proof, rootHash, name): 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") if 'txhash' in proof and 'nOut' in proof: if not verified_value: diff --git a/lbrynet/schema/address.py b/lbrynet/schema/address.py index 35c31588d..d6c1c4aac 100644 --- a/lbrynet/schema/address.py +++ b/lbrynet/schema/address.py @@ -1,4 +1,3 @@ -import six import lbrynet.schema from lbrynet.schema.base import b58encode, b58decode, validate_b58_checksum from lbrynet.schema.hashing import double_sha256, hash160 @@ -12,10 +11,7 @@ def validate_address_length(addr_bytes): def validate_address_prefix(addr_bytes): - if six.PY3: - prefix = addr_bytes[0] - else: - prefix = ord(addr_bytes[0]) + prefix = addr_bytes[0] if prefix not in ADDRESS_PREFIXES[lbrynet.schema.BLOCKCHAIN_NAME].values(): raise InvalidAddress("Invalid address prefix: %.2X" % prefix) diff --git a/lbrynet/schema/base.py b/lbrynet/schema/base.py index 3bdbd5df1..1a87d4e97 100644 --- a/lbrynet/schema/base.py +++ b/lbrynet/schema/base.py @@ -1,4 +1,3 @@ -import six from lbrynet.schema.schema import ADDRESS_CHECKSUM_LENGTH from lbrynet.schema.hashing import double_sha256 from lbrynet.schema.error import InvalidAddress @@ -7,20 +6,6 @@ from lbrynet.schema.error import InvalidAddress 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): if isinstance(v, str) and not isinstance(v, bytes): v = v.encode('ascii') @@ -48,7 +33,7 @@ def b58encode(v): nPad -= len(v) p, acc = 1, 0 - for c in iseq(reversed(v)): + for c in reversed(v): acc += p * c p = p << 8 @@ -84,7 +69,7 @@ def b58decode(v): acc, mod = divmod(acc, 256) 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): diff --git a/lbrynet/schema/decode.py b/lbrynet/schema/decode.py index e5be724e4..bb9e31ea2 100644 --- a/lbrynet/schema/decode.py +++ b/lbrynet/schema/decode.py @@ -1,7 +1,6 @@ import json import binascii from google.protobuf import json_format # pylint: disable=no-name-in-module -import six from lbrynet.schema.error import DecodeError, InvalidAddress 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('{')]: try: - if six.PY3 and isinstance(claim_value, six.binary_type): + if isinstance(claim_value, bytes): claim_value = claim_value.decode() decoded_json = json.loads(claim_value) return migrate_json_claim_value(decoded_json) except (ValueError, TypeError): pass try: - if six.PY3 and isinstance(claim_value, six.text_type): + if isinstance(claim_value, str): claim_value = claim_value.encode() return ClaimDict.deserialize(claim_value) except (DecodeError, InvalidAddress, KeyError, TypeError): - raise DecodeError(claim_value) + raise DecodeError(claim_value) diff --git a/lbrynet/schema/hashing.py b/lbrynet/schema/hashing.py index 1b2adc5ee..ee1a8f16e 100644 --- a/lbrynet/schema/hashing.py +++ b/lbrynet/schema/hashing.py @@ -1,9 +1,8 @@ -import six import hashlib def sha256(x): - if isinstance(x, six.text_type): + if isinstance(x, str): x = x.encode('utf-8') return hashlib.sha256(x).digest() @@ -13,7 +12,7 @@ def double_sha256(x): def ripemd160(x): - if isinstance(x, six.text_type): + if isinstance(x, str): x = x.encode('utf-8') md = hashlib.new('ripemd160') md.update(x) diff --git a/lbrynet/schema/validator.py b/lbrynet/schema/validator.py index 68830adb7..77b01fca4 100644 --- a/lbrynet/schema/validator.py +++ b/lbrynet/schema/validator.py @@ -1,5 +1,4 @@ from string import hexdigits -import six import ecdsa import hashlib import binascii @@ -20,13 +19,13 @@ from lbrynet.schema.schema import NIST256p, NIST384p, SECP256k1, ECDSA_CURVES, C def validate_claim_id(claim_id): if not len(claim_id) == 40: 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') if set(claim_id).difference(hexdigits): raise Exception("Claim id is not hex encoded") -class Validator(object): +class Validator: CURVE_NAME = None HASHFUNC = hashlib.sha256 diff --git a/setup.py b/setup.py index 7966d9725..f42aaec4e 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,6 @@ setup( 'treq', 'docopt', 'colorama==0.3.7', - 'six' ], extras_require={ 'test': ( diff --git a/tests/unit/cryptstream/test_cryptblob.py b/tests/unit/cryptstream/test_cryptblob.py index a7062ee4b..d25ef3755 100644 --- a/tests/unit/cryptstream/test_cryptblob.py +++ b/tests/unit/cryptstream/test_cryptblob.py @@ -8,7 +8,7 @@ from tests.mocks import mock_conf_settings from cryptography.hazmat.primitives.ciphers.algorithms import AES import random import string -from six import BytesIO +from io import BytesIO import os AES_BLOCK_SIZE_BYTES = int(AES.block_size / 8)