Skip to content

Commit 60dd9cc

Browse files
author
MarcoFalke
committed
Merge #10781: Python cleanups
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/bitcoin#10753 (comment)) Tree-SHA512: 1238dfbc1d20f7edadea5e5406a589f293065638f6234809f0d5b6ba746dffe3d276bc5884c7af388a6c798c61a8759faaccf57f381225644754c0f61914eb4b
2 parents cee4fe1 + 7821458 commit 60dd9cc

33 files changed

+70
-398
lines changed

contrib/devtools/check-doc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from subprocess import check_output
1414
import re
15+
import sys
1516

1617
FOLDER_GREP = 'src'
1718
FOLDER_TEST = 'src/test/'
@@ -39,7 +40,7 @@ def main():
3940
print "Args unknown : %s" % len(args_unknown)
4041
print args_unknown
4142

42-
exit(len(args_need_doc))
43+
sys.exit(len(args_need_doc))
4344

4445
if __name__ == "__main__":
4546
main()

contrib/devtools/github-merge.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import argparse
2121
import hashlib
2222
import subprocess
23+
import sys
2324
import json,codecs
2425
try:
2526
from urllib.request import Request,urlopen
@@ -158,11 +159,11 @@ def main():
158159
if repo is None:
159160
print("ERROR: No repository configured. Use this command to set:", file=stderr)
160161
print("git config githubmerge.repository <owner>/<repo>", file=stderr)
161-
exit(1)
162+
sys.exit(1)
162163
if signingkey is None:
163164
print("ERROR: No GPG signing key set. Set one using:",file=stderr)
164165
print("git config --global user.signingkey <key>",file=stderr)
165-
exit(1)
166+
sys.exit(1)
166167

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

@@ -173,7 +174,7 @@ def main():
173174
# Receive pull information from github
174175
info = retrieve_pr_info(repo,pull)
175176
if info is None:
176-
exit(1)
177+
sys.exit(1)
177178
title = info['title'].strip()
178179
body = info['body'].strip()
179180
# precedence order for destination branch argument:
@@ -194,27 +195,27 @@ def main():
194195
subprocess.check_call([GIT,'checkout','-q',branch])
195196
except subprocess.CalledProcessError as e:
196197
print("ERROR: Cannot check out branch %s." % (branch), file=stderr)
197-
exit(3)
198+
sys.exit(3)
198199
try:
199200
subprocess.check_call([GIT,'fetch','-q',host_repo,'+refs/pull/'+pull+'/*:refs/heads/pull/'+pull+'/*'])
200201
except subprocess.CalledProcessError as e:
201202
print("ERROR: Cannot find pull request #%s on %s." % (pull,host_repo), file=stderr)
202-
exit(3)
203+
sys.exit(3)
203204
try:
204205
subprocess.check_call([GIT,'log','-q','-1','refs/heads/'+head_branch], stdout=devnull, stderr=stdout)
205206
except subprocess.CalledProcessError as e:
206207
print("ERROR: Cannot find head of pull request #%s on %s." % (pull,host_repo), file=stderr)
207-
exit(3)
208+
sys.exit(3)
208209
try:
209210
subprocess.check_call([GIT,'log','-q','-1','refs/heads/'+merge_branch], stdout=devnull, stderr=stdout)
210211
except subprocess.CalledProcessError as e:
211212
print("ERROR: Cannot find merge of pull request #%s on %s." % (pull,host_repo), file=stderr)
212-
exit(3)
213+
sys.exit(3)
213214
try:
214215
subprocess.check_call([GIT,'fetch','-q',host_repo,'+refs/heads/'+branch+':refs/heads/'+base_branch])
215216
except subprocess.CalledProcessError as e:
216217
print("ERROR: Cannot find branch %s on %s." % (branch,host_repo), file=stderr)
217-
exit(3)
218+
sys.exit(3)
218219
subprocess.check_call([GIT,'checkout','-q',base_branch])
219220
subprocess.call([GIT,'branch','-q','-D',local_merge_branch], stderr=devnull)
220221
subprocess.check_call([GIT,'checkout','-q','-b',local_merge_branch])
@@ -236,30 +237,30 @@ def main():
236237
except subprocess.CalledProcessError as e:
237238
print("ERROR: Cannot be merged cleanly.",file=stderr)
238239
subprocess.check_call([GIT,'merge','--abort'])
239-
exit(4)
240+
sys.exit(4)
240241
logmsg = subprocess.check_output([GIT,'log','--pretty=format:%s','-n','1']).decode('utf-8')
241242
if logmsg.rstrip() != firstline.rstrip():
242243
print("ERROR: Creating merge failed (already merged?).",file=stderr)
243-
exit(4)
244+
sys.exit(4)
244245

245246
symlink_files = get_symlink_files()
246247
for f in symlink_files:
247248
print("ERROR: File %s was a symlink" % f)
248249
if len(symlink_files) > 0:
249-
exit(4)
250+
sys.exit(4)
250251

251252
# Put tree SHA512 into the message
252253
try:
253254
first_sha512 = tree_sha512sum()
254255
message += '\n\nTree-SHA512: ' + first_sha512
255256
except subprocess.CalledProcessError as e:
256-
printf("ERROR: Unable to compute tree hash")
257-
exit(4)
257+
print("ERROR: Unable to compute tree hash")
258+
sys.exit(4)
258259
try:
259260
subprocess.check_call([GIT,'commit','--amend','-m',message.encode('utf-8')])
260261
except subprocess.CalledProcessError as e:
261-
printf("ERROR: Cannot update message.",file=stderr)
262-
exit(4)
262+
print("ERROR: Cannot update message.", file=stderr)
263+
sys.exit(4)
263264

264265
print_merge_details(pull, title, branch, base_branch, head_branch)
265266
print()
@@ -268,7 +269,7 @@ def main():
268269
if testcmd:
269270
if subprocess.call(testcmd,shell=True):
270271
print("ERROR: Running %s failed." % testcmd,file=stderr)
271-
exit(5)
272+
sys.exit(5)
272273

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

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

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

331332
if __name__ == '__main__':
332333
main()

contrib/devtools/security-check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,5 @@ def identify_executable(executable):
212212
except IOError:
213213
print('%s: cannot open' % filename)
214214
retval = 1
215-
exit(retval)
215+
sys.exit(retval)
216216

contrib/devtools/symbol-check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,6 @@ def read_libraries(filename):
159159
print('%s: NEEDED library %s is not allowed' % (filename, library_name.decode('utf-8')))
160160
retval = 1
161161

162-
exit(retval)
162+
sys.exit(retval)
163163

164164

contrib/devtools/update-translations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ def check_at_repository_root():
3636
if not os.path.exists('.git'):
3737
print('No .git directory found')
3838
print('Execute this script at the root of the repository', file=sys.stderr)
39-
exit(1)
39+
sys.exit(1)
4040

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

4646
def find_format_specifiers(s):
4747
'''Find all format specifiers in a string.'''

contrib/linearize/linearize-hashes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def get_block_hashes(settings, max_blocks_per_call=10000):
8787
for x,resp_obj in enumerate(reply):
8888
if rpc.response_is_error(resp_obj):
8989
print('JSON-RPC: error at height', height+x, ': ', resp_obj['error'], file=sys.stderr)
90-
exit(1)
90+
sys.exit(1)
9191
assert(resp_obj['id'] == x) # assume replies are in-sequence
9292
if settings['rev_hash_bytes'] == 'true':
9393
resp_obj['result'] = hex_switchEndian(resp_obj['result'])
@@ -140,7 +140,7 @@ def get_rpc_cookie():
140140
if 'datadir' in settings and not use_userpass:
141141
use_datadir = True
142142
if not use_userpass and not use_datadir:
143-
print("Missing datadir or username and/or password in cfg file", file=stderr)
143+
print("Missing datadir or username and/or password in cfg file", file=sys.stderr)
144144
sys.exit(1)
145145

146146
settings['port'] = int(settings['port'])

contrib/seeds/generate-seeds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def process_nodes(g, f, structname, defaultport):
114114
def main():
115115
if len(sys.argv)<2:
116116
print(('Usage: %s <path_to_nodes_txt>' % sys.argv[0]), file=sys.stderr)
117-
exit(1)
117+
sys.exit(1)
118118
g = sys.stdout
119119
indir = sys.argv[1]
120120
g.write('#ifndef BITCOIN_CHAINPARAMSSEEDS_H\n')

contrib/zmq/zmq_sub.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

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

3737
port = 28332
3838

contrib/zmq/zmq_sub3.4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

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

4141
port = 28332
4242

share/qt/extract_strings_qt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def parse_po(text):
5858
if not XGETTEXT:
5959
print('Cannot extract strings: xgettext utility is not installed or not configured.',file=sys.stderr)
6060
print('Please install package "gettext" and re-run \'./configure\'.',file=sys.stderr)
61-
exit(1)
61+
sys.exit(1)
6262
child = Popen([XGETTEXT,'--output=-','-n','--keyword=_'] + files, stdout=PIPE)
6363
(out, err) = child.communicate()
6464

0 commit comments

Comments
 (0)