summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorMariatta <Mariatta@users.noreply.github.com>2017-04-14 02:26:16 (GMT)
committerGitHub <noreply@github.com>2017-04-14 02:26:16 (GMT)
commitcbc46afa59dcc43c2c8c90ae7a0a0dc404325a89 (patch)
tree44df38cbed9accd263365cb2eb250bcc7b16dcef /Lib/test
parent2cdf087d1fd48f7d0f95b5a0b31b9a624fa84751 (diff)
downloadcpython-cbc46afa59dcc43c2c8c90ae7a0a0dc404325a89.zip
cpython-cbc46afa59dcc43c2c8c90ae7a0a0dc404325a89.tar.gz
cpython-cbc46afa59dcc43c2c8c90ae7a0a0dc404325a89.tar.bz2
[3.6] bpo-29694: race condition in pathlib mkdir with flags parents=True (GH-1089). (GH-1126)
(cherry picked from commit 22a594a0047d7706537ff2ac676cdc0f1dcb329c)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_pathlib.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 3ff9726..846f721 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -8,6 +8,7 @@ import socket
import stat
import tempfile
import unittest
+from unittest import mock
from test import support
android_not_root = support.android_not_root
@@ -1816,6 +1817,35 @@ class _BasePathTest(object):
p.mkdir(exist_ok=True)
self.assertEqual(cm.exception.errno, errno.EEXIST)
+ def test_mkdir_concurrent_parent_creation(self):
+ for pattern_num in range(32):
+ p = self.cls(BASE, 'dirCPC%d' % pattern_num)
+ self.assertFalse(p.exists())
+
+ def my_mkdir(path, mode=0o777):
+ path = str(path)
+ # Emulate another process that would create the directory
+ # just before we try to create it ourselves. We do it
+ # in all possible pattern combinations, assuming that this
+ # function is called at most 5 times (dirCPC/dir1/dir2,
+ # dirCPC/dir1, dirCPC, dirCPC/dir1, dirCPC/dir1/dir2).
+ if pattern.pop():
+ os.mkdir(path, mode) # from another process
+ concurrently_created.add(path)
+ os.mkdir(path, mode) # our real call
+
+ pattern = [bool(pattern_num & (1 << n)) for n in range(5)]
+ concurrently_created = set()
+ p12 = p / 'dir1' / 'dir2'
+ try:
+ with mock.patch("pathlib._normal_accessor.mkdir", my_mkdir):
+ p12.mkdir(parents=True, exist_ok=False)
+ except FileExistsError:
+ self.assertIn(str(p12), concurrently_created)
+ else:
+ self.assertNotIn(str(p12), concurrently_created)
+ self.assertTrue(p.exists())
+
@with_symlinks
def test_symlink_to(self):
P = self.cls(BASE)