[Python-checkins] cpython (merge 3.4 -> default): Issue 21044: tarfile.open() now handles fileobj with an integer 'name'

serhiy.storchaka python-checkins at python.org
Wed Jul 16 23:07:04 CEST 2014


http://hg.python.org/cpython/rev/4c2f3240ad65
changeset:   91710:4c2f3240ad65
parent:      91706:0a4f286105b4
parent:      91709:d6b71971b228
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Thu Jul 17 00:00:26 2014 +0300
summary:
  Issue 21044: tarfile.open() now handles fileobj with an integer 'name'
attribute.  Based on patch by Martin Panter.

files:
  Lib/tarfile.py           |   3 +-
  Lib/test/test_tarfile.py |  36 +++++++++++++++++++++++----
  Misc/ACKS                |   1 +
  Misc/NEWS                |   3 ++
  4 files changed, 36 insertions(+), 7 deletions(-)


diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1423,7 +1423,8 @@
             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
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -351,10 +351,16 @@
 
 
 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:
@@ -362,7 +368,7 @@
         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:
@@ -370,7 +376,25 @@
         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'):
@@ -548,11 +572,11 @@
     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")
 
 
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1004,6 +1004,7 @@
 Todd R. Palmer
 Juan David Ibáñez Palomar
 Jan Palus
+Martin Panter
 Mathias Panzenböck
 M. Papillon
 Peter Parente
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -108,6 +108,9 @@
 Library
 -------
 
+- Issue 21044: tarfile.open() now handles fileobj with an integer 'name'
+  attribute.  Based on patch by Martin Panter.
+
 - Issue #21966: Respect -q command-line option when code module is ran.
 
 - Issue #19076: Don't pass the redundant 'file' argument to self.error().

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list