Skip to content

Commit f31fc0e

Browse files
committed
[tests] fix flake8 warnings in script.py and bignum.py
1 parent 6a97e8a commit f31fc0e

File tree

2 files changed

+144
-141
lines changed

2 files changed

+144
-141
lines changed

test/functional/test_framework/bignum.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Big number routines.
66
7+
Functions for converting numbers to MPI format and Bitcoin-specific little
8+
endian format.
9+
710
This file is copied from python-bitcoinlib.
811
"""
9-
1012
import struct
1113

12-
13-
# generic big endian MPI format
14-
1514
def bn_bytes(v, have_ext=False):
1615
ext = 0
1716
if have_ext:
1817
ext = 1
19-
return ((v.bit_length()+7)//8) + ext
18+
return (v.bit_length() + 7) // 8 + ext
2019

2120
def bn2bin(v):
2221
s = bytearray()
2322
i = bn_bytes(v)
2423
while i > 0:
25-
s.append((v >> ((i-1) * 8)) & 0xff)
24+
s.append((v >> ((i - 1) * 8)) & 0xff)
2625
i -= 1
2726
return s
2827

test/functional/test_framework/script.py

Lines changed: 139 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66
77
This file is modified from python-bitcoinlib.
88
"""
9-
10-
from .messages import CTransaction, CTxOut, sha256, hash256, uint256_from_str, ser_uint256, ser_string
11-
129
import hashlib
1310
import struct
1411

1512
from .bignum import bn2vch
13+
from .messages import (
14+
CTransaction,
15+
CTxOut,
16+
hash256,
17+
ser_string,
18+
ser_uint256,
19+
sha256,
20+
uint256_from_str,
21+
)
1622

1723
MAX_SCRIPT_ELEMENT_SIZE = 520
18-
1924
OPCODE_NAMES = {}
2025

2126
def hash160(s):
@@ -31,13 +36,13 @@ class CScriptOp(int):
3136
def encode_op_pushdata(d):
3237
"""Encode a PUSHDATA op, returning bytes"""
3338
if len(d) < 0x4c:
34-
return b'' + bytes([len(d)]) + d # OP_PUSHDATA
39+
return b'' + bytes([len(d)]) + d # OP_PUSHDATA
3540
elif len(d) <= 0xff:
36-
return b'\x4c' + bytes([len(d)]) + d # OP_PUSHDATA1
41+
return b'\x4c' + bytes([len(d)]) + d # OP_PUSHDATA1
3742
elif len(d) <= 0xffff:
38-
return b'\x4d' + struct.pack(b'<H', len(d)) + d # OP_PUSHDATA2
43+
return b'\x4d' + struct.pack(b'<H', len(d)) + d # OP_PUSHDATA2
3944
elif len(d) <= 0xffffffff:
40-
return b'\x4e' + struct.pack(b'<I', len(d)) + d # OP_PUSHDATA4
45+
return b'\x4e' + struct.pack(b'<I', len(d)) + d # OP_PUSHDATA4
4146
else:
4247
raise ValueError("Data too long to encode in a PUSHDATA op")
4348

@@ -50,7 +55,7 @@ def encode_op_n(n):
5055
if n == 0:
5156
return OP_0
5257
else:
53-
return CScriptOp(OP_1 + n-1)
58+
return CScriptOp(OP_1 + n - 1)
5459

5560
def decode_op_n(self):
5661
"""Decode a small integer opcode, returning an integer"""
@@ -60,7 +65,7 @@ def decode_op_n(self):
6065
if not (self == OP_0 or OP_1 <= self <= OP_16):
6166
raise ValueError('op %r is not an OP_N' % self)
6267

63-
return int(self - OP_1+1)
68+
return int(self - OP_1 + 1)
6469

6570
def is_small_int(self):
6671
"""Return true if the op pushes a small integer to the stack"""
@@ -87,7 +92,7 @@ def __new__(cls, n):
8792
return _opcode_instances[n]
8893

8994
# Populate opcode instance table
90-
for n in range(0xff+1):
95+
for n in range(0xff + 1):
9196
CScriptOp(n)
9297

9398

@@ -100,7 +105,7 @@ def __new__(cls, n):
100105
OP_1NEGATE = CScriptOp(0x4f)
101106
OP_RESERVED = CScriptOp(0x50)
102107
OP_1 = CScriptOp(0x51)
103-
OP_TRUE=OP_1
108+
OP_TRUE = OP_1
104109
OP_2 = CScriptOp(0x52)
105110
OP_3 = CScriptOp(0x53)
106111
OP_4 = CScriptOp(0x54)
@@ -232,122 +237,122 @@ def __new__(cls, n):
232237
OP_INVALIDOPCODE = CScriptOp(0xff)
233238

234239
OPCODE_NAMES.update({
235-
OP_0 : 'OP_0',
236-
OP_PUSHDATA1 : 'OP_PUSHDATA1',
237-
OP_PUSHDATA2 : 'OP_PUSHDATA2',
238-
OP_PUSHDATA4 : 'OP_PUSHDATA4',
239-
OP_1NEGATE : 'OP_1NEGATE',
240-
OP_RESERVED : 'OP_RESERVED',
241-
OP_1 : 'OP_1',
242-
OP_2 : 'OP_2',
243-
OP_3 : 'OP_3',
244-
OP_4 : 'OP_4',
245-
OP_5 : 'OP_5',
246-
OP_6 : 'OP_6',
247-
OP_7 : 'OP_7',
248-
OP_8 : 'OP_8',
249-
OP_9 : 'OP_9',
250-
OP_10 : 'OP_10',
251-
OP_11 : 'OP_11',
252-
OP_12 : 'OP_12',
253-
OP_13 : 'OP_13',
254-
OP_14 : 'OP_14',
255-
OP_15 : 'OP_15',
256-
OP_16 : 'OP_16',
257-
OP_NOP : 'OP_NOP',
258-
OP_VER : 'OP_VER',
259-
OP_IF : 'OP_IF',
260-
OP_NOTIF : 'OP_NOTIF',
261-
OP_VERIF : 'OP_VERIF',
262-
OP_VERNOTIF : 'OP_VERNOTIF',
263-
OP_ELSE : 'OP_ELSE',
264-
OP_ENDIF : 'OP_ENDIF',
265-
OP_VERIFY : 'OP_VERIFY',
266-
OP_RETURN : 'OP_RETURN',
267-
OP_TOALTSTACK : 'OP_TOALTSTACK',
268-
OP_FROMALTSTACK : 'OP_FROMALTSTACK',
269-
OP_2DROP : 'OP_2DROP',
270-
OP_2DUP : 'OP_2DUP',
271-
OP_3DUP : 'OP_3DUP',
272-
OP_2OVER : 'OP_2OVER',
273-
OP_2ROT : 'OP_2ROT',
274-
OP_2SWAP : 'OP_2SWAP',
275-
OP_IFDUP : 'OP_IFDUP',
276-
OP_DEPTH : 'OP_DEPTH',
277-
OP_DROP : 'OP_DROP',
278-
OP_DUP : 'OP_DUP',
279-
OP_NIP : 'OP_NIP',
280-
OP_OVER : 'OP_OVER',
281-
OP_PICK : 'OP_PICK',
282-
OP_ROLL : 'OP_ROLL',
283-
OP_ROT : 'OP_ROT',
284-
OP_SWAP : 'OP_SWAP',
285-
OP_TUCK : 'OP_TUCK',
286-
OP_CAT : 'OP_CAT',
287-
OP_SUBSTR : 'OP_SUBSTR',
288-
OP_LEFT : 'OP_LEFT',
289-
OP_RIGHT : 'OP_RIGHT',
290-
OP_SIZE : 'OP_SIZE',
291-
OP_INVERT : 'OP_INVERT',
292-
OP_AND : 'OP_AND',
293-
OP_OR : 'OP_OR',
294-
OP_XOR : 'OP_XOR',
295-
OP_EQUAL : 'OP_EQUAL',
296-
OP_EQUALVERIFY : 'OP_EQUALVERIFY',
297-
OP_RESERVED1 : 'OP_RESERVED1',
298-
OP_RESERVED2 : 'OP_RESERVED2',
299-
OP_1ADD : 'OP_1ADD',
300-
OP_1SUB : 'OP_1SUB',
301-
OP_2MUL : 'OP_2MUL',
302-
OP_2DIV : 'OP_2DIV',
303-
OP_NEGATE : 'OP_NEGATE',
304-
OP_ABS : 'OP_ABS',
305-
OP_NOT : 'OP_NOT',
306-
OP_0NOTEQUAL : 'OP_0NOTEQUAL',
307-
OP_ADD : 'OP_ADD',
308-
OP_SUB : 'OP_SUB',
309-
OP_MUL : 'OP_MUL',
310-
OP_DIV : 'OP_DIV',
311-
OP_MOD : 'OP_MOD',
312-
OP_LSHIFT : 'OP_LSHIFT',
313-
OP_RSHIFT : 'OP_RSHIFT',
314-
OP_BOOLAND : 'OP_BOOLAND',
315-
OP_BOOLOR : 'OP_BOOLOR',
316-
OP_NUMEQUAL : 'OP_NUMEQUAL',
317-
OP_NUMEQUALVERIFY : 'OP_NUMEQUALVERIFY',
318-
OP_NUMNOTEQUAL : 'OP_NUMNOTEQUAL',
319-
OP_LESSTHAN : 'OP_LESSTHAN',
320-
OP_GREATERTHAN : 'OP_GREATERTHAN',
321-
OP_LESSTHANOREQUAL : 'OP_LESSTHANOREQUAL',
322-
OP_GREATERTHANOREQUAL : 'OP_GREATERTHANOREQUAL',
323-
OP_MIN : 'OP_MIN',
324-
OP_MAX : 'OP_MAX',
325-
OP_WITHIN : 'OP_WITHIN',
326-
OP_RIPEMD160 : 'OP_RIPEMD160',
327-
OP_SHA1 : 'OP_SHA1',
328-
OP_SHA256 : 'OP_SHA256',
329-
OP_HASH160 : 'OP_HASH160',
330-
OP_HASH256 : 'OP_HASH256',
331-
OP_CODESEPARATOR : 'OP_CODESEPARATOR',
332-
OP_CHECKSIG : 'OP_CHECKSIG',
333-
OP_CHECKSIGVERIFY : 'OP_CHECKSIGVERIFY',
334-
OP_CHECKMULTISIG : 'OP_CHECKMULTISIG',
335-
OP_CHECKMULTISIGVERIFY : 'OP_CHECKMULTISIGVERIFY',
336-
OP_NOP1 : 'OP_NOP1',
337-
OP_CHECKLOCKTIMEVERIFY : 'OP_CHECKLOCKTIMEVERIFY',
338-
OP_CHECKSEQUENCEVERIFY : 'OP_CHECKSEQUENCEVERIFY',
339-
OP_NOP4 : 'OP_NOP4',
340-
OP_NOP5 : 'OP_NOP5',
341-
OP_NOP6 : 'OP_NOP6',
342-
OP_NOP7 : 'OP_NOP7',
343-
OP_NOP8 : 'OP_NOP8',
344-
OP_NOP9 : 'OP_NOP9',
345-
OP_NOP10 : 'OP_NOP10',
346-
OP_SMALLINTEGER : 'OP_SMALLINTEGER',
347-
OP_PUBKEYS : 'OP_PUBKEYS',
348-
OP_PUBKEYHASH : 'OP_PUBKEYHASH',
349-
OP_PUBKEY : 'OP_PUBKEY',
350-
OP_INVALIDOPCODE : 'OP_INVALIDOPCODE',
240+
OP_0: 'OP_0',
241+
OP_PUSHDATA1: 'OP_PUSHDATA1',
242+
OP_PUSHDATA2: 'OP_PUSHDATA2',
243+
OP_PUSHDATA4: 'OP_PUSHDATA4',
244+
OP_1NEGATE: 'OP_1NEGATE',
245+
OP_RESERVED: 'OP_RESERVED',
246+
OP_1: 'OP_1',
247+
OP_2: 'OP_2',
248+
OP_3: 'OP_3',
249+
OP_4: 'OP_4',
250+
OP_5: 'OP_5',
251+
OP_6: 'OP_6',
252+
OP_7: 'OP_7',
253+
OP_8: 'OP_8',
254+
OP_9: 'OP_9',
255+
OP_10: 'OP_10',
256+
OP_11: 'OP_11',
257+
OP_12: 'OP_12',
258+
OP_13: 'OP_13',
259+
OP_14: 'OP_14',
260+
OP_15: 'OP_15',
261+
OP_16: 'OP_16',
262+
OP_NOP: 'OP_NOP',
263+
OP_VER: 'OP_VER',
264+
OP_IF: 'OP_IF',
265+
OP_NOTIF: 'OP_NOTIF',
266+
OP_VERIF: 'OP_VERIF',
267+
OP_VERNOTIF: 'OP_VERNOTIF',
268+
OP_ELSE: 'OP_ELSE',
269+
OP_ENDIF: 'OP_ENDIF',
270+
OP_VERIFY: 'OP_VERIFY',
271+
OP_RETURN: 'OP_RETURN',
272+
OP_TOALTSTACK: 'OP_TOALTSTACK',
273+
OP_FROMALTSTACK: 'OP_FROMALTSTACK',
274+
OP_2DROP: 'OP_2DROP',
275+
OP_2DUP: 'OP_2DUP',
276+
OP_3DUP: 'OP_3DUP',
277+
OP_2OVER: 'OP_2OVER',
278+
OP_2ROT: 'OP_2ROT',
279+
OP_2SWAP: 'OP_2SWAP',
280+
OP_IFDUP: 'OP_IFDUP',
281+
OP_DEPTH: 'OP_DEPTH',
282+
OP_DROP: 'OP_DROP',
283+
OP_DUP: 'OP_DUP',
284+
OP_NIP: 'OP_NIP',
285+
OP_OVER: 'OP_OVER',
286+
OP_PICK: 'OP_PICK',
287+
OP_ROLL: 'OP_ROLL',
288+
OP_ROT: 'OP_ROT',
289+
OP_SWAP: 'OP_SWAP',
290+
OP_TUCK: 'OP_TUCK',
291+
OP_CAT: 'OP_CAT',
292+
OP_SUBSTR: 'OP_SUBSTR',
293+
OP_LEFT: 'OP_LEFT',
294+
OP_RIGHT: 'OP_RIGHT',
295+
OP_SIZE: 'OP_SIZE',
296+
OP_INVERT: 'OP_INVERT',
297+
OP_AND: 'OP_AND',
298+
OP_OR: 'OP_OR',
299+
OP_XOR: 'OP_XOR',
300+
OP_EQUAL: 'OP_EQUAL',
301+
OP_EQUALVERIFY: 'OP_EQUALVERIFY',
302+
OP_RESERVED1: 'OP_RESERVED1',
303+
OP_RESERVED2: 'OP_RESERVED2',
304+
OP_1ADD: 'OP_1ADD',
305+
OP_1SUB: 'OP_1SUB',
306+
OP_2MUL: 'OP_2MUL',
307+
OP_2DIV: 'OP_2DIV',
308+
OP_NEGATE: 'OP_NEGATE',
309+
OP_ABS: 'OP_ABS',
310+
OP_NOT: 'OP_NOT',
311+
OP_0NOTEQUAL: 'OP_0NOTEQUAL',
312+
OP_ADD: 'OP_ADD',
313+
OP_SUB: 'OP_SUB',
314+
OP_MUL: 'OP_MUL',
315+
OP_DIV: 'OP_DIV',
316+
OP_MOD: 'OP_MOD',
317+
OP_LSHIFT: 'OP_LSHIFT',
318+
OP_RSHIFT: 'OP_RSHIFT',
319+
OP_BOOLAND: 'OP_BOOLAND',
320+
OP_BOOLOR: 'OP_BOOLOR',
321+
OP_NUMEQUAL: 'OP_NUMEQUAL',
322+
OP_NUMEQUALVERIFY: 'OP_NUMEQUALVERIFY',
323+
OP_NUMNOTEQUAL: 'OP_NUMNOTEQUAL',
324+
OP_LESSTHAN: 'OP_LESSTHAN',
325+
OP_GREATERTHAN: 'OP_GREATERTHAN',
326+
OP_LESSTHANOREQUAL: 'OP_LESSTHANOREQUAL',
327+
OP_GREATERTHANOREQUAL: 'OP_GREATERTHANOREQUAL',
328+
OP_MIN: 'OP_MIN',
329+
OP_MAX: 'OP_MAX',
330+
OP_WITHIN: 'OP_WITHIN',
331+
OP_RIPEMD160: 'OP_RIPEMD160',
332+
OP_SHA1: 'OP_SHA1',
333+
OP_SHA256: 'OP_SHA256',
334+
OP_HASH160: 'OP_HASH160',
335+
OP_HASH256: 'OP_HASH256',
336+
OP_CODESEPARATOR: 'OP_CODESEPARATOR',
337+
OP_CHECKSIG: 'OP_CHECKSIG',
338+
OP_CHECKSIGVERIFY: 'OP_CHECKSIGVERIFY',
339+
OP_CHECKMULTISIG: 'OP_CHECKMULTISIG',
340+
OP_CHECKMULTISIGVERIFY: 'OP_CHECKMULTISIGVERIFY',
341+
OP_NOP1: 'OP_NOP1',
342+
OP_CHECKLOCKTIMEVERIFY: 'OP_CHECKLOCKTIMEVERIFY',
343+
OP_CHECKSEQUENCEVERIFY: 'OP_CHECKSEQUENCEVERIFY',
344+
OP_NOP4: 'OP_NOP4',
345+
OP_NOP5: 'OP_NOP5',
346+
OP_NOP6: 'OP_NOP6',
347+
OP_NOP7: 'OP_NOP7',
348+
OP_NOP8: 'OP_NOP8',
349+
OP_NOP9: 'OP_NOP9',
350+
OP_NOP10: 'OP_NOP10',
351+
OP_SMALLINTEGER: 'OP_SMALLINTEGER',
352+
OP_PUBKEYS: 'OP_PUBKEYS',
353+
OP_PUBKEYHASH: 'OP_PUBKEYHASH',
354+
OP_PUBKEY: 'OP_PUBKEY',
355+
OP_INVALIDOPCODE: 'OP_INVALIDOPCODE',
351356
})
352357

353358
class CScriptInvalidError(Exception):
@@ -392,10 +397,10 @@ def decode(vch):
392397
if len(value) == 0:
393398
return result
394399
for i, byte in enumerate(value):
395-
result |= int(byte) << 8*i
400+
result |= int(byte) << 8 * i
396401
if value[-1] >= 0x80:
397402
# Mask for all but the highest result bit
398-
num_mask = (2**(len(value)*8) - 1) >> 1
403+
num_mask = (2**(len(value) * 8) - 1) >> 1
399404
result &= num_mask
400405
result *= -1
401406
return result
@@ -493,21 +498,20 @@ def raw_iter(self):
493498
pushdata_type = 'PUSHDATA2'
494499
if i + 1 >= len(self):
495500
raise CScriptInvalidError('PUSHDATA2: missing data length')
496-
datasize = self[i] + (self[i+1] << 8)
501+
datasize = self[i] + (self[i + 1] << 8)
497502
i += 2
498503

499504
elif opcode == OP_PUSHDATA4:
500505
pushdata_type = 'PUSHDATA4'
501506
if i + 3 >= len(self):
502507
raise CScriptInvalidError('PUSHDATA4: missing data length')
503-
datasize = self[i] + (self[i+1] << 8) + (self[i+2] << 16) + (self[i+3] << 24)
508+
datasize = self[i] + (self[i + 1] << 8) + (self[i + 2] << 16) + (self[i + 3] << 24)
504509
i += 4
505510

506511
else:
507-
assert False # shouldn't happen
508-
512+
assert False # shouldn't happen
509513

510-
data = bytes(self[i:i+datasize])
514+
data = bytes(self[i:i + datasize])
511515

512516
# Check for truncation
513517
if len(data) < datasize:

0 commit comments

Comments
 (0)