From 76bec72845586c3af5a8e73013b31e28f78e351b Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Sat, 1 Jun 2019 19:38:32 -0700 Subject: [PATCH 1/8] bpo-35964: exclude '.' directory entries when adding a directory to a tar archive --- Doc/whatsnew/3.8.rst | 8 ++++++++ Lib/tarfile.py | 8 +++++--- Lib/test/test_shutil.py | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 76d00938dbec46..4de15f11b46510 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -577,6 +577,11 @@ format for new archives to improve portability and standards conformance, inherited from the corresponding change to the :mod:`tarfile` module. (Contributed by C.A.M. Gerlach in :issue:`30661`.) +:func:`shutil.make_archive` now excludes '.' directory entries when adding +directories to a tar archive. The new behavior is inherited from the +corresponding change to the :mod:`tarfile` module. +Contributed by Jeffrey Kintscher in :issue:`35964`.) + ssl --- @@ -652,6 +657,9 @@ This improves cross-platform portability with a consistent encoding (UTF-8) in a standardized and extensible format, and offers several other benefits. (Contributed by C.A.M. Gerlach in :issue:`36268`.) +The :mod:`tarfile` module now excludes '.' directory entries when adding +directories to a tar archive. (Contributed by Jeffrey Kintscher +in :issue:`35964`.) threading --------- diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 2c06f9160c658a..0eec7f66c1b0b6 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1969,11 +1969,13 @@ def add(self, name, arcname=None, recursive=True, *, filter=None): self.addfile(tarinfo, f) elif tarinfo.isdir(): - self.addfile(tarinfo) + if tarinfo.name != '.': + self.addfile(tarinfo) if recursive: for f in sorted(os.listdir(name)): - self.add(os.path.join(name, f), os.path.join(arcname, f), - recursive, filter=filter) + if f != '.': + self.add(os.path.join(name, f), os.path.join(arcname, f), + recursive, filter=filter) else: self.addfile(tarinfo) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 208718bb128105..2d56c3185a2a78 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -1091,7 +1091,7 @@ def test_make_tarball(self): self.assertTrue(tarfile.is_tarfile(tarball)) with tarfile.open(tarball, 'r:gz') as tf: self.assertCountEqual(tf.getnames(), - ['.', './sub', './sub2', + ['./sub', './sub2', './file1', './file2', './sub/file3']) # trying an uncompressed one @@ -1102,7 +1102,7 @@ def test_make_tarball(self): self.assertTrue(tarfile.is_tarfile(tarball)) with tarfile.open(tarball, 'r') as tf: self.assertCountEqual(tf.getnames(), - ['.', './sub', './sub2', + ['./sub', './sub2', './file1', './file2', './sub/file3']) def _tarinfo(self, path): From 0f311e13b4c33731df7a332085351f24155cd31e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Sun, 2 Jun 2019 02:42:44 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2019-06-02-02-42-43.bpo-35964.wG23fC.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2019-06-02-02-42-43.bpo-35964.wG23fC.rst diff --git a/Misc/NEWS.d/next/Library/2019-06-02-02-42-43.bpo-35964.wG23fC.rst b/Misc/NEWS.d/next/Library/2019-06-02-02-42-43.bpo-35964.wG23fC.rst new file mode 100644 index 00000000000000..e31958b7a9eaf5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-06-02-02-42-43.bpo-35964.wG23fC.rst @@ -0,0 +1 @@ +The tarfile module now excludes '.' directory entries when adding a directory to a tar archive. \ No newline at end of file From a50f7484e563f7f653b9bccbefa4a68529dc6b47 Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Wed, 3 Jul 2019 11:38:11 -0700 Subject: [PATCH 3/8] bpo-35964: remove whatsnew entries per reviewer --- Doc/whatsnew/3.8.rst | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 4de15f11b46510..76d00938dbec46 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -577,11 +577,6 @@ format for new archives to improve portability and standards conformance, inherited from the corresponding change to the :mod:`tarfile` module. (Contributed by C.A.M. Gerlach in :issue:`30661`.) -:func:`shutil.make_archive` now excludes '.' directory entries when adding -directories to a tar archive. The new behavior is inherited from the -corresponding change to the :mod:`tarfile` module. -Contributed by Jeffrey Kintscher in :issue:`35964`.) - ssl --- @@ -657,9 +652,6 @@ This improves cross-platform portability with a consistent encoding (UTF-8) in a standardized and extensible format, and offers several other benefits. (Contributed by C.A.M. Gerlach in :issue:`36268`.) -The :mod:`tarfile` module now excludes '.' directory entries when adding -directories to a tar archive. (Contributed by Jeffrey Kintscher -in :issue:`35964`.) threading --------- From 157ab21e645304a5c777e54fc3ce0cd42c5ca8d7 Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Wed, 3 Jul 2019 11:39:00 -0700 Subject: [PATCH 4/8] bpo-35964: add name to News blurb --- .../next/Library/2019-06-02-02-42-43.bpo-35964.wG23fC.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-06-02-02-42-43.bpo-35964.wG23fC.rst b/Misc/NEWS.d/next/Library/2019-06-02-02-42-43.bpo-35964.wG23fC.rst index e31958b7a9eaf5..0edbf6106c4f3a 100644 --- a/Misc/NEWS.d/next/Library/2019-06-02-02-42-43.bpo-35964.wG23fC.rst +++ b/Misc/NEWS.d/next/Library/2019-06-02-02-42-43.bpo-35964.wG23fC.rst @@ -1 +1,2 @@ -The tarfile module now excludes '.' directory entries when adding a directory to a tar archive. \ No newline at end of file +The tarfile module now excludes '.' directory entries when adding +a directory to a tar archive. Patch by Jeffrey Kintscher. From fcae73086d7cdbf928c28aafd0c5b4f93a80740a Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Wed, 3 Jul 2019 11:39:42 -0700 Subject: [PATCH 5/8] bpo-35964: test for exclusion of "." entries in tar files as part of the tarfile module tests --- Lib/test/test_tarfile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 7e32cbccd6c56d..7402c15c584d14 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -1352,8 +1352,8 @@ def test_cwd(self): tar = tarfile.open(tmpname, "r") try: for t in tar: - if t.name != ".": - self.assertTrue(t.name.startswith("./"), t.name) + self.assertNotEqual(t.name, os.curdir) + self.assertTrue(t.name.startswith("./"), t.name) finally: tar.close() From 6664a5ad17ffb81e60613ecd04fb532e4754987d Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Wed, 3 Jul 2019 12:18:53 -0700 Subject: [PATCH 6/8] bpo-35964: add explanatory comment --- Lib/test/test_tarfile.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 7402c15c584d14..0229b18d1100f0 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -1352,6 +1352,7 @@ def test_cwd(self): tar = tarfile.open(tmpname, "r") try: for t in tar: + # bpo-35964: "." is now excluded from tarfiles self.assertNotEqual(t.name, os.curdir) self.assertTrue(t.name.startswith("./"), t.name) finally: From 2bb229e968c81fb9308b57f48c2fd6db7c6644bd Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Wed, 3 Jul 2019 19:35:19 -0700 Subject: [PATCH 7/8] bpo-35964: use os.curdir; remove extraneous directory name check --- Lib/tarfile.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 0eec7f66c1b0b6..4a654b154c8a9f 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1969,13 +1969,12 @@ def add(self, name, arcname=None, recursive=True, *, filter=None): self.addfile(tarinfo, f) elif tarinfo.isdir(): - if tarinfo.name != '.': + if tarinfo.name != os.curdir: self.addfile(tarinfo) if recursive: for f in sorted(os.listdir(name)): - if f != '.': - self.add(os.path.join(name, f), os.path.join(arcname, f), - recursive, filter=filter) + self.add(os.path.join(name, f), os.path.join(arcname, f), + recursive, filter=filter) else: self.addfile(tarinfo) From 5ea50059edf937ff4c835558399f3ecc5898ebea Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sun, 21 Apr 2024 18:10:09 -0400 Subject: [PATCH 8/8] Update Lib/test/test_tarfile.py --- Lib/test/test_tarfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 427e95d0a668bb..cfc08038d0a190 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -1493,7 +1493,7 @@ def test_cwd(self): tar = tarfile.open(tmpname, "r") try: for t in tar: - # bpo-35964: "." is now excluded from tarfiles + # gh-80145: "." is now excluded from tarfiles self.assertNotEqual(t.name, os.curdir) self.assertTrue(t.name.startswith("./"), t.name) finally: