[3.6] bpo-28230: Document the pathlib support in tarfile and add test… · python/cpython@666165f (original) (raw)

`@@ -4,6 +4,7 @@

`

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):

`

`@@ -1528,6 +1576,34 @@ def test_create_existing_taropen(self):

`

1528

1576

`self.assertEqual(len(names), 1)

`

1529

1577

`self.assertIn("spameggs42", names[0])

`

1530

1578

``

``

1579

`+

def test_create_pathlike_name(self):

`

``

1580

`+

with tarfile.open(pathlib.Path(tmpname), self.mode) as tobj:

`

``

1581

`+

self.assertIsInstance(tobj.name, str)

`

``

1582

`+

self.assertEqual(tobj.name, os.path.abspath(tmpname))

`

``

1583

`+

tobj.add(pathlib.Path(self.file_path))

`

``

1584

`+

names = tobj.getnames()

`

``

1585

`+

self.assertEqual(len(names), 1)

`

``

1586

`+

self.assertIn('spameggs42', names[0])

`

``

1587

+

``

1588

`+

with self.taropen(tmpname) as tobj:

`

``

1589

`+

names = tobj.getnames()

`

``

1590

`+

self.assertEqual(len(names), 1)

`

``

1591

`+

self.assertIn('spameggs42', names[0])

`

``

1592

+

``

1593

`+

def test_create_taropen_pathlike_name(self):

`

``

1594

`+

with self.taropen(pathlib.Path(tmpname), "x") as tobj:

`

``

1595

`+

self.assertIsInstance(tobj.name, str)

`

``

1596

`+

self.assertEqual(tobj.name, os.path.abspath(tmpname))

`

``

1597

`+

tobj.add(pathlib.Path(self.file_path))

`

``

1598

`+

names = tobj.getnames()

`

``

1599

`+

self.assertEqual(len(names), 1)

`

``

1600

`+

self.assertIn('spameggs42', names[0])

`

``

1601

+

``

1602

`+

with self.taropen(tmpname) as tobj:

`

``

1603

`+

names = tobj.getnames()

`

``

1604

`+

self.assertEqual(len(names), 1)

`

``

1605

`+

self.assertIn('spameggs42', names[0])

`

``

1606

+

1531

1607

``

1532

1608

`class GzipCreateTest(GzipTest, CreateTest):

`

1533

1609

`pass

`