Skip to content

Commit e3cdf68

Browse files
committed
Add __all__ to all modules
Be explicit about what we're exporting everywhere.
1 parent 2cb4cf7 commit e3cdf68

File tree

11 files changed

+275
-14
lines changed

11 files changed

+275
-14
lines changed

README

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ accomodate this. In addition see b2x() and b2lx() for conversion from bytes to
6969
big/little-endian hex.
7070

7171

72+
Module import style
73+
-------------------
74+
75+
While not always good style, it's often convenient for quick scripts if import
76+
* can be used. To support that all the modules have __all__ defined
77+
appropriately.
78+
79+
7280
Example Code
7381
------------
7482

bitcoin/base58.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,13 @@ def __str__(self):
144144

145145
def __repr__(self):
146146
return '%s(%r)' % (self.__class__.__name__, str(self))
147+
148+
__all__ = (
149+
'B58_DIGITS',
150+
'Base58Error',
151+
'InvalidBase58Error',
152+
'encode',
153+
'decode',
154+
'Base58ChecksumError',
155+
'CBase58Data',
156+
)

bitcoin/bloom.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,8 @@ def stream_serialize(self, f):
184184
# 2.7 has problems with f.write(bytearray())
185185
bitcoin.core.serialize.BytesSerializer.stream_serialize(bytes(self.vData), f)
186186
f.write(self.__struct.pack(self.nHashFuncs, self.nTweak, self.nFlags))
187+
188+
__all__ = (
189+
'MurmurHash3',
190+
'CBloomFilter',
191+
)

bitcoin/core/__init__.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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
12+
from __future__ import absolute_import, division, print_function
1313

1414
import binascii
1515
import hashlib
@@ -732,3 +732,42 @@ def CheckBlock(block, fCheckPoW = True, fCheckMerkleRoot = True, cur_time=None):
732732
# Check merkle root
733733
if fCheckMerkleRoot and block.hashMerkleRoot != block.calc_merkle_root():
734734
raise CheckBlockError("CheckBlock() : hashMerkleRoot mismatch")
735+
736+
__all__ = (
737+
'Hash',
738+
'Hash160',
739+
'COIN',
740+
'MAX_MONEY',
741+
'MAX_BLOCK_SIZE',
742+
'MAX_BLOCK_SIGOPS',
743+
'MoneyRange',
744+
'x',
745+
'b2x',
746+
'lx',
747+
'b2lx',
748+
'str_money_value',
749+
'ValidationError',
750+
'COutPoint',
751+
'CMutableOutPoint',
752+
'CTxIn',
753+
'CMutableTxIn',
754+
'CTxOut',
755+
'CMutableTxOut',
756+
'CTransaction',
757+
'CMutableTransaction',
758+
'CBlockHeader',
759+
'CBlock',
760+
'CoreChainParams',
761+
'CoreMainParams',
762+
'CoreTestNetParams',
763+
'CoreRegTestParams',
764+
'CheckTransactionError',
765+
'CheckTransaction',
766+
'CheckBlockHeaderError',
767+
'CheckProofOfWorkError',
768+
'CheckProofOfWork',
769+
'CheckBlockHeader',
770+
'CheckBlockError',
771+
'GetLegacySigOpCount',
772+
'CheckBlock',
773+
)

bitcoin/core/key.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,7 @@ def __repr__(self):
164164
else:
165165
return '%s(b%s)' % (self.__class__.__name__, super(CPubKey, self).__repr__())
166166

167+
__all__ = (
168+
'CECKey',
169+
'CPubKey',
170+
)

bitcoin/core/script.py

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
is in bitcoin.core.scripteval
1616
"""
1717

18-
from __future__ import absolute_import, division, print_function, unicode_literals
18+
from __future__ import absolute_import, division, print_function
1919

2020
import sys
2121
_bchr = chr
@@ -863,3 +863,149 @@ def SignatureHash(script, txTo, inIdx, hashtype):
863863
if err is not None:
864864
raise ValueError(err)
865865
return h
866+
867+
868+
__all__ = (
869+
'MAX_SCRIPT_SIZE',
870+
'MAX_SCRIPT_ELEMENT_SIZE',
871+
'MAX_SCRIPT_OPCODES',
872+
'OPCODE_NAMES',
873+
'CScriptOp',
874+
875+
# every opcode
876+
'OP_0',
877+
'OP_FALSE',
878+
'OP_PUSHDATA1',
879+
'OP_PUSHDATA2',
880+
'OP_PUSHDATA4',
881+
'OP_1NEGATE',
882+
'OP_RESERVED',
883+
'OP_1',
884+
'OP_TRUE',
885+
'OP_2',
886+
'OP_3',
887+
'OP_4',
888+
'OP_5',
889+
'OP_6',
890+
'OP_7',
891+
'OP_8',
892+
'OP_9',
893+
'OP_10',
894+
'OP_11',
895+
'OP_12',
896+
'OP_13',
897+
'OP_14',
898+
'OP_15',
899+
'OP_16',
900+
'OP_NOP',
901+
'OP_VER',
902+
'OP_IF',
903+
'OP_NOTIF',
904+
'OP_VERIF',
905+
'OP_VERNOTIF',
906+
'OP_ELSE',
907+
'OP_ENDIF',
908+
'OP_VERIFY',
909+
'OP_RETURN',
910+
'OP_TOALTSTACK',
911+
'OP_FROMALTSTACK',
912+
'OP_2DROP',
913+
'OP_2DUP',
914+
'OP_3DUP',
915+
'OP_2OVER',
916+
'OP_2ROT',
917+
'OP_2SWAP',
918+
'OP_IFDUP',
919+
'OP_DEPTH',
920+
'OP_DROP',
921+
'OP_DUP',
922+
'OP_NIP',
923+
'OP_OVER',
924+
'OP_PICK',
925+
'OP_ROLL',
926+
'OP_ROT',
927+
'OP_SWAP',
928+
'OP_TUCK',
929+
'OP_CAT',
930+
'OP_SUBSTR',
931+
'OP_LEFT',
932+
'OP_RIGHT',
933+
'OP_SIZE',
934+
'OP_INVERT',
935+
'OP_AND',
936+
'OP_OR',
937+
'OP_XOR',
938+
'OP_EQUAL',
939+
'OP_EQUALVERIFY',
940+
'OP_RESERVED1',
941+
'OP_RESERVED2',
942+
'OP_1ADD',
943+
'OP_1SUB',
944+
'OP_2MUL',
945+
'OP_2DIV',
946+
'OP_NEGATE',
947+
'OP_ABS',
948+
'OP_NOT',
949+
'OP_0NOTEQUAL',
950+
'OP_ADD',
951+
'OP_SUB',
952+
'OP_MUL',
953+
'OP_DIV',
954+
'OP_MOD',
955+
'OP_LSHIFT',
956+
'OP_RSHIFT',
957+
'OP_BOOLAND',
958+
'OP_BOOLOR',
959+
'OP_NUMEQUAL',
960+
'OP_NUMEQUALVERIFY',
961+
'OP_NUMNOTEQUAL',
962+
'OP_LESSTHAN',
963+
'OP_GREATERTHAN',
964+
'OP_LESSTHANOREQUAL',
965+
'OP_GREATERTHANOREQUAL',
966+
'OP_MIN',
967+
'OP_MAX',
968+
'OP_WITHIN',
969+
'OP_RIPEMD160',
970+
'OP_SHA1',
971+
'OP_SHA256',
972+
'OP_HASH160',
973+
'OP_HASH256',
974+
'OP_CODESEPARATOR',
975+
'OP_CHECKSIG',
976+
'OP_CHECKSIGVERIFY',
977+
'OP_CHECKMULTISIG',
978+
'OP_CHECKMULTISIGVERIFY',
979+
'OP_NOP1',
980+
'OP_NOP2',
981+
'OP_NOP3',
982+
'OP_NOP4',
983+
'OP_NOP5',
984+
'OP_NOP6',
985+
'OP_NOP7',
986+
'OP_NOP8',
987+
'OP_NOP9',
988+
'OP_NOP10',
989+
'OP_SMALLINTEGER',
990+
'OP_PUBKEYS',
991+
'OP_PUBKEYHASH',
992+
'OP_PUBKEY',
993+
'OP_INVALIDOPCODE',
994+
995+
'OPCODES_BY_NAME',
996+
'DISABLED_OPCODES',
997+
'CScriptInvalidError',
998+
'CScriptTruncatedPushDataError',
999+
'CScript',
1000+
'SCRIPT_VERIFY_P2SH',
1001+
'SCRIPT_VERIFY_STRICTENC',
1002+
'SCRIPT_VERIFY_EVEN_S',
1003+
'SCRIPT_VERIFY_NOCACHE',
1004+
'SIGHASH_ALL',
1005+
'SIGHASH_NONE',
1006+
'SIGHASH_SINGLE',
1007+
'SIGHASH_ANYONECANPAY',
1008+
'FindAndDelete',
1009+
'RawSignatureHash',
1010+
'SignatureHash',
1011+
)

bitcoin/core/serialize.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
if sys.version > '3':
2626
_bchr = lambda x: bytes([x])
2727
_bord = lambda x: x[0]
28-
from io import BytesIO
28+
from io import BytesIO as _BytesIO
2929
else:
3030
_bchr = chr
3131
_bord = ord
32-
from cStringIO import StringIO as BytesIO
32+
from cStringIO import StringIO as _BytesIO
3333

3434
MAX_SIZE = 0x02000000
3535

@@ -98,7 +98,7 @@ def stream_deserialize(cls, f):
9898

9999
def serialize(self):
100100
"""Serialize, returning bytes"""
101-
f = BytesIO()
101+
f = _BytesIO()
102102
self.stream_serialize(f)
103103
return f.getvalue()
104104

@@ -111,7 +111,7 @@ def deserialize(cls, buf, allow_padding=False):
111111
If allow_padding is False and not all bytes are consumed during
112112
deserialization DeserializationExtraDataError will be raised.
113113
"""
114-
fd = BytesIO(buf)
114+
fd = _BytesIO(buf)
115115
r = cls.stream_deserialize(fd)
116116
if not allow_padding:
117117
padding = fd.read()
@@ -178,13 +178,13 @@ def stream_deserialize(cls, f):
178178

179179
@classmethod
180180
def serialize(cls, obj):
181-
f = BytesIO()
181+
f = _BytesIO()
182182
cls.stream_serialize(obj, f)
183183
return f.getvalue()
184184

185185
@classmethod
186186
def deserialize(cls, buf):
187-
return cls.stream_deserialize(BytesIO(buf))
187+
return cls.stream_deserialize(_BytesIO(buf))
188188

189189

190190
class VarIntSerializer(Serializer):
@@ -320,3 +320,24 @@ def uint256_to_shortstr(u):
320320
return s[:16]
321321

322322

323+
__all__ = (
324+
'MAX_SIZE',
325+
'Hash',
326+
'Hash160',
327+
'SerializationError',
328+
'SerializationTruncationError',
329+
'DeserializationExtraDataError',
330+
'ser_read',
331+
'Serializable',
332+
'ImmutableSerializable',
333+
'Serializer',
334+
'VarIntSerializer',
335+
'BytesSerializer',
336+
'VectorSerializer',
337+
'uint256VectorSerializer',
338+
'intVectorSerialzer',
339+
'VarStringSerializer',
340+
'uint256_from_str',
341+
'uint256_from_compact',
342+
'uint256_to_shortstr',
343+
)

bitcoin/messages.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@
1111

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

14+
import hashlib
15+
import random
1416
import struct
1517
import time
16-
import random
18+
19+
# Py3 compatibility
1720
import sys
1821

1922
if sys.version > '3':
20-
import io
23+
_bchr = lambda x: bytes([x])
24+
_bord = lambda x: x[0]
25+
from io import BytesIO as _BytesIO
2126
else:
22-
import cStringIO as io
27+
_bchr = chr
28+
_bord = ord
29+
from cStringIO import StringIO as _BytesIO
2330

2431
# Bad practice, so we have a __all__ at the end; this should be cleaned up
2532
# later.
@@ -45,7 +52,7 @@ def msg_deser(cls, f, protover=PROTO_VERSION):
4552
raise NotImplementedError
4653

4754
def to_bytes(self, params=MainParams()):
48-
f = BytesIO()
55+
f = _BytesIO()
4956
self.msg_ser(f)
5057
body = f.getvalue()
5158
res = params.MESSAGE_START
@@ -63,7 +70,7 @@ def to_bytes(self, params=MainParams()):
6370

6471
@classmethod
6572
def from_bytes(cls, b, protover=PROTO_VERSION):
66-
f = BytesIO(b)
73+
f = _BytesIO(b)
6774
return MsgSerializable.stream_deserialize(f, protover=protover)
6875

6976
@classmethod
@@ -93,7 +100,7 @@ def stream_deserialize(cls, f, params=MainParams(), protover=PROTO_VERSION):
93100
if command in messagemap:
94101
cls = messagemap[command]
95102
# print("Going to deserialize '%s'" % msg)
96-
return cls.msg_deser(BytesIO(msg))
103+
return cls.msg_deser(_BytesIO(msg))
97104
else:
98105
print("Command '%s' not in messagemap" % str(command, 'ascii'))
99106
return None

bitcoin/rpc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,9 @@ def addnodeonetry(self, node):
466466

467467
def removenode(self, node):
468468
return self._addnode(node, 'remove')
469+
470+
__all__ = (
471+
'JSONRPCException',
472+
'RawProxy',
473+
'Proxy',
474+
)

0 commit comments

Comments
 (0)