Implementation of pathlib.types.PathInfo that provides status information by querying a wrapped os.stat_result object. Don't try to construct it yourself.
| 615 | |
| 616 | |
| 617 | class _Info: |
| 618 | """Implementation of pathlib.types.PathInfo that provides status |
| 619 | information by querying a wrapped os.stat_result object. Don't try to |
| 620 | construct it yourself.""" |
| 621 | __slots__ = ('_path', '_entry', '_stat_result', '_lstat_result') |
| 622 | |
| 623 | def __init__(self, path, entry=None): |
| 624 | self._path = path |
| 625 | self._entry = entry |
| 626 | self._stat_result = None |
| 627 | self._lstat_result = None |
| 628 | |
| 629 | def __repr__(self): |
| 630 | path_type = "WindowsPath" if os.name == "nt" else "PosixPath" |
| 631 | return f"<{path_type}.info>" |
| 632 | |
| 633 | def _stat(self, *, follow_symlinks=True): |
| 634 | """Return the status as an os.stat_result.""" |
| 635 | if self._entry: |
| 636 | return self._entry.stat(follow_symlinks=follow_symlinks) |
| 637 | if follow_symlinks: |
| 638 | if not self._stat_result: |
| 639 | try: |
| 640 | self._stat_result = os.stat(self._path) |
| 641 | except (OSError, ValueError): |
| 642 | self._stat_result = _STAT_RESULT_ERROR |
| 643 | raise |
| 644 | return self._stat_result |
| 645 | else: |
| 646 | if not self._lstat_result: |
| 647 | try: |
| 648 | self._lstat_result = os.lstat(self._path) |
| 649 | except (OSError, ValueError): |
| 650 | self._lstat_result = _STAT_RESULT_ERROR |
| 651 | raise |
| 652 | return self._lstat_result |
| 653 | |
| 654 | def exists(self, *, follow_symlinks=True): |
| 655 | """Whether this path exists.""" |
| 656 | if self._entry: |
| 657 | if not follow_symlinks: |
| 658 | return True |
| 659 | if follow_symlinks: |
| 660 | if self._stat_result is _STAT_RESULT_ERROR: |
| 661 | return False |
| 662 | else: |
| 663 | if self._lstat_result is _STAT_RESULT_ERROR: |
| 664 | return False |
| 665 | try: |
| 666 | self._stat(follow_symlinks=follow_symlinks) |
| 667 | except (OSError, ValueError): |
| 668 | return False |
| 669 | return True |
| 670 | |
| 671 | def is_dir(self, *, follow_symlinks=True): |
| 672 | """Whether this path is a directory.""" |
| 673 | if self._entry: |
| 674 | try: |
no outgoing calls
no test coverage detected
searching dependent graphs…