Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c6a3ae

Browse files
Issue 21044: tarfile.open() now handles fileobj with an integer 'name'
attribute. Based on patch by Martin Panter.
1 parent 8faecbf commit 2c6a3ae

4 files changed

Lines changed: 36 additions & 7 deletions

File tree

Lib/tarfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,8 @@ def __init__(self, name=None, mode="r", fileobj=None, format=None,
14231423
fileobj = bltn_open(name, self._mode)
14241424
self._extfileobj = False
14251425
else:
1426-
if name is None and hasattr(fileobj, "name"):
1426+
if (name is None and hasattr(fileobj, "name") and
1427+
isinstance(fileobj.name, (str, bytes))):
14271428
name = fileobj.name
14281429
if hasattr(fileobj, "mode"):
14291430
self._mode = fileobj.mode

Lib/test/test_tarfile.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,26 +352,50 @@ def test_ignore_zeros(self):
352352

353353

354354
class MiscReadTestBase(CommonReadTest):
355+
def requires_name_attribute(self):
356+
pass
357+
355358
def test_no_name_argument(self):
359+
self.requires_name_attribute()
356360
with open(self.tarname, "rb") as fobj:
357-
tar = tarfile.open(fileobj=fobj, mode=self.mode)
358-
self.assertEqual(tar.name, os.path.abspath(fobj.name))
361+
self.assertIsInstance(fobj.name, str)
362+
with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
363+
self.assertIsInstance(tar.name, str)
364+
self.assertEqual(tar.name, os.path.abspath(fobj.name))
359365

360366
def test_no_name_attribute(self):
361367
with open(self.tarname, "rb") as fobj:
362368
data = fobj.read()
363369
fobj = io.BytesIO(data)
364370
self.assertRaises(AttributeError, getattr, fobj, "name")
365371
tar = tarfile.open(fileobj=fobj, mode=self.mode)
366-
self.assertEqual(tar.name, None)
372+
self.assertIsNone(tar.name)
367373

368374
def test_empty_name_attribute(self):
369375
with open(self.tarname, "rb") as fobj:
370376
data = fobj.read()
371377
fobj = io.BytesIO(data)
372378
fobj.name = ""
373379
with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
374-
self.assertEqual(tar.name, None)
380+
self.assertIsNone(tar.name)
381+
382+
def test_int_name_attribute(self):
383+
# Issue 21044: tarfile.open() should handle fileobj with an integer
384+
# 'name' attribute.
385+
fd = os.open(self.tarname, os.O_RDONLY)
386+
with open(fd, 'rb') as fobj:
387+
self.assertIsInstance(fobj.name, int)
388+
with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
389+
self.assertIsNone(tar.name)
390+
391+
def test_bytes_name_attribute(self):
392+
self.requires_name_attribute()
393+
tarname = os.fsencode(self.tarname)
394+
with open(tarname, 'rb') as fobj:
395+
self.assertIsInstance(fobj.name, bytes)
396+
with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
397+
self.assertIsInstance(tar.name, bytes)
398+
self.assertEqual(tar.name, os.path.abspath(fobj.name))
375399

376400
def test_illegal_mode_arg(self):
377401
with open(tmpname, 'wb'):
@@ -549,11 +573,11 @@ class GzipMiscReadTest(GzipTest, MiscReadTestBase, unittest.TestCase):
549573
pass
550574

551575
class Bz2MiscReadTest(Bz2Test, MiscReadTestBase, unittest.TestCase):
552-
def test_no_name_argument(self):
576+
def requires_name_attribute(self):
553577
self.skipTest("BZ2File have no name attribute")
554578

555579
class LzmaMiscReadTest(LzmaTest, MiscReadTestBase, unittest.TestCase):
556-
def test_no_name_argument(self):
580+
def requires_name_attribute(self):
557581
self.skipTest("LZMAFile have no name attribute")
558582

559583

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ Mike Pall
996996
Todd R. Palmer
997997
Juan David Ibáñez Palomar
998998
Jan Palus
999+
Martin Panter
9991000
Mathias Panzenböck
10001001
M. Papillon
10011002
Peter Parente

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue 21044: tarfile.open() now handles fileobj with an integer 'name'
31+
attribute. Based on patch by Martin Panter.
32+
3033
- Issue #19076: Don't pass the redundant 'file' argument to self.error().
3134

3235
- Issue #21942: Fixed source file viewing in pydoc's server mode on Windows.

0 commit comments

Comments
 (0)