summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-01-24 17:55:52 (GMT)
committerEthan Furman <ethan@stoneleaf.us>2020-01-24 17:55:52 (GMT)
commit9017e0bd5e124ae6d2ed94b9e9cacb2e86270980 (patch)
treedc0c524bf18c6d0c7bae08d1423512632cc4a56f
parent656c45ec9a9dc2e94cec199ebde553a6979e0e05 (diff)
downloadcpython-9017e0bd5e124ae6d2ed94b9e9cacb2e86270980.zip
cpython-9017e0bd5e124ae6d2ed94b9e9cacb2e86270980.tar.gz
cpython-9017e0bd5e124ae6d2ed94b9e9cacb2e86270980.tar.bz2
bpo-39430: Fix race condition in lazy imports in tarfile. (GH-18161)
Use `from ... import ...` to ensure module is fully loaded before accessing its attributes.
-rwxr-xr-xLib/tarfile.py18
-rw-r--r--Misc/NEWS.d/next/Library/2020-01-24-11-05-21.bpo-39430.I0UQzM.rst1
2 files changed, 9 insertions, 10 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index d0b748c..90a2c95 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1655,13 +1655,12 @@ class TarFile(object):
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")
@@ -1689,12 +1688,11 @@ class TarFile(object):
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)
@@ -1718,15 +1716,15 @@ class TarFile(object):
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")
diff --git a/Misc/NEWS.d/next/Library/2020-01-24-11-05-21.bpo-39430.I0UQzM.rst b/Misc/NEWS.d/next/Library/2020-01-24-11-05-21.bpo-39430.I0UQzM.rst
new file mode 100644
index 0000000..712fc5d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-24-11-05-21.bpo-39430.I0UQzM.rst
@@ -0,0 +1 @@
+Fixed race condition in lazy imports in :mod:`tarfile`.