[Python-checkins] bpo-39430: Fix race condition in lazy imports in tarfile. (GH-18161)
Miss Islington (bot)
webhook-mailer at python.org
Fri Jan 24 15:10:57 EST 2020
https://github.com/python/cpython/commit/1a274359283d3d1f4f60dd527843f72e0368caf3
commit: 1a274359283d3d1f4f60dd527843f72e0368caf3
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-01-24T12:10:52-08:00
summary:
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.
(cherry picked from commit 9017e0bd5e124ae6d2ed94b9e9cacb2e86270980)
Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>
files:
A Misc/NEWS.d/next/Library/2020-01-24-11-05-21.bpo-39430.I0UQzM.rst
M Lib/tarfile.py
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index edd31e96fb469..3b596cbf49d27 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1629,13 +1629,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")
@@ -1663,12 +1662,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)
@@ -1692,15 +1690,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")
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 0000000000000..712fc5d34bbe0
--- /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`.
More information about the Python-checkins
mailing list