mirror of
https://github.com/python/cpython.git
synced 2025-08-19 08:11:46 +00:00
TarFile.__init__() no longer fails if no name argument is passed and
the fileobj argument has no usable name attribute (e.g. StringIO). (backported from r57616)
This commit is contained in:
parent
1ab6a25401
commit
a9bad98e3d
3 changed files with 26 additions and 12 deletions
|
@ -1044,24 +1044,21 @@ class TarFile(object):
|
||||||
can be determined, `mode' is overridden by `fileobj's mode.
|
can be determined, `mode' is overridden by `fileobj's mode.
|
||||||
`fileobj' is not closed, when TarFile is closed.
|
`fileobj' is not closed, when TarFile is closed.
|
||||||
"""
|
"""
|
||||||
self.name = name
|
|
||||||
if self.name is not None:
|
|
||||||
self.name = os.path.abspath(name)
|
|
||||||
|
|
||||||
if len(mode) > 1 or mode not in "raw":
|
if len(mode) > 1 or mode not in "raw":
|
||||||
raise ValueError("mode must be 'r', 'a' or 'w'")
|
raise ValueError("mode must be 'r', 'a' or 'w'")
|
||||||
self._mode = mode
|
self._mode = mode
|
||||||
self.mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode]
|
self.mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode]
|
||||||
|
|
||||||
if not fileobj:
|
if not fileobj:
|
||||||
fileobj = file(self.name, self.mode)
|
fileobj = file(name, self.mode)
|
||||||
self._extfileobj = False
|
self._extfileobj = False
|
||||||
else:
|
else:
|
||||||
if self.name is None and hasattr(fileobj, "name"):
|
if name is None and hasattr(fileobj, "name"):
|
||||||
self.name = os.path.abspath(fileobj.name)
|
name = fileobj.name
|
||||||
if hasattr(fileobj, "mode"):
|
if hasattr(fileobj, "mode"):
|
||||||
self.mode = fileobj.mode
|
self.mode = fileobj.mode
|
||||||
self._extfileobj = True
|
self._extfileobj = True
|
||||||
|
self.name = os.path.abspath(name) if name else None
|
||||||
self.fileobj = fileobj
|
self.fileobj = fileobj
|
||||||
|
|
||||||
# Init datastructures
|
# Init datastructures
|
||||||
|
|
|
@ -642,11 +642,25 @@ class OpenFileobjTest(BaseTest):
|
||||||
except tarfile.ReadError:
|
except tarfile.ReadError:
|
||||||
self.assertEqual(fobj.tell(), 0, "fileobj's position has moved")
|
self.assertEqual(fobj.tell(), 0, "fileobj's position has moved")
|
||||||
|
|
||||||
def test_fileobj(self):
|
def test_no_name_argument(self):
|
||||||
# Test for SF bug #1695229, opening a tarfile without
|
fobj = open(testtar, "rb")
|
||||||
# a name argument.
|
tar = tarfile.open(fileobj=fobj, mode="r")
|
||||||
tarfile.open(mode="r", fileobj=open(tarname("")))
|
self.assertEqual(tar.name, os.path.abspath(fobj.name))
|
||||||
tarfile.TarFile(mode="r", fileobj=open(tarname("")))
|
|
||||||
|
def test_no_name_attribute(self):
|
||||||
|
data = open(testtar, "rb").read()
|
||||||
|
fobj = StringIO.StringIO(data)
|
||||||
|
self.assertRaises(AttributeError, getattr, fobj, "name")
|
||||||
|
tar = tarfile.open(fileobj=fobj, mode="r")
|
||||||
|
self.assertEqual(tar.name, None)
|
||||||
|
|
||||||
|
def test_empty_name_attribute(self):
|
||||||
|
data = open(testtar, "rb").read()
|
||||||
|
fobj = StringIO.StringIO(data)
|
||||||
|
fobj.name = ""
|
||||||
|
tar = tarfile.open(fileobj=fobj, mode="r")
|
||||||
|
self.assertEqual(tar.name, None)
|
||||||
|
|
||||||
|
|
||||||
if bz2:
|
if bz2:
|
||||||
# Bzip2 TestCases
|
# Bzip2 TestCases
|
||||||
|
|
|
@ -26,6 +26,9 @@ Core and builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- TarFile.__init__() no longer fails if no name argument is passed and
|
||||||
|
the fileobj argument has no usable name attribute (e.g. StringIO).
|
||||||
|
|
||||||
- Reverted the fix for bug #1548891 because it broke compatibility with
|
- Reverted the fix for bug #1548891 because it broke compatibility with
|
||||||
arbitrary read buffers. Added a note in the documentation.
|
arbitrary read buffers. Added a note in the documentation.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue