]> granicus.if.org Git - python/commitdiff
TarFile.__init__() no longer fails if no name argument is passed and
authorLars Gustäbel <lars@gustaebel.de>
Tue, 28 Aug 2007 12:31:09 +0000 (12:31 +0000)
committerLars Gustäbel <lars@gustaebel.de>
Tue, 28 Aug 2007 12:31:09 +0000 (12:31 +0000)
the fileobj argument has no usable name attribute (e.g. StringIO).

(will backport to 2.5)

Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index 1ab13f0e41f1953e5ee4036a47fc7e8b2c595ac8..ee9922c60ab730e11c477f6c689bdc0b61d07691 100644 (file)
@@ -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.
index 596b0adac06e31811e4a1fcf3022af044995186c..1f0825831cd3151c28af60f8c2d9b0a7b5a4ab8d 100644 (file)
@@ -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:":
index 5ba18bc656dfafbf0a2b97a71f592829df6c2149..f9a3a0c98566ac94eb4cd76176938c36d862bd43 100644 (file)
--- 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.