Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
7fd59e09
Commit
7fd59e09
authored
Aug 27, 2014
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
properly handle file closing in error cases (closes #22266)
parent
ce2ec49d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
29 deletions
+48
-29
Lib/tarfile.py
Lib/tarfile.py
+48
-29
No files found.
Lib/tarfile.py
View file @
7fd59e09
...
...
@@ -417,28 +417,34 @@ class _Stream:
self
.
pos
=
0L
self
.
closed
=
False
if
comptype
==
"gz"
:
try
:
import
zlib
except
ImportError
:
raise
CompressionError
(
"zlib module is not available"
)
self
.
zlib
=
zlib
self
.
crc
=
zlib
.
crc32
(
""
)
&
0xffffffff
L
if
mode
==
"r"
:
self
.
_init_read_gz
()
else
:
self
.
_init_write_gz
()
try
:
if
comptype
==
"gz"
:
try
:
import
zlib
except
ImportError
:
raise
CompressionError
(
"zlib module is not available"
)
self
.
zlib
=
zlib
self
.
crc
=
zlib
.
crc32
(
""
)
&
0xffffffff
L
if
mode
==
"r"
:
self
.
_init_read_gz
()
else
:
self
.
_init_write_gz
()
if
comptype
==
"bz2"
:
try
:
import
bz2
except
ImportError
:
raise
CompressionError
(
"bz2 module is not available"
)
if
mode
==
"r"
:
self
.
dbuf
=
""
self
.
cmp
=
bz2
.
BZ2Decompressor
()
else
:
self
.
cmp
=
bz2
.
BZ2Compressor
()
elif
comptype
==
"bz2"
:
try
:
import
bz2
except
ImportError
:
raise
CompressionError
(
"bz2 module is not available"
)
if
mode
==
"r"
:
self
.
dbuf
=
""
self
.
cmp
=
bz2
.
BZ2Decompressor
()
else
:
self
.
cmp
=
bz2
.
BZ2Compressor
()
except
:
if
not
self
.
_extfileobj
:
self
.
fileobj
.
close
()
self
.
closed
=
True
raise
def
__del__
(
self
):
if
hasattr
(
self
,
"closed"
)
and
not
self
.
closed
:
...
...
@@ -1685,9 +1691,12 @@ class TarFile(object):
if filemode not in ("r", "
w
"):
raise ValueError("
mode
must
be
'r'
or
'w'")
t = cls(name, filemode,
_Stream(name, filemode, comptype, fileobj, bufsize),
**kwargs)
stream = _Stream(name, filemode, comptype, fileobj, bufsize)
try:
t = cls(name, filemode, stream, **kwargs)
except:
stream.close()
raise
t._extfileobj = False
return t
...
...
@@ -1718,17 +1727,23 @@ class TarFile(object):
except (ImportError, AttributeError):
raise CompressionError("
gzip
module
is
not
available
")
if fileobj is None:
fileobj = bltn_open(name, mode + "b")
try:
fileobj = gzip.GzipFile(name, mode, compresslevel, fileobj)
except OSError:
if fileobj is not None and mode == 'r':
raise ReadError("
not
a
gzip
file
")
raise
try:
t = cls.taropen(name, mode,
gzip.GzipFile(name, mode, compresslevel, fileobj),
**kwargs)
t = cls.taropen(name, mode, fileobj, **kwargs)
except IOError:
fileobj.close()
if mode == 'r':
raise ReadError("
not
a
gzip
file
")
raise
except:
fileobj.close()
raise
t._extfileobj = False
return t
...
...
@@ -1753,9 +1768,13 @@ class TarFile(object):
try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (IOError, EOFError):
fileobj.close()
if mode == 'r':
raise ReadError("
not
a
bzip2
file
")
raise
except:
fileobj.close()
raise
t._extfileobj = False
return t
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment