Skip to content

Commit 734940f

Browse files
committed
Delete pathlib._Accessor.
1 parent f3855f8 commit 734940f

File tree

2 files changed

+57
-111
lines changed

2 files changed

+57
-111
lines changed

Lib/pathlib.py

Lines changed: 53 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -274,93 +274,6 @@ def make_uri(self, path):
274274
_posix_flavour = _PosixFlavour()
275275

276276

277-
class _Accessor:
278-
"""An accessor implements a particular (system-specific or not) way of
279-
accessing paths on the filesystem."""
280-
281-
282-
class _NormalAccessor(_Accessor):
283-
284-
stat = os.stat
285-
286-
open = io.open
287-
288-
listdir = os.listdir
289-
290-
scandir = os.scandir
291-
292-
chmod = os.chmod
293-
294-
mkdir = os.mkdir
295-
296-
unlink = os.unlink
297-
298-
if hasattr(os, "link"):
299-
link = os.link
300-
else:
301-
def link(self, src, dst):
302-
raise NotImplementedError("os.link() not available on this system")
303-
304-
rmdir = os.rmdir
305-
306-
rename = os.rename
307-
308-
replace = os.replace
309-
310-
if hasattr(os, "symlink"):
311-
symlink = os.symlink
312-
else:
313-
def symlink(self, src, dst, target_is_directory=False):
314-
raise NotImplementedError("os.symlink() not available on this system")
315-
316-
def touch(self, path, mode=0o666, exist_ok=True):
317-
if exist_ok:
318-
# First try to bump modification time
319-
# Implementation note: GNU touch uses the UTIME_NOW option of
320-
# the utimensat() / futimens() functions.
321-
try:
322-
os.utime(path, None)
323-
except OSError:
324-
# Avoid exception chaining
325-
pass
326-
else:
327-
return
328-
flags = os.O_CREAT | os.O_WRONLY
329-
if not exist_ok:
330-
flags |= os.O_EXCL
331-
fd = os.open(path, flags, mode)
332-
os.close(fd)
333-
334-
if hasattr(os, "readlink"):
335-
readlink = os.readlink
336-
else:
337-
def readlink(self, path):
338-
raise NotImplementedError("os.readlink() not available on this system")
339-
340-
def owner(self, path):
341-
try:
342-
import pwd
343-
return pwd.getpwuid(self.stat(path).st_uid).pw_name
344-
except ImportError:
345-
raise NotImplementedError("Path.owner() is unsupported on this system")
346-
347-
def group(self, path):
348-
try:
349-
import grp
350-
return grp.getgrgid(self.stat(path).st_gid).gr_name
351-
except ImportError:
352-
raise NotImplementedError("Path.group() is unsupported on this system")
353-
354-
getcwd = os.getcwd
355-
356-
expanduser = staticmethod(os.path.expanduser)
357-
358-
realpath = staticmethod(os.path.realpath)
359-
360-
361-
_normal_accessor = _NormalAccessor()
362-
363-
364277
#
365278
# Globbing helpers
366279
#
@@ -948,7 +861,6 @@ class Path(PurePath):
948861
object. You can also instantiate a PosixPath or WindowsPath directly,
949862
but cannot instantiate a WindowsPath on a POSIX system or vice versa.
950863
"""
951-
_accessor = _normal_accessor
952864
__slots__ = ()
953865

954866
def __new__(cls, *args, **kwargs):
@@ -987,7 +899,7 @@ def cwd(cls):
987899
"""Return a new path pointing to the current working directory
988900
(as returned by os.getcwd()).
989901
"""
990-
return cls(cls._accessor.getcwd())
902+
return cls(os.getcwd())
991903

992904
@classmethod
993905
def home(cls):
@@ -1011,14 +923,14 @@ def iterdir(self):
1011923
"""Iterate over the files in this directory. Does not yield any
1012924
result for the special paths '.' and '..'.
1013925
"""
1014-
for name in self._accessor.listdir(self):
926+
for name in os.listdir(self):
1015927
if name in {'.', '..'}:
1016928
# Yielding a path object for these makes little sense
1017929
continue
1018930
yield self._make_child_relpath(name)
1019931

1020932
def scandir(self):
1021-
return self._accessor.scandir(self)
933+
return os.scandir(self)
1022934

1023935
def glob(self, pattern):
1024936
"""Iterate over this subtree and yield all existing files (of any
@@ -1072,7 +984,7 @@ def check_eloop(e):
1072984
raise RuntimeError("Symlink loop from %r" % e.filename)
1073985

1074986
try:
1075-
s = self._accessor.realpath(self, strict=strict)
987+
s = os.path.realpath(self, strict=strict)
1076988
except OSError as e:
1077989
check_eloop(e)
1078990
raise
@@ -1092,19 +1004,28 @@ def stat(self, *, follow_symlinks=True):
10921004
Return the result of the stat() system call on this path, like
10931005
os.stat() does.
10941006
"""
1095-
return self._accessor.stat(self, follow_symlinks=follow_symlinks)
1007+
return os.stat(self, follow_symlinks=follow_symlinks)
10961008

10971009
def owner(self):
10981010
"""
10991011
Return the login name of the file owner.
11001012
"""
1101-
return self._accessor.owner(self)
1013+
try:
1014+
import pwd
1015+
return pwd.getpwuid(self.stat().st_uid).pw_name
1016+
except ImportError:
1017+
raise NotImplementedError("Path.owner() is unsupported on this system")
11021018

11031019
def group(self):
11041020
"""
11051021
Return the group name of the file gid.
11061022
"""
1107-
return self._accessor.group(self)
1023+
1024+
try:
1025+
import grp
1026+
return grp.getgrgid(self.stat().st_gid).gr_name
1027+
except ImportError:
1028+
raise NotImplementedError("Path.group() is unsupported on this system")
11081029

11091030
def open(self, mode='r', buffering=-1, encoding=None,
11101031
errors=None, newline=None):
@@ -1114,8 +1035,7 @@ def open(self, mode='r', buffering=-1, encoding=None,
11141035
"""
11151036
if "b" not in mode:
11161037
encoding = io.text_encoding(encoding)
1117-
return self._accessor.open(self, mode, buffering, encoding, errors,
1118-
newline)
1038+
return io.open(self, mode, buffering, encoding, errors, newline)
11191039

11201040
def read_bytes(self):
11211041
"""
@@ -1156,21 +1076,40 @@ def readlink(self):
11561076
"""
11571077
Return the path to which the symbolic link points.
11581078
"""
1159-
path = self._accessor.readlink(self)
1079+
if hasattr(os, "readlink"):
1080+
path = os.readlink(self)
1081+
else:
1082+
raise NotImplementedError("os.readlink() not available on this system")
11601083
return self._from_parts((path,))
11611084

11621085
def touch(self, mode=0o666, exist_ok=True):
11631086
"""
11641087
Create this file with the given access mode, if it doesn't exist.
11651088
"""
1166-
self._accessor.touch(self, mode, exist_ok)
1089+
1090+
if exist_ok:
1091+
# First try to bump modification time
1092+
# Implementation note: GNU touch uses the UTIME_NOW option of
1093+
# the utimensat() / futimens() functions.
1094+
try:
1095+
os.utime(self, None)
1096+
except OSError:
1097+
# Avoid exception chaining
1098+
pass
1099+
else:
1100+
return
1101+
flags = os.O_CREAT | os.O_WRONLY
1102+
if not exist_ok:
1103+
flags |= os.O_EXCL
1104+
fd = os.open(self, flags, mode)
1105+
os.close(fd)
11671106

11681107
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
11691108
"""
11701109
Create a new directory at this given path.
11711110
"""
11721111
try:
1173-
self._accessor.mkdir(self, mode)
1112+
os.mkdir(self, mode)
11741113
except FileNotFoundError:
11751114
if not parents or self.parent == self:
11761115
raise
@@ -1186,7 +1125,7 @@ def chmod(self, mode, *, follow_symlinks=True):
11861125
"""
11871126
Change the permissions of the path, like os.chmod().
11881127
"""
1189-
self._accessor.chmod(self, mode, follow_symlinks=follow_symlinks)
1128+
os.chmod(self, mode, follow_symlinks=follow_symlinks)
11901129

11911130
def lchmod(self, mode):
11921131
"""
@@ -1201,7 +1140,7 @@ def unlink(self, missing_ok=False):
12011140
If the path is a directory, use rmdir() instead.
12021141
"""
12031142
try:
1204-
self._accessor.unlink(self)
1143+
os.unlink(self)
12051144
except FileNotFoundError:
12061145
if not missing_ok:
12071146
raise
@@ -1210,7 +1149,7 @@ def rmdir(self):
12101149
"""
12111150
Remove this directory. The directory must be empty.
12121151
"""
1213-
self._accessor.rmdir(self)
1152+
os.rmdir(self)
12141153

12151154
def lstat(self):
12161155
"""
@@ -1229,7 +1168,7 @@ def rename(self, target):
12291168
12301169
Returns the new Path instance pointing to the target path.
12311170
"""
1232-
self._accessor.rename(self, target)
1171+
os.rename(self, target)
12331172
return self.__class__(target)
12341173

12351174
def replace(self, target):
@@ -1242,23 +1181,29 @@ def replace(self, target):
12421181
12431182
Returns the new Path instance pointing to the target path.
12441183
"""
1245-
self._accessor.replace(self, target)
1184+
os.replace(self, target)
12461185
return self.__class__(target)
12471186

12481187
def symlink_to(self, target, target_is_directory=False):
12491188
"""
12501189
Make this path a symlink pointing to the target path.
12511190
Note the order of arguments (link, target) is the reverse of os.symlink.
12521191
"""
1253-
self._accessor.symlink(target, self, target_is_directory)
1192+
if hasattr(os, "symlink"):
1193+
os.symlink(target, self, target_is_directory)
1194+
else:
1195+
raise NotImplementedError("os.symlink() not available on this system")
12541196

12551197
def hardlink_to(self, target):
12561198
"""
12571199
Make this path a hard link pointing to the same file as *target*.
12581200
12591201
Note the order of arguments (self, target) is the reverse of os.link's.
12601202
"""
1261-
self._accessor.link(target, self)
1203+
if hasattr(os, "link"):
1204+
os.link(target, self)
1205+
else:
1206+
raise NotImplementedError("os.link() not available on this system")
12621207

12631208
def link_to(self, target):
12641209
"""
@@ -1433,7 +1378,7 @@ def expanduser(self):
14331378
"""
14341379
if (not (self._drv or self._root) and
14351380
self._parts and self._parts[0][:1] == '~'):
1436-
homedir = self._accessor.expanduser(self._parts[0])
1381+
homedir = os.path.expanduser(self._parts[0])
14371382
if homedir[:1] == "~":
14381383
raise RuntimeError("Could not determine home directory.")
14391384
return self._from_parts([homedir] + self._parts[1:])

Lib/test/test_pathlib.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,7 @@ def test_mkdir_concurrent_parent_creation(self):
21772177
p = self.cls(BASE, 'dirCPC%d' % pattern_num)
21782178
self.assertFalse(p.exists())
21792179

2180+
real_mkdir = os.mkdir
21802181
def my_mkdir(path, mode=0o777):
21812182
path = str(path)
21822183
# Emulate another process that would create the directory
@@ -2185,15 +2186,15 @@ def my_mkdir(path, mode=0o777):
21852186
# function is called at most 5 times (dirCPC/dir1/dir2,
21862187
# dirCPC/dir1, dirCPC, dirCPC/dir1, dirCPC/dir1/dir2).
21872188
if pattern.pop():
2188-
os.mkdir(path, mode) # From another process.
2189+
real_mkdir(path, mode) # From another process.
21892190
concurrently_created.add(path)
2190-
os.mkdir(path, mode) # Our real call.
2191+
real_mkdir(path, mode) # Our real call.
21912192

21922193
pattern = [bool(pattern_num & (1 << n)) for n in range(5)]
21932194
concurrently_created = set()
21942195
p12 = p / 'dir1' / 'dir2'
21952196
try:
2196-
with mock.patch("pathlib._normal_accessor.mkdir", my_mkdir):
2197+
with mock.patch("os.mkdir", my_mkdir):
21972198
p12.mkdir(parents=True, exist_ok=False)
21982199
except FileExistsError:
21992200
self.assertIn(str(p12), concurrently_created)

0 commit comments

Comments
 (0)