|
4 | 4 | from hashlib import md5
|
5 | 5 | from contextlib import contextmanager
|
6 | 6 | from random import Random
|
| 7 | +import pathlib |
7 | 8 |
|
8 | 9 | import unittest
|
9 | 10 | import unittest.mock
|
@@ -440,6 +441,22 @@ def test_bytes_name_attribute(self):
|
440 | 441 | self.assertIsInstance(tar.name, bytes)
|
441 | 442 | self.assertEqual(tar.name, os.path.abspath(fobj.name))
|
442 | 443 |
|
| 444 | + def test_pathlike_name(self): |
| 445 | + tarname = pathlib.Path(self.tarname) |
| 446 | + with tarfile.open(tarname, mode=self.mode) as tar: |
| 447 | + self.assertIsInstance(tar.name, str) |
| 448 | + self.assertEqual(tar.name, os.path.abspath(os.fspath(tarname))) |
| 449 | + with self.taropen(tarname) as tar: |
| 450 | + self.assertIsInstance(tar.name, str) |
| 451 | + self.assertEqual(tar.name, os.path.abspath(os.fspath(tarname))) |
| 452 | + with tarfile.TarFile.open(tarname, mode=self.mode) as tar: |
| 453 | + self.assertIsInstance(tar.name, str) |
| 454 | + self.assertEqual(tar.name, os.path.abspath(os.fspath(tarname))) |
| 455 | + if self.suffix == '': |
| 456 | + with tarfile.TarFile(tarname, mode='r') as tar: |
| 457 | + self.assertIsInstance(tar.name, str) |
| 458 | + self.assertEqual(tar.name, os.path.abspath(os.fspath(tarname))) |
| 459 | + |
443 | 460 | def test_illegal_mode_arg(self):
|
444 | 461 | with open(tmpname, 'wb'):
|
445 | 462 | pass
|
@@ -582,6 +599,26 @@ def test_extract_directory(self):
|
582 | 599 | finally:
|
583 | 600 | support.rmtree(DIR)
|
584 | 601 |
|
| 602 | + def test_extractall_pathlike_name(self): |
| 603 | + DIR = pathlib.Path(TEMPDIR) / "extractall" |
| 604 | + with support.temp_dir(DIR), \ |
| 605 | + tarfile.open(tarname, encoding="iso8859-1") as tar: |
| 606 | + directories = [t for t in tar if t.isdir()] |
| 607 | + tar.extractall(DIR, directories) |
| 608 | + for tarinfo in directories: |
| 609 | + path = DIR / tarinfo.name |
| 610 | + self.assertEqual(os.path.getmtime(path), tarinfo.mtime) |
| 611 | + |
| 612 | + def test_extract_pathlike_name(self): |
| 613 | + dirtype = "ustar/dirtype" |
| 614 | + DIR = pathlib.Path(TEMPDIR) / "extractall" |
| 615 | + with support.temp_dir(DIR), \ |
| 616 | + tarfile.open(tarname, encoding="iso8859-1") as tar: |
| 617 | + tarinfo = tar.getmember(dirtype) |
| 618 | + tar.extract(tarinfo, path=DIR) |
| 619 | + extracted = DIR / dirtype |
| 620 | + self.assertEqual(os.path.getmtime(extracted), tarinfo.mtime) |
| 621 | + |
585 | 622 | def test_init_close_fobj(self):
|
586 | 623 | # Issue #7341: Close the internal file object in the TarFile
|
587 | 624 | # constructor in case of an error. For the test we rely on
|
@@ -1092,6 +1129,17 @@ def test_directory_size(self):
|
1092 | 1129 | finally:
|
1093 | 1130 | support.rmdir(path)
|
1094 | 1131 |
|
| 1132 | + def test_gettarinfo_pathlike_name(self): |
| 1133 | + with tarfile.open(tmpname, self.mode) as tar: |
| 1134 | + path = pathlib.Path(TEMPDIR) / "file" |
| 1135 | + with open(path, "wb") as fobj: |
| 1136 | + fobj.write(b"aaa") |
| 1137 | + tarinfo = tar.gettarinfo(path) |
| 1138 | + tarinfo2 = tar.gettarinfo(os.fspath(path)) |
| 1139 | + self.assertIsInstance(tarinfo.name, str) |
| 1140 | + self.assertEqual(tarinfo.name, tarinfo2.name) |
| 1141 | + self.assertEqual(tarinfo.size, 3) |
| 1142 | + |
1095 | 1143 | @unittest.skipUnless(hasattr(os, "link"),
|
1096 | 1144 | "Missing hardlink implementation")
|
1097 | 1145 | def test_link_size(self):
|
@@ -1501,6 +1549,34 @@ def test_create_existing_taropen(self):
|
1501 | 1549 | self.assertEqual(len(names), 1)
|
1502 | 1550 | self.assertIn("spameggs42", names[0])
|
1503 | 1551 |
|
| 1552 | + def test_create_pathlike_name(self): |
| 1553 | + with tarfile.open(pathlib.Path(tmpname), self.mode) as tobj: |
| 1554 | + self.assertIsInstance(tobj.name, str) |
| 1555 | + self.assertEqual(tobj.name, os.path.abspath(tmpname)) |
| 1556 | + tobj.add(pathlib.Path(self.file_path)) |
| 1557 | + names = tobj.getnames() |
| 1558 | + self.assertEqual(len(names), 1) |
| 1559 | + self.assertIn('spameggs42', names[0]) |
| 1560 | + |
| 1561 | + with self.taropen(tmpname) as tobj: |
| 1562 | + names = tobj.getnames() |
| 1563 | + self.assertEqual(len(names), 1) |
| 1564 | + self.assertIn('spameggs42', names[0]) |
| 1565 | + |
| 1566 | + def test_create_taropen_pathlike_name(self): |
| 1567 | + with self.taropen(pathlib.Path(tmpname), "x") as tobj: |
| 1568 | + self.assertIsInstance(tobj.name, str) |
| 1569 | + self.assertEqual(tobj.name, os.path.abspath(tmpname)) |
| 1570 | + tobj.add(pathlib.Path(self.file_path)) |
| 1571 | + names = tobj.getnames() |
| 1572 | + self.assertEqual(len(names), 1) |
| 1573 | + self.assertIn('spameggs42', names[0]) |
| 1574 | + |
| 1575 | + with self.taropen(tmpname) as tobj: |
| 1576 | + names = tobj.getnames() |
| 1577 | + self.assertEqual(len(names), 1) |
| 1578 | + self.assertIn('spameggs42', names[0]) |
| 1579 | + |
1504 | 1580 |
|
1505 | 1581 | class GzipCreateTest(GzipTest, CreateTest):
|
1506 | 1582 | pass
|
|
0 commit comments