Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1 | import fnmatch |
| 2 | import functools |
| 3 | import io |
| 4 | import ntpath |
| 5 | import os |
| 6 | import posixpath |
| 7 | import re |
| 8 | import sys |
Serhiy Storchaka | 8110837 | 2017-09-26 00:55:55 +0300 | [diff] [blame] | 9 | from _collections_abc import Sequence |
Jörg Stucke | d5c120f | 2019-05-21 19:44:40 +0200 | [diff] [blame] | 10 | from errno import EINVAL, ENOENT, ENOTDIR, EBADF, ELOOP |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 11 | from operator import attrgetter |
| 12 | from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO |
Antoine Pitrou | 069a5e1 | 2013-12-03 09:41:35 +0100 | [diff] [blame] | 13 | from urllib.parse import quote_from_bytes as urlquote_from_bytes |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 14 | |
| 15 | |
Antoine Pitrou | db118f5 | 2014-11-19 00:32:08 +0100 | [diff] [blame] | 16 | if os.name == 'nt': |
Barney Gale | b57e045 | 2021-04-07 00:01:22 +0100 | [diff] [blame] | 17 | from nt import _getfinalpathname |
Antoine Pitrou | db118f5 | 2014-11-19 00:32:08 +0100 | [diff] [blame] | 18 | else: |
Barney Gale | b57e045 | 2021-04-07 00:01:22 +0100 | [diff] [blame] | 19 | _getfinalpathname = None |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 20 | |
| 21 | |
| 22 | __all__ = [ |
| 23 | "PurePath", "PurePosixPath", "PureWindowsPath", |
| 24 | "Path", "PosixPath", "WindowsPath", |
| 25 | ] |
| 26 | |
| 27 | # |
| 28 | # Internals |
| 29 | # |
| 30 | |
penguindustin | 9646630 | 2019-05-06 14:57:17 -0400 | [diff] [blame] | 31 | # EBADF - guard against macOS `stat` throwing EBADF |
Jörg Stucke | d5c120f | 2019-05-21 19:44:40 +0200 | [diff] [blame] | 32 | _IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF, ELOOP) |
Przemysław Spodymek | 216b745 | 2018-08-27 23:33:45 +0200 | [diff] [blame] | 33 | |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 34 | _IGNORED_WINERRORS = ( |
| 35 | 21, # ERROR_NOT_READY - drive exists but is not accessible |
Jörg Stucke | d5c120f | 2019-05-21 19:44:40 +0200 | [diff] [blame] | 36 | 1921, # ERROR_CANT_RESOLVE_FILENAME - fix for broken symlink pointing to itself |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 37 | ) |
| 38 | |
| 39 | def _ignore_error(exception): |
| 40 | return (getattr(exception, 'errno', None) in _IGNORED_ERROS or |
| 41 | getattr(exception, 'winerror', None) in _IGNORED_WINERRORS) |
| 42 | |
| 43 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 44 | def _is_wildcard_pattern(pat): |
| 45 | # Whether this pattern needs actual matching using fnmatch, or can |
| 46 | # be looked up directly as a file. |
| 47 | return "*" in pat or "?" in pat or "[" in pat |
| 48 | |
| 49 | |
| 50 | class _Flavour(object): |
| 51 | """A flavour implements a particular (platform-specific) set of path |
| 52 | semantics.""" |
| 53 | |
| 54 | def __init__(self): |
| 55 | self.join = self.sep.join |
| 56 | |
| 57 | def parse_parts(self, parts): |
| 58 | parsed = [] |
| 59 | sep = self.sep |
| 60 | altsep = self.altsep |
| 61 | drv = root = '' |
| 62 | it = reversed(parts) |
| 63 | for part in it: |
| 64 | if not part: |
| 65 | continue |
| 66 | if altsep: |
| 67 | part = part.replace(altsep, sep) |
| 68 | drv, root, rel = self.splitroot(part) |
| 69 | if sep in rel: |
| 70 | for x in reversed(rel.split(sep)): |
| 71 | if x and x != '.': |
| 72 | parsed.append(sys.intern(x)) |
| 73 | else: |
| 74 | if rel and rel != '.': |
| 75 | parsed.append(sys.intern(rel)) |
| 76 | if drv or root: |
| 77 | if not drv: |
| 78 | # If no drive is present, try to find one in the previous |
| 79 | # parts. This makes the result of parsing e.g. |
| 80 | # ("C:", "/", "a") reasonably intuitive. |
| 81 | for part in it: |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 82 | if not part: |
| 83 | continue |
| 84 | if altsep: |
| 85 | part = part.replace(altsep, sep) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 86 | drv = self.splitroot(part)[0] |
| 87 | if drv: |
| 88 | break |
| 89 | break |
| 90 | if drv or root: |
| 91 | parsed.append(drv + root) |
| 92 | parsed.reverse() |
| 93 | return drv, root, parsed |
| 94 | |
| 95 | def join_parsed_parts(self, drv, root, parts, drv2, root2, parts2): |
| 96 | """ |
| 97 | Join the two paths represented by the respective |
| 98 | (drive, root, parts) tuples. Return a new (drive, root, parts) tuple. |
| 99 | """ |
| 100 | if root2: |
Serhiy Storchaka | a993902 | 2013-12-06 17:14:12 +0200 | [diff] [blame] | 101 | if not drv2 and drv: |
| 102 | return drv, root2, [drv + root2] + parts2[1:] |
| 103 | elif drv2: |
| 104 | if drv2 == drv or self.casefold(drv2) == self.casefold(drv): |
| 105 | # Same drive => second path is relative to the first |
| 106 | return drv, root, parts + parts2[1:] |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 107 | else: |
Serhiy Storchaka | a993902 | 2013-12-06 17:14:12 +0200 | [diff] [blame] | 108 | # Second path is non-anchored (common case) |
| 109 | return drv, root, parts + parts2 |
| 110 | return drv2, root2, parts2 |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 111 | |
| 112 | |
| 113 | class _WindowsFlavour(_Flavour): |
| 114 | # Reference for Windows paths can be found at |
| 115 | # http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx |
| 116 | |
| 117 | sep = '\\' |
| 118 | altsep = '/' |
| 119 | has_drv = True |
| 120 | pathmod = ntpath |
| 121 | |
Antoine Pitrou | db118f5 | 2014-11-19 00:32:08 +0100 | [diff] [blame] | 122 | is_supported = (os.name == 'nt') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 123 | |
Jon Dufresne | 3972628 | 2017-05-18 07:35:54 -0700 | [diff] [blame] | 124 | drive_letters = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 125 | ext_namespace_prefix = '\\\\?\\' |
| 126 | |
| 127 | reserved_names = ( |
| 128 | {'CON', 'PRN', 'AUX', 'NUL'} | |
| 129 | {'COM%d' % i for i in range(1, 10)} | |
| 130 | {'LPT%d' % i for i in range(1, 10)} |
| 131 | ) |
| 132 | |
| 133 | # Interesting findings about extended paths: |
| 134 | # - '\\?\c:\a', '//?/c:\a' and '//?/c:/a' are all supported |
| 135 | # but '\\?\c:/a' is not |
| 136 | # - extended paths are always absolute; "relative" extended paths will |
| 137 | # fail. |
| 138 | |
| 139 | def splitroot(self, part, sep=sep): |
| 140 | first = part[0:1] |
| 141 | second = part[1:2] |
| 142 | if (second == sep and first == sep): |
| 143 | # XXX extended paths should also disable the collapsing of "." |
| 144 | # components (according to MSDN docs). |
| 145 | prefix, part = self._split_extended_path(part) |
| 146 | first = part[0:1] |
| 147 | second = part[1:2] |
| 148 | else: |
| 149 | prefix = '' |
| 150 | third = part[2:3] |
| 151 | if (second == sep and first == sep and third != sep): |
| 152 | # is a UNC path: |
| 153 | # vvvvvvvvvvvvvvvvvvvvv root |
| 154 | # \\machine\mountpoint\directory\etc\... |
| 155 | # directory ^^^^^^^^^^^^^^ |
| 156 | index = part.find(sep, 2) |
| 157 | if index != -1: |
| 158 | index2 = part.find(sep, index + 1) |
| 159 | # a UNC path can't have two slashes in a row |
| 160 | # (after the initial two) |
| 161 | if index2 != index + 1: |
| 162 | if index2 == -1: |
| 163 | index2 = len(part) |
| 164 | if prefix: |
| 165 | return prefix + part[1:index2], sep, part[index2+1:] |
| 166 | else: |
| 167 | return part[:index2], sep, part[index2+1:] |
| 168 | drv = root = '' |
| 169 | if second == ':' and first in self.drive_letters: |
| 170 | drv = part[:2] |
| 171 | part = part[2:] |
| 172 | first = third |
| 173 | if first == sep: |
| 174 | root = first |
| 175 | part = part.lstrip(sep) |
| 176 | return prefix + drv, root, part |
| 177 | |
| 178 | def casefold(self, s): |
| 179 | return s.lower() |
| 180 | |
| 181 | def casefold_parts(self, parts): |
| 182 | return [p.lower() for p in parts] |
| 183 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 184 | def compile_pattern(self, pattern): |
| 185 | return re.compile(fnmatch.translate(pattern), re.IGNORECASE).fullmatch |
| 186 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 187 | def resolve(self, path, strict=False): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 188 | s = str(path) |
| 189 | if not s: |
Barney Gale | b05440c | 2021-04-07 17:31:49 +0100 | [diff] [blame] | 190 | return path._accessor.getcwd() |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 191 | previous_s = None |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 192 | if _getfinalpathname is not None: |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 193 | if strict: |
| 194 | return self._ext_to_normal(_getfinalpathname(s)) |
| 195 | else: |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 196 | tail_parts = [] # End of the path after the first one not found |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 197 | while True: |
| 198 | try: |
| 199 | s = self._ext_to_normal(_getfinalpathname(s)) |
| 200 | except FileNotFoundError: |
| 201 | previous_s = s |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 202 | s, tail = os.path.split(s) |
| 203 | tail_parts.append(tail) |
Steve Dower | 4b1e98b | 2016-12-28 16:02:59 -0800 | [diff] [blame] | 204 | if previous_s == s: |
| 205 | return path |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 206 | else: |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 207 | return os.path.join(s, *reversed(tail_parts)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 208 | # Means fallback on absolute |
| 209 | return None |
| 210 | |
| 211 | def _split_extended_path(self, s, ext_prefix=ext_namespace_prefix): |
| 212 | prefix = '' |
| 213 | if s.startswith(ext_prefix): |
| 214 | prefix = s[:4] |
| 215 | s = s[4:] |
| 216 | if s.startswith('UNC\\'): |
| 217 | prefix += s[:3] |
| 218 | s = '\\' + s[3:] |
| 219 | return prefix, s |
| 220 | |
| 221 | def _ext_to_normal(self, s): |
| 222 | # Turn back an extended path into a normal DOS-like path |
| 223 | return self._split_extended_path(s)[1] |
| 224 | |
| 225 | def is_reserved(self, parts): |
| 226 | # NOTE: the rules for reserved names seem somewhat complicated |
| 227 | # (e.g. r"..\NUL" is reserved but not r"foo\NUL"). |
| 228 | # We err on the side of caution and return True for paths which are |
| 229 | # not considered reserved by Windows. |
| 230 | if not parts: |
| 231 | return False |
| 232 | if parts[0].startswith('\\\\'): |
| 233 | # UNC paths are never reserved |
| 234 | return False |
| 235 | return parts[-1].partition('.')[0].upper() in self.reserved_names |
| 236 | |
| 237 | def make_uri(self, path): |
| 238 | # Under Windows, file URIs use the UTF-8 encoding. |
| 239 | drive = path.drive |
| 240 | if len(drive) == 2 and drive[1] == ':': |
| 241 | # It's a path on a local drive => 'file:///c:/a/b' |
| 242 | rest = path.as_posix()[2:].lstrip('/') |
| 243 | return 'file:///%s/%s' % ( |
| 244 | drive, urlquote_from_bytes(rest.encode('utf-8'))) |
| 245 | else: |
| 246 | # It's a path on a network drive => 'file://host/share/a/b' |
| 247 | return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8')) |
| 248 | |
| 249 | |
| 250 | class _PosixFlavour(_Flavour): |
| 251 | sep = '/' |
| 252 | altsep = '' |
| 253 | has_drv = False |
| 254 | pathmod = posixpath |
| 255 | |
| 256 | is_supported = (os.name != 'nt') |
| 257 | |
| 258 | def splitroot(self, part, sep=sep): |
| 259 | if part and part[0] == sep: |
| 260 | stripped_part = part.lstrip(sep) |
| 261 | # According to POSIX path resolution: |
| 262 | # http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11 |
| 263 | # "A pathname that begins with two successive slashes may be |
| 264 | # interpreted in an implementation-defined manner, although more |
| 265 | # than two leading slashes shall be treated as a single slash". |
| 266 | if len(part) - len(stripped_part) == 2: |
| 267 | return '', sep * 2, stripped_part |
| 268 | else: |
| 269 | return '', sep, stripped_part |
| 270 | else: |
| 271 | return '', '', part |
| 272 | |
| 273 | def casefold(self, s): |
| 274 | return s |
| 275 | |
| 276 | def casefold_parts(self, parts): |
| 277 | return parts |
| 278 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 279 | def compile_pattern(self, pattern): |
| 280 | return re.compile(fnmatch.translate(pattern)).fullmatch |
| 281 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 282 | def resolve(self, path, strict=False): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 283 | sep = self.sep |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 284 | accessor = path._accessor |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 285 | seen = {} |
| 286 | def _resolve(path, rest): |
| 287 | if rest.startswith(sep): |
| 288 | path = '' |
| 289 | |
| 290 | for name in rest.split(sep): |
| 291 | if not name or name == '.': |
| 292 | # current dir |
| 293 | continue |
| 294 | if name == '..': |
| 295 | # parent dir |
| 296 | path, _, _ = path.rpartition(sep) |
| 297 | continue |
Dong-hee Na | 94ad6c6 | 2018-06-12 23:30:45 +0900 | [diff] [blame] | 298 | if path.endswith(sep): |
| 299 | newpath = path + name |
| 300 | else: |
| 301 | newpath = path + sep + name |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 302 | if newpath in seen: |
| 303 | # Already seen this path |
| 304 | path = seen[newpath] |
| 305 | if path is not None: |
| 306 | # use cached value |
| 307 | continue |
| 308 | # The symlink is not resolved, so we must have a symlink loop. |
| 309 | raise RuntimeError("Symlink loop from %r" % newpath) |
| 310 | # Resolve the symbolic link |
| 311 | try: |
| 312 | target = accessor.readlink(newpath) |
| 313 | except OSError as e: |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 314 | if e.errno != EINVAL and strict: |
| 315 | raise |
| 316 | # Not a symlink, or non-strict mode. We just leave the path |
| 317 | # untouched. |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 318 | path = newpath |
| 319 | else: |
| 320 | seen[newpath] = None # not resolved symlink |
| 321 | path = _resolve(path, target) |
| 322 | seen[newpath] = path # resolved symlink |
| 323 | |
| 324 | return path |
| 325 | # NOTE: according to POSIX, getcwd() cannot contain path components |
| 326 | # which are symlinks. |
Barney Gale | b05440c | 2021-04-07 17:31:49 +0100 | [diff] [blame] | 327 | base = '' if path.is_absolute() else accessor.getcwd() |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 328 | return _resolve(base, str(path)) or sep |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 329 | |
| 330 | def is_reserved(self, parts): |
| 331 | return False |
| 332 | |
| 333 | def make_uri(self, path): |
| 334 | # We represent the path using the local filesystem encoding, |
| 335 | # for portability to other applications. |
| 336 | bpath = bytes(path) |
| 337 | return 'file://' + urlquote_from_bytes(bpath) |
| 338 | |
| 339 | |
| 340 | _windows_flavour = _WindowsFlavour() |
| 341 | _posix_flavour = _PosixFlavour() |
| 342 | |
| 343 | |
| 344 | class _Accessor: |
| 345 | """An accessor implements a particular (system-specific or not) way of |
| 346 | accessing paths on the filesystem.""" |
| 347 | |
| 348 | |
| 349 | class _NormalAccessor(_Accessor): |
| 350 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 351 | stat = os.stat |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 352 | |
Barney Gale | 11c3bd3 | 2021-04-09 21:52:49 +0100 | [diff] [blame] | 353 | open = io.open |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 354 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 355 | listdir = os.listdir |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 356 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 357 | scandir = os.scandir |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 358 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 359 | chmod = os.chmod |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 360 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 361 | mkdir = os.mkdir |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 362 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 363 | unlink = os.unlink |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 364 | |
Toke Høiland-Jørgensen | 092435e | 2019-12-16 13:23:55 +0100 | [diff] [blame] | 365 | if hasattr(os, "link"): |
Barney Gale | b57e045 | 2021-04-07 00:01:22 +0100 | [diff] [blame] | 366 | link = os.link |
Toke Høiland-Jørgensen | 092435e | 2019-12-16 13:23:55 +0100 | [diff] [blame] | 367 | else: |
Barney Gale | b57e045 | 2021-04-07 00:01:22 +0100 | [diff] [blame] | 368 | def link(self, src, dst): |
Toke Høiland-Jørgensen | 092435e | 2019-12-16 13:23:55 +0100 | [diff] [blame] | 369 | raise NotImplementedError("os.link() not available on this system") |
Joannah Nanjekye | 6b5b013 | 2019-05-04 11:27:10 -0400 | [diff] [blame] | 370 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 371 | rmdir = os.rmdir |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 372 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 373 | rename = os.rename |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 374 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 375 | replace = os.replace |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 376 | |
Barney Gale | b57e045 | 2021-04-07 00:01:22 +0100 | [diff] [blame] | 377 | if hasattr(os, "symlink"): |
| 378 | symlink = os.symlink |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 379 | else: |
Barney Gale | b57e045 | 2021-04-07 00:01:22 +0100 | [diff] [blame] | 380 | def symlink(self, src, dst, target_is_directory=False): |
| 381 | raise NotImplementedError("os.symlink() not available on this system") |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 382 | |
Barney Gale | 986da8e | 2021-04-07 01:25:37 +0100 | [diff] [blame] | 383 | def touch(self, path, mode=0o666, exist_ok=True): |
| 384 | if exist_ok: |
| 385 | # First try to bump modification time |
| 386 | # Implementation note: GNU touch uses the UTIME_NOW option of |
| 387 | # the utimensat() / futimens() functions. |
| 388 | try: |
| 389 | os.utime(path, None) |
| 390 | except OSError: |
| 391 | # Avoid exception chaining |
| 392 | pass |
| 393 | else: |
| 394 | return |
| 395 | flags = os.O_CREAT | os.O_WRONLY |
| 396 | if not exist_ok: |
| 397 | flags |= os.O_EXCL |
| 398 | fd = os.open(path, flags, mode) |
| 399 | os.close(fd) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 400 | |
Barney Gale | b57e045 | 2021-04-07 00:01:22 +0100 | [diff] [blame] | 401 | if hasattr(os, "readlink"): |
| 402 | readlink = os.readlink |
| 403 | else: |
| 404 | def readlink(self, path): |
| 405 | raise NotImplementedError("os.readlink() not available on this system") |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 406 | |
Barney Gale | 22386bb | 2020-04-17 17:41:07 +0100 | [diff] [blame] | 407 | def owner(self, path): |
| 408 | try: |
| 409 | import pwd |
| 410 | return pwd.getpwuid(self.stat(path).st_uid).pw_name |
| 411 | except ImportError: |
| 412 | raise NotImplementedError("Path.owner() is unsupported on this system") |
| 413 | |
| 414 | def group(self, path): |
| 415 | try: |
| 416 | import grp |
| 417 | return grp.getgrgid(self.stat(path).st_gid).gr_name |
| 418 | except ImportError: |
| 419 | raise NotImplementedError("Path.group() is unsupported on this system") |
| 420 | |
Barney Gale | b05440c | 2021-04-07 17:31:49 +0100 | [diff] [blame] | 421 | getcwd = os.getcwd |
| 422 | |
Barney Gale | 3f3d82b | 2021-04-07 23:50:13 +0100 | [diff] [blame] | 423 | expanduser = staticmethod(os.path.expanduser) |
| 424 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 425 | |
| 426 | _normal_accessor = _NormalAccessor() |
| 427 | |
| 428 | |
| 429 | # |
| 430 | # Globbing helpers |
| 431 | # |
| 432 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 433 | def _make_selector(pattern_parts, flavour): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 434 | pat = pattern_parts[0] |
| 435 | child_parts = pattern_parts[1:] |
| 436 | if pat == '**': |
| 437 | cls = _RecursiveWildcardSelector |
| 438 | elif '**' in pat: |
| 439 | raise ValueError("Invalid pattern: '**' can only be an entire path component") |
| 440 | elif _is_wildcard_pattern(pat): |
| 441 | cls = _WildcardSelector |
| 442 | else: |
| 443 | cls = _PreciseSelector |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 444 | return cls(pat, child_parts, flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 445 | |
| 446 | if hasattr(functools, "lru_cache"): |
| 447 | _make_selector = functools.lru_cache()(_make_selector) |
| 448 | |
| 449 | |
| 450 | class _Selector: |
| 451 | """A selector matches a specific glob pattern part against the children |
| 452 | of a given path.""" |
| 453 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 454 | def __init__(self, child_parts, flavour): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 455 | self.child_parts = child_parts |
| 456 | if child_parts: |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 457 | self.successor = _make_selector(child_parts, flavour) |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 458 | self.dironly = True |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 459 | else: |
| 460 | self.successor = _TerminatingSelector() |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 461 | self.dironly = False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 462 | |
| 463 | def select_from(self, parent_path): |
| 464 | """Iterate over all child paths of `parent_path` matched by this |
| 465 | selector. This can contain parent_path itself.""" |
| 466 | path_cls = type(parent_path) |
| 467 | is_dir = path_cls.is_dir |
| 468 | exists = path_cls.exists |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 469 | scandir = parent_path._accessor.scandir |
| 470 | if not is_dir(parent_path): |
| 471 | return iter([]) |
| 472 | return self._select_from(parent_path, is_dir, exists, scandir) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 473 | |
| 474 | |
| 475 | class _TerminatingSelector: |
| 476 | |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 477 | def _select_from(self, parent_path, is_dir, exists, scandir): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 478 | yield parent_path |
| 479 | |
| 480 | |
| 481 | class _PreciseSelector(_Selector): |
| 482 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 483 | def __init__(self, name, child_parts, flavour): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 484 | self.name = name |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 485 | _Selector.__init__(self, child_parts, flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 486 | |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 487 | def _select_from(self, parent_path, is_dir, exists, scandir): |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 488 | try: |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 489 | path = parent_path._make_child_relpath(self.name) |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 490 | if (is_dir if self.dironly else exists)(path): |
| 491 | for p in self.successor._select_from(path, is_dir, exists, scandir): |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 492 | yield p |
| 493 | except PermissionError: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 494 | return |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 495 | |
| 496 | |
| 497 | class _WildcardSelector(_Selector): |
| 498 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 499 | def __init__(self, pat, child_parts, flavour): |
| 500 | self.match = flavour.compile_pattern(pat) |
| 501 | _Selector.__init__(self, child_parts, flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 502 | |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 503 | def _select_from(self, parent_path, is_dir, exists, scandir): |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 504 | try: |
Serhiy Storchaka | 704e206 | 2020-03-11 18:42:03 +0200 | [diff] [blame] | 505 | with scandir(parent_path) as scandir_it: |
| 506 | entries = list(scandir_it) |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 507 | for entry in entries: |
Pablo Galindo | eb7560a | 2020-03-07 17:53:20 +0000 | [diff] [blame] | 508 | if self.dironly: |
| 509 | try: |
| 510 | # "entry.is_dir()" can raise PermissionError |
| 511 | # in some cases (see bpo-38894), which is not |
| 512 | # among the errors ignored by _ignore_error() |
| 513 | if not entry.is_dir(): |
| 514 | continue |
| 515 | except OSError as e: |
| 516 | if not _ignore_error(e): |
| 517 | raise |
| 518 | continue |
| 519 | name = entry.name |
| 520 | if self.match(name): |
| 521 | path = parent_path._make_child_relpath(name) |
| 522 | for p in self.successor._select_from(path, is_dir, exists, scandir): |
| 523 | yield p |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 524 | except PermissionError: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 525 | return |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 526 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 527 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 528 | class _RecursiveWildcardSelector(_Selector): |
| 529 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 530 | def __init__(self, pat, child_parts, flavour): |
| 531 | _Selector.__init__(self, child_parts, flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 532 | |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 533 | def _iterate_directories(self, parent_path, is_dir, scandir): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 534 | yield parent_path |
Guido van Rossum | bc9fdda | 2016-01-07 10:56:36 -0800 | [diff] [blame] | 535 | try: |
Serhiy Storchaka | 704e206 | 2020-03-11 18:42:03 +0200 | [diff] [blame] | 536 | with scandir(parent_path) as scandir_it: |
| 537 | entries = list(scandir_it) |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 538 | for entry in entries: |
Przemysław Spodymek | 216b745 | 2018-08-27 23:33:45 +0200 | [diff] [blame] | 539 | entry_is_dir = False |
| 540 | try: |
| 541 | entry_is_dir = entry.is_dir() |
| 542 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 543 | if not _ignore_error(e): |
Przemysław Spodymek | 216b745 | 2018-08-27 23:33:45 +0200 | [diff] [blame] | 544 | raise |
| 545 | if entry_is_dir and not entry.is_symlink(): |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 546 | path = parent_path._make_child_relpath(entry.name) |
| 547 | for p in self._iterate_directories(path, is_dir, scandir): |
Guido van Rossum | bc9fdda | 2016-01-07 10:56:36 -0800 | [diff] [blame] | 548 | yield p |
| 549 | except PermissionError: |
| 550 | return |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 551 | |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 552 | def _select_from(self, parent_path, is_dir, exists, scandir): |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 553 | try: |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 554 | yielded = set() |
| 555 | try: |
| 556 | successor_select = self.successor._select_from |
| 557 | for starting_point in self._iterate_directories(parent_path, is_dir, scandir): |
| 558 | for p in successor_select(starting_point, is_dir, exists, scandir): |
| 559 | if p not in yielded: |
| 560 | yield p |
| 561 | yielded.add(p) |
| 562 | finally: |
| 563 | yielded.clear() |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 564 | except PermissionError: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 565 | return |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 566 | |
| 567 | |
| 568 | # |
| 569 | # Public API |
| 570 | # |
| 571 | |
| 572 | class _PathParents(Sequence): |
| 573 | """This object provides sequence-like access to the logical ancestors |
| 574 | of a path. Don't try to construct it yourself.""" |
| 575 | __slots__ = ('_pathcls', '_drv', '_root', '_parts') |
| 576 | |
| 577 | def __init__(self, path): |
| 578 | # We don't store the instance to avoid reference cycles |
| 579 | self._pathcls = type(path) |
| 580 | self._drv = path._drv |
| 581 | self._root = path._root |
| 582 | self._parts = path._parts |
| 583 | |
| 584 | def __len__(self): |
| 585 | if self._drv or self._root: |
| 586 | return len(self._parts) - 1 |
| 587 | else: |
| 588 | return len(self._parts) |
| 589 | |
| 590 | def __getitem__(self, idx): |
Joshua Cannon | 4520584 | 2020-11-20 09:40:39 -0600 | [diff] [blame] | 591 | if isinstance(idx, slice): |
| 592 | return tuple(self[i] for i in range(*idx.indices(len(self)))) |
Yaroslav Pankovych | 79d2e62 | 2020-11-23 22:06:22 +0200 | [diff] [blame] | 593 | |
| 594 | if idx >= len(self) or idx < -len(self): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 595 | raise IndexError(idx) |
| 596 | return self._pathcls._from_parsed_parts(self._drv, self._root, |
| 597 | self._parts[:-idx - 1]) |
| 598 | |
| 599 | def __repr__(self): |
| 600 | return "<{}.parents>".format(self._pathcls.__name__) |
| 601 | |
| 602 | |
| 603 | class PurePath(object): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 604 | """Base class for manipulating paths without I/O. |
| 605 | |
| 606 | PurePath represents a filesystem path and offers operations which |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 607 | don't imply any actual filesystem I/O. Depending on your system, |
| 608 | instantiating a PurePath will return either a PurePosixPath or a |
| 609 | PureWindowsPath object. You can also instantiate either of these classes |
| 610 | directly, regardless of your system. |
| 611 | """ |
| 612 | __slots__ = ( |
| 613 | '_drv', '_root', '_parts', |
| 614 | '_str', '_hash', '_pparts', '_cached_cparts', |
| 615 | ) |
| 616 | |
| 617 | def __new__(cls, *args): |
| 618 | """Construct a PurePath from one or several strings and or existing |
| 619 | PurePath objects. The strings and path objects are combined so as |
| 620 | to yield a canonicalized path, which is incorporated into the |
| 621 | new PurePath object. |
| 622 | """ |
| 623 | if cls is PurePath: |
| 624 | cls = PureWindowsPath if os.name == 'nt' else PurePosixPath |
| 625 | return cls._from_parts(args) |
| 626 | |
| 627 | def __reduce__(self): |
| 628 | # Using the parts tuple helps share interned path parts |
| 629 | # when pickling related paths. |
| 630 | return (self.__class__, tuple(self._parts)) |
| 631 | |
| 632 | @classmethod |
| 633 | def _parse_args(cls, args): |
| 634 | # This is useful when you don't want to create an instance, just |
| 635 | # canonicalize some constructor arguments. |
| 636 | parts = [] |
| 637 | for a in args: |
| 638 | if isinstance(a, PurePath): |
| 639 | parts += a._parts |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 640 | else: |
Brett Cannon | 568be63 | 2016-06-10 12:20:49 -0700 | [diff] [blame] | 641 | a = os.fspath(a) |
| 642 | if isinstance(a, str): |
| 643 | # Force-cast str subclasses to str (issue #21127) |
| 644 | parts.append(str(a)) |
| 645 | else: |
| 646 | raise TypeError( |
| 647 | "argument should be a str object or an os.PathLike " |
| 648 | "object returning str, not %r" |
| 649 | % type(a)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 650 | return cls._flavour.parse_parts(parts) |
| 651 | |
| 652 | @classmethod |
Barney Gale | 2219187 | 2021-04-07 01:26:37 +0100 | [diff] [blame] | 653 | def _from_parts(cls, args): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 654 | # We need to call _parse_args on the instance, so as to get the |
| 655 | # right flavour. |
| 656 | self = object.__new__(cls) |
| 657 | drv, root, parts = self._parse_args(args) |
| 658 | self._drv = drv |
| 659 | self._root = root |
| 660 | self._parts = parts |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 661 | return self |
| 662 | |
| 663 | @classmethod |
Barney Gale | 2219187 | 2021-04-07 01:26:37 +0100 | [diff] [blame] | 664 | def _from_parsed_parts(cls, drv, root, parts): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 665 | self = object.__new__(cls) |
| 666 | self._drv = drv |
| 667 | self._root = root |
| 668 | self._parts = parts |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 669 | return self |
| 670 | |
| 671 | @classmethod |
| 672 | def _format_parsed_parts(cls, drv, root, parts): |
| 673 | if drv or root: |
| 674 | return drv + root + cls._flavour.join(parts[1:]) |
| 675 | else: |
| 676 | return cls._flavour.join(parts) |
| 677 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 678 | def _make_child(self, args): |
| 679 | drv, root, parts = self._parse_args(args) |
| 680 | drv, root, parts = self._flavour.join_parsed_parts( |
| 681 | self._drv, self._root, self._parts, drv, root, parts) |
| 682 | return self._from_parsed_parts(drv, root, parts) |
| 683 | |
| 684 | def __str__(self): |
| 685 | """Return the string representation of the path, suitable for |
| 686 | passing to system calls.""" |
| 687 | try: |
| 688 | return self._str |
| 689 | except AttributeError: |
| 690 | self._str = self._format_parsed_parts(self._drv, self._root, |
| 691 | self._parts) or '.' |
| 692 | return self._str |
| 693 | |
Brett Cannon | 568be63 | 2016-06-10 12:20:49 -0700 | [diff] [blame] | 694 | def __fspath__(self): |
| 695 | return str(self) |
| 696 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 697 | def as_posix(self): |
| 698 | """Return the string representation of the path with forward (/) |
| 699 | slashes.""" |
| 700 | f = self._flavour |
| 701 | return str(self).replace(f.sep, '/') |
| 702 | |
| 703 | def __bytes__(self): |
| 704 | """Return the bytes representation of the path. This is only |
| 705 | recommended to use under Unix.""" |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 706 | return os.fsencode(self) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 707 | |
| 708 | def __repr__(self): |
| 709 | return "{}({!r})".format(self.__class__.__name__, self.as_posix()) |
| 710 | |
| 711 | def as_uri(self): |
| 712 | """Return the path as a 'file' URI.""" |
| 713 | if not self.is_absolute(): |
| 714 | raise ValueError("relative path can't be expressed as a file URI") |
| 715 | return self._flavour.make_uri(self) |
| 716 | |
| 717 | @property |
| 718 | def _cparts(self): |
| 719 | # Cached casefolded parts, for hashing and comparison |
| 720 | try: |
| 721 | return self._cached_cparts |
| 722 | except AttributeError: |
| 723 | self._cached_cparts = self._flavour.casefold_parts(self._parts) |
| 724 | return self._cached_cparts |
| 725 | |
| 726 | def __eq__(self, other): |
| 727 | if not isinstance(other, PurePath): |
| 728 | return NotImplemented |
| 729 | return self._cparts == other._cparts and self._flavour is other._flavour |
| 730 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 731 | def __hash__(self): |
| 732 | try: |
| 733 | return self._hash |
| 734 | except AttributeError: |
| 735 | self._hash = hash(tuple(self._cparts)) |
| 736 | return self._hash |
| 737 | |
| 738 | def __lt__(self, other): |
| 739 | if not isinstance(other, PurePath) or self._flavour is not other._flavour: |
| 740 | return NotImplemented |
| 741 | return self._cparts < other._cparts |
| 742 | |
| 743 | def __le__(self, other): |
| 744 | if not isinstance(other, PurePath) or self._flavour is not other._flavour: |
| 745 | return NotImplemented |
| 746 | return self._cparts <= other._cparts |
| 747 | |
| 748 | def __gt__(self, other): |
| 749 | if not isinstance(other, PurePath) or self._flavour is not other._flavour: |
| 750 | return NotImplemented |
| 751 | return self._cparts > other._cparts |
| 752 | |
| 753 | def __ge__(self, other): |
| 754 | if not isinstance(other, PurePath) or self._flavour is not other._flavour: |
| 755 | return NotImplemented |
| 756 | return self._cparts >= other._cparts |
| 757 | |
Batuhan Taşkaya | 526606b | 2019-12-08 23:31:15 +0300 | [diff] [blame] | 758 | def __class_getitem__(cls, type): |
| 759 | return cls |
| 760 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 761 | drive = property(attrgetter('_drv'), |
| 762 | doc="""The drive prefix (letter or UNC path), if any.""") |
| 763 | |
| 764 | root = property(attrgetter('_root'), |
| 765 | doc="""The root of the path, if any.""") |
| 766 | |
| 767 | @property |
| 768 | def anchor(self): |
| 769 | """The concatenation of the drive and root, or ''.""" |
| 770 | anchor = self._drv + self._root |
| 771 | return anchor |
| 772 | |
| 773 | @property |
| 774 | def name(self): |
| 775 | """The final path component, if any.""" |
| 776 | parts = self._parts |
| 777 | if len(parts) == (1 if (self._drv or self._root) else 0): |
| 778 | return '' |
| 779 | return parts[-1] |
| 780 | |
| 781 | @property |
| 782 | def suffix(self): |
Ram Rachum | 8d4fef4 | 2019-11-02 18:46:24 +0200 | [diff] [blame] | 783 | """ |
| 784 | The final component's last suffix, if any. |
| 785 | |
| 786 | This includes the leading period. For example: '.txt' |
| 787 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 788 | name = self.name |
| 789 | i = name.rfind('.') |
| 790 | if 0 < i < len(name) - 1: |
| 791 | return name[i:] |
| 792 | else: |
| 793 | return '' |
| 794 | |
| 795 | @property |
| 796 | def suffixes(self): |
Ram Rachum | 8d4fef4 | 2019-11-02 18:46:24 +0200 | [diff] [blame] | 797 | """ |
| 798 | A list of the final component's suffixes, if any. |
| 799 | |
| 800 | These include the leading periods. For example: ['.tar', '.gz'] |
| 801 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 802 | name = self.name |
| 803 | if name.endswith('.'): |
| 804 | return [] |
| 805 | name = name.lstrip('.') |
| 806 | return ['.' + suffix for suffix in name.split('.')[1:]] |
| 807 | |
| 808 | @property |
| 809 | def stem(self): |
| 810 | """The final path component, minus its last suffix.""" |
| 811 | name = self.name |
| 812 | i = name.rfind('.') |
| 813 | if 0 < i < len(name) - 1: |
| 814 | return name[:i] |
| 815 | else: |
| 816 | return name |
| 817 | |
| 818 | def with_name(self, name): |
| 819 | """Return a new path with the file name changed.""" |
| 820 | if not self.name: |
| 821 | raise ValueError("%r has an empty name" % (self,)) |
Antoine Pitrou | 7084e73 | 2014-07-06 21:31:12 -0400 | [diff] [blame] | 822 | drv, root, parts = self._flavour.parse_parts((name,)) |
| 823 | if (not name or name[-1] in [self._flavour.sep, self._flavour.altsep] |
| 824 | or drv or root or len(parts) != 1): |
| 825 | raise ValueError("Invalid name %r" % (name)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 826 | return self._from_parsed_parts(self._drv, self._root, |
| 827 | self._parts[:-1] + [name]) |
| 828 | |
Tim Hoffmann | 8aea4b3 | 2020-04-19 17:29:49 +0200 | [diff] [blame] | 829 | def with_stem(self, stem): |
| 830 | """Return a new path with the stem changed.""" |
| 831 | return self.with_name(stem + self.suffix) |
| 832 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 833 | def with_suffix(self, suffix): |
Stefan Otte | 46dc4e3 | 2018-08-03 22:49:42 +0200 | [diff] [blame] | 834 | """Return a new path with the file suffix changed. If the path |
| 835 | has no suffix, add given suffix. If the given suffix is an empty |
| 836 | string, remove the suffix from the path. |
| 837 | """ |
Antoine Pitrou | e50dafc | 2014-07-06 21:37:15 -0400 | [diff] [blame] | 838 | f = self._flavour |
| 839 | if f.sep in suffix or f.altsep and f.altsep in suffix: |
Berker Peksag | 423d05f | 2018-08-11 08:45:06 +0300 | [diff] [blame] | 840 | raise ValueError("Invalid suffix %r" % (suffix,)) |
Antoine Pitrou | e50dafc | 2014-07-06 21:37:15 -0400 | [diff] [blame] | 841 | if suffix and not suffix.startswith('.') or suffix == '.': |
Antoine Pitrou | 1b02da9 | 2014-01-03 00:07:17 +0100 | [diff] [blame] | 842 | raise ValueError("Invalid suffix %r" % (suffix)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 843 | name = self.name |
| 844 | if not name: |
| 845 | raise ValueError("%r has an empty name" % (self,)) |
| 846 | old_suffix = self.suffix |
| 847 | if not old_suffix: |
| 848 | name = name + suffix |
| 849 | else: |
| 850 | name = name[:-len(old_suffix)] + suffix |
| 851 | return self._from_parsed_parts(self._drv, self._root, |
| 852 | self._parts[:-1] + [name]) |
| 853 | |
| 854 | def relative_to(self, *other): |
| 855 | """Return the relative path to another path identified by the passed |
| 856 | arguments. If the operation is not possible (because this is not |
| 857 | a subpath of the other path), raise ValueError. |
| 858 | """ |
| 859 | # For the purpose of this method, drive and root are considered |
| 860 | # separate parts, i.e.: |
| 861 | # Path('c:/').relative_to('c:') gives Path('/') |
| 862 | # Path('c:/').relative_to('/') raise ValueError |
| 863 | if not other: |
| 864 | raise TypeError("need at least one argument") |
| 865 | parts = self._parts |
| 866 | drv = self._drv |
| 867 | root = self._root |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 868 | if root: |
| 869 | abs_parts = [drv, root] + parts[1:] |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 870 | else: |
| 871 | abs_parts = parts |
| 872 | to_drv, to_root, to_parts = self._parse_args(other) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 873 | if to_root: |
| 874 | to_abs_parts = [to_drv, to_root] + to_parts[1:] |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 875 | else: |
| 876 | to_abs_parts = to_parts |
| 877 | n = len(to_abs_parts) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 878 | cf = self._flavour.casefold_parts |
| 879 | if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 880 | formatted = self._format_parsed_parts(to_drv, to_root, to_parts) |
Rotuna | 4483253 | 2020-05-25 21:42:28 +0200 | [diff] [blame] | 881 | raise ValueError("{!r} is not in the subpath of {!r}" |
| 882 | " OR one path is relative and the other is absolute." |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 883 | .format(str(self), str(formatted))) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 884 | return self._from_parsed_parts('', root if n == 1 else '', |
| 885 | abs_parts[n:]) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 886 | |
Hai Shi | 82642a0 | 2019-08-13 14:54:02 -0500 | [diff] [blame] | 887 | def is_relative_to(self, *other): |
| 888 | """Return True if the path is relative to another path or False. |
| 889 | """ |
| 890 | try: |
| 891 | self.relative_to(*other) |
| 892 | return True |
| 893 | except ValueError: |
| 894 | return False |
| 895 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 896 | @property |
| 897 | def parts(self): |
| 898 | """An object providing sequence-like access to the |
| 899 | components in the filesystem path.""" |
| 900 | # We cache the tuple to avoid building a new one each time .parts |
| 901 | # is accessed. XXX is this necessary? |
| 902 | try: |
| 903 | return self._pparts |
| 904 | except AttributeError: |
| 905 | self._pparts = tuple(self._parts) |
| 906 | return self._pparts |
| 907 | |
| 908 | def joinpath(self, *args): |
| 909 | """Combine this path with one or several arguments, and return a |
| 910 | new path representing either a subpath (if all arguments are relative |
| 911 | paths) or a totally different path (if one of the arguments is |
| 912 | anchored). |
| 913 | """ |
| 914 | return self._make_child(args) |
| 915 | |
| 916 | def __truediv__(self, key): |
aiudirog | 4c69be2 | 2019-08-08 01:41:10 -0400 | [diff] [blame] | 917 | try: |
| 918 | return self._make_child((key,)) |
| 919 | except TypeError: |
| 920 | return NotImplemented |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 921 | |
| 922 | def __rtruediv__(self, key): |
aiudirog | 4c69be2 | 2019-08-08 01:41:10 -0400 | [diff] [blame] | 923 | try: |
| 924 | return self._from_parts([key] + self._parts) |
| 925 | except TypeError: |
| 926 | return NotImplemented |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 927 | |
| 928 | @property |
| 929 | def parent(self): |
| 930 | """The logical parent of the path.""" |
| 931 | drv = self._drv |
| 932 | root = self._root |
| 933 | parts = self._parts |
| 934 | if len(parts) == 1 and (drv or root): |
| 935 | return self |
| 936 | return self._from_parsed_parts(drv, root, parts[:-1]) |
| 937 | |
| 938 | @property |
| 939 | def parents(self): |
| 940 | """A sequence of this path's logical parents.""" |
| 941 | return _PathParents(self) |
| 942 | |
| 943 | def is_absolute(self): |
| 944 | """True if the path is absolute (has both a root and, if applicable, |
| 945 | a drive).""" |
| 946 | if not self._root: |
| 947 | return False |
| 948 | return not self._flavour.has_drv or bool(self._drv) |
| 949 | |
| 950 | def is_reserved(self): |
| 951 | """Return True if the path contains one of the special names reserved |
| 952 | by the system, if any.""" |
| 953 | return self._flavour.is_reserved(self._parts) |
| 954 | |
| 955 | def match(self, path_pattern): |
| 956 | """ |
| 957 | Return True if this path matches the given pattern. |
| 958 | """ |
| 959 | cf = self._flavour.casefold |
| 960 | path_pattern = cf(path_pattern) |
| 961 | drv, root, pat_parts = self._flavour.parse_parts((path_pattern,)) |
| 962 | if not pat_parts: |
| 963 | raise ValueError("empty pattern") |
| 964 | if drv and drv != cf(self._drv): |
| 965 | return False |
| 966 | if root and root != cf(self._root): |
| 967 | return False |
| 968 | parts = self._cparts |
| 969 | if drv or root: |
| 970 | if len(pat_parts) != len(parts): |
| 971 | return False |
| 972 | pat_parts = pat_parts[1:] |
| 973 | elif len(pat_parts) > len(parts): |
| 974 | return False |
| 975 | for part, pat in zip(reversed(parts), reversed(pat_parts)): |
| 976 | if not fnmatch.fnmatchcase(part, pat): |
| 977 | return False |
| 978 | return True |
| 979 | |
Brett Cannon | 568be63 | 2016-06-10 12:20:49 -0700 | [diff] [blame] | 980 | # Can't subclass os.PathLike from PurePath and keep the constructor |
| 981 | # optimizations in PurePath._parse_args(). |
| 982 | os.PathLike.register(PurePath) |
| 983 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 984 | |
| 985 | class PurePosixPath(PurePath): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 986 | """PurePath subclass for non-Windows systems. |
| 987 | |
| 988 | On a POSIX system, instantiating a PurePath should return this object. |
| 989 | However, you can also instantiate it directly on any system. |
| 990 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 991 | _flavour = _posix_flavour |
| 992 | __slots__ = () |
| 993 | |
| 994 | |
| 995 | class PureWindowsPath(PurePath): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 996 | """PurePath subclass for Windows systems. |
| 997 | |
| 998 | On a Windows system, instantiating a PurePath should return this object. |
| 999 | However, you can also instantiate it directly on any system. |
| 1000 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1001 | _flavour = _windows_flavour |
| 1002 | __slots__ = () |
| 1003 | |
| 1004 | |
| 1005 | # Filesystem-accessing classes |
| 1006 | |
| 1007 | |
| 1008 | class Path(PurePath): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 1009 | """PurePath subclass that can make system calls. |
| 1010 | |
| 1011 | Path represents a filesystem path but unlike PurePath, also offers |
| 1012 | methods to do system calls on path objects. Depending on your system, |
| 1013 | instantiating a Path will return either a PosixPath or a WindowsPath |
| 1014 | object. You can also instantiate a PosixPath or WindowsPath directly, |
| 1015 | but cannot instantiate a WindowsPath on a POSIX system or vice versa. |
| 1016 | """ |
Barney Gale | 2219187 | 2021-04-07 01:26:37 +0100 | [diff] [blame] | 1017 | _accessor = _normal_accessor |
| 1018 | __slots__ = () |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1019 | |
| 1020 | def __new__(cls, *args, **kwargs): |
| 1021 | if cls is Path: |
| 1022 | cls = WindowsPath if os.name == 'nt' else PosixPath |
Barney Gale | 2219187 | 2021-04-07 01:26:37 +0100 | [diff] [blame] | 1023 | self = cls._from_parts(args) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1024 | if not self._flavour.is_supported: |
| 1025 | raise NotImplementedError("cannot instantiate %r on your system" |
| 1026 | % (cls.__name__,)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1027 | return self |
| 1028 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1029 | def _make_child_relpath(self, part): |
| 1030 | # This is an optimization used for dir walking. `part` must be |
| 1031 | # a single part relative to this path. |
| 1032 | parts = self._parts + [part] |
| 1033 | return self._from_parsed_parts(self._drv, self._root, parts) |
| 1034 | |
| 1035 | def __enter__(self): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1036 | return self |
| 1037 | |
| 1038 | def __exit__(self, t, v, tb): |
Barney Gale | 00002e6 | 2020-04-01 15:10:51 +0100 | [diff] [blame] | 1039 | # https://bugs.python.org/issue39682 |
| 1040 | # In previous versions of pathlib, this method marked this path as |
| 1041 | # closed; subsequent attempts to perform I/O would raise an IOError. |
| 1042 | # This functionality was never documented, and had the effect of |
| 1043 | # making Path objects mutable, contrary to PEP 428. In Python 3.9 the |
| 1044 | # _closed attribute was removed, and this method made a no-op. |
| 1045 | # This method and __enter__()/__exit__() should be deprecated and |
| 1046 | # removed in the future. |
| 1047 | pass |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1048 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1049 | # Public API |
| 1050 | |
| 1051 | @classmethod |
| 1052 | def cwd(cls): |
| 1053 | """Return a new path pointing to the current working directory |
| 1054 | (as returned by os.getcwd()). |
| 1055 | """ |
Barney Gale | b05440c | 2021-04-07 17:31:49 +0100 | [diff] [blame] | 1056 | return cls(cls()._accessor.getcwd()) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1057 | |
Antoine Pitrou | 17cba7d | 2015-01-12 21:03:41 +0100 | [diff] [blame] | 1058 | @classmethod |
| 1059 | def home(cls): |
| 1060 | """Return a new path pointing to the user's home directory (as |
| 1061 | returned by os.path.expanduser('~')). |
| 1062 | """ |
Barney Gale | 3f3d82b | 2021-04-07 23:50:13 +0100 | [diff] [blame] | 1063 | return cls("~").expanduser() |
Antoine Pitrou | 17cba7d | 2015-01-12 21:03:41 +0100 | [diff] [blame] | 1064 | |
Antoine Pitrou | 43e3d94 | 2014-05-13 10:50:15 +0200 | [diff] [blame] | 1065 | def samefile(self, other_path): |
Berker Peksag | 05492b8 | 2015-10-22 03:34:16 +0300 | [diff] [blame] | 1066 | """Return whether other_path is the same or not as this file |
Berker Peksag | 267597f | 2015-10-21 20:10:24 +0300 | [diff] [blame] | 1067 | (as returned by os.path.samefile()). |
Antoine Pitrou | 43e3d94 | 2014-05-13 10:50:15 +0200 | [diff] [blame] | 1068 | """ |
| 1069 | st = self.stat() |
| 1070 | try: |
| 1071 | other_st = other_path.stat() |
| 1072 | except AttributeError: |
Barney Gale | 5b1d918 | 2020-04-17 18:47:27 +0100 | [diff] [blame] | 1073 | other_st = self._accessor.stat(other_path) |
Antoine Pitrou | 43e3d94 | 2014-05-13 10:50:15 +0200 | [diff] [blame] | 1074 | return os.path.samestat(st, other_st) |
| 1075 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1076 | def iterdir(self): |
| 1077 | """Iterate over the files in this directory. Does not yield any |
| 1078 | result for the special paths '.' and '..'. |
| 1079 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1080 | for name in self._accessor.listdir(self): |
| 1081 | if name in {'.', '..'}: |
| 1082 | # Yielding a path object for these makes little sense |
| 1083 | continue |
| 1084 | yield self._make_child_relpath(name) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1085 | |
| 1086 | def glob(self, pattern): |
| 1087 | """Iterate over this subtree and yield all existing files (of any |
Eivind Teig | 537b6ca | 2019-02-11 11:47:09 +0100 | [diff] [blame] | 1088 | kind, including directories) matching the given relative pattern. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1089 | """ |
Serhiy Storchaka | f4f445b | 2020-02-12 12:11:34 +0200 | [diff] [blame] | 1090 | sys.audit("pathlib.Path.glob", self, pattern) |
Berker Peksag | 4a208e4 | 2016-01-30 17:50:48 +0200 | [diff] [blame] | 1091 | if not pattern: |
| 1092 | raise ValueError("Unacceptable pattern: {!r}".format(pattern)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1093 | drv, root, pattern_parts = self._flavour.parse_parts((pattern,)) |
| 1094 | if drv or root: |
| 1095 | raise NotImplementedError("Non-relative patterns are unsupported") |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 1096 | selector = _make_selector(tuple(pattern_parts), self._flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1097 | for p in selector.select_from(self): |
| 1098 | yield p |
| 1099 | |
| 1100 | def rglob(self, pattern): |
| 1101 | """Recursively yield all existing files (of any kind, including |
Eivind Teig | 537b6ca | 2019-02-11 11:47:09 +0100 | [diff] [blame] | 1102 | directories) matching the given relative pattern, anywhere in |
| 1103 | this subtree. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1104 | """ |
Serhiy Storchaka | f4f445b | 2020-02-12 12:11:34 +0200 | [diff] [blame] | 1105 | sys.audit("pathlib.Path.rglob", self, pattern) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1106 | drv, root, pattern_parts = self._flavour.parse_parts((pattern,)) |
| 1107 | if drv or root: |
| 1108 | raise NotImplementedError("Non-relative patterns are unsupported") |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 1109 | selector = _make_selector(("**",) + tuple(pattern_parts), self._flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1110 | for p in selector.select_from(self): |
| 1111 | yield p |
| 1112 | |
| 1113 | def absolute(self): |
| 1114 | """Return an absolute version of this path. This function works |
| 1115 | even if the path doesn't point to anything. |
| 1116 | |
| 1117 | No normalization is done, i.e. all '.' and '..' will be kept along. |
| 1118 | Use resolve() to get the canonical path to a file. |
| 1119 | """ |
| 1120 | # XXX untested yet! |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1121 | if self.is_absolute(): |
| 1122 | return self |
| 1123 | # FIXME this must defer to the specific flavour (and, under Windows, |
| 1124 | # use nt._getfullpathname()) |
Barney Gale | b05440c | 2021-04-07 17:31:49 +0100 | [diff] [blame] | 1125 | return self._from_parts([self._accessor.getcwd()] + self._parts) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1126 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1127 | def resolve(self, strict=False): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1128 | """ |
| 1129 | Make the path absolute, resolving all symlinks on the way and also |
| 1130 | normalizing it (for example turning slashes into backslashes under |
| 1131 | Windows). |
| 1132 | """ |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1133 | s = self._flavour.resolve(self, strict=strict) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1134 | if s is None: |
| 1135 | # No symlink resolution => for consistency, raise an error if |
| 1136 | # the path doesn't exist or is forbidden |
| 1137 | self.stat() |
| 1138 | s = str(self.absolute()) |
| 1139 | # Now we have no symlinks in the path, it's safe to normalize it. |
| 1140 | normed = self._flavour.pathmod.normpath(s) |
Barney Gale | 2219187 | 2021-04-07 01:26:37 +0100 | [diff] [blame] | 1141 | return self._from_parts((normed,)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1142 | |
Barney Gale | abf9649 | 2021-04-07 16:53:39 +0100 | [diff] [blame] | 1143 | def stat(self, *, follow_symlinks=True): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1144 | """ |
| 1145 | Return the result of the stat() system call on this path, like |
| 1146 | os.stat() does. |
| 1147 | """ |
Barney Gale | abf9649 | 2021-04-07 16:53:39 +0100 | [diff] [blame] | 1148 | return self._accessor.stat(self, follow_symlinks=follow_symlinks) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1149 | |
| 1150 | def owner(self): |
| 1151 | """ |
| 1152 | Return the login name of the file owner. |
| 1153 | """ |
Barney Gale | 22386bb | 2020-04-17 17:41:07 +0100 | [diff] [blame] | 1154 | return self._accessor.owner(self) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1155 | |
| 1156 | def group(self): |
| 1157 | """ |
| 1158 | Return the group name of the file gid. |
| 1159 | """ |
Barney Gale | 22386bb | 2020-04-17 17:41:07 +0100 | [diff] [blame] | 1160 | return self._accessor.group(self) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1161 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1162 | def open(self, mode='r', buffering=-1, encoding=None, |
| 1163 | errors=None, newline=None): |
| 1164 | """ |
| 1165 | Open the file pointed by this path and return a file object, as |
| 1166 | the built-in open() function does. |
| 1167 | """ |
Inada Naoki | 4827483 | 2021-03-29 12:28:14 +0900 | [diff] [blame] | 1168 | if "b" not in mode: |
| 1169 | encoding = io.text_encoding(encoding) |
Barney Gale | 11c3bd3 | 2021-04-09 21:52:49 +0100 | [diff] [blame] | 1170 | return self._accessor.open(self, mode, buffering, encoding, errors, |
| 1171 | newline) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1172 | |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1173 | def read_bytes(self): |
| 1174 | """ |
| 1175 | Open the file in bytes mode, read it, and close the file. |
| 1176 | """ |
| 1177 | with self.open(mode='rb') as f: |
| 1178 | return f.read() |
| 1179 | |
| 1180 | def read_text(self, encoding=None, errors=None): |
| 1181 | """ |
| 1182 | Open the file in text mode, read it, and close the file. |
| 1183 | """ |
Inada Naoki | 4827483 | 2021-03-29 12:28:14 +0900 | [diff] [blame] | 1184 | encoding = io.text_encoding(encoding) |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1185 | with self.open(mode='r', encoding=encoding, errors=errors) as f: |
| 1186 | return f.read() |
| 1187 | |
| 1188 | def write_bytes(self, data): |
| 1189 | """ |
| 1190 | Open the file in bytes mode, write to it, and close the file. |
| 1191 | """ |
| 1192 | # type-check for the buffer interface before truncating the file |
| 1193 | view = memoryview(data) |
| 1194 | with self.open(mode='wb') as f: |
| 1195 | return f.write(view) |
| 1196 | |
Максим | 5f22741 | 2020-10-21 05:08:19 +0300 | [diff] [blame] | 1197 | def write_text(self, data, encoding=None, errors=None, newline=None): |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1198 | """ |
| 1199 | Open the file in text mode, write to it, and close the file. |
| 1200 | """ |
| 1201 | if not isinstance(data, str): |
| 1202 | raise TypeError('data must be str, not %s' % |
| 1203 | data.__class__.__name__) |
Inada Naoki | 4827483 | 2021-03-29 12:28:14 +0900 | [diff] [blame] | 1204 | encoding = io.text_encoding(encoding) |
Максим | 5f22741 | 2020-10-21 05:08:19 +0300 | [diff] [blame] | 1205 | with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f: |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1206 | return f.write(data) |
| 1207 | |
Girts | a01ba33 | 2019-10-23 14:18:40 -0700 | [diff] [blame] | 1208 | def readlink(self): |
| 1209 | """ |
| 1210 | Return the path to which the symbolic link points. |
| 1211 | """ |
| 1212 | path = self._accessor.readlink(self) |
Barney Gale | 2219187 | 2021-04-07 01:26:37 +0100 | [diff] [blame] | 1213 | return self._from_parts((path,)) |
Girts | a01ba33 | 2019-10-23 14:18:40 -0700 | [diff] [blame] | 1214 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1215 | def touch(self, mode=0o666, exist_ok=True): |
| 1216 | """ |
| 1217 | Create this file with the given access mode, if it doesn't exist. |
| 1218 | """ |
Barney Gale | 986da8e | 2021-04-07 01:25:37 +0100 | [diff] [blame] | 1219 | self._accessor.touch(self, mode, exist_ok) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1220 | |
Barry Warsaw | 7c549c4 | 2014-08-05 11:28:12 -0400 | [diff] [blame] | 1221 | def mkdir(self, mode=0o777, parents=False, exist_ok=False): |
Serhiy Storchaka | af7b9ec | 2017-03-24 20:51:53 +0200 | [diff] [blame] | 1222 | """ |
| 1223 | Create a new directory at this given path. |
| 1224 | """ |
Serhiy Storchaka | af7b9ec | 2017-03-24 20:51:53 +0200 | [diff] [blame] | 1225 | try: |
| 1226 | self._accessor.mkdir(self, mode) |
| 1227 | except FileNotFoundError: |
| 1228 | if not parents or self.parent == self: |
| 1229 | raise |
Armin Rigo | 22a594a | 2017-04-13 20:08:15 +0200 | [diff] [blame] | 1230 | self.parent.mkdir(parents=True, exist_ok=True) |
| 1231 | self.mkdir(mode, parents=False, exist_ok=exist_ok) |
Serhiy Storchaka | af7b9ec | 2017-03-24 20:51:53 +0200 | [diff] [blame] | 1232 | except OSError: |
| 1233 | # Cannot rely on checking for EEXIST, since the operating system |
| 1234 | # could give priority to other errors like EACCES or EROFS |
| 1235 | if not exist_ok or not self.is_dir(): |
| 1236 | raise |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1237 | |
Barney Gale | abf9649 | 2021-04-07 16:53:39 +0100 | [diff] [blame] | 1238 | def chmod(self, mode, *, follow_symlinks=True): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1239 | """ |
| 1240 | Change the permissions of the path, like os.chmod(). |
| 1241 | """ |
Barney Gale | abf9649 | 2021-04-07 16:53:39 +0100 | [diff] [blame] | 1242 | self._accessor.chmod(self, mode, follow_symlinks=follow_symlinks) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1243 | |
| 1244 | def lchmod(self, mode): |
| 1245 | """ |
| 1246 | Like chmod(), except if the path points to a symlink, the symlink's |
| 1247 | permissions are changed, rather than its target's. |
| 1248 | """ |
Barney Gale | abf9649 | 2021-04-07 16:53:39 +0100 | [diff] [blame] | 1249 | self.chmod(mode, follow_symlinks=False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1250 | |
zlohhcuB treboR | d9e006b | 2019-05-16 00:02:11 +0200 | [diff] [blame] | 1251 | def unlink(self, missing_ok=False): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1252 | """ |
| 1253 | Remove this file or link. |
| 1254 | If the path is a directory, use rmdir() instead. |
| 1255 | """ |
zlohhcuB treboR | d9e006b | 2019-05-16 00:02:11 +0200 | [diff] [blame] | 1256 | try: |
| 1257 | self._accessor.unlink(self) |
| 1258 | except FileNotFoundError: |
| 1259 | if not missing_ok: |
| 1260 | raise |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1261 | |
| 1262 | def rmdir(self): |
| 1263 | """ |
| 1264 | Remove this directory. The directory must be empty. |
| 1265 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1266 | self._accessor.rmdir(self) |
| 1267 | |
| 1268 | def lstat(self): |
| 1269 | """ |
| 1270 | Like stat(), except if the path points to a symlink, the symlink's |
| 1271 | status information is returned, rather than its target's. |
| 1272 | """ |
Barney Gale | abf9649 | 2021-04-07 16:53:39 +0100 | [diff] [blame] | 1273 | return self.stat(follow_symlinks=False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1274 | |
| 1275 | def rename(self, target): |
| 1276 | """ |
Ram Rachum | f97e42e | 2020-10-03 12:52:13 +0300 | [diff] [blame] | 1277 | Rename this path to the target path. |
| 1278 | |
| 1279 | The target path may be absolute or relative. Relative paths are |
| 1280 | interpreted relative to the current working directory, *not* the |
| 1281 | directory of the Path object. |
| 1282 | |
| 1283 | Returns the new Path instance pointing to the target path. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1284 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1285 | self._accessor.rename(self, target) |
hui shang | 088a09a | 2019-09-11 21:26:49 +0800 | [diff] [blame] | 1286 | return self.__class__(target) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1287 | |
| 1288 | def replace(self, target): |
| 1289 | """ |
Ram Rachum | f97e42e | 2020-10-03 12:52:13 +0300 | [diff] [blame] | 1290 | Rename this path to the target path, overwriting if that path exists. |
| 1291 | |
| 1292 | The target path may be absolute or relative. Relative paths are |
| 1293 | interpreted relative to the current working directory, *not* the |
| 1294 | directory of the Path object. |
| 1295 | |
| 1296 | Returns the new Path instance pointing to the target path. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1297 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1298 | self._accessor.replace(self, target) |
hui shang | 088a09a | 2019-09-11 21:26:49 +0800 | [diff] [blame] | 1299 | return self.__class__(target) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1300 | |
| 1301 | def symlink_to(self, target, target_is_directory=False): |
| 1302 | """ |
Barney Gale | 8aac1be | 2021-04-07 16:56:32 +0100 | [diff] [blame] | 1303 | Make this path a symlink pointing to the target path. |
| 1304 | Note the order of arguments (link, target) is the reverse of os.symlink. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1305 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1306 | self._accessor.symlink(target, self, target_is_directory) |
| 1307 | |
Barney Gale | 8aac1be | 2021-04-07 16:56:32 +0100 | [diff] [blame] | 1308 | def link_to(self, target): |
| 1309 | """ |
| 1310 | Make the target path a hard link pointing to this path. |
| 1311 | |
| 1312 | Note this function does not make this path a hard link to *target*, |
| 1313 | despite the implication of the function and argument names. The order |
| 1314 | of arguments (target, link) is the reverse of Path.symlink_to, but |
| 1315 | matches that of os.link. |
| 1316 | |
| 1317 | """ |
| 1318 | self._accessor.link(self, target) |
| 1319 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1320 | # Convenience functions for querying the stat results |
| 1321 | |
| 1322 | def exists(self): |
| 1323 | """ |
| 1324 | Whether this path exists. |
| 1325 | """ |
| 1326 | try: |
| 1327 | self.stat() |
| 1328 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1329 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1330 | raise |
| 1331 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1332 | except ValueError: |
| 1333 | # Non-encodable path |
| 1334 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1335 | return True |
| 1336 | |
| 1337 | def is_dir(self): |
| 1338 | """ |
| 1339 | Whether this path is a directory. |
| 1340 | """ |
| 1341 | try: |
| 1342 | return S_ISDIR(self.stat().st_mode) |
| 1343 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1344 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1345 | raise |
| 1346 | # Path doesn't exist or is a broken symlink |
| 1347 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1348 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1349 | except ValueError: |
| 1350 | # Non-encodable path |
| 1351 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1352 | |
| 1353 | def is_file(self): |
| 1354 | """ |
| 1355 | Whether this path is a regular file (also True for symlinks pointing |
| 1356 | to regular files). |
| 1357 | """ |
| 1358 | try: |
| 1359 | return S_ISREG(self.stat().st_mode) |
| 1360 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1361 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1362 | raise |
| 1363 | # Path doesn't exist or is a broken symlink |
| 1364 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1365 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1366 | except ValueError: |
| 1367 | # Non-encodable path |
| 1368 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1369 | |
Cooper Lees | 173ff4a | 2017-08-01 15:35:45 -0700 | [diff] [blame] | 1370 | def is_mount(self): |
| 1371 | """ |
| 1372 | Check if this path is a POSIX mount point |
| 1373 | """ |
| 1374 | # Need to exist and be a dir |
| 1375 | if not self.exists() or not self.is_dir(): |
| 1376 | return False |
| 1377 | |
Cooper Lees | 173ff4a | 2017-08-01 15:35:45 -0700 | [diff] [blame] | 1378 | try: |
Barney Gale | c746c4f | 2020-04-17 18:42:06 +0100 | [diff] [blame] | 1379 | parent_dev = self.parent.stat().st_dev |
Cooper Lees | 173ff4a | 2017-08-01 15:35:45 -0700 | [diff] [blame] | 1380 | except OSError: |
| 1381 | return False |
| 1382 | |
| 1383 | dev = self.stat().st_dev |
| 1384 | if dev != parent_dev: |
| 1385 | return True |
| 1386 | ino = self.stat().st_ino |
Barney Gale | c746c4f | 2020-04-17 18:42:06 +0100 | [diff] [blame] | 1387 | parent_ino = self.parent.stat().st_ino |
Cooper Lees | 173ff4a | 2017-08-01 15:35:45 -0700 | [diff] [blame] | 1388 | return ino == parent_ino |
| 1389 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1390 | def is_symlink(self): |
| 1391 | """ |
| 1392 | Whether this path is a symbolic link. |
| 1393 | """ |
| 1394 | try: |
| 1395 | return S_ISLNK(self.lstat().st_mode) |
| 1396 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1397 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1398 | raise |
| 1399 | # Path doesn't exist |
| 1400 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1401 | except ValueError: |
| 1402 | # Non-encodable path |
| 1403 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1404 | |
| 1405 | def is_block_device(self): |
| 1406 | """ |
| 1407 | Whether this path is a block device. |
| 1408 | """ |
| 1409 | try: |
| 1410 | return S_ISBLK(self.stat().st_mode) |
| 1411 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1412 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1413 | raise |
| 1414 | # Path doesn't exist or is a broken symlink |
| 1415 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1416 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1417 | except ValueError: |
| 1418 | # Non-encodable path |
| 1419 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1420 | |
| 1421 | def is_char_device(self): |
| 1422 | """ |
| 1423 | Whether this path is a character device. |
| 1424 | """ |
| 1425 | try: |
| 1426 | return S_ISCHR(self.stat().st_mode) |
| 1427 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1428 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1429 | raise |
| 1430 | # Path doesn't exist or is a broken symlink |
| 1431 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1432 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1433 | except ValueError: |
| 1434 | # Non-encodable path |
| 1435 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1436 | |
| 1437 | def is_fifo(self): |
| 1438 | """ |
| 1439 | Whether this path is a FIFO. |
| 1440 | """ |
| 1441 | try: |
| 1442 | return S_ISFIFO(self.stat().st_mode) |
| 1443 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1444 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1445 | raise |
| 1446 | # Path doesn't exist or is a broken symlink |
| 1447 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1448 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1449 | except ValueError: |
| 1450 | # Non-encodable path |
| 1451 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1452 | |
| 1453 | def is_socket(self): |
| 1454 | """ |
| 1455 | Whether this path is a socket. |
| 1456 | """ |
| 1457 | try: |
| 1458 | return S_ISSOCK(self.stat().st_mode) |
| 1459 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1460 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1461 | raise |
| 1462 | # Path doesn't exist or is a broken symlink |
| 1463 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1464 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1465 | except ValueError: |
| 1466 | # Non-encodable path |
| 1467 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1468 | |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 1469 | def expanduser(self): |
| 1470 | """ Return a new path with expanded ~ and ~user constructs |
| 1471 | (as returned by os.path.expanduser) |
| 1472 | """ |
| 1473 | if (not (self._drv or self._root) and |
| 1474 | self._parts and self._parts[0][:1] == '~'): |
Barney Gale | 3f3d82b | 2021-04-07 23:50:13 +0100 | [diff] [blame] | 1475 | homedir = self._accessor.expanduser(self._parts[0]) |
| 1476 | if homedir[:1] == "~": |
| 1477 | raise RuntimeError("Could not determine home directory.") |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 1478 | return self._from_parts([homedir] + self._parts[1:]) |
| 1479 | |
| 1480 | return self |
| 1481 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1482 | |
| 1483 | class PosixPath(Path, PurePosixPath): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 1484 | """Path subclass for non-Windows systems. |
| 1485 | |
| 1486 | On a POSIX system, instantiating a Path should return this object. |
| 1487 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1488 | __slots__ = () |
| 1489 | |
| 1490 | class WindowsPath(Path, PureWindowsPath): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 1491 | """Path subclass for Windows systems. |
| 1492 | |
| 1493 | On a Windows system, instantiating a Path should return this object. |
| 1494 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1495 | __slots__ = () |
Berker Peksag | 04d4229 | 2016-03-11 23:07:27 +0200 | [diff] [blame] | 1496 | |
Cooper Lees | 173ff4a | 2017-08-01 15:35:45 -0700 | [diff] [blame] | 1497 | def is_mount(self): |
| 1498 | raise NotImplementedError("Path.is_mount() is unsupported on this system") |