Python cleanups #10781

Open
wants to merge 6 commits into
from

Conversation

Projects
None yet
4 participants
Contributor

practicalswift commented Jul 9, 2017 edited

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
@jnewbery

Looks good. utACK. One question inline and another general question:

In commit Use the variable name _ to show that we are intentionally ignoring a return value, why not just ignore the return value?

@@ -293,7 +293,7 @@ def test_compactblock_construction(self, node, test_node, version, use_witness_a
# 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]
@jnewbery

jnewbery Jul 10, 2017

Member

I can't see how using tx is an issue. Does this cause a linter warning?

@practicalswift

practicalswift Jul 16, 2017

Contributor

Yes it does :-)

$ flake8 test/functional/p2p-compactblocks.py | grep redefine
test/functional/p2p-compactblocks.py:296:31: F812 list comprehension redefines 'tx' from line 276

Should I remove the commit 263686d from this PR?

@jnewbery

jnewbery Jul 20, 2017

Member

actually, let's just swap this list comprehension for a for loop. We're using it for its side-effects, so a for loop is clearer.

test/functional/bumpfee.py
@@ -168,7 +168,7 @@ def test_bumpfee_with_descendant_fails(rbf_node, rbf_node_address, dest_address)
parent_id = spend_one_input(rbf_node, rbf_node_address)
tx = rbf_node.createrawtransaction([{"txid": parent_id, "vout": 0}], {dest_address: 0.00020000})
tx = rbf_node.signrawtransaction(tx)
- txid = rbf_node.sendrawtransaction(tx["hex"])
+ _ = rbf_node.sendrawtransaction(tx["hex"])
@jtimon

jtimon Jul 13, 2017

Member

what about something more clear like throwaway_txid or something of the sort?

Member

jtimon commented Jul 13, 2017

fast review ACK

Contributor

practicalswift commented Jul 16, 2017 edited

@jnewbery Good point! I've now changed from _ = f() to f() for calls that appear to have side effects. For calls that appear to have no side effects I've simply removed the calls.

@jnewbery @jtimon Could you take a look at the updated version and see if it looks correct? :-)

Contributor

practicalswift commented Jul 20, 2017

Added two commits :-)

test/functional/test_framework/socks5.py
@@ -91,7 +91,7 @@ def handle(self):
self.conn.sendall(bytearray([0x01, 0x00]))
# Read connect request
- (ver,cmd,rsv,atyp) = recvall(self.conn, 4)
+ (ver, cmd, _, atyp) = recvall(self.conn, 4)
@jnewbery

jnewbery Jul 20, 2017 edited

Member

no need for this to be a tuple. Just:

ver, cmd, _, atyp = recvall(self.conn, 4)

There are other examples in commit Use the variable name _ for unused return values. I won't comment on each of them.

test/functional/preciousblock.py
@@ -47,16 +47,16 @@ def run_test(self):
self.log.info("Ensure submitblock can in principle reorg to a competing chain")
self.nodes[0].generate(1)
assert_equal(self.nodes[0].getblockcount(), 1)
- (hashY, hashZ) = self.nodes[1].generate(2)
+ (_, hashZ) = self.nodes[1].generate(2)
@jnewbery

jnewbery Jul 20, 2017

Member

prefer:

hashZ = self.nodes[1].generate(2)[-1]

since it conveys the meaning better: "give me the hash of the last block generated".

same for the two examples below

@@ -15,17 +15,7 @@
import os
from binascii import unhexlify, hexlify
-STATE_ESTABLISHED = '01'
@jnewbery

jnewbery Jul 20, 2017

Member

Perhaps change these to comments rather than erase them (for context).

@practicalswift

practicalswift Jul 20, 2017

Contributor

Good point! Fixed!

@@ -48,8 +48,6 @@
COIN = 100000000 # 1 btc in satoshis
NODE_NETWORK = (1 << 0)
-NODE_GETUTXO = (1 << 1)
@jnewbery

jnewbery Jul 20, 2017

Member

No harm in leaving these as comments.

@practicalswift

practicalswift Jul 20, 2017 edited

Contributor

Good point! Updated but GitHub doesn't show the # NODE_GETUTXO = (1 << 1) below (in the comment view), so it appears as if it is not fixed :-)

test/functional/test_framework/key.py
@@ -112,40 +112,16 @@ def set_secretbytes(self, secret):
ssl.BN_CTX_free(ctx)
return self.k
- def set_privkey(self, key):
@jnewbery

jnewbery Jul 20, 2017

Member

I can imagine these methods potentially being useful at some point. I don't think there's any need to remove them.

@@ -158,11 +158,6 @@ def __call__(self, *args, **argsn):
else:
return response['result']
- def _batch(self, rpc_call_list):
@jnewbery

jnewbery Jul 20, 2017

Member

Please leave this for now.

Member

jnewbery commented Jul 20, 2017

Mostly ACK, but a few comments.

Contributor

practicalswift commented Jul 20, 2017

@jnewbery Thanks for reviewing! Good feedback - I've addressed all points raised. Would you mind re-reviewing? :-)

Member

jnewbery commented Jul 20, 2017

Looks good. Just #10781 (comment) unaddressed (using a for loop instead of list comprehension for functions with side-effects)

Contributor

practicalswift commented Jul 20, 2017 edited

@jnewbery Thanks! List comprehension now fixed as well. Looks good? :-)

Member

jnewbery commented Jul 20, 2017

looks good. utACK db442ad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment