Skip to content

Commit

Permalink
Merge bitcoin#10781: Python cleanups
Browse files Browse the repository at this point in the history
7821458 Use for-loop instead of list comprehension (practicalswift)
8239794 Use the variable name _ for unused return values (practicalswift)
2e6080b Remove unused variables and/or function calls (practicalswift)
9b94054 Avoid reference to undefined name: stderr does not exist, sys.stderr does (practicalswift)
51cb6b8 Use print(...) instead of undefined printf(...) (practicalswift)
25cd520 Use sys.exit(...) instead of exit(...): exit(...) should not be used in programs (practicalswift)

Pull request description:

  Python cleanups:
  * Avoid reference to undefined name: `stderr` does not exist, `sys.stderr` does
  * Use `print(...)` instead of undefined `printf(...)`
  * Avoid redefinition of variable (`tx`) in list comprehension
  * Remove unused variables and/or function calls
  * Use `sys.exit(...)` instead of `exit(...)`: [`exit(...)` should not be used in programs](bitcoin#10753 (comment))

Tree-SHA512: 1238dfbc1d20f7edadea5e5406a589f293065638f6234809f0d5b6ba746dffe3d276bc5884c7af388a6c798c61a8759faaccf57f381225644754c0f61914eb4b
  • Loading branch information
MarcoFalke authored and PastaPastaPasta committed Jan 2, 2020
1 parent 0ee32a7 commit c7aa2d6
Show file tree
Hide file tree
Showing 28 changed files with 66 additions and 382 deletions.
3 changes: 2 additions & 1 deletion contrib/devtools/check-doc.py
Expand Up @@ -12,6 +12,7 @@

from subprocess import check_output
import re
import sys

FOLDER_GREP = 'src'
FOLDER_TEST = 'src/test/'
Expand Down Expand Up @@ -39,7 +40,7 @@ def main():
print("Args unknown : {}".format(len(args_unknown)))
print(args_unknown)

exit(len(args_need_doc))
sys.exit(len(args_need_doc))

if __name__ == "__main__":
main()
41 changes: 21 additions & 20 deletions contrib/devtools/github-merge.py
Expand Up @@ -20,6 +20,7 @@
import argparse
import hashlib
import subprocess
import sys
import json,codecs
try:
from urllib.request import Request,urlopen
Expand Down Expand Up @@ -158,11 +159,11 @@ def main():
if repo is None:
print("ERROR: No repository configured. Use this command to set:", file=stderr)
print("git config githubmerge.repository <owner>/<repo>", file=stderr)
exit(1)
sys.exit(1)
if signingkey is None:
print("ERROR: No GPG signing key set. Set one using:",file=stderr)
print("git config --global user.signingkey <key>",file=stderr)
exit(1)
sys.exit(1)

host_repo = host+":"+repo # shortcut for push/pull target

Expand All @@ -173,7 +174,7 @@ def main():
# Receive pull information from github
info = retrieve_pr_info(repo,pull)
if info is None:
exit(1)
sys.exit(1)
title = info['title'].strip()
body = info['body'].strip()
# precedence order for destination branch argument:
Expand All @@ -194,27 +195,27 @@ def main():
subprocess.check_call([GIT,'checkout','-q',branch])
except subprocess.CalledProcessError as e:
print("ERROR: Cannot check out branch %s." % (branch), file=stderr)
exit(3)
sys.exit(3)
try:
subprocess.check_call([GIT,'fetch','-q',host_repo,'+refs/pull/'+pull+'/*:refs/heads/pull/'+pull+'/*'])
except subprocess.CalledProcessError as e:
print("ERROR: Cannot find pull request #%s on %s." % (pull,host_repo), file=stderr)
exit(3)
sys.exit(3)
try:
subprocess.check_call([GIT,'log','-q','-1','refs/heads/'+head_branch], stdout=devnull, stderr=stdout)
except subprocess.CalledProcessError as e:
print("ERROR: Cannot find head of pull request #%s on %s." % (pull,host_repo), file=stderr)
exit(3)
sys.exit(3)
try:
subprocess.check_call([GIT,'log','-q','-1','refs/heads/'+merge_branch], stdout=devnull, stderr=stdout)
except subprocess.CalledProcessError as e:
print("ERROR: Cannot find merge of pull request #%s on %s." % (pull,host_repo), file=stderr)
exit(3)
sys.exit(3)
try:
subprocess.check_call([GIT,'fetch','-q',host_repo,'+refs/heads/'+branch+':refs/heads/'+base_branch])
except subprocess.CalledProcessError as e:
print("ERROR: Cannot find branch %s on %s." % (branch,host_repo), file=stderr)
exit(3)
sys.exit(3)
subprocess.check_call([GIT,'checkout','-q',base_branch])
subprocess.call([GIT,'branch','-q','-D',local_merge_branch], stderr=devnull)
subprocess.check_call([GIT,'checkout','-q','-b',local_merge_branch])
Expand All @@ -236,30 +237,30 @@ def main():
except subprocess.CalledProcessError as e:
print("ERROR: Cannot be merged cleanly.",file=stderr)
subprocess.check_call([GIT,'merge','--abort'])
exit(4)
sys.exit(4)
logmsg = subprocess.check_output([GIT,'log','--pretty=format:%s','-n','1']).decode('utf-8')
if logmsg.rstrip() != firstline.rstrip():
print("ERROR: Creating merge failed (already merged?).",file=stderr)
exit(4)
sys.exit(4)

symlink_files = get_symlink_files()
for f in symlink_files:
print("ERROR: File %s was a symlink" % f)
if len(symlink_files) > 0:
exit(4)
sys.exit(4)

# Put tree SHA512 into the message
try:
first_sha512 = tree_sha512sum()
message += '\n\nTree-SHA512: ' + first_sha512
except subprocess.CalledProcessError as e:
printf("ERROR: Unable to compute tree hash")
exit(4)
print("ERROR: Unable to compute tree hash")
sys.exit(4)
try:
subprocess.check_call([GIT,'commit','--amend','-m',message.encode('utf-8')])
except subprocess.CalledProcessError as e:
printf("ERROR: Cannot update message.",file=stderr)
exit(4)
print("ERROR: Cannot update message.", file=stderr)
sys.exit(4)

print_merge_details(pull, title, branch, base_branch, head_branch)
print()
Expand All @@ -268,7 +269,7 @@ def main():
if testcmd:
if subprocess.call(testcmd,shell=True):
print("ERROR: Running %s failed." % testcmd,file=stderr)
exit(5)
sys.exit(5)

# Show the created merge.
diff = subprocess.check_output([GIT,'diff',merge_branch+'..'+local_merge_branch])
Expand All @@ -279,7 +280,7 @@ def main():
if reply.lower() == 'ignore':
print("Difference with github ignored.",file=stderr)
else:
exit(6)
sys.exit(6)
else:
# Verify the result manually.
print("Dropping you on a shell so you can try building/testing the merged source.",file=stderr)
Expand All @@ -292,7 +293,7 @@ def main():
second_sha512 = tree_sha512sum()
if first_sha512 != second_sha512:
print("ERROR: Tree hash changed unexpectedly",file=stderr)
exit(8)
sys.exit(8)

# Sign the merge commit.
print_merge_details(pull, title, branch, base_branch, head_branch)
Expand All @@ -306,7 +307,7 @@ def main():
print("Error while signing, asking again.",file=stderr)
elif reply == 'x':
print("Not signing off on merge, exiting.",file=stderr)
exit(1)
sys.exit(1)

# Put the result in branch.
subprocess.check_call([GIT,'checkout','-q',branch])
Expand All @@ -326,7 +327,7 @@ def main():
subprocess.check_call([GIT,'push',host_repo,'refs/heads/'+branch])
break
elif reply == 'x':
exit(1)
sys.exit(1)

if __name__ == '__main__':
main()
Expand Down
2 changes: 1 addition & 1 deletion contrib/devtools/security-check.py
Expand Up @@ -211,5 +211,5 @@ def identify_executable(executable):
except IOError:
print('%s: cannot open' % filename)
retval = 1
exit(retval)
sys.exit(retval)

2 changes: 1 addition & 1 deletion contrib/devtools/symbol-check.py
Expand Up @@ -158,6 +158,6 @@ def read_libraries(filename):
print('%s: NEEDED library %s is not allowed' % (filename, library_name))
retval = 1

exit(retval)
sys.exit(retval)


4 changes: 2 additions & 2 deletions contrib/devtools/update-translations.py
Expand Up @@ -35,12 +35,12 @@ def check_at_repository_root():
if not os.path.exists('.git'):
print('No .git directory found')
print('Execute this script at the root of the repository', file=sys.stderr)
exit(1)
sys.exit(1)

def fetch_all_translations():
if subprocess.call([TX, 'pull', '-f', '-a']):
print('Error while fetching translations', file=sys.stderr)
exit(1)
sys.exit(1)

def find_format_specifiers(s):
'''Find all format specifiers in a string.'''
Expand Down
4 changes: 2 additions & 2 deletions contrib/linearize/linearize-hashes.py
Expand Up @@ -87,7 +87,7 @@ def get_block_hashes(settings, max_blocks_per_call=10000):
for x,resp_obj in enumerate(reply):
if rpc.response_is_error(resp_obj):
print('JSON-RPC: error at height', height+x, ': ', resp_obj['error'], file=sys.stderr)
exit(1)
sys.exit(1)
assert(resp_obj['id'] == x) # assume replies are in-sequence
if settings['rev_hash_bytes'] == 'true':
resp_obj['result'] = hex_switchEndian(resp_obj['result'])
Expand Down Expand Up @@ -140,7 +140,7 @@ def get_rpc_cookie():
if 'datadir' in settings and not use_userpass:
use_datadir = True
if not use_userpass and not use_datadir:
print("Missing datadir or username and/or password in cfg file", file=stderr)
print("Missing datadir or username and/or password in cfg file", file=sys.stderr)
sys.exit(1)

settings['port'] = int(settings['port'])
Expand Down
2 changes: 1 addition & 1 deletion contrib/seeds/generate-seeds.py
Expand Up @@ -114,7 +114,7 @@ def process_nodes(g, f, structname, defaultport):
def main():
if len(sys.argv)<2:
print(('Usage: %s <path_to_nodes_txt>' % sys.argv[0]), file=sys.stderr)
exit(1)
sys.exit(1)
g = sys.stdout
indir = sys.argv[1]
g.write('#ifndef DASH_CHAINPARAMSSEEDS_H\n')
Expand Down
2 changes: 1 addition & 1 deletion contrib/zmq/zmq_sub.py
Expand Up @@ -32,7 +32,7 @@

if not (sys.version_info.major >= 3 and sys.version_info.minor >= 5):
print("This example only works with Python 3.5 and greater")
exit(1)
sys.exit(1)

port = 28332

Expand Down
2 changes: 1 addition & 1 deletion contrib/zmq/zmq_sub3.4.py
Expand Up @@ -36,7 +36,7 @@

if not (sys.version_info.major >= 3 and sys.version_info.minor >= 4):
print("This example only works with Python 3.4 and greater")
exit(1)
sys.exit(1)

port = 28332

Expand Down
2 changes: 1 addition & 1 deletion share/qt/extract_strings_qt.py
Expand Up @@ -57,7 +57,7 @@ def parse_po(text):
if not XGETTEXT:
print('Cannot extract strings: xgettext utility is not installed or not configured.',file=sys.stderr)
print('Please install package "gettext" and re-run \'./configure\'.',file=sys.stderr)
exit(1)
sys.exit(1)
child = Popen([XGETTEXT,'--output=-','-n','--keyword=_'] + files, stdout=PIPE)
(out, err) = child.communicate()

Expand Down
2 changes: 1 addition & 1 deletion test/functional/bip68-sequence.py
Expand Up @@ -388,7 +388,7 @@ def test_version2_relay(self):
tx = FromHex(CTransaction(), rawtxfund)
tx.nVersion = 2
tx_signed = self.nodes[1].signrawtransaction(ToHex(tx))["hex"]
tx_id = self.nodes[1].sendrawtransaction(tx_signed)
self.nodes[1].sendrawtransaction(tx_signed)

if __name__ == '__main__':
BIP68Test().main()
1 change: 0 additions & 1 deletion test/functional/fundrawtransaction.py
Expand Up @@ -310,7 +310,6 @@ def run_test(self):
##############################################
# test a fundrawtransaction with invalid vin #
##############################################
listunspent = self.nodes[2].listunspent()
inputs = [ {'txid' : "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout' : 0} ] #invalid vin!
outputs = { self.nodes[0].getnewaddress() : 10}
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
Expand Down
1 change: 0 additions & 1 deletion test/functional/import-rescan.py
Expand Up @@ -167,7 +167,6 @@ def run_test(self):
variant.check()

# Create new transactions sending to each address.
fee = self.nodes[0].getnetworkinfo()["relayfee"]
for i, variant in enumerate(IMPORT_VARIANTS):
variant.sent_amount = 10 - (2 * i + 1) / 8.0
variant.sent_txid = self.nodes[0].sendtoaddress(variant.address["address"], variant.sent_amount)
Expand Down
13 changes: 0 additions & 13 deletions test/functional/importmulti.py
Expand Up @@ -20,16 +20,7 @@ def run_test (self):
self.nodes[1].generate(1)
timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']

# keyword definition
PRIV_KEY = 'privkey'
PUB_KEY = 'pubkey'
ADDRESS_KEY = 'address'
SCRIPT_KEY = 'script'


node0_address1 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
node0_address2 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())
node0_address3 = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())

#Check only one address
assert_equal(node0_address1['ismine'], True)
Expand Down Expand Up @@ -241,7 +232,6 @@ def run_test (self):
transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
self.nodes[1].generate(1)
timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
transaction = self.nodes[1].gettransaction(transactionid)

self.log.info("Should import a p2sh")
result = self.nodes[1].importmulti([{
Expand Down Expand Up @@ -269,7 +259,6 @@ def run_test (self):
transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
self.nodes[1].generate(1)
timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
transaction = self.nodes[1].gettransaction(transactionid)

self.log.info("Should import a p2sh with respective redeem script")
result = self.nodes[1].importmulti([{
Expand Down Expand Up @@ -297,7 +286,6 @@ def run_test (self):
transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
self.nodes[1].generate(1)
timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
transaction = self.nodes[1].gettransaction(transactionid)

self.log.info("Should import a p2sh with respective redeem script and private keys")
result = self.nodes[1].importmulti([{
Expand Down Expand Up @@ -325,7 +313,6 @@ def run_test (self):
transactionid = self.nodes[1].sendtoaddress(multi_sig_script['address'], 10.00)
self.nodes[1].generate(1)
timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
transaction = self.nodes[1].gettransaction(transactionid)

self.log.info("Should import a p2sh with respective redeem script and private keys")
result = self.nodes[1].importmulti([{
Expand Down
5 changes: 2 additions & 3 deletions test/functional/importprunedfunds.py
Expand Up @@ -21,7 +21,6 @@ def run_test(self):
address1 = self.nodes[0].getnewaddress()
# pubkey
address2 = self.nodes[0].getnewaddress()
address2_pubkey = self.nodes[0].validateaddress(address2)['pubkey'] # Using pubkey
# privkey
address3 = self.nodes[0].getnewaddress()
address3_privkey = self.nodes[0].dumpprivkey(address3) # Using privkey
Expand Down Expand Up @@ -74,13 +73,13 @@ def run_test(self):

#Import with affiliated address with no rescan
self.nodes[1].importaddress(address2, "add2", False)
result2 = self.nodes[1].importprunedfunds(rawtxn2, proof2)
self.nodes[1].importprunedfunds(rawtxn2, proof2)
balance2 = self.nodes[1].getbalance("add2", 0, False, True)
assert_equal(balance2, Decimal('0.05'))

#Import with private key with no rescan
self.nodes[1].importprivkey(privkey=address3_privkey, label="add3", rescan=False)
result3 = self.nodes[1].importprunedfunds(rawtxn3, proof3)
self.nodes[1].importprunedfunds(rawtxn3, proof3)
balance3 = self.nodes[1].getbalance("add3", 0, False, False)
assert_equal(balance3, Decimal('0.025'))
balance3 = self.nodes[1].getbalance("*", 0, False, True)
Expand Down
2 changes: 1 addition & 1 deletion test/functional/mempool_packages.py
Expand Up @@ -211,7 +211,7 @@ def run_test(self):
value = send_value

# Create tx1
(tx1_id, tx1_value) = self.chain_transaction(self.nodes[0], tx0_id, 0, value, fee, 1)
tx1_id, _ = self.chain_transaction(self.nodes[0], tx0_id, 0, value, fee, 1)

# Create tx2-7
vout = 1
Expand Down
3 changes: 2 additions & 1 deletion test/functional/p2p-compactblocks.py
Expand Up @@ -258,7 +258,8 @@ def test_compactblock_construction(self, node, test_node, version):

# Store the raw block in our internal format.
block = FromHex(CBlock(), node.getblock("%02x" % block_hash, False))
[tx.calc_sha256() for tx in block.vtx]
for tx in block.vtx:
tx.calc_sha256()
block.rehash()

# Wait until the block was announced (via compact blocks)
Expand Down

0 comments on commit c7aa2d6

Please sign in to comment.