[Python-checkins] cpython (2.7): Issue 21044: tarfile.open() now handles fileobj with an integer 'name'
serhiy.storchaka
python-checkins at python.org
Wed Jul 16 23:07:01 CEST 2014
http://hg.python.org/cpython/rev/308f3c1e36d3
changeset: 91708:308f3c1e36d3
branch: 2.7
user: Serhiy Storchaka <storchaka at gmail.com>
date: Wed Jul 16 23:58:12 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 | 38 ++++++++++++++++++++++++---
Misc/ACKS | 1 +
Misc/NEWS | 3 ++
4 files changed, 39 insertions(+), 6 deletions(-)
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1522,7 +1522,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, basestring)):
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
@@ -1,6 +1,7 @@
# -*- coding: iso-8859-15 -*-
import sys
+import io
import os
import shutil
import StringIO
@@ -289,24 +290,49 @@
class MiscReadTest(CommonReadTest):
taropen = tarfile.TarFile.taropen
+ def requires_name_attribute(self):
+ pass
+
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))
+ self.requires_name_attribute()
+ with open(self.tarname, "rb") as fobj:
+ 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):
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)
+ self.assertIsNone(tar.name)
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)
+ 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 io.open(fd, 'rb') as fobj:
+ self.assertIsInstance(fobj.name, int)
+ with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
+ self.assertIsNone(tar.name)
+
+ @test_support.requires_unicode
+ def test_unicode_name_attribute(self):
+ self.requires_name_attribute()
+ tarname = unicode(self.tarname, test_support.TESTFN_ENCODING)
+ with io.open(tarname, 'rb') as fobj:
+ self.assertIsInstance(fobj.name, unicode)
+ with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
+ self.assertIsInstance(tar.name, unicode)
+ self.assertEqual(tar.name, os.path.abspath(fobj.name))
def test_illegal_mode_arg(self):
with open(tmpname, 'wb'):
@@ -1668,6 +1694,8 @@
tarname = bz2name
mode = "r:bz2"
taropen = tarfile.TarFile.bz2open
+ def requires_name_attribute(self):
+ self.skipTest("BZ2File have no name attribute")
class Bz2UstarReadTest(UstarReadTest):
tarname = bz2name
mode = "r:bz2"
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -995,6 +995,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
@@ -13,6 +13,9 @@
Library
-------
+- Issue 21044: tarfile.open() now handles fileobj with an integer 'name'
+ attribute. Based on patch by Martin Panter.
+
- Issue #21151: Fixed a segfault in the _winreg module when ``None`` is passed
as a ``REG_BINARY`` value to SetValueEx. Patch by John Ehresman.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list