This PR removes the remaining binascii method calls outside test/functional and test_framework, as pointed out here #22619#pullrequestreview-722153458.
refactor: Replace remaining binascii method calls #22633
pull Zero-1729 wants to merge 1 commits into bitcoin:master from Zero-1729:follow-ups-to-22605 changing 7 files +14 −19-
Zero-1729 commented at 11:18 AM on August 5, 2021: contributor
- DrahtBot added the label Refactoring on Aug 5, 2021
- DrahtBot added the label Scripts and tools on Aug 5, 2021
- Zero-1729 force-pushed on Aug 8, 2021
-
Zero-1729 commented at 12:19 PM on August 8, 2021: contributor
Diff from 3a2ad3b to 33b8a31: Updated Python2 code sample to Python3 and removed
binascii.hexlifycall.Now
git grep binasciino longer yields any result. -
in contrib/zmq/zmq_sub.py:60 in 33b8a3107f outdated
56 | @@ -58,18 +57,18 @@ async def handle(self) : 57 | sequence = str(struct.unpack('<I', seq)[-1]) 58 | if topic == b"hashblock": 59 | print('- HASH BLOCK ('+sequence+') -') 60 | - print(binascii.hexlify(body)) 61 | + print(body.hex().encode())
theStack commented at 10:40 PM on August 8, 2021:nit: Though this is right from a refactoring perspective, I don't think the
encode()part is needed here (and in the substitutions below). This leads to displaying an example hex-string '1234abcd' asb'1234abcd'instead of
1234abcdThe latter seems more natural and was also the behaviour of
print(binascii.hexlify(...))on Python 2.7 (which was used when this file was first introduced, see commit e6a14b64d665eb1fafd03a6bbc8d14597ce1c83c), when there was no 'bytes' type yet.print(body.hex())
Zero-1729 commented at 1:34 AM on August 9, 2021:Ahh, makes sense now, the issue was wrongly assuming it was Py3 code to begin with :)
Fixed to replicate the original Py2 behavior (i.e. printing the hex-strings).
theStack approvedtheStack commented at 10:53 PM on August 8, 2021: memberCode-review ACK 33b8a3107fc2d647b2c575f4c081db51f6041e16
It's nice to see that with this PR, there is now only one method left for hex-string/bytes conversions in the functional tests and contrib scripts. Special kudos for also replacing the Python code comment in
serfloat_tests.cpp. I tested this directly in the Python interpreter and it works as expected, i.e. the statement yieldsTrue.Zero-1729 force-pushed on Aug 9, 2021theStack approvedtheStack commented at 8:51 AM on August 9, 2021: memberre-ACK b95d5ad3676a8cc42165ab4d51a2aae7c7b059fb 💯
josibake commented at 2:40 PM on August 12, 2021: memberACK https://github.com/bitcoin/bitcoin/pull/22633/commits/b95d5ad3676a8cc42165ab4d51a2aae7c7b059fb
+1 for consistency! did a code review and everything looks good. also ran the standalone scripts where possible and tested a few individual functions (e.g
python3 -c 'from rpcauth import generate_salt; print(generate_salt(10))'). copied the python snippet from the comment and ran it, everything working as expectedjosibake approvedlaanwj renamed this:refactor: follow-ups to 22619
refactor: Replace remaining binascii method calls
on Aug 16, 2021laanwj commented at 3:21 PM on August 16, 2021: memberConcept ACK. Let's be sure that this covers all and is the last PR in the series, it's been dragging on for a while.
theStack commented at 5:17 PM on August 16, 2021: memberLet's be sure that this covers all and is the last PR in the series, it's been dragging on for a while.
Agreed!
I think this is the last PR in this series. No other instances of
binasciiremain. Unless @theStack has any other suggestions that need to be added?Doing a search in test/function via "git grep -i hex", I unfortunately found yet another variant of converting bin-to-hex that should be fixed (I verified via
git grep hex_codecthat this is the only place it is used):diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index 6e57107f8..65d90f844 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -19,7 +19,6 @@ Classes use __slots__ to ensure extraneous attributes aren't accidentally added by tests, compromising their intended effect. """ from base64 import b32decode, b32encode -from codecs import encode import copy import hashlib from io import BytesIO @@ -681,7 +680,7 @@ class CBlockHeader: r += struct.pack("<I", self.nBits) r += struct.pack("<I", self.nNonce) self.sha256 = uint256_from_str(hash256(r)) - self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii') + self.hash = hash256(r)[::-1].hex() def rehash(self): self.sha256 = NoneThis one is not using
binascii(thus not matching the PR title), but should still be tackled in this PR for the reasons laanwj mentioned. After this I'm pretty sure we have them all and #22605 can be closed.refactor: replace remaining binascii method calls 021daedfa1Zero-1729 force-pushed on Aug 16, 2021Zero-1729 commented at 6:38 PM on August 16, 2021: contributorNice catch! Updated.
Diff b95d5ad -> 021daed:
a/test/functional/test_framework/messages.py
- from codecs import encode ... - self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii') + self.hash = hash256(r)[::-1].hex() ...theStack approvedtheStack commented at 7:38 PM on August 16, 2021: memberre-ACK 021daedfa100defad553d69cd3d628aaa5bc7caf
fanquake commented at 3:57 AM on August 17, 2021: membercc @practicalswift @tryphe given you both reviewed the parent PR.
josibake commented at 8:14 AM on August 17, 2021: memberMarcoFalke merged this on Aug 21, 2021MarcoFalke closed this on Aug 21, 2021jonatack commented at 3:52 PM on August 21, 2021: memberPosthumous ACK, modulo could have updated the Python example in
src/test/serfloat_tests.cpp--git grep binasciitakes you there.Edit: oops, I ran grep from /src instead of root, there are some other ones, idem for
git grep hexlify. Maybe a follow-up if it matters.Zero-1729 commented at 4:40 PM on August 21, 2021: contributorPosthumous ACK, modulo could have updated the Python example in src/test/serfloat_tests.cpp -- git grep binascii takes you there.
This PR also tackled the Python code sample in
src/test/serfloat_tests.cpp.Edit: oops, I ran grep from /src instead of root, there are some other ones, idem for
git grep hexlify. Maybe a follow-up if it matters.Just pulled master locally, running both
git grep binasciiandgit grep hexlifyin the root shows nothing for me. Also tried it insrcand got no results. @jonatack are you still getting any results after f5a406f003a060325128db47552089b3254044e3?jonatack commented at 5:15 PM on August 21, 2021: member@Zero-1729 You're right! I grepped from the PR branch (edit: nope, another PR branch :sweat_smile:) sorry for the noise.
sidhujag referenced this in commit ab83d376bc on Aug 22, 2021DrahtBot locked this on Aug 21, 2022
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-14 21:14 UTC
More mirrored repositories can be found on mirror.b10c.me