]> granicus.if.org Git - python/commitdiff
Issue 21044: tarfile.open() now handles fileobj with an integer 'name'
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 16 Jul 2014 20:58:58 +0000 (23:58 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 16 Jul 2014 20:58:58 +0000 (23:58 +0300)
attribute.  Based on patch by Martin Panter.

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

index 0a9025774d73306af467147590449785583b3e7b..9e291c2a584609fec742862075da078ad8f5f665 100755 (executable)
@@ -1423,7 +1423,8 @@ class TarFile(object):
             fileobj = bltn_open(name, self._mode)
             self._extfileobj = False
         else:
-            if name is None and hasattr(fileobj, "name"):
+            if (name is None and hasattr(fileobj, "name") and
+                isinstance(fileobj.name, (str, bytes))):
                 name = fileobj.name
             if hasattr(fileobj, "mode"):
                 self._mode = fileobj.mode
index 92f8bfe920c6b5d60f8d5cd1ee01d5374b42eaf2..82aa840d869ee74d1b16bac0f644b38fb2a052d2 100644 (file)
@@ -352,10 +352,16 @@ class CommonReadTest(ReadTest):
 
 
 class MiscReadTestBase(CommonReadTest):
+    def requires_name_attribute(self):
+        pass
+
     def test_no_name_argument(self):
+        self.requires_name_attribute()
         with open(self.tarname, "rb") as fobj:
-            tar = tarfile.open(fileobj=fobj, mode=self.mode)
-            self.assertEqual(tar.name, os.path.abspath(fobj.name))
+            self.assertIsInstance(fobj.name, str)
+            with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
+                self.assertIsInstance(tar.name, str)
+                self.assertEqual(tar.name, os.path.abspath(fobj.name))
 
     def test_no_name_attribute(self):
         with open(self.tarname, "rb") as fobj:
@@ -363,7 +369,7 @@ class MiscReadTestBase(CommonReadTest):
         fobj = io.BytesIO(data)
         self.assertRaises(AttributeError, getattr, fobj, "name")
         tar = tarfile.open(fileobj=fobj, mode=self.mode)
-        self.assertEqual(tar.name, None)
+        self.assertIsNone(tar.name)
 
     def test_empty_name_attribute(self):
         with open(self.tarname, "rb") as fobj:
@@ -371,7 +377,25 @@ class MiscReadTestBase(CommonReadTest):
         fobj = io.BytesIO(data)
         fobj.name = ""
         with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
-            self.assertEqual(tar.name, None)
+            self.assertIsNone(tar.name)
+
+    def test_int_name_attribute(self):
+        # Issue 21044: tarfile.open() should handle fileobj with an integer
+        # 'name' attribute.
+        fd = os.open(self.tarname, os.O_RDONLY)
+        with open(fd, 'rb') as fobj:
+            self.assertIsInstance(fobj.name, int)
+            with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
+                self.assertIsNone(tar.name)
+
+    def test_bytes_name_attribute(self):
+        self.requires_name_attribute()
+        tarname = os.fsencode(self.tarname)
+        with open(tarname, 'rb') as fobj:
+            self.assertIsInstance(fobj.name, bytes)
+            with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
+                self.assertIsInstance(tar.name, bytes)
+                self.assertEqual(tar.name, os.path.abspath(fobj.name))
 
     def test_illegal_mode_arg(self):
         with open(tmpname, 'wb'):
@@ -549,11 +573,11 @@ class GzipMiscReadTest(GzipTest, MiscReadTestBase, unittest.TestCase):
     pass
 
 class Bz2MiscReadTest(Bz2Test, MiscReadTestBase, unittest.TestCase):
-    def test_no_name_argument(self):
+    def requires_name_attribute(self):
         self.skipTest("BZ2File have no name attribute")
 
 class LzmaMiscReadTest(LzmaTest, MiscReadTestBase, unittest.TestCase):
-    def test_no_name_argument(self):
+    def requires_name_attribute(self):
         self.skipTest("LZMAFile have no name attribute")
 
 
index 0c484773008d5acc76582709b5bb93386ed97644..0b557671bde07fb3d2546f27618772260a071ad2 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -996,6 +996,7 @@ Mike Pall
 Todd R. Palmer
 Juan David Ibáñez Palomar
 Jan Palus
+Martin Panter
 Mathias Panzenböck
 M. Papillon
 Peter Parente
index d5715789f46ef9bdf6f32550eb64d7041ea6e6a5..86014e424a7b9ea9a7e5ca61cfe7fcf1cfcaa539 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@ Core and Builtins
 Library
 -------
 
+- Issue 21044: tarfile.open() now handles fileobj with an integer 'name'
+  attribute.  Based on patch by Martin Panter.
+
 - Issue #19076: Don't pass the redundant 'file' argument to self.error().
 
 - Issue #21942: Fixed source file viewing in pydoc's server mode on Windows.