|  | 
| 7 | 7 | from errno import * | 
| 8 | 8 | from glob import _StringGlobber, _no_recurse_symlinks | 
| 9 | 9 | from itertools import chain | 
| 10 |  | -from stat import S_IMODE, S_ISDIR, S_ISREG, S_ISLNK, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO | 
|  | 10 | +from stat import S_IMODE, S_ISDIR, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO | 
| 11 | 11 | from _collections_abc import Sequence | 
| 12 | 12 | 
 | 
| 13 | 13 | try: | 
|  | 
| 19 | 19 | except ImportError: | 
| 20 | 20 |     grp = None | 
| 21 | 21 | 
 | 
| 22 |  | -from pathlib._os import copyfile | 
|  | 22 | +from pathlib._os import copyfile, PathInfo, DirEntryInfo | 
| 23 | 23 | from pathlib._abc import CopyWriter, JoinablePath, WritablePath | 
| 24 | 24 | 
 | 
| 25 | 25 | 
 | 
| @@ -65,165 +65,6 @@ def __repr__(self): | 
| 65 | 65 |         return "<{}.parents>".format(type(self._path).__name__) | 
| 66 | 66 | 
 | 
| 67 | 67 | 
 | 
| 68 |  | -class _PathInfoBase: | 
| 69 |  | -    __slots__ = () | 
| 70 |  | - | 
| 71 |  | -    def __repr__(self): | 
| 72 |  | -        path_type = "WindowsPath" if os.name == "nt" else "PosixPath" | 
| 73 |  | -        return f"<{path_type}.info>" | 
| 74 |  | - | 
| 75 |  | - | 
| 76 |  | -class _DirEntryInfo(_PathInfoBase): | 
| 77 |  | -    """Implementation of pathlib.types.PathInfo that provides status | 
| 78 |  | -    information by querying a wrapped os.DirEntry object. Don't try to | 
| 79 |  | -    construct it yourself.""" | 
| 80 |  | -    __slots__ = ('_entry', '_exists') | 
| 81 |  | - | 
| 82 |  | -    def __init__(self, entry): | 
| 83 |  | -        self._entry = entry | 
| 84 |  | - | 
| 85 |  | -    def exists(self, *, follow_symlinks=True): | 
| 86 |  | -        """Whether this path exists.""" | 
| 87 |  | -        if not follow_symlinks: | 
| 88 |  | -            return True | 
| 89 |  | -        try: | 
| 90 |  | -            return self._exists | 
| 91 |  | -        except AttributeError: | 
| 92 |  | -            try: | 
| 93 |  | -                self._entry.stat() | 
| 94 |  | -            except OSError: | 
| 95 |  | -                self._exists = False | 
| 96 |  | -            else: | 
| 97 |  | -                self._exists = True | 
| 98 |  | -            return self._exists | 
| 99 |  | - | 
| 100 |  | -    def is_dir(self, *, follow_symlinks=True): | 
| 101 |  | -        """Whether this path is a directory.""" | 
| 102 |  | -        try: | 
| 103 |  | -            return self._entry.is_dir(follow_symlinks=follow_symlinks) | 
| 104 |  | -        except OSError: | 
| 105 |  | -            return False | 
| 106 |  | - | 
| 107 |  | -    def is_file(self, *, follow_symlinks=True): | 
| 108 |  | -        """Whether this path is a regular file.""" | 
| 109 |  | -        try: | 
| 110 |  | -            return self._entry.is_file(follow_symlinks=follow_symlinks) | 
| 111 |  | -        except OSError: | 
| 112 |  | -            return False | 
| 113 |  | - | 
| 114 |  | -    def is_symlink(self): | 
| 115 |  | -        """Whether this path is a symbolic link.""" | 
| 116 |  | -        try: | 
| 117 |  | -            return self._entry.is_symlink() | 
| 118 |  | -        except OSError: | 
| 119 |  | -            return False | 
| 120 |  | - | 
| 121 |  | - | 
| 122 |  | -class _WindowsPathInfo(_PathInfoBase): | 
| 123 |  | -    """Implementation of pathlib.types.PathInfo that provides status | 
| 124 |  | -    information for Windows paths. Don't try to construct it yourself.""" | 
| 125 |  | -    __slots__ = ('_path', '_exists', '_is_dir', '_is_file', '_is_symlink') | 
| 126 |  | - | 
| 127 |  | -    def __init__(self, path): | 
| 128 |  | -        self._path = str(path) | 
| 129 |  | - | 
| 130 |  | -    def exists(self, *, follow_symlinks=True): | 
| 131 |  | -        """Whether this path exists.""" | 
| 132 |  | -        if not follow_symlinks and self.is_symlink(): | 
| 133 |  | -            return True | 
| 134 |  | -        try: | 
| 135 |  | -            return self._exists | 
| 136 |  | -        except AttributeError: | 
| 137 |  | -            if os.path.exists(self._path): | 
| 138 |  | -                self._exists = True | 
| 139 |  | -                return True | 
| 140 |  | -            else: | 
| 141 |  | -                self._exists = self._is_dir = self._is_file = False | 
| 142 |  | -                return False | 
| 143 |  | - | 
| 144 |  | -    def is_dir(self, *, follow_symlinks=True): | 
| 145 |  | -        """Whether this path is a directory.""" | 
| 146 |  | -        if not follow_symlinks and self.is_symlink(): | 
| 147 |  | -            return False | 
| 148 |  | -        try: | 
| 149 |  | -            return self._is_dir | 
| 150 |  | -        except AttributeError: | 
| 151 |  | -            if os.path.isdir(self._path): | 
| 152 |  | -                self._is_dir = self._exists = True | 
| 153 |  | -                return True | 
| 154 |  | -            else: | 
| 155 |  | -                self._is_dir = False | 
| 156 |  | -                return False | 
| 157 |  | - | 
| 158 |  | -    def is_file(self, *, follow_symlinks=True): | 
| 159 |  | -        """Whether this path is a regular file.""" | 
| 160 |  | -        if not follow_symlinks and self.is_symlink(): | 
| 161 |  | -            return False | 
| 162 |  | -        try: | 
| 163 |  | -            return self._is_file | 
| 164 |  | -        except AttributeError: | 
| 165 |  | -            if os.path.isfile(self._path): | 
| 166 |  | -                self._is_file = self._exists = True | 
| 167 |  | -                return True | 
| 168 |  | -            else: | 
| 169 |  | -                self._is_file = False | 
| 170 |  | -                return False | 
| 171 |  | - | 
| 172 |  | -    def is_symlink(self): | 
| 173 |  | -        """Whether this path is a symbolic link.""" | 
| 174 |  | -        try: | 
| 175 |  | -            return self._is_symlink | 
| 176 |  | -        except AttributeError: | 
| 177 |  | -            self._is_symlink = os.path.islink(self._path) | 
| 178 |  | -            return self._is_symlink | 
| 179 |  | - | 
| 180 |  | - | 
| 181 |  | -class _PosixPathInfo(_PathInfoBase): | 
| 182 |  | -    """Implementation of pathlib.types.PathInfo that provides status | 
| 183 |  | -    information for POSIX paths. Don't try to construct it yourself.""" | 
| 184 |  | -    __slots__ = ('_path', '_mode') | 
| 185 |  | - | 
| 186 |  | -    def __init__(self, path): | 
| 187 |  | -        self._path = str(path) | 
| 188 |  | -        self._mode = [None, None] | 
| 189 |  | - | 
| 190 |  | -    def _get_mode(self, *, follow_symlinks=True): | 
| 191 |  | -        idx = bool(follow_symlinks) | 
| 192 |  | -        mode = self._mode[idx] | 
| 193 |  | -        if mode is None: | 
| 194 |  | -            try: | 
| 195 |  | -                st = os.stat(self._path, follow_symlinks=follow_symlinks) | 
| 196 |  | -            except (OSError, ValueError): | 
| 197 |  | -                mode = 0 | 
| 198 |  | -            else: | 
| 199 |  | -                mode = st.st_mode | 
| 200 |  | -            if follow_symlinks or S_ISLNK(mode): | 
| 201 |  | -                self._mode[idx] = mode | 
| 202 |  | -            else: | 
| 203 |  | -                # Not a symlink, so stat() will give the same result | 
| 204 |  | -                self._mode = [mode, mode] | 
| 205 |  | -        return mode | 
| 206 |  | - | 
| 207 |  | -    def exists(self, *, follow_symlinks=True): | 
| 208 |  | -        """Whether this path exists.""" | 
| 209 |  | -        return self._get_mode(follow_symlinks=follow_symlinks) > 0 | 
| 210 |  | - | 
| 211 |  | -    def is_dir(self, *, follow_symlinks=True): | 
| 212 |  | -        """Whether this path is a directory.""" | 
| 213 |  | -        return S_ISDIR(self._get_mode(follow_symlinks=follow_symlinks)) | 
| 214 |  | - | 
| 215 |  | -    def is_file(self, *, follow_symlinks=True): | 
| 216 |  | -        """Whether this path is a regular file.""" | 
| 217 |  | -        return S_ISREG(self._get_mode(follow_symlinks=follow_symlinks)) | 
| 218 |  | - | 
| 219 |  | -    def is_symlink(self): | 
| 220 |  | -        """Whether this path is a symbolic link.""" | 
| 221 |  | -        return S_ISLNK(self._get_mode(follow_symlinks=False)) | 
| 222 |  | - | 
| 223 |  | - | 
| 224 |  | -_PathInfo = _WindowsPathInfo if os.name == 'nt' else _PosixPathInfo | 
| 225 |  | - | 
| 226 |  | - | 
| 227 | 68 | class _LocalCopyWriter(CopyWriter): | 
| 228 | 69 |     """This object implements the Path.copy callable.  Don't try to construct | 
| 229 | 70 |     it yourself.""" | 
| @@ -867,7 +708,7 @@ def info(self): | 
| 867 | 708 |         try: | 
| 868 | 709 |             return self._info | 
| 869 | 710 |         except AttributeError: | 
| 870 |  | -            self._info = _PathInfo(self) | 
|  | 711 | +            self._info = PathInfo(self) | 
| 871 | 712 |             return self._info | 
| 872 | 713 | 
 | 
| 873 | 714 |     def stat(self, *, follow_symlinks=True): | 
| @@ -1026,7 +867,7 @@ def _filter_trailing_slash(self, paths): | 
| 1026 | 867 |     def _from_dir_entry(self, dir_entry, path_str): | 
| 1027 | 868 |         path = self.with_segments(path_str) | 
| 1028 | 869 |         path._str = path_str | 
| 1029 |  | -        path._info = _DirEntryInfo(dir_entry) | 
|  | 870 | +        path._info = DirEntryInfo(dir_entry) | 
| 1030 | 871 |         return path | 
| 1031 | 872 | 
 | 
| 1032 | 873 |     def iterdir(self): | 
|  | 
0 commit comments