if upperdirs and not os.path.exists(upperdirs):
# Create directories that are not part of the archive with
# default permissions.
- os.makedirs(upperdirs)
+ os.makedirs(upperdirs, exist_ok=True)
if tarinfo.islnk() or tarinfo.issym():
self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname))
import os
import sys
+from test.support import swap_attr
from test.support import os_helper
class OverwriteTests:
self.extractall(ar)
self.assertTrue(os.path.islink(target))
self.assertFalse(os.path.exists(target2))
+
+ def test_concurrent_extract_dir(self):
+ target = os.path.join(self.testdir, 'test')
+ def concurrent_mkdir(*args, **kwargs):
+ orig_mkdir(*args, **kwargs)
+ orig_mkdir(*args, **kwargs)
+ with swap_attr(os, 'mkdir', concurrent_mkdir) as orig_mkdir:
+ with self.open(self.ar_with_dir) as ar:
+ self.extractall(ar)
+ self.assertTrue(os.path.isdir(target))
+
+ def test_concurrent_extract_implicit_dir(self):
+ target = os.path.join(self.testdir, 'test')
+ def concurrent_mkdir(*args, **kwargs):
+ orig_mkdir(*args, **kwargs)
+ orig_mkdir(*args, **kwargs)
+ with swap_attr(os, 'mkdir', concurrent_mkdir) as orig_mkdir:
+ with self.open(self.ar_with_implicit_dir) as ar:
+ self.extractall(ar)
+ self.assertTrue(os.path.isdir(target))
+ self.assertTrue(os.path.isfile(os.path.join(target, 'file')))
# Create all upper directories if necessary.
upperdirs = os.path.dirname(targetpath)
if upperdirs and not os.path.exists(upperdirs):
- os.makedirs(upperdirs)
+ os.makedirs(upperdirs, exist_ok=True)
if member.is_dir():
if not os.path.isdir(targetpath):
- os.mkdir(targetpath)
+ try:
+ os.mkdir(targetpath)
+ except FileExistsError:
+ if not os.path.isdir(targetpath):
+ raise
return targetpath
with self.open(member, pwd=pwd) as source, \