cpython: 308f3c1e36d3 (original) (raw)
Mercurial > cpython
changeset 91708:308f3c1e36d3 2.7
Issue 21044: tarfile.open() now handles fileobj with an integer 'name' attribute. Based on patch by Martin Panter.
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Wed, 16 Jul 2014 23:58:12 +0300 |
parents | f3b9814fb81a |
children | 4c98086194d5 |
files | Lib/tarfile.py Lib/test/test_tarfile.py Misc/ACKS Misc/NEWS |
diffstat | 4 files changed, 39 insertions(+), 6 deletions(-)[+] [-] Lib/tarfile.py 3 Lib/test/test_tarfile.py 38 Misc/ACKS 1 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1522,7 +1522,8 @@ class TarFile(object): fileobj = bltn_open(name, self._mode) self._extfileobj = False else:
if name is None and hasattr(fileobj, "name"):[](#l1.7)
if (name is None and hasattr(fileobj, "name") and[](#l1.8)
isinstance(fileobj.name, basestring)):[](#l1.9) name = fileobj.name[](#l1.10) if hasattr(fileobj, "mode"):[](#l1.11) self._mode = fileobj.mode[](#l1.12)
--- 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 CommonReadTest(ReadTest): class MiscReadTest(CommonReadTest): taropen = tarfile.TarFile.taropen
+ def test_no_name_argument(self):
fobj = open(self.tarname, "rb")[](#l2.19)
tar = tarfile.open(fileobj=fobj, mode=self.mode)[](#l2.20)
self.assertEqual(tar.name, os.path.abspath(fobj.name))[](#l2.21)
self.requires_name_attribute()[](#l2.22)
with open(self.tarname, "rb") as fobj:[](#l2.23)
self.assertIsInstance(fobj.name, str)[](#l2.24)
with tarfile.open(fileobj=fobj, mode=self.mode) as tar:[](#l2.25)
self.assertIsInstance(tar.name, str)[](#l2.26)
self.assertEqual(tar.name, os.path.abspath(fobj.name))[](#l2.27)
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)[](#l2.34)
self.assertIsNone(tar.name)[](#l2.35)
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)[](#l2.42)
self.assertIsNone(tar.name)[](#l2.43)
- def test_int_name_attribute(self):
# Issue 21044: tarfile.open() should handle fileobj with an integer[](#l2.46)
# 'name' attribute.[](#l2.47)
fd = os.open(self.tarname, os.O_RDONLY)[](#l2.48)
with io.open(fd, 'rb') as fobj:[](#l2.49)
self.assertIsInstance(fobj.name, int)[](#l2.50)
with tarfile.open(fileobj=fobj, mode=self.mode) as tar:[](#l2.51)
self.assertIsNone(tar.name)[](#l2.52)
- @test_support.requires_unicode
- def test_unicode_name_attribute(self):
self.requires_name_attribute()[](#l2.56)
tarname = unicode(self.tarname, test_support.TESTFN_ENCODING)[](#l2.57)
with io.open(tarname, 'rb') as fobj:[](#l2.58)
self.assertIsInstance(fobj.name, unicode)[](#l2.59)
with tarfile.open(fileobj=fobj, mode=self.mode) as tar:[](#l2.60)
self.assertIsInstance(tar.name, unicode)[](#l2.61)
self.assertEqual(tar.name, os.path.abspath(fobj.name))[](#l2.62)
def test_illegal_mode_arg(self): with open(tmpname, 'wb'): @@ -1668,6 +1694,8 @@ class Bz2MiscReadTest(MiscReadTest): tarname = bz2name mode = "r:bz2" taropen = tarfile.TarFile.bz2open
class Bz2UstarReadTest(UstarReadTest): tarname = bz2name mode = "r:bz2"
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -995,6 +995,7 @@ Mike Pall Todd R. Palmer Juan David Ibáñez Palomar Jan Palus +Martin Panter Mathias Panzenböck M. Papillon Peter Parente
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -13,6 +13,9 @@ Core and Builtins Library ------- +- Issue 21044: tarfile.open() now handles fileobj with an integer 'name'