mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
Backport from rev. 53161.
This commit is contained in:
parent
12e087a1b1
commit
f9a2c63c79
3 changed files with 16 additions and 0 deletions
|
@ -1133,9 +1133,13 @@ class TarFile(object):
|
||||||
# Find out which *open() is appropriate for opening the file.
|
# Find out which *open() is appropriate for opening the file.
|
||||||
for comptype in cls.OPEN_METH:
|
for comptype in cls.OPEN_METH:
|
||||||
func = getattr(cls, cls.OPEN_METH[comptype])
|
func = getattr(cls, cls.OPEN_METH[comptype])
|
||||||
|
if fileobj is not None:
|
||||||
|
saved_pos = fileobj.tell()
|
||||||
try:
|
try:
|
||||||
return func(name, "r", fileobj)
|
return func(name, "r", fileobj)
|
||||||
except (ReadError, CompressionError):
|
except (ReadError, CompressionError):
|
||||||
|
if fileobj is not None:
|
||||||
|
fileobj.seek(saved_pos)
|
||||||
continue
|
continue
|
||||||
raise ReadError("file could not be opened successfully")
|
raise ReadError("file could not be opened successfully")
|
||||||
|
|
||||||
|
|
|
@ -625,6 +625,15 @@ class FileModeTest(unittest.TestCase):
|
||||||
self.assertEqual(tarfile.filemode(0755), '-rwxr-xr-x')
|
self.assertEqual(tarfile.filemode(0755), '-rwxr-xr-x')
|
||||||
self.assertEqual(tarfile.filemode(07111), '---s--s--t')
|
self.assertEqual(tarfile.filemode(07111), '---s--s--t')
|
||||||
|
|
||||||
|
class OpenFileobjTest(BaseTest):
|
||||||
|
# Test for SF bug #1496501.
|
||||||
|
|
||||||
|
def test_opener(self):
|
||||||
|
fobj = StringIO.StringIO("foo\n")
|
||||||
|
try:
|
||||||
|
tarfile.open("", "r", fileobj=fobj)
|
||||||
|
except tarfile.ReadError:
|
||||||
|
self.assertEqual(fobj.tell(), 0, "fileobj's position has moved")
|
||||||
|
|
||||||
if bz2:
|
if bz2:
|
||||||
# Bzip2 TestCases
|
# Bzip2 TestCases
|
||||||
|
@ -670,6 +679,7 @@ def test_main():
|
||||||
|
|
||||||
tests = [
|
tests = [
|
||||||
FileModeTest,
|
FileModeTest,
|
||||||
|
OpenFileobjTest,
|
||||||
ReadTest,
|
ReadTest,
|
||||||
ReadStreamTest,
|
ReadStreamTest,
|
||||||
ReadDetectTest,
|
ReadDetectTest,
|
||||||
|
|
|
@ -122,6 +122,8 @@ Extension Modules
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
|
||||||
|
|
||||||
- Patch #1262036: Prevent TarFiles from being added to themselves under
|
- Patch #1262036: Prevent TarFiles from being added to themselves under
|
||||||
certain conditions.
|
certain conditions.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue