mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-43012: remove pathlib._Accessor
(GH-25701)
Per Pitrou: > The original intent for the “accessor” thing was to have a variant that did all accesses under a filesystem tree in a race condition-free way using openat and friends. It turned out to be much too hairy to actually implement, so was entirely abandoned, but the accessor abstraction was left there. https://discuss.python.org/t/make-pathlib-extensible/3428/2 Accessors are: - Lacking any internal purpose - '_NormalAccessor' is the only implementation - Lacking any firm conceptual difference to `Path` objects themselves (inc. subclasses) - Non-public, i.e. underscore prefixed - '_Accessor' and '_NormalAccessor' - Unofficially used to implement customized `Path` objects, but once once [bpo-24132]() is addressed there will be a supported route for that. This patch preserves all existing behaviour.
This commit is contained in:
parent
187930f74c
commit
08f8301b21
3 changed files with 77 additions and 129 deletions
|
@ -1,3 +1,4 @@
|
|||
import contextlib
|
||||
import collections.abc
|
||||
import io
|
||||
import os
|
||||
|
@ -1459,7 +1460,7 @@ class _BasePathTest(object):
|
|||
def test_absolute_common(self):
|
||||
P = self.cls
|
||||
|
||||
with mock.patch("pathlib._normal_accessor.getcwd") as getcwd:
|
||||
with mock.patch("os.getcwd") as getcwd:
|
||||
getcwd.return_value = BASE
|
||||
|
||||
# Simple relative paths.
|
||||
|
@ -1738,21 +1739,18 @@ class _BasePathTest(object):
|
|||
# Patching is needed to avoid relying on the filesystem
|
||||
# to return the order of the files as the error will not
|
||||
# happen if the symlink is the last item.
|
||||
real_scandir = os.scandir
|
||||
def my_scandir(path):
|
||||
with real_scandir(path) as scandir_it:
|
||||
entries = list(scandir_it)
|
||||
entries.sort(key=lambda entry: entry.name)
|
||||
return contextlib.nullcontext(entries)
|
||||
|
||||
with mock.patch("os.scandir") as scandir:
|
||||
scandir.return_value = sorted(os.scandir(base))
|
||||
with mock.patch("os.scandir", my_scandir):
|
||||
self.assertEqual(len(set(base.glob("*"))), 3)
|
||||
|
||||
subdir.mkdir()
|
||||
|
||||
with mock.patch("os.scandir") as scandir:
|
||||
scandir.return_value = sorted(os.scandir(base))
|
||||
subdir.mkdir()
|
||||
self.assertEqual(len(set(base.glob("*"))), 4)
|
||||
|
||||
subdir.chmod(000)
|
||||
|
||||
with mock.patch("os.scandir") as scandir:
|
||||
scandir.return_value = sorted(os.scandir(base))
|
||||
subdir.chmod(000)
|
||||
self.assertEqual(len(set(base.glob("*"))), 4)
|
||||
|
||||
def _check_resolve(self, p, expected, strict=True):
|
||||
|
@ -2199,6 +2197,7 @@ class _BasePathTest(object):
|
|||
p = self.cls(BASE, 'dirCPC%d' % pattern_num)
|
||||
self.assertFalse(p.exists())
|
||||
|
||||
real_mkdir = os.mkdir
|
||||
def my_mkdir(path, mode=0o777):
|
||||
path = str(path)
|
||||
# Emulate another process that would create the directory
|
||||
|
@ -2207,15 +2206,15 @@ class _BasePathTest(object):
|
|||
# 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.
|
||||
real_mkdir(path, mode) # From another process.
|
||||
concurrently_created.add(path)
|
||||
os.mkdir(path, mode) # Our real call.
|
||||
real_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):
|
||||
with mock.patch("os.mkdir", my_mkdir):
|
||||
p12.mkdir(parents=True, exist_ok=False)
|
||||
except FileExistsError:
|
||||
self.assertIn(str(p12), concurrently_created)
|
||||
|
@ -2676,7 +2675,7 @@ class WindowsPathTest(_BasePathTest, unittest.TestCase):
|
|||
self.assertEqual(str(P(share + 'a\\b').absolute()), share + 'a\\b')
|
||||
|
||||
# UNC relative paths.
|
||||
with mock.patch("pathlib._normal_accessor.getcwd") as getcwd:
|
||||
with mock.patch("os.getcwd") as getcwd:
|
||||
getcwd.return_value = share
|
||||
|
||||
self.assertEqual(str(P().absolute()), share)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue