diff --git a/monero/address.py b/monero/address.py index 4ad97d8..8379006 100644 --- a/monero/address.py +++ b/monero/address.py @@ -1,18 +1,13 @@ from binascii import hexlify, unhexlify import re from sha3 import keccak_256 +import six import struct -import sys from . import base58 from . import ed25519 from . import numbers -if sys.version_info < (3,): # pragma: no cover - _str_types = (str, bytes, unicode) -else: # pragma: no cover - _str_types = (str, bytes) - _ADDR_REGEX = re.compile(r'^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{95}$') _IADDR_REGEX = re.compile(r'^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{106}$') @@ -78,8 +73,8 @@ class BaseAddress(object): def __eq__(self, other): if isinstance(other, BaseAddress): return str(self) == str(other) - if isinstance(other, _str_types): - return str(self) == other + elif isinstance(other, six.text_type) or isinstance(other, six.string_types): + return str(self) == six.ensure_str(other) return super(BaseAddress, self).__eq__(other) def __hash__(self): diff --git a/monero/ed25519.py b/monero/ed25519.py index 2735bf9..35506b9 100644 --- a/monero/ed25519.py +++ b/monero/ed25519.py @@ -32,21 +32,15 @@ arithmetic, so we cannot handle secrets without risking their disclosure. """ import binascii -import operator +import six import sys if sys.version_info >= (3,): # pragma: no cover - indexbytes = operator.getitem intlist2bytes = bytes - int2byte = operator.methodcaller("to_bytes", 1, "big") else: # pragma: no cover - int2byte = chr range = xrange - def indexbytes(buf, i): - return ord(buf[i]) - def intlist2bytes(l): return b"".join(chr(c) for c in l) @@ -192,7 +186,7 @@ def scalarmult_B(e): def encodeint(y): bits = [(y >> i) & 1 for i in range(b)] return b''.join([ - int2byte(sum([bits[i * 8 + j] << j for j in range(8)])) + six.int2byte(sum([bits[i * 8 + j] << j for j in range(8)])) for i in range(b//8) ]) @@ -204,13 +198,13 @@ def encodepoint(P): y = (y * zi) % q bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1] return b''.join([ - int2byte(sum([bits[i * 8 + j] << j for j in range(8)])) + six.int2byte(sum([bits[i * 8 + j] << j for j in range(8)])) for i in range(b // 8) ]) def bit(h, i): - return (indexbytes(h, i // 8) >> (i % 8)) & 1 + return (six.indexbytes(h, i // 8) >> (i % 8)) & 1 def isoncurve(P): diff --git a/monero/numbers.py b/monero/numbers.py index f03b9d8..d52fe69 100644 --- a/monero/numbers.py +++ b/monero/numbers.py @@ -1,20 +1,13 @@ from decimal import Decimal -import sys +import six PICONERO = Decimal('0.000000000001') EMPTY_KEY = '0' * 64 -if sys.version_info < (3,): # pragma: no cover - _integer_types = (int, long,) - _str_types = (str, bytes, unicode) -else: # pragma: no cover - _integer_types = (int,) - _str_types = (str, bytes) - def to_atomic(amount): """Convert Monero decimal to atomic integer of piconero.""" - if not isinstance(amount, (Decimal, float) + _integer_types): + if not isinstance(amount, (Decimal, float) + six.integer_types): raise ValueError("Amount '{}' doesn't have numeric type. Only Decimal, int, long and " "float (not recommended) are accepted as amounts.") return int(amount * 10**12) @@ -43,9 +36,9 @@ class PaymentID(object): def __init__(self, payment_id): if isinstance(payment_id, PaymentID): payment_id = int(payment_id) - if isinstance(payment_id, _str_types): + if isinstance(payment_id, six.text_type) or isinstance(payment_id, six.string_types): payment_id = int(payment_id, 16) - elif not isinstance(payment_id, _integer_types): + elif not isinstance(payment_id, six.integer_types): raise TypeError("payment_id must be either int or hexadecimal str or bytes, " "is {0}".format(type(payment_id))) if payment_id.bit_length() > 256: @@ -68,8 +61,8 @@ class PaymentID(object): def __eq__(self, other): if isinstance(other, PaymentID): return int(self) == int(other) - elif isinstance(other, _integer_types): + elif isinstance(other, six.integer_types): return int(self) == other - elif isinstance(other, _str_types): - return str(self) == other + elif isinstance(other, six.text_type) or isinstance(other, six.string_types): + return str(self) == six.ensure_str(other) return super(PaymentID, self).__eq__(other) diff --git a/monero/transaction.py b/monero/transaction.py index 454363d..f2ebb46 100644 --- a/monero/transaction.py +++ b/monero/transaction.py @@ -1,5 +1,5 @@ import re -import sys +import six import warnings from .address import address from .numbers import PaymentID @@ -93,12 +93,6 @@ class Transaction(object): return self.hash -if sys.version_info < (3,): # pragma: no cover - _str_types = (str, bytes, unicode) -else: # pragma: no cover - _str_types = (str, bytes) - - class PaymentManager(object): """ A payment query manager, handling either incoming or outgoing payments of @@ -185,7 +179,8 @@ class PaymentFilter(object): if _local_address is None: self.local_addresses = [] else: - if isinstance(_local_address, _str_types): + if isinstance(_local_address, six.string_types) \ + or isinstance(_local_address, six.text_type): local_addresses = [_local_address] else: try: @@ -197,7 +192,7 @@ class PaymentFilter(object): if _tx_id is None: self.tx_ids = [] else: - if isinstance(_tx_id, _str_types): + if isinstance(_tx_id, six.string_types) or isinstance(_tx_id, six.text_type): tx_ids = [_tx_id] else: try: @@ -209,7 +204,7 @@ class PaymentFilter(object): if _payment_id is None: self.payment_ids = [] else: - if isinstance(_payment_id, _str_types): + if isinstance(_payment_id, six.string_types) or isinstance(_payment_id, six.text_type): payment_ids = [_payment_id] else: try: diff --git a/requirements.txt b/requirements.txt index 9190238..028dbd7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ pysha3 requests -six +six>=1.12.0