Page MenuHomePhabricator

[electrum] make read_compact_size return None at EOF instead of raising an error
ClosedPublic

Authored by PiRK on Jun 14 2024, 20:16.

Details

Summary

This is useful for detecting the end of the file without having to wrap the entire parser code in `try: ... except: ... clauses when parsing a PSBT. See D16333 for a use case.
Now this moves the responsibility to raise an error to callers of that function when None is returned, do that in the two callsites.

Add test coverage for read_compact_size, add test coverage for the two errors now raised in deserialize_blob and deserialize_sequence (one of these is already indirectly tested in test_avalanche.py when deserializing garbage data as a Delegation)

Test Plan

python test_runner.py

Diff Detail

Event Timeline

PiRK retitled this revision from [electrum] make read_compact_size return None at EOL instead of raising an error to [electrum] make read_compact_size return None at EOF instead of raising an error.Jun 15 2024, 05:40
PiRK edited the summary of this revision. (Show Details)

rewrite this in a cleaner way (no error catching, no need for a comment to explain what's happening)

Fabien added a subscriber: Fabien.
Fabien added inline comments.
electrum/electrumabc/serialize.py
95–98

You have a good use case for the walrus operator:

if not (next_byte := stream.read(1)):
This revision is now accepted and ready to land.Jun 17 2024, 08:06

use the walrus operator