Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(decode_bytes): error handling led to data loss in subsequent chunks #39

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions datasalad/itertools/decode_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ def handle_decoding_error(
try:
yield joined_data[position:].decode(encoding)
joined_data = b''
# must reset the pointer for successful decoded
# parts too, otherwise we start too far into a new chunk's
# content
position = 0
except UnicodeDecodeError as e:
# If an encoding error occurs, we first check whether it was
# in the middle of `joined_data` or whether it extends until the
Expand Down
7 changes: 7 additions & 0 deletions datasalad/itertools/tests/test_decode_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ def test_no_empty_strings():
def test_multiple_errors():
r = ''.join(decode_bytes([b'08 War \xaf No \xaf More \xaf Trouble.shn.mp3']))
assert r == '08 War \\xaf No \\xaf More \\xaf Trouble.shn.mp3'


def test_error_chunks():
# this verifies that error handling in a previous chunk does not
# cause data loss in a subsequent chunk
r = ''.join(decode_bytes([b'08 War \xaf No', b'1234567890']))
assert r == '08 War \\xaf No1234567890'