diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-07-16 21:00:26 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-07-16 21:00:26 (GMT) |
commit | a98faefd68da474d5155979f5f9174f98bb6227b (patch) | |
tree | 8e5ea7a584f4073f1eb1eb858b749d410a6183c4 /Lib/test | |
parent | 05d7d156266ac561e988ed84d6fbaf044541d855 (diff) | |
parent | 2c6a3aedeb47ce763454f936545b6dddabbe29cb (diff) | |
download | cpython-a98faefd68da474d5155979f5f9174f98bb6227b.zip cpython-a98faefd68da474d5155979f5f9174f98bb6227b.tar.gz cpython-a98faefd68da474d5155979f5f9174f98bb6227b.tar.bz2 |
Issue 21044: tarfile.open() now handles fileobj with an integer 'name'
attribute. Based on patch by Martin Panter.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_tarfile.py | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index d828979..e527e40 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -351,10 +351,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: @@ -362,7 +368,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: @@ -370,7 +376,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'): @@ -548,11 +572,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") |