Kaydet (Commit) 0f4a14b5 authored tarafından Lars Gustäbel's avatar Lars Gustäbel

TarFile.__init__() no longer fails if no name argument is passed and

the fileobj argument has no usable name attribute (e.g. StringIO).

(will backport to 2.5)
üst 23b8ddc1
...@@ -1522,7 +1522,7 @@ class TarFile(object): ...@@ -1522,7 +1522,7 @@ class TarFile(object):
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) self.name = os.path.abspath(name) if name else None
self.fileobj = fileobj self.fileobj = fileobj
# Init attributes. # Init attributes.
......
...@@ -141,11 +141,25 @@ class UstarReadTest(ReadTest): ...@@ -141,11 +141,25 @@ class UstarReadTest(ReadTest):
class MiscReadTest(ReadTest): class MiscReadTest(ReadTest):
def test_no_filename(self): def test_no_name_argument(self):
fobj = open(self.tarname, "rb") fobj = open(self.tarname, "rb")
tar = tarfile.open(fileobj=fobj, mode=self.mode) tar = tarfile.open(fileobj=fobj, mode=self.mode)
self.assertEqual(tar.name, os.path.abspath(fobj.name)) self.assertEqual(tar.name, os.path.abspath(fobj.name))
def test_no_name_attribute(self):
data = open(self.tarname, "rb").read()
fobj = StringIO.StringIO(data)
self.assertRaises(AttributeError, getattr, fobj, "name")
tar = tarfile.open(fileobj=fobj, mode=self.mode)
self.assertEqual(tar.name, None)
def test_empty_name_attribute(self):
data = open(self.tarname, "rb").read()
fobj = StringIO.StringIO(data)
fobj.name = ""
tar = tarfile.open(fileobj=fobj, mode=self.mode)
self.assertEqual(tar.name, None)
def test_fail_comp(self): def test_fail_comp(self):
# For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file. # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file.
if self.mode == "r:": if self.mode == "r:":
......
...@@ -240,6 +240,9 @@ Core and builtins ...@@ -240,6 +240,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).
- The functools module now provides 'reduce', for forward compatibility - The functools module now provides 'reduce', for forward compatibility
with Python 3000. with Python 3000.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment