@@ -274,93 +274,6 @@ def make_uri(self, path):
274
274
_posix_flavour = _PosixFlavour ()
275
275
276
276
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
-
364
277
#
365
278
# Globbing helpers
366
279
#
@@ -948,7 +861,6 @@ class Path(PurePath):
948
861
object. You can also instantiate a PosixPath or WindowsPath directly,
949
862
but cannot instantiate a WindowsPath on a POSIX system or vice versa.
950
863
"""
951
- _accessor = _normal_accessor
952
864
__slots__ = ()
953
865
954
866
def __new__ (cls , * args , ** kwargs ):
@@ -987,7 +899,7 @@ def cwd(cls):
987
899
"""Return a new path pointing to the current working directory
988
900
(as returned by os.getcwd()).
989
901
"""
990
- return cls (cls . _accessor .getcwd ())
902
+ return cls (os .getcwd ())
991
903
992
904
@classmethod
993
905
def home (cls ):
@@ -1011,14 +923,14 @@ def iterdir(self):
1011
923
"""Iterate over the files in this directory. Does not yield any
1012
924
result for the special paths '.' and '..'.
1013
925
"""
1014
- for name in self . _accessor .listdir (self ):
926
+ for name in os .listdir (self ):
1015
927
if name in {'.' , '..' }:
1016
928
# Yielding a path object for these makes little sense
1017
929
continue
1018
930
yield self ._make_child_relpath (name )
1019
931
1020
932
def scandir (self ):
1021
- return self . _accessor .scandir (self )
933
+ return os .scandir (self )
1022
934
1023
935
def glob (self , pattern ):
1024
936
"""Iterate over this subtree and yield all existing files (of any
@@ -1072,7 +984,7 @@ def check_eloop(e):
1072
984
raise RuntimeError ("Symlink loop from %r" % e .filename )
1073
985
1074
986
try :
1075
- s = self . _accessor .realpath (self , strict = strict )
987
+ s = os . path .realpath (self , strict = strict )
1076
988
except OSError as e :
1077
989
check_eloop (e )
1078
990
raise
@@ -1092,19 +1004,28 @@ def stat(self, *, follow_symlinks=True):
1092
1004
Return the result of the stat() system call on this path, like
1093
1005
os.stat() does.
1094
1006
"""
1095
- return self . _accessor .stat (self , follow_symlinks = follow_symlinks )
1007
+ return os .stat (self , follow_symlinks = follow_symlinks )
1096
1008
1097
1009
def owner (self ):
1098
1010
"""
1099
1011
Return the login name of the file owner.
1100
1012
"""
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" )
1102
1018
1103
1019
def group (self ):
1104
1020
"""
1105
1021
Return the group name of the file gid.
1106
1022
"""
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" )
1108
1029
1109
1030
def open (self , mode = 'r' , buffering = - 1 , encoding = None ,
1110
1031
errors = None , newline = None ):
@@ -1114,8 +1035,7 @@ def open(self, mode='r', buffering=-1, encoding=None,
1114
1035
"""
1115
1036
if "b" not in mode :
1116
1037
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 )
1119
1039
1120
1040
def read_bytes (self ):
1121
1041
"""
@@ -1156,21 +1076,40 @@ def readlink(self):
1156
1076
"""
1157
1077
Return the path to which the symbolic link points.
1158
1078
"""
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" )
1160
1083
return self ._from_parts ((path ,))
1161
1084
1162
1085
def touch (self , mode = 0o666 , exist_ok = True ):
1163
1086
"""
1164
1087
Create this file with the given access mode, if it doesn't exist.
1165
1088
"""
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 )
1167
1106
1168
1107
def mkdir (self , mode = 0o777 , parents = False , exist_ok = False ):
1169
1108
"""
1170
1109
Create a new directory at this given path.
1171
1110
"""
1172
1111
try :
1173
- self . _accessor .mkdir (self , mode )
1112
+ os .mkdir (self , mode )
1174
1113
except FileNotFoundError :
1175
1114
if not parents or self .parent == self :
1176
1115
raise
@@ -1186,7 +1125,7 @@ def chmod(self, mode, *, follow_symlinks=True):
1186
1125
"""
1187
1126
Change the permissions of the path, like os.chmod().
1188
1127
"""
1189
- self . _accessor .chmod (self , mode , follow_symlinks = follow_symlinks )
1128
+ os .chmod (self , mode , follow_symlinks = follow_symlinks )
1190
1129
1191
1130
def lchmod (self , mode ):
1192
1131
"""
@@ -1201,7 +1140,7 @@ def unlink(self, missing_ok=False):
1201
1140
If the path is a directory, use rmdir() instead.
1202
1141
"""
1203
1142
try :
1204
- self . _accessor .unlink (self )
1143
+ os .unlink (self )
1205
1144
except FileNotFoundError :
1206
1145
if not missing_ok :
1207
1146
raise
@@ -1210,7 +1149,7 @@ def rmdir(self):
1210
1149
"""
1211
1150
Remove this directory. The directory must be empty.
1212
1151
"""
1213
- self . _accessor .rmdir (self )
1152
+ os .rmdir (self )
1214
1153
1215
1154
def lstat (self ):
1216
1155
"""
@@ -1229,7 +1168,7 @@ def rename(self, target):
1229
1168
1230
1169
Returns the new Path instance pointing to the target path.
1231
1170
"""
1232
- self . _accessor .rename (self , target )
1171
+ os .rename (self , target )
1233
1172
return self .__class__ (target )
1234
1173
1235
1174
def replace (self , target ):
@@ -1242,23 +1181,29 @@ def replace(self, target):
1242
1181
1243
1182
Returns the new Path instance pointing to the target path.
1244
1183
"""
1245
- self . _accessor .replace (self , target )
1184
+ os .replace (self , target )
1246
1185
return self .__class__ (target )
1247
1186
1248
1187
def symlink_to (self , target , target_is_directory = False ):
1249
1188
"""
1250
1189
Make this path a symlink pointing to the target path.
1251
1190
Note the order of arguments (link, target) is the reverse of os.symlink.
1252
1191
"""
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" )
1254
1196
1255
1197
def hardlink_to (self , target ):
1256
1198
"""
1257
1199
Make this path a hard link pointing to the same file as *target*.
1258
1200
1259
1201
Note the order of arguments (self, target) is the reverse of os.link's.
1260
1202
"""
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" )
1262
1207
1263
1208
def link_to (self , target ):
1264
1209
"""
@@ -1433,7 +1378,7 @@ def expanduser(self):
1433
1378
"""
1434
1379
if (not (self ._drv or self ._root ) and
1435
1380
self ._parts and self ._parts [0 ][:1 ] == '~' ):
1436
- homedir = self . _accessor .expanduser (self ._parts [0 ])
1381
+ homedir = os . path .expanduser (self ._parts [0 ])
1437
1382
if homedir [:1 ] == "~" :
1438
1383
raise RuntimeError ("Could not determine home directory." )
1439
1384
return self ._from_parts ([homedir ] + self ._parts [1 :])
0 commit comments