Skip to content

Commit 6980e17

Browse files
authored
Merge pull request petertodd#247 from ysangkok/remove-py2-compat
Remove attempted Python 2 compatibility
2 parents 0b24db2 + bc5400c commit 6980e17

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+135
-370
lines changed

bitcoin/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# propagated, or distributed except according to the terms contained in the
1010
# LICENSE file.
1111

12-
from __future__ import absolute_import, division, print_function, unicode_literals
1312

1413
import bitcoin.core
1514

bitcoin/base58.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@
1212

1313
"""Base58 encoding and decoding"""
1414

15-
from __future__ import absolute_import, division, print_function, unicode_literals
16-
17-
import sys
18-
_bchr = chr
19-
_bord = ord
20-
if sys.version > '3':
21-
long = int
22-
_bchr = lambda x: bytes([x])
23-
_bord = lambda x: x
2415

2516
import binascii
2617

@@ -52,10 +43,7 @@ def encode(b):
5243
res = ''.join(res[::-1])
5344

5445
# Encode leading zeros as base58 zeros
55-
czero = b'\x00'
56-
if sys.version > '3':
57-
# In Python3 indexing a bytes returns numbers, not characters.
58-
czero = 0
46+
czero = 0
5947
pad = 0
6048
for c in b:
6149
if c == czero:
@@ -108,7 +96,7 @@ def __new__(cls, s):
10896
if check0 != check1:
10997
raise Base58ChecksumError('Checksum mismatch: expected %r, calculated %r' % (check0, check1))
11098

111-
return cls.from_bytes(data, _bord(verbyte[0]))
99+
return cls.from_bytes(data, verbyte[0])
112100

113101
def __init__(self, s):
114102
"""Initialize from base58-encoded string
@@ -138,7 +126,7 @@ def to_bytes(self):
138126

139127
def __str__(self):
140128
"""Convert to string"""
141-
vs = _bchr(self.nVersion) + self
129+
vs = bytes([self.nVersion]) + self
142130
check = bitcoin.core.Hash(vs)[0:4]
143131
return encode(vs + check)
144132

bitcoin/bech32.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111

1212
"""Bech32 encoding and decoding"""
1313

14-
import sys
15-
_bchr = chr
16-
_bord = ord
17-
if sys.version > '3':
18-
long = int
19-
_bchr = lambda x: bytes([x])
20-
_bord = lambda x: x
21-
2214
from bitcoin.segwit_addr import encode, decode
2315
import bitcoin
2416

bitcoin/bloom.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111

1212
"""Bloom filter support"""
1313

14-
from __future__ import absolute_import, division, print_function, unicode_literals
1514

1615
import struct
17-
import sys
1816
import math
1917

2018
import bitcoin.core
@@ -56,16 +54,12 @@ def MurmurHash3(nHashSeed, vDataToHash):
5654
# tail
5755
k1 = 0
5856
j = (len(vDataToHash) // 4) * 4
59-
bord = ord
60-
if sys.version > '3':
61-
# In Py3 indexing bytes returns numbers, not characters
62-
bord = lambda x: x
6357
if len(vDataToHash) & 3 >= 3:
64-
k1 ^= bord(vDataToHash[j+2]) << 16
58+
k1 ^= vDataToHash[j+2] << 16
6559
if len(vDataToHash) & 3 >= 2:
66-
k1 ^= bord(vDataToHash[j+1]) << 8
60+
k1 ^= vDataToHash[j+1] << 8
6761
if len(vDataToHash) & 3 >= 1:
68-
k1 ^= bord(vDataToHash[j])
62+
k1 ^= vDataToHash[j]
6963

7064
k1 &= 0xFFFFFFFF
7165
k1 = (k1 * c1) & 0xFFFFFFFF
@@ -180,11 +174,7 @@ def stream_deserialize(cls, f):
180174
return deserialized
181175

182176
def stream_serialize(self, f):
183-
if sys.version > '3':
184-
bitcoin.core.serialize.BytesSerializer.stream_serialize(self.vData, f)
185-
else:
186-
# 2.7 has problems with f.write(bytearray())
187-
bitcoin.core.serialize.BytesSerializer.stream_serialize(bytes(self.vData), f)
177+
bitcoin.core.serialize.BytesSerializer.stream_serialize(self.vData, f)
188178
f.write(self.__struct.pack(self.nHashFuncs, self.nTweak, self.nFlags))
189179

190180
__all__ = (

bitcoin/core/__init__.py

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,22 @@
99
# propagated, or distributed except according to the terms contained in the
1010
# LICENSE file.
1111

12-
from __future__ import absolute_import, division, print_function
1312

1413
import binascii
1514
import struct
16-
import sys
1715
import time
1816

19-
from .script import CScript, CScriptWitness, CScriptOp, OP_RETURN
17+
from . import script
18+
from .script import CScript, CScriptWitness, OP_RETURN
2019

2120
from .serialize import *
2221

23-
if sys.version > '3':
24-
_bytes = bytes
25-
else:
26-
_bytes = lambda x: bytes(bytearray(x))
27-
2822
# Core definitions
2923
COIN = 100000000
3024
MAX_BLOCK_SIZE = 1000000
3125
MAX_BLOCK_WEIGHT = 4000000
3226
MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50
33-
WITNESS_COINBASE_SCRIPTPUBKEY_MAGIC = _bytes([OP_RETURN, 0x24, 0xaa, 0x21, 0xa9, 0xed])
27+
WITNESS_COINBASE_SCRIPTPUBKEY_MAGIC = bytes([OP_RETURN, 0x24, 0xaa, 0x21, 0xa9, 0xed])
3428

3529
def MoneyRange(nValue, params=None):
3630
global coreparams
@@ -39,30 +33,14 @@ def MoneyRange(nValue, params=None):
3933

4034
return 0 <= nValue <= params.MAX_MONEY
4135

42-
def _py2_x(h):
43-
"""Convert a hex string to bytes"""
44-
return binascii.unhexlify(h)
45-
4636
def x(h):
4737
"""Convert a hex string to bytes"""
4838
return binascii.unhexlify(h.encode('utf8'))
4939

50-
def _py2_b2x(b):
51-
"""Convert bytes to a hex string"""
52-
return binascii.hexlify(b)
53-
5440
def b2x(b):
5541
"""Convert bytes to a hex string"""
5642
return binascii.hexlify(b).decode('utf8')
5743

58-
def _py2_lx(h):
59-
"""Convert a little-endian hex string to bytes
60-
61-
Lets you write uint256's and uint160's the way the Satoshi codebase shows
62-
them.
63-
"""
64-
return binascii.unhexlify(h)[::-1]
65-
6644
def lx(h):
6745
"""Convert a little-endian hex string to bytes
6846
@@ -71,14 +49,6 @@ def lx(h):
7149
"""
7250
return binascii.unhexlify(h.encode('utf8'))[::-1]
7351

74-
def _py2_b2lx(b):
75-
"""Convert bytes to a little-endian hex string
76-
77-
Lets you show uint256's and uint160's the way the Satoshi codebase shows
78-
them.
79-
"""
80-
return binascii.hexlify(b[::-1])
81-
8252
def b2lx(b):
8353
"""Convert bytes to a little-endian hex string
8454
@@ -87,18 +57,6 @@ def b2lx(b):
8757
"""
8858
return binascii.hexlify(b[::-1]).decode('utf8')
8959

90-
if not (sys.version > '3'):
91-
x = _py2_x
92-
b2x = _py2_b2x
93-
lx = _py2_lx
94-
b2lx = _py2_b2lx
95-
96-
del _py2_x
97-
del _py2_b2x
98-
del _py2_lx
99-
del _py2_b2lx
100-
101-
10260
def str_money_value(value):
10361
"""Convert an integer money value to a fixed point string"""
10462
r = '%i.%08i' % (value // COIN, value % COIN)

bitcoin/core/_bignum.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#
1414
# Internally used for script evaluation; not to be used externally.
1515

16-
from __future__ import absolute_import, division, print_function, unicode_literals
1716

1817
import struct
1918

bitcoin/core/key.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,10 @@
1818
import ctypes
1919
import ctypes.util
2020
import hashlib
21-
import sys
2221
from os import urandom
2322
import bitcoin
2423
import bitcoin.signature
2524

26-
_bchr = chr
27-
_bord = ord
28-
if sys.version > '3':
29-
_bchr = lambda x: bytes([x])
30-
_bord = lambda x: x
31-
3225
import bitcoin.core.script
3326

3427
_ssl = ctypes.cdll.LoadLibrary(
@@ -587,8 +580,8 @@ def recover_compact(cls, hash, sig): # pylint: disable=redefined-builtin
587580
if len(sig) != 65:
588581
raise ValueError("Signature should be 65 characters, not [%d]" % (len(sig), ))
589582

590-
recid = (_bord(sig[0]) - 27) & 3
591-
compressed = (_bord(sig[0]) - 27) & 4 != 0
583+
recid = (sig[0] - 27) & 3
584+
compressed = (sig[0] - 27) & 4 != 0
592585

593586
cec_key = CECKey()
594587
cec_key.set_compressed(compressed)
@@ -620,12 +613,7 @@ def __str__(self):
620613
return repr(self)
621614

622615
def __repr__(self):
623-
# Always have represent as b'<secret>' so test cases don't have to
624-
# change for py2/3
625-
if sys.version > '3':
626-
return '%s(%s)' % (self.__class__.__name__, super(CPubKey, self).__repr__())
627-
else:
628-
return '%s(b%s)' % (self.__class__.__name__, super(CPubKey, self).__repr__())
616+
return '%s(%s)' % (self.__class__.__name__, super(CPubKey, self).__repr__())
629617

630618
__all__ = (
631619
'CECKey',

bitcoin/core/script.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,8 @@
1515
is in bitcoin.core.scripteval
1616
"""
1717

18-
from __future__ import absolute_import, division, print_function
19-
20-
import sys
21-
_bchr = chr
22-
_bord = ord
23-
if sys.version > '3':
24-
long = int
25-
_bchr = lambda x: bytes([x])
26-
_bord = lambda x: x
27-
from io import BytesIO as _BytesIO
28-
else:
29-
from cStringIO import StringIO as _BytesIO
18+
19+
from io import BytesIO
3020

3121
import struct
3222

@@ -50,9 +40,9 @@ class CScriptOp(int):
5040
def encode_op_pushdata(d):
5141
"""Encode a PUSHDATA op, returning bytes"""
5242
if len(d) < 0x4c:
53-
return b'' + _bchr(len(d)) + d # OP_PUSHDATA
43+
return bytes([len(d)]) + d # OP_PUSHDATA
5444
elif len(d) <= 0xff:
55-
return b'\x4c' + _bchr(len(d)) + d # OP_PUSHDATA1
45+
return b'\x4c' + bytes([len(d)]) + d # OP_PUSHDATA1
5646
elif len(d) <= 0xffff:
5747
return b'\x4d' + struct.pack(b'<H', len(d)) + d # OP_PUSHDATA2
5848
elif len(d) <= 0xffffffff:
@@ -524,12 +514,12 @@ class CScript(bytes):
524514
def __coerce_instance(cls, other):
525515
# Coerce other into bytes
526516
if isinstance(other, CScriptOp):
527-
other = _bchr(other)
528-
elif isinstance(other, (int, long)):
517+
other = bytes([other])
518+
elif isinstance(other, int):
529519
if 0 <= other <= 16:
530-
other = bytes(_bchr(CScriptOp.encode_op_n(other)))
520+
other = bytes([CScriptOp.encode_op_n(other)])
531521
elif other == -1:
532-
other = bytes(_bchr(OP_1NEGATE))
522+
other = bytes([OP_1NEGATE])
533523
else:
534524
other = CScriptOp.encode_op_pushdata(bitcoin.core._bignum.bn2vch(other))
535525
elif isinstance(other, (bytes, bytearray)):
@@ -572,7 +562,7 @@ def raw_iter(self):
572562
i = 0
573563
while i < len(self):
574564
sop_idx = i
575-
opcode = _bord(self[i])
565+
opcode = self[i]
576566
i += 1
577567

578568
if opcode > OP_PUSHDATA4:
@@ -588,21 +578,21 @@ def raw_iter(self):
588578
pushdata_type = 'PUSHDATA1'
589579
if i >= len(self):
590580
raise CScriptInvalidError('PUSHDATA1: missing data length')
591-
datasize = _bord(self[i])
581+
datasize = self[i]
592582
i += 1
593583

594584
elif opcode == OP_PUSHDATA2:
595585
pushdata_type = 'PUSHDATA2'
596586
if i + 1 >= len(self):
597587
raise CScriptInvalidError('PUSHDATA2: missing data length')
598-
datasize = _bord(self[i]) + (_bord(self[i+1]) << 8)
588+
datasize = self[i] + (self[i+1] << 8)
599589
i += 2
600590

601591
elif opcode == OP_PUSHDATA4:
602592
pushdata_type = 'PUSHDATA4'
603593
if i + 3 >= len(self):
604594
raise CScriptInvalidError('PUSHDATA4: missing data length')
605-
datasize = _bord(self[i]) + (_bord(self[i+1]) << 8) + (_bord(self[i+2]) << 16) + (_bord(self[i+3]) << 24)
595+
datasize = self[i] + (self[i+1] << 8) + (self[i+2] << 16) + (self[i+3] << 24)
606596
i += 4
607597

608598
else:
@@ -676,9 +666,9 @@ def is_p2sh(self):
676666
Note that this test is consensus-critical.
677667
"""
678668
return (len(self) == 23 and
679-
_bord(self[0]) == OP_HASH160 and
680-
_bord(self[1]) == 0x14 and
681-
_bord(self[22]) == OP_EQUAL)
669+
self[0] == OP_HASH160 and
670+
self[1] == 0x14 and
671+
self[22] == OP_EQUAL)
682672

683673
def is_witness_scriptpubkey(self):
684674
"""Returns true if this is a scriptpubkey signaling segregated witness data.
@@ -747,7 +737,7 @@ def has_canonical_pushes(self):
747737
if op > OP_16:
748738
continue
749739

750-
elif op < OP_PUSHDATA1 and op > OP_0 and len(data) == 1 and _bord(data[0]) <= 16:
740+
elif op < OP_PUSHDATA1 and op > OP_0 and len(data) == 1 and data[0] <= 16:
751741
# Could have used an OP_n code, rather than a 1-byte push.
752742
return False
753743

@@ -770,7 +760,7 @@ def has_canonical_pushes(self):
770760
def is_unspendable(self):
771761
"""Test if the script is provably unspendable"""
772762
return (len(self) > 0 and
773-
_bord(self[0]) == OP_RETURN)
763+
self[0] == OP_RETURN)
774764

775765
def is_valid(self):
776766
"""Return True if the script is valid, False otherwise
@@ -1018,7 +1008,7 @@ def SignatureHash(script, txTo, inIdx, hashtype, amount=None, sigversion=SIGVERS
10181008
serialize_outputs = txTo.vout[inIdx].serialize()
10191009
hashOutputs = bitcoin.core.Hash(serialize_outputs)
10201010

1021-
f = _BytesIO()
1011+
f = BytesIO()
10221012
f.write(struct.pack("<i", txTo.nVersion))
10231013
f.write(hashPrevouts)
10241014
f.write(hashSequence)

0 commit comments

Comments
 (0)