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 Exception ignored in: <b2sdk._internal.stream.progress.ReadingStr… #381

Merged
merged 1 commit into from
Oct 28, 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 b2sdk/_internal/stream/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def flush(self):
"""
self.stream.flush()

@property
def closed(self):
return self.stream.closed

def readable(self):
return self.stream.readable()

Expand Down
9 changes: 9 additions & 0 deletions test/unit/stream/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
######################################################################
#
# File: test/unit/stream/__init__.py
#
# Copyright 2024 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
53 changes: 53 additions & 0 deletions test/unit/stream/test_progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
######################################################################
#
# File: test/unit/stream/test_progress.py
#
# Copyright 2024 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
import io
from unittest.mock import Mock

from apiver_deps import ReadingStreamWithProgress


def test_reading_stream_with_progress(tmp_path):
stream = io.BytesIO(b"1234567890")
progress_listener = Mock()
with ReadingStreamWithProgress(stream, progress_listener=progress_listener) as wrapped_stream:
assert wrapped_stream.read(1) == b"1"
assert wrapped_stream.read(2) == b"23"
assert wrapped_stream.read(3) == b"456"

assert progress_listener.bytes_completed.call_count == 3
assert wrapped_stream.bytes_completed == 6

assert not stream.closed


def test_reading_stream_with_progress__not_closing_wrapped_stream(tmp_path):
stream = io.BytesIO(b"1234567890")
progress_listener = Mock()
with ReadingStreamWithProgress(stream, progress_listener=progress_listener) as wrapped_stream:
assert wrapped_stream.read()

assert not stream.closed


def test_reading_stream_with_progress__closed_proxy(tmp_path):
"""
Test that the wrapped stream is closed when the original stream is closed.

This is important for Python 3.13+ to prevent:
'Exception ignored in: <b2sdk._internal.stream.progress.ReadingStreamWithProgress object at 0x748cf40d5180>'
messages.
"""
stream = io.BytesIO(b"1234567890")
progress_listener = Mock()
wrapped_stream = ReadingStreamWithProgress(stream, progress_listener=progress_listener)

assert not stream.closed
stream.close()
assert wrapped_stream.closed