From 0f4a14b56fcbd939e60f424517db61ca6f2f3885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Gust=C3=A4bel?= Date: Tue, 28 Aug 2007 12:31:09 +0000 Subject: 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) --- Lib/tarfile.py | 2 +- Lib/test/test_tarfile.py | 16 +++++++++++++++- Misc/NEWS | 3 +++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 1ab13f0..ee9922c 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1522,7 +1522,7 @@ class TarFile(object): if hasattr(fileobj, "mode"): self._mode = fileobj.mode self._extfileobj = True - self.name = os.path.abspath(name) + self.name = os.path.abspath(name) if name else None self.fileobj = fileobj # Init attributes. diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 596b0ad..1f08258 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -141,11 +141,25 @@ class UstarReadTest(ReadTest): class MiscReadTest(ReadTest): - def test_no_filename(self): + def test_no_name_argument(self): fobj = open(self.tarname, "rb") tar = tarfile.open(fileobj=fobj, mode=self.mode) 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): # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file. if self.mode == "r:": diff --git a/Misc/NEWS b/Misc/NEWS index 5ba18bc..f9a3a0c 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -240,6 +240,9 @@ Core and builtins 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 with Python 3000. -- cgit v0.12