Skip to content
Closed
3 changes: 2 additions & 1 deletion Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2208,7 +2208,8 @@ def add(self, name, arcname=None, recursive=True, *, filter=None):
self.addfile(tarinfo, f)

elif tarinfo.isdir():
self.addfile(tarinfo)
if tarinfo.name != os.curdir:
self.addfile(tarinfo)
if recursive:
for f in sorted(os.listdir(name)):
self.add(os.path.join(name, f), os.path.join(arcname, f),
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,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
Expand All @@ -1648,7 +1648,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):
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,8 +1587,9 @@ def test_cwd(self):
tar = tarfile.open(tmpname, "r")
try:
for t in tar:
if t.name != ".":
self.assertTrue(t.name.startswith("./"), t.name)
# gh-80145: "." is now excluded from tarfiles
self.assertNotEqual(t.name, os.curdir)
self.assertTrue(t.name.startswith("./"), t.name)
finally:
tar.close()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The tarfile module now excludes '.' directory entries when adding
a directory to a tar archive. Patch by Jeffrey Kintscher.