Skip to content

Commit

Permalink
Merge pull request Backblaze#515 from reef-technologies/py313
Browse files Browse the repository at this point in the history
fix Exception ignored in: <b2sdk._internal.stream.progress.ReadingStr…
  • Loading branch information
mjurbanski-reef authored Oct 28, 2024
2 parents a4a67b1 + cad8b2b commit 69fc05c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
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

0 comments on commit 69fc05c

Please sign in to comment.