Skip to content

Conversation

@jnewbery
Copy link
Contributor

@jnewbery jnewbery commented Mar 7, 2017

This PR is part of a larger effort to make the test_framework API more intuitive. See #9876 for background.

Several testcases use a wildcard import from test_framework.script. That's not ideal as it pollutes the namespace with all of the names defined in test_framework.script, including anything that script.py has imported itself.

This PR adds an __all__ module variable to script.py which indexes all of the names that should commonly be required by individual test cases. That makes the wildcard import a well-defined API and test cases can wildcard import without worrying about importing unexpected names. If a test case needs to import something from script.py which isn't included in __all__, it can explicitly import it.

I've not included hash160() or CScriptOp() in __all__ because hash160() is a helper function which I think should be defined elsewhere (ideally in the same module as sha256(), ripemd160() and hash256()), and CScriptOp should not commonly be required by testcases (the only place it's currently used is in GetP2PKHScript() in p2p-segwit.py, which arguably should be pulled up into the script.py module).

@fanquake fanquake added the Tests label Mar 7, 2017
@fanquake
Copy link
Member

Needs a rebase.

@jnewbery jnewbery force-pushed the rpctestsprimitives branch from 5d64f54 to 105483c Compare April 12, 2017 14:06
Copy link
Contributor

@jimmysong jimmysong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like smartfees.py, bip9-softforks.py and bip65-cltv-p2p.py could also use the same import in test/functional. Also, address.py and blocktools.py could use the same import cleanup in test/functional/test_framework

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a little strange that hash160 is in script.py and hash256/sha256 are in mininode.py can we put all three in one place (probably mininode.py)? You're changing most of the places where this is being imported (smartfees.py and test_framework/address.py seem to be the only other ones). It might be worth just changing this, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree that they should all live in the same place. My preference would be for utils.py to actually be pure utility functions and for the hash functions to live in there.

I haven't moved it in this commit because that seemed the minimal change. We should move hash160 to live alongside the other functions in a future PR that cleans up utils.py and mininode.py.

@jnewbery jnewbery force-pushed the rpctestsprimitives branch from 105483c to 4114e1f Compare April 28, 2017 19:55
@jnewbery jnewbery force-pushed the rpctestsprimitives branch from 4114e1f to d5cc6ff Compare April 28, 2017 19:59
@jnewbery
Copy link
Contributor Author

I didn't change the imports in smartfees.py, bip9-softforks.py and bip65-cltv-p2p.py because there's no harm in leaving them as explicit. I only moved segwit.py from exlicit to wildcard because it was importing an absurdly long list of names.

for imports within the test_framework module itself, I think we should aim for them all to be explicit. I have updated address.py. It was previously importing hash256 and sha256 indirectly via script.py. I've now changed it so that it imports them directly from mininode.py.

@jimmysong
Copy link
Contributor

ACK d5cc6ff

@jnewbery
Copy link
Contributor Author

I'd love to tidy up imports in the test framework, but there doesn't seem to be general enthusiasm for this. Closing for now.

@jnewbery jnewbery closed this Jun 30, 2017
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants