Skip to content

bpo-39430: Fix race condition in lazy imports in tarfile. #18161

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

Merged
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
18 changes: 8 additions & 10 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,13 +1655,12 @@ def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
raise ValueError("mode must be 'r', 'w' or 'x'")

try:
import gzip
gzip.GzipFile
except (ImportError, AttributeError):
from gzip import GzipFile
except ImportError:
raise CompressionError("gzip module is not available")

try:
fileobj = gzip.GzipFile(name, mode + "b", compresslevel, fileobj)
fileobj = GzipFile(name, mode + "b", compresslevel, fileobj)
except OSError:
if fileobj is not None and mode == 'r':
raise ReadError("not a gzip file")
Expand Down Expand Up @@ -1689,12 +1688,11 @@ def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
raise ValueError("mode must be 'r', 'w' or 'x'")

try:
import bz2
from bz2 import BZ2File
except ImportError:
raise CompressionError("bz2 module is not available")

fileobj = bz2.BZ2File(fileobj or name, mode,
compresslevel=compresslevel)
fileobj = BZ2File(fileobj or name, mode, compresslevel=compresslevel)

try:
t = cls.taropen(name, mode, fileobj, **kwargs)
Expand All @@ -1718,15 +1716,15 @@ def xzopen(cls, name, mode="r", fileobj=None, preset=None, **kwargs):
raise ValueError("mode must be 'r', 'w' or 'x'")

try:
import lzma
from lzma import LZMAFile, LZMAError
except ImportError:
raise CompressionError("lzma module is not available")

fileobj = lzma.LZMAFile(fileobj or name, mode, preset=preset)
fileobj = LZMAFile(fileobj or name, mode, preset=preset)

try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (lzma.LZMAError, EOFError):
except (LZMAError, EOFError):
fileobj.close()
if mode == 'r':
raise ReadError("not an lzma file")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed race condition in lazy imports in :mod:`tarfile`.