Skip to content

Commit 4e8a3e0

Browse files
committed
Move sys.version test out of x/b2x/etc. functions
1 parent cc4d17c commit 4e8a3e0

1 file changed

Lines changed: 43 additions & 20 deletions

File tree

bitcoin/core/__init__.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
from __future__ import absolute_import, division, print_function, unicode_literals
1313

14-
import struct
15-
import socket
1614
import binascii
1715
import hashlib
16+
import socket
17+
import struct
18+
import sys
1819

1920
from .script import CScript
2021

@@ -35,43 +36,65 @@
3536
def MoneyRange(nValue):
3637
return 0 <= nValue <= MAX_MONEY
3738

39+
def py2_x(h):
40+
"""Convert a hex string to bytes"""
41+
return binascii.unhexlify(h)
42+
3843
def x(h):
3944
"""Convert a hex string to bytes"""
40-
import sys
41-
if sys.version > '3':
42-
return binascii.unhexlify(h.encode('utf8'))
43-
else:
44-
return binascii.unhexlify(h)
45+
return binascii.unhexlify(h.encode('utf8'))
46+
47+
def py2_b2x(b):
48+
"""Convert bytes to a hex string"""
49+
return binascii.hexlify(b)
4550

4651
def b2x(b):
4752
"""Convert bytes to a hex string"""
48-
if sys.version > '3':
49-
return binascii.hexlify(b).decode('utf8')
50-
else:
51-
return binascii.hexlify(b)
53+
return binascii.hexlify(b).decode('utf8')
54+
55+
def py2_lx(h):
56+
"""Convert a little-endian hex string to bytes
57+
58+
Lets you write uint256's and uint160's the way the Satoshi codebase shows
59+
them.
60+
"""
61+
return binascii.unhexlify(h)[::-1]
5262

5363
def lx(h):
5464
"""Convert a little-endian hex string to bytes
5565
5666
Lets you write uint256's and uint160's the way the Satoshi codebase shows
5767
them.
5868
"""
59-
import sys
60-
if sys.version > '3':
61-
return binascii.unhexlify(h.encode('utf8'))[::-1]
62-
else:
63-
return binascii.unhexlify(h)[::-1]
69+
return binascii.unhexlify(h.encode('utf8'))[::-1]
70+
71+
def py2_b2lx(b):
72+
"""Convert bytes to a little-endian hex string
73+
74+
Lets you show uint256's and uint160's the way the Satoshi codebase shows
75+
them.
76+
"""
77+
return binascii.hexlify(b[::-1])
6478

6579
def b2lx(b):
6680
"""Convert bytes to a little-endian hex string
6781
6882
Lets you show uint256's and uint160's the way the Satoshi codebase shows
6983
them.
7084
"""
71-
if sys.version > '3':
72-
return binascii.hexlify(b[::-1]).decode('utf8')
73-
else:
74-
return binascii.hexlify(b[::-1])
85+
return binascii.hexlify(b[::-1]).decode('utf8')
86+
87+
if not (sys.version > '3'):
88+
x = py2_x
89+
b2x = py2_b2x
90+
lx = py2_lx
91+
b2lx = py2_b2lx
92+
93+
del py2_x
94+
del py2_b2x
95+
del py2_lx
96+
del py2_b2lx
97+
7598

7699
def str_money_value(value):
77100
"""Convert an integer money value to a fixed point string"""

0 commit comments

Comments
 (0)