Skip to content
Closed
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
8 changes: 4 additions & 4 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2896,12 +2896,12 @@ def next(self):
except Exception as e:
try:
import zlib
except ImportError:
pass
else:
if isinstance(e, zlib.error):
raise ReadError(f'zlib error: {e}') from None
else:
raise e
except ImportError:
raise e
raise
break

if tarinfo is not None:
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,27 @@ def test_zlib_error_does_not_leak(self):
with self.assertRaises(tarfile.ReadError):
tarfile.open(self.tarname)

def test_next_preserves_exception_traceback(self):
class ExpectedError(Exception):
pass

class FailingFile(io.BytesIO):
def read(self, *args):
raise ExpectedError()

with self.assertRaises(ExpectedError) as cm:
tarfile.open(fileobj=FailingFile(), mode="r:")

next_frames = []
tb = cm.exception.__traceback__
while tb is not None:
code = tb.tb_frame.f_code
if (code.co_filename == tarfile.__file__
and code.co_name == "next"):
next_frames.append(tb.tb_lineno)
tb = tb.tb_next
self.assertEqual(len(next_frames), 1)

def test_next_on_empty_tarfile(self):
fd = io.BytesIO()
tf = tarfile.open(fileobj=fd, mode="w")
Expand Down
Loading