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.
This commit is contained in:
parent
656c45ec9a
commit
9017e0bd5e
|
@ -1655,13 +1655,12 @@ class TarFile(object):
|
||||||
raise ValueError("mode must be 'r', 'w' or 'x'")
|
raise ValueError("mode must be 'r', 'w' or 'x'")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import gzip
|
from gzip import GzipFile
|
||||||
gzip.GzipFile
|
except ImportError:
|
||||||
except (ImportError, AttributeError):
|
|
||||||
raise CompressionError("gzip module is not available")
|
raise CompressionError("gzip module is not available")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fileobj = gzip.GzipFile(name, mode + "b", compresslevel, fileobj)
|
fileobj = GzipFile(name, mode + "b", compresslevel, fileobj)
|
||||||
except OSError:
|
except OSError:
|
||||||
if fileobj is not None and mode == 'r':
|
if fileobj is not None and mode == 'r':
|
||||||
raise ReadError("not a gzip file")
|
raise ReadError("not a gzip file")
|
||||||
|
@ -1689,12 +1688,11 @@ class TarFile(object):
|
||||||
raise ValueError("mode must be 'r', 'w' or 'x'")
|
raise ValueError("mode must be 'r', 'w' or 'x'")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import bz2
|
from bz2 import BZ2File
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise CompressionError("bz2 module is not available")
|
raise CompressionError("bz2 module is not available")
|
||||||
|
|
||||||
fileobj = bz2.BZ2File(fileobj or name, mode,
|
fileobj = BZ2File(fileobj or name, mode, compresslevel=compresslevel)
|
||||||
compresslevel=compresslevel)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
t = cls.taropen(name, mode, fileobj, **kwargs)
|
t = cls.taropen(name, mode, fileobj, **kwargs)
|
||||||
|
@ -1718,15 +1716,15 @@ class TarFile(object):
|
||||||
raise ValueError("mode must be 'r', 'w' or 'x'")
|
raise ValueError("mode must be 'r', 'w' or 'x'")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import lzma
|
from lzma import LZMAFile, LZMAError
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise CompressionError("lzma module is not available")
|
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:
|
try:
|
||||||
t = cls.taropen(name, mode, fileobj, **kwargs)
|
t = cls.taropen(name, mode, fileobj, **kwargs)
|
||||||
except (lzma.LZMAError, EOFError):
|
except (LZMAError, EOFError):
|
||||||
fileobj.close()
|
fileobj.close()
|
||||||
if mode == 'r':
|
if mode == 'r':
|
||||||
raise ReadError("not an lzma file")
|
raise ReadError("not an lzma file")
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fixed race condition in lazy imports in :mod:`tarfile`.
|
Loading…
Reference in New Issue