Skip to content

Commit 0c57920

Browse files
committed
Merge bitcoin/bitcoin#25867: lint: enable E722 do not use bare except
61bb4e7 lint: enable E722 do not use bare except (Leonardo Lazzaro) Pull request description: Improve test code and enable E722 lint check. If you want to catch all exceptions that signal program errors, use except Exception: (bare except is equivalent to except BaseException:). Reference: https://peps.python.org/pep-0008/#programming-recommendations ACKs for top commit: MarcoFalke: lgtm ACK 61bb4e7 Tree-SHA512: c7497769d5745fa02c78a20f4a0e555d8d3996d64af6faf1ce28e22ac1d8be415b98e967294679007b7bda2a9fd04031a9d140b24201e00257ceadeb5c5d7665
2 parents 80f4979 + 61bb4e7 commit 0c57920

10 files changed

+11
-10
lines changed

contrib/devtools/security-check.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def check_ELF_RELRO(binary) -> bool:
3434
flags = binary.get(lief.ELF.DYNAMIC_TAGS.FLAGS)
3535
if flags.value & lief.ELF.DYNAMIC_FLAGS.BIND_NOW:
3636
have_bindnow = True
37-
except:
37+
except Exception:
3838
have_bindnow = False
3939

4040
return have_gnu_relro and have_bindnow

contrib/signet/getcoins.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def bitcoin_cli(rpc_command_and_params):
142142

143143
try:
144144
res = session.post(args.faucet, data=data)
145-
except:
145+
except Exception:
146146
raise SystemExit(f"Unexpected error when contacting faucet: {sys.exc_info()[0]}")
147147

148148
# Display the output as per the returned status code

test/functional/feature_dbcrash.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def restart_node(self, node_index, expected_tip):
8585
self.nodes[node_index].waitforblock(expected_tip)
8686
utxo_hash = self.nodes[node_index].gettxoutsetinfo()['hash_serialized_2']
8787
return utxo_hash
88-
except:
88+
except Exception:
8989
# An exception here should mean the node is about to crash.
9090
# If bitcoind exits, then try again. wait_for_node_exit()
9191
# should raise an exception if bitcoind doesn't exit.

test/functional/p2p_node_network_limited.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def run_test(self):
8585
self.connect_nodes(0, 2)
8686
try:
8787
self.sync_blocks([self.nodes[0], self.nodes[2]], timeout=5)
88-
except:
88+
except Exception:
8989
pass
9090
# node2 must remain at height 0
9191
assert_equal(self.nodes[2].getblockheader(self.nodes[2].getbestblockhash())['height'], 0)

test/functional/rpc_preciousblock.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def unidirectional_node_sync_via_rpc(node_src, node_dest):
1616
try:
1717
assert len(node_dest.getblock(blockhash, False)) > 0
1818
break
19-
except:
19+
except Exception:
2020
blocks_to_copy.append(blockhash)
2121
blockhash = node_src.getblockheader(blockhash, True)['previousblockhash']
2222
blocks_to_copy.reverse()

test/functional/test_framework/p2p.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def on_message(self, message):
385385
self.message_count[msgtype] += 1
386386
self.last_message[msgtype] = message
387387
getattr(self, 'on_' + msgtype)(message)
388-
except:
388+
except Exception:
389389
print("ERROR delivering %s (%s)" % (repr(message), sys.exc_info()[0]))
390390
raise
391391

test/functional/test_framework/test_framework.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ def start_nodes(self, extra_args=None, *args, **kwargs):
557557
node.start(extra_args[i], *args, **kwargs)
558558
for node in self.nodes:
559559
node.wait_for_rpc_connection()
560-
except:
560+
except Exception:
561561
# If one node failed to start, stop the others
562562
self.stop_nodes()
563563
raise

test/functional/test_framework/test_node.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ def importaddress(self, address, label=None, rescan=None, p2sh=None):
829829
int(address ,16)
830830
is_hex = True
831831
desc = descsum_create('raw(' + address + ')')
832-
except:
832+
except Exception:
833833
desc = descsum_create('addr(' + address + ')')
834834
reqs = [{
835835
'desc': desc,

test/lint/lint-python.py

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
'E711,' # comparison to None should be 'if cond is None:'
4848
'E714,' # test for object identity should be "is not"
4949
'E721,' # do not compare types, use "isinstance()"
50+
'E722,' # do not use bare 'except'
5051
'E742,' # do not define classes named "l", "O", or "I"
5152
'E743,' # do not define functions named "l", "O", or "I"
5253
'E901,' # SyntaxError: invalid syntax

test/util/test_runner.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def bctester(testDir, input_basename, buildenv):
5454
try:
5555
bctest(testDir, testObj, buildenv)
5656
logging.info("PASSED: " + testObj["description"])
57-
except:
57+
except Exception:
5858
logging.info("FAILED: " + testObj["description"])
5959
failed_testcases.append(testObj["description"])
6060

@@ -96,7 +96,7 @@ def bctest(testDir, testObj, buildenv):
9696
try:
9797
with open(os.path.join(testDir, outputFn), encoding="utf8") as f:
9898
outputData = f.read()
99-
except:
99+
except Exception:
100100
logging.error("Output file " + outputFn + " cannot be opened")
101101
raise
102102
if not outputData:

0 commit comments

Comments
 (0)