From 3912f2e05e6cfa45b4d88809cb0add26baf293ee Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Thu, 14 Jul 2022 10:39:57 +0300 Subject: [PATCH 01/11] Add pathlib support to shutil.make_archive --- Lib/shutil.py | 2 +- .../next/Library/2022-07-14-10-28-10.gh-issue-94844.NJ3W80.rst | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-07-14-10-28-10.gh-issue-94844.NJ3W80.rst diff --git a/Lib/shutil.py b/Lib/shutil.py index f4128647861b81c..8d950df2a172863 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -975,7 +975,7 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, """ import zipfile # late import for breaking circular dependency - zip_filename = base_name + ".zip" + zip_filename = f'{base_name}.zip' archive_dir = os.path.dirname(base_name) if archive_dir and not os.path.exists(archive_dir): diff --git a/Misc/NEWS.d/next/Library/2022-07-14-10-28-10.gh-issue-94844.NJ3W80.rst b/Misc/NEWS.d/next/Library/2022-07-14-10-28-10.gh-issue-94844.NJ3W80.rst new file mode 100644 index 000000000000000..4e584cffd8588ef --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-07-14-10-28-10.gh-issue-94844.NJ3W80.rst @@ -0,0 +1,2 @@ +:meth:`shutil.make_archive` now accepts any :class:`os.PathLike`. Patch by +Oleg Iarygin. From 828abde59743e057d6ad885019a7960fc6326bab Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Thu, 14 Jul 2022 11:07:36 +0300 Subject: [PATCH 02/11] Add pathlib support to shutil._make_tarball --- Lib/shutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/shutil.py b/Lib/shutil.py index 8d950df2a172863..243a605af4614c0 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -927,7 +927,7 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, import tarfile # late import for breaking circular dependency compress_ext = '.' + tar_compression if compress else '' - archive_name = base_name + '.tar' + compress_ext + archive_name = f'{base_name}.tar{compress_ext}' archive_dir = os.path.dirname(archive_name) if archive_dir and not os.path.exists(archive_dir): From 49a21588e0eff667f6bbcd8e16fccb387123e5af Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Thu, 14 Jul 2022 10:41:14 +0300 Subject: [PATCH 03/11] Extend TestArchives with pathlib cases --- Lib/test/test_shutil.py | 55 +++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index a2c4ab508195b34..262913b66da3a62 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -1327,9 +1327,9 @@ def test_copyfile_copy_dir(self): self.assertRaises(err, shutil.copyfile, dir2, src_dir) -class TestArchives(BaseTest, unittest.TestCase): +class _ArchiveTests(BaseTest): - ### shutil.make_archive + ### shutil.make_archive with str, pathlib, and probably something else @support.requires_zlib() def test_make_tarball(self): @@ -1345,7 +1345,7 @@ def test_make_tarball(self): with os_helper.change_cwd(work_dir), no_chdir: base_name = os.path.abspath(rel_base_name) - tarball = make_archive(rel_base_name, 'gztar', root_dir, '.') + tarball = self._make_archive(rel_base_name, 'gztar', root_dir, '.') # check if the compressed tarball was created self.assertEqual(tarball, base_name + '.tar.gz') @@ -1358,7 +1358,7 @@ def test_make_tarball(self): # trying an uncompressed one with os_helper.change_cwd(work_dir), no_chdir: - tarball = make_archive(rel_base_name, 'tar', root_dir, '.') + tarball = self._make_archive(rel_base_name, 'tar', root_dir, '.') self.assertEqual(tarball, base_name + '.tar') self.assertTrue(os.path.isfile(tarball)) self.assertTrue(tarfile.is_tarfile(tarball)) @@ -1394,7 +1394,7 @@ def test_tarfile_vs_tar(self): root_dir, base_dir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') with no_chdir: - tarball = make_archive(base_name, 'gztar', root_dir, base_dir) + tarball = self._make_archive(base_name, 'gztar', root_dir, base_dir) # check if the compressed tarball was created self.assertEqual(tarball, base_name + '.tar.gz') @@ -1412,13 +1412,13 @@ def test_tarfile_vs_tar(self): # trying an uncompressed one with no_chdir: - tarball = make_archive(base_name, 'tar', root_dir, base_dir) + tarball = self._make_archive(base_name, 'tar', root_dir, base_dir) self.assertEqual(tarball, base_name + '.tar') self.assertTrue(os.path.isfile(tarball)) # now for a dry_run with no_chdir: - tarball = make_archive(base_name, 'tar', root_dir, base_dir, + tarball = self._make_archive(base_name, 'tar', root_dir, base_dir, dry_run=True) self.assertEqual(tarball, base_name + '.tar') self.assertTrue(os.path.isfile(tarball)) @@ -1437,7 +1437,7 @@ def test_make_zipfile(self): with os_helper.change_cwd(work_dir), no_chdir: base_name = os.path.abspath(rel_base_name) - res = make_archive(rel_base_name, 'zip', root_dir) + res = self._make_archive(rel_base_name, 'zip', root_dir) self.assertEqual(res, base_name + '.zip') self.assertTrue(os.path.isfile(res)) @@ -1450,7 +1450,7 @@ def test_make_zipfile(self): with os_helper.change_cwd(work_dir), no_chdir: base_name = os.path.abspath(rel_base_name) - res = make_archive(rel_base_name, 'zip', root_dir, base_dir) + res = self._make_archive(rel_base_name, 'zip', root_dir, base_dir) self.assertEqual(res, base_name + '.zip') self.assertTrue(os.path.isfile(res)) @@ -1467,7 +1467,7 @@ def test_zipfile_vs_zip(self): root_dir, base_dir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') with no_chdir: - archive = make_archive(base_name, 'zip', root_dir, base_dir) + archive = self._make_archive(base_name, 'zip', root_dir, base_dir) # check if ZIP file was created self.assertEqual(archive, base_name + '.zip') @@ -1494,7 +1494,7 @@ def test_unzip_zipfile(self): root_dir, base_dir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') with no_chdir: - archive = make_archive(base_name, 'zip', root_dir, base_dir) + archive = self._make_archive(base_name, 'zip', root_dir, base_dir) # check if ZIP file was created self.assertEqual(archive, base_name + '.zip') @@ -1515,7 +1515,7 @@ def test_unzip_zipfile(self): def test_make_archive(self): tmpdir = self.mkdtemp() base_name = os.path.join(tmpdir, 'archive') - self.assertRaises(ValueError, make_archive, base_name, 'xxx') + self.assertRaises(ValueError, self._make_archive, base_name, 'xxx') @support.requires_zlib() def test_make_archive_owner_group(self): @@ -1529,18 +1529,18 @@ def test_make_archive_owner_group(self): root_dir, base_dir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') - res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, + res = self._make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, group=group) self.assertTrue(os.path.isfile(res)) - res = make_archive(base_name, 'zip', root_dir, base_dir) + res = self._make_archive(base_name, 'zip', root_dir, base_dir) self.assertTrue(os.path.isfile(res)) - res = make_archive(base_name, 'tar', root_dir, base_dir, + res = self._make_archive(base_name, 'tar', root_dir, base_dir, owner=owner, group=group) self.assertTrue(os.path.isfile(res)) - res = make_archive(base_name, 'tar', root_dir, base_dir, + res = self._make_archive(base_name, 'tar', root_dir, base_dir, owner='kjhkjhkjg', group='oihohoh') self.assertTrue(os.path.isfile(res)) @@ -1553,7 +1553,7 @@ def test_tarfile_root_owner(self): group = grp.getgrgid(0)[0] owner = pwd.getpwuid(0)[0] with os_helper.change_cwd(root_dir), no_chdir: - archive_name = make_archive(base_name, 'gztar', root_dir, 'dist', + archive_name = self._make_archive(base_name, 'gztar', root_dir, 'dist', owner=owner, group=group) # check if the compressed tarball was created @@ -1582,7 +1582,7 @@ def _chdir(path): try: with support.swap_attr(os, 'chdir', _chdir) as orig_chdir: try: - make_archive('xxx', 'xxx', root_dir=root_dir) + self._make_archive('xxx', 'xxx', root_dir=root_dir) except Exception: pass self.assertEqual(os.getcwd(), current_dir) @@ -1594,7 +1594,7 @@ def test_make_tarfile_in_curdir(self): # Issue #21280 root_dir = self.mkdtemp() with os_helper.change_cwd(root_dir), no_chdir: - self.assertEqual(make_archive('test', 'tar'), 'test.tar') + self.assertEqual(self._make_archive('test', 'tar'), 'test.tar') self.assertTrue(os.path.isfile('test.tar')) @support.requires_zlib() @@ -1602,7 +1602,7 @@ def test_make_zipfile_in_curdir(self): # Issue #21280 root_dir = self.mkdtemp() with os_helper.change_cwd(root_dir), no_chdir: - self.assertEqual(make_archive('test', 'zip'), 'test.zip') + self.assertEqual(self._make_archive('test', 'zip'), 'test.zip') self.assertTrue(os.path.isfile('test.zip')) def test_register_archive_format(self): @@ -1634,7 +1634,7 @@ def check_unpack_archive_with_converter(self, format, converter): expected.remove('outer') base_name = os.path.join(self.mkdtemp(), 'archive') - filename = make_archive(base_name, format, root_dir, base_dir) + filename = self._make_archive(base_name, format, root_dir, base_dir) # let's try to unpack it now tmpdir2 = self.mkdtemp() @@ -1696,6 +1696,19 @@ def _boo(filename, extract_dir, extra): self.assertEqual(get_unpack_formats(), formats) +class TestStrPathArchives(_ArchiveTests, unittest.TestCase): + + _make_archive = make_archive + + +class TestPathlibPathArchives(_ArchiveTests, unittest.TestCase): + + def _make_archive_with_path(self, path, /, *args, **kwargs): + return make_archive(pathlib.Path(path), *args, **kwargs) + + _make_archive = _make_archive_with_path + + class TestMisc(BaseTest, unittest.TestCase): @unittest.skipUnless(hasattr(shutil, 'disk_usage'), From deebebcff9935dc4b73eecc38e8f16320999a7cd Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Thu, 14 Jul 2022 11:39:42 +0300 Subject: [PATCH 04/11] Address a Barney's review --- Lib/shutil.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/shutil.py b/Lib/shutil.py index 243a605af4614c0..afeb2f435f55095 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -927,6 +927,7 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, import tarfile # late import for breaking circular dependency compress_ext = '.' + tar_compression if compress else '' + base_name = os.fspath(base_name) archive_name = f'{base_name}.tar{compress_ext}' archive_dir = os.path.dirname(archive_name) @@ -975,6 +976,7 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, """ import zipfile # late import for breaking circular dependency + base_name = os.fspath(base_name) zip_filename = f'{base_name}.zip' archive_dir = os.path.dirname(base_name) From 0e1424934225f1858cce074b596c0321928be4fd Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Thu, 14 Jul 2022 11:44:01 +0300 Subject: [PATCH 05/11] Fix accidental passing of `self` into a non-method --- Lib/test/test_shutil.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 262913b66da3a62..c01fdb29dca706e 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -1697,8 +1697,11 @@ def _boo(filename, extract_dir, extra): class TestStrPathArchives(_ArchiveTests, unittest.TestCase): - - _make_archive = make_archive + + def _make_archive_with_path(self, *args, **kwargs): + return make_archive(*args, **kwargs) + + _make_archive = _make_archive_with_path class TestPathlibPathArchives(_ArchiveTests, unittest.TestCase): From 26d736cea34c59f434a45a3ee6bcf29761348dd2 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Thu, 14 Jul 2022 12:21:57 +0300 Subject: [PATCH 06/11] Move os.fspath call into a higher level --- Lib/shutil.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/shutil.py b/Lib/shutil.py index afeb2f435f55095..caa9c5b5d963c5d 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -927,7 +927,6 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, import tarfile # late import for breaking circular dependency compress_ext = '.' + tar_compression if compress else '' - base_name = os.fspath(base_name) archive_name = f'{base_name}.tar{compress_ext}' archive_dir = os.path.dirname(archive_name) @@ -976,7 +975,6 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, """ import zipfile # late import for breaking circular dependency - base_name = os.fspath(base_name) zip_filename = f'{base_name}.zip' archive_dir = os.path.dirname(base_name) @@ -1129,6 +1127,7 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, if not dry_run: os.chdir(root_dir) + base_name = os.fspath(base_name) try: filename = func(base_name, base_dir, **kwargs) finally: From 85bf9f18c6c025c45db1471ff957d27f2fa8e473 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Thu, 14 Jul 2022 14:34:41 +0300 Subject: [PATCH 07/11] Revert the extra set of tests and the replacement with f-strings --- Lib/shutil.py | 4 +-- Lib/test/test_shutil.py | 60 +++++++++++++++-------------------------- 2 files changed, 24 insertions(+), 40 deletions(-) diff --git a/Lib/shutil.py b/Lib/shutil.py index caa9c5b5d963c5d..59a99b2498184ce 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -927,7 +927,7 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, import tarfile # late import for breaking circular dependency compress_ext = '.' + tar_compression if compress else '' - archive_name = f'{base_name}.tar{compress_ext}' + archive_name = base_name + '.tar' + compress_ext archive_dir = os.path.dirname(archive_name) if archive_dir and not os.path.exists(archive_dir): @@ -975,7 +975,7 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, """ import zipfile # late import for breaking circular dependency - zip_filename = f'{base_name}.zip' + zip_filename = base_name + ".zip" archive_dir = os.path.dirname(base_name) if archive_dir and not os.path.exists(archive_dir): diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index c01fdb29dca706e..5d8b4a2614dce80 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -1327,9 +1327,9 @@ def test_copyfile_copy_dir(self): self.assertRaises(err, shutil.copyfile, dir2, src_dir) -class _ArchiveTests(BaseTest): +class TestArchives(BaseTest, unittest.TestCase): - ### shutil.make_archive with str, pathlib, and probably something else + ### shutil.make_archive @support.requires_zlib() def test_make_tarball(self): @@ -1345,7 +1345,7 @@ def test_make_tarball(self): with os_helper.change_cwd(work_dir), no_chdir: base_name = os.path.abspath(rel_base_name) - tarball = self._make_archive(rel_base_name, 'gztar', root_dir, '.') + tarball = make_archive(rel_base_name, 'gztar', root_dir, '.') # check if the compressed tarball was created self.assertEqual(tarball, base_name + '.tar.gz') @@ -1358,7 +1358,7 @@ def test_make_tarball(self): # trying an uncompressed one with os_helper.change_cwd(work_dir), no_chdir: - tarball = self._make_archive(rel_base_name, 'tar', root_dir, '.') + tarball = make_archive(rel_base_name, 'tar', root_dir, '.') self.assertEqual(tarball, base_name + '.tar') self.assertTrue(os.path.isfile(tarball)) self.assertTrue(tarfile.is_tarfile(tarball)) @@ -1394,7 +1394,7 @@ def test_tarfile_vs_tar(self): root_dir, base_dir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') with no_chdir: - tarball = self._make_archive(base_name, 'gztar', root_dir, base_dir) + tarball = make_archive(base_name, 'gztar', root_dir, base_dir) # check if the compressed tarball was created self.assertEqual(tarball, base_name + '.tar.gz') @@ -1412,13 +1412,13 @@ def test_tarfile_vs_tar(self): # trying an uncompressed one with no_chdir: - tarball = self._make_archive(base_name, 'tar', root_dir, base_dir) + tarball = make_archive(base_name, 'tar', root_dir, base_dir) self.assertEqual(tarball, base_name + '.tar') self.assertTrue(os.path.isfile(tarball)) # now for a dry_run with no_chdir: - tarball = self._make_archive(base_name, 'tar', root_dir, base_dir, + tarball = make_archive(base_name, 'tar', root_dir, base_dir, dry_run=True) self.assertEqual(tarball, base_name + '.tar') self.assertTrue(os.path.isfile(tarball)) @@ -1437,7 +1437,7 @@ def test_make_zipfile(self): with os_helper.change_cwd(work_dir), no_chdir: base_name = os.path.abspath(rel_base_name) - res = self._make_archive(rel_base_name, 'zip', root_dir) + res = make_archive(rel_base_name, 'zip', root_dir) self.assertEqual(res, base_name + '.zip') self.assertTrue(os.path.isfile(res)) @@ -1450,7 +1450,7 @@ def test_make_zipfile(self): with os_helper.change_cwd(work_dir), no_chdir: base_name = os.path.abspath(rel_base_name) - res = self._make_archive(rel_base_name, 'zip', root_dir, base_dir) + res = make_archive(rel_base_name, 'zip', root_dir, base_dir) self.assertEqual(res, base_name + '.zip') self.assertTrue(os.path.isfile(res)) @@ -1467,7 +1467,7 @@ def test_zipfile_vs_zip(self): root_dir, base_dir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') with no_chdir: - archive = self._make_archive(base_name, 'zip', root_dir, base_dir) + archive = make_archive(base_name, 'zip', root_dir, base_dir) # check if ZIP file was created self.assertEqual(archive, base_name + '.zip') @@ -1494,7 +1494,7 @@ def test_unzip_zipfile(self): root_dir, base_dir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') with no_chdir: - archive = self._make_archive(base_name, 'zip', root_dir, base_dir) + archive = make_archive(base_name, 'zip', root_dir, base_dir) # check if ZIP file was created self.assertEqual(archive, base_name + '.zip') @@ -1515,7 +1515,7 @@ def test_unzip_zipfile(self): def test_make_archive(self): tmpdir = self.mkdtemp() base_name = os.path.join(tmpdir, 'archive') - self.assertRaises(ValueError, self._make_archive, base_name, 'xxx') + self.assertRaises(ValueError, make_archive, base_name, 'xxx') @support.requires_zlib() def test_make_archive_owner_group(self): @@ -1529,18 +1529,18 @@ def test_make_archive_owner_group(self): root_dir, base_dir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') - res = self._make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, + res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, group=group) self.assertTrue(os.path.isfile(res)) - res = self._make_archive(base_name, 'zip', root_dir, base_dir) + res = make_archive(base_name, 'zip', root_dir, base_dir) self.assertTrue(os.path.isfile(res)) - res = self._make_archive(base_name, 'tar', root_dir, base_dir, + res = make_archive(base_name, 'tar', root_dir, base_dir, owner=owner, group=group) self.assertTrue(os.path.isfile(res)) - res = self._make_archive(base_name, 'tar', root_dir, base_dir, + res = make_archive(base_name, 'tar', root_dir, base_dir, owner='kjhkjhkjg', group='oihohoh') self.assertTrue(os.path.isfile(res)) @@ -1553,7 +1553,7 @@ def test_tarfile_root_owner(self): group = grp.getgrgid(0)[0] owner = pwd.getpwuid(0)[0] with os_helper.change_cwd(root_dir), no_chdir: - archive_name = self._make_archive(base_name, 'gztar', root_dir, 'dist', + archive_name = make_archive(base_name, 'gztar', root_dir, 'dist', owner=owner, group=group) # check if the compressed tarball was created @@ -1582,7 +1582,7 @@ def _chdir(path): try: with support.swap_attr(os, 'chdir', _chdir) as orig_chdir: try: - self._make_archive('xxx', 'xxx', root_dir=root_dir) + make_archive('xxx', 'xxx', root_dir=root_dir) except Exception: pass self.assertEqual(os.getcwd(), current_dir) @@ -1594,7 +1594,7 @@ def test_make_tarfile_in_curdir(self): # Issue #21280 root_dir = self.mkdtemp() with os_helper.change_cwd(root_dir), no_chdir: - self.assertEqual(self._make_archive('test', 'tar'), 'test.tar') + self.assertEqual(make_archive('test', 'tar'), 'test.tar') self.assertTrue(os.path.isfile('test.tar')) @support.requires_zlib() @@ -1602,7 +1602,7 @@ def test_make_zipfile_in_curdir(self): # Issue #21280 root_dir = self.mkdtemp() with os_helper.change_cwd(root_dir), no_chdir: - self.assertEqual(self._make_archive('test', 'zip'), 'test.zip') + self.assertEqual(make_archive('test', 'zip'), 'test.zip') self.assertTrue(os.path.isfile('test.zip')) def test_register_archive_format(self): @@ -1634,7 +1634,7 @@ def check_unpack_archive_with_converter(self, format, converter): expected.remove('outer') base_name = os.path.join(self.mkdtemp(), 'archive') - filename = self._make_archive(base_name, format, root_dir, base_dir) + filename = make_archive(base_name, format, root_dir, base_dir) # let's try to unpack it now tmpdir2 = self.mkdtemp() @@ -1696,22 +1696,6 @@ def _boo(filename, extract_dir, extra): self.assertEqual(get_unpack_formats(), formats) -class TestStrPathArchives(_ArchiveTests, unittest.TestCase): - - def _make_archive_with_path(self, *args, **kwargs): - return make_archive(*args, **kwargs) - - _make_archive = _make_archive_with_path - - -class TestPathlibPathArchives(_ArchiveTests, unittest.TestCase): - - def _make_archive_with_path(self, path, /, *args, **kwargs): - return make_archive(pathlib.Path(path), *args, **kwargs) - - _make_archive = _make_archive_with_path - - class TestMisc(BaseTest, unittest.TestCase): @unittest.skipUnless(hasattr(shutil, 'disk_usage'), @@ -2721,4 +2705,4 @@ def test_module_all_attribute(self): if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file From f099b325225c6dffdfa9545653d7f022a58a1803 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Thu, 14 Jul 2022 14:36:59 +0300 Subject: [PATCH 08/11] Restore a newline missing in Github raw view --- Lib/test/test_shutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 5d8b4a2614dce80..a2c4ab508195b34 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -2705,4 +2705,4 @@ def test_module_all_attribute(self): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() From ff949665cfb7d7a5fe674bcf2bce234b897b52ce Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sat, 16 Jul 2022 17:29:45 +0300 Subject: [PATCH 09/11] Address the Serhiy's review --- Lib/shutil.py | 4 +++- .../Library/2022-07-14-10-28-10.gh-issue-94844.NJ3W80.rst | 2 -- 2 files changed, 3 insertions(+), 3 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2022-07-14-10-28-10.gh-issue-94844.NJ3W80.rst diff --git a/Lib/shutil.py b/Lib/shutil.py index 59a99b2498184ce..460e8ef3e6ee2eb 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -1127,7 +1127,9 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, if not dry_run: os.chdir(root_dir) - base_name = os.fspath(base_name) + if root_dir is not None and support_root_dir: + base_name = os.fspath(base_name) + try: filename = func(base_name, base_dir, **kwargs) finally: diff --git a/Misc/NEWS.d/next/Library/2022-07-14-10-28-10.gh-issue-94844.NJ3W80.rst b/Misc/NEWS.d/next/Library/2022-07-14-10-28-10.gh-issue-94844.NJ3W80.rst deleted file mode 100644 index 4e584cffd8588ef..000000000000000 --- a/Misc/NEWS.d/next/Library/2022-07-14-10-28-10.gh-issue-94844.NJ3W80.rst +++ /dev/null @@ -1,2 +0,0 @@ -:meth:`shutil.make_archive` now accepts any :class:`os.PathLike`. Patch by -Oleg Iarygin. From bdaf3f99425ef9d7e3b3e2a9051ff30a04e59aef Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sat, 16 Jul 2022 18:56:48 +0300 Subject: [PATCH 10/11] Address the Barney's review --- Lib/shutil.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/shutil.py b/Lib/shutil.py index 460e8ef3e6ee2eb..ee8b7c9df3e4a48 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -1118,6 +1118,7 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, save_cwd = None if root_dir is not None: if support_root_dir: + base_name = os.fspath(base_name) kwargs['root_dir'] = root_dir else: save_cwd = os.getcwd() @@ -1127,9 +1128,6 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, if not dry_run: os.chdir(root_dir) - if root_dir is not None and support_root_dir: - base_name = os.fspath(base_name) - try: filename = func(base_name, base_dir, **kwargs) finally: From 5a1c3470cf6e2daaa05ca37a0c0197e9737cb713 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sat, 16 Jul 2022 18:58:05 +0300 Subject: [PATCH 11/11] Add a forgotten comment from the review --- Lib/shutil.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/shutil.py b/Lib/shutil.py index ee8b7c9df3e4a48..b49437cd1f3e87c 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -1118,6 +1118,7 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, save_cwd = None if root_dir is not None: if support_root_dir: + # Support path-like base_name here for backwards-compatibility. base_name = os.fspath(base_name) kwargs['root_dir'] = root_dir else: