You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tarfiles: AttributeError: '_Stream' object has no attribute 'exception' while trying to open tgz file #107396
If a tar file appears to be a tar.gz file, it can fail in this block of tarfile.py
while c < size:
# Skip underlying buffer to avoid unaligned double buffering.
if self.buf:
buf = self.buf
self.buf = b""
else:
buf = self.fileobj.read(self.bufsize)
if not buf:
break
try:
buf = self.cmp.decompress(buf)
except self.exception as e:
raise ReadError("invalid compressed data") from e
t.append(buf)
c += len(buf)
at:
buf = self.cmp.decompress(buf)
before self.exception is set.
Because around:
if comptype == "gz":
try:
import zlib
except ImportError:
raise CompressionError("zlib module is not available") from None
self.zlib = zlib
self.crc = zlib.crc32(b"")
if mode == "r":
self._init_read_gz()
self.exception = zlib.error
else:
self._init_write_gz()
This: self._init_read_gz()
is called before self.exception = zlib.error
Hello,
How does this issue manifest? exception appears like an write-only attribute of an internal class.
I'd like to add a test for the PR, but can't think of a reasonable one. Do you have a use case we currently don't have tests for?
Maybe that one will explain better the sequence where this failure can happen.
My interpretation is that gzip supports backwards skipping and it appears to be rarely used. If I open a tgz with r|* (which, by design, doesn't support backwards skipping) and the gzip stream requests a backwards skip, an exception is raised. At the except statement it has except self.exception which is were the attribute is read, which means the attribute is actually read-write.
If you can add it as a test using with self.assertRaises(tarfile.ReadError): to Lib/test/test_tarfile.py, it'd speed things up. If not I'll get to it later.
@encukou Made a commit with the tests. I'm unsure if I placed the new class in the correct placement but it seemed to be one of the best places to place it. I tried to figure out if a new class should be done and seemed to be a reasonable choice. #107485
Activity
sunmy2019 commentedon Jul 29, 2023
Looks reasonable. Will you open a PR?
balmeida-nokia commentedon Jul 31, 2023
Sure
pythongh-107396: tarfiles: set self.exception before _init_read_gz()
pythongh-107396: tarfiles: set self.exception before _init_read_gz()
pythongh-107396: tarfiles: set self.exception before _init_read_gz()
Merge branch 'main' into pythongh-107396-tarfiles-swap_self.exception
pythongh-107396: tarfiles: set self.exception before _init_read_gz()
encukou commentedon Aug 10, 2023
Hello,
How does this issue manifest?
exceptionappears like an write-only attribute of an internal class.I'd like to add a test for the PR, but can't think of a reasonable one. Do you have a use case we currently don't have tests for?
balmeida-nokia commentedon Aug 10, 2023
@encukou Please see: #107398
Maybe that one will explain better the sequence where this failure can happen.
My interpretation is that gzip supports backwards skipping and it appears to be rarely used. If I open a tgz with
r|*(which, by design, doesn't support backwards skipping) and the gzip stream requests a backwards skip, an exception is raised. At theexceptstatement it hasexcept self.exceptionwhich is were the attribute is read, which means the attribute is actually read-write.encukou commentedon Aug 10, 2023
Oh, I see!
Here's a reproducer:
If you can add it as a test using
with self.assertRaises(tarfile.ReadError):toLib/test/test_tarfile.py, it'd speed things up. If not I'll get to it later.balmeida-nokia commentedon Aug 10, 2023
OK. I will.
pythongh-107396: tarfiles: set self.exception before _init_read_gz()
balmeida-nokia commentedon Aug 18, 2023
@encukou Made a commit with the tests. I'm unsure if I placed the new class in the correct placement but it seemed to be one of the best places to place it. I tried to figure out if a new class should be done and seemed to be a reasonable choice.
#107485
(sorry for the delay. Ty for waiting)
Merge branch 'main' into pythongh-107396-tarfiles-swap_self.exception
gh-107396: tarfiles: set self.exception before _init_read_gz() (GH-10…
pythongh-107396: tarfiles: set self.exception before _init_read_gz() (p…
pythongh-107396: tarfiles: set self.exception before _init_read_gz() (p…
[3.12] gh-107396: tarfiles: set self.exception before _init_read_gz() (…
[3.11] gh-107396: tarfiles: set self.exception before _init_read_gz() (…