aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_pathlib.py
Commit message (Collapse)AuthorAge
* GH-110109: Move tests for pathlib ABCs to new module. (#112904)Barney Gale2023-12-16
|
* GH-110109: Move pathlib ABCs to new `pathlib._abc` module. (#112881)Barney Gale2023-12-09
| | | | | | | | | | | Move `_PurePathBase` and `_PathBase` to a new `pathlib._abc` module, and drop the underscores from the class names. Tests are mostly left alone in this commit, but they'll be similarly split in a subsequent commit. The `pathlib._abc` module will be published as an independent PyPI package (similar to how `zipfile._path` is published as `zipp`), to be refined and stabilised prior to its possible addition to the standard library.
* GH-110109: Add `pathlib._PurePathBase` (#110670)Barney Gale2023-12-08
| | | | | Add private `pathlib._PurePathBase` class: a private superclass of both `PurePath` and `_PathBase`. Unlike `PurePath`, it does not define any of these special methods: `__fspath__`, `__bytes__`, `__reduce__`, `__hash__`, `__eq__`, `__lt__`, `__le__`, `__gt__`, `__ge__`. Its initializer and path joining methods accept only strings, not os.PathLike objects more broadly. This is important for supporting *virtual paths*: user subclasses of `_PathBase` that provide access to archive files, FTP servers, etc. In these classes, the above methods should be implemented by users only as appropriate, with due consideration for the hash/equality of any backing objects, such as file objects or sockets.
* GH-112675: Move path joining tests into `test_posixpath` and `test_ntpath` ↵Barney Gale2023-12-07
| | | | | | | | | | | (#112676) In `test_pathlib`, the `check_drive_root_parts` test methods evaluated both joining and parsing/normalisation of paths. This dates from a time when pathlib implemented both functions itself, but nowadays path joining is done with `posixpath.join()` and `ntpath.join()`. This commit moves the joining-related test cases into `test_posixpath` and `test_ntpath`.
* gh-103363: Add follow_symlinks argument to `pathlib.Path.owner()` and ↵Kamil Turek2023-12-04
| | | | `group()` (#107962)
* GH-106747: Improve `Path.glob()` expectations in pathlib tests (#112365)Barney Gale2023-12-03
| | | | | | | Add trailing slashes to expected `Path.glob()` results wherever a pattern has a trailing slash. This matches what `glob.glob()` produces. Due to another bug (GH-65238) pathlib strips all trailing slashes, so this change is academic for now.
* GH-112361: Speed up pathlib by removing some temporary objects. (#112362)Barney Gale2023-11-25
| | | | | Construct only one new list object (using `list.copy()`) when creating a new path object with a modified tail. This slightly speeds up `with_name()` and `with_suffix()`
* gh-110745: add a newline argument to pathlib.Path.read_text (#110880)Junya Okabe2023-11-21
| | | | Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Barney Gale <barney.gale@gmail.com>
* GH-110109: Test pure functionality of `pathlib.Path` user subclasses (#112242)Barney Gale2023-11-18
| | | | | | | | | | | Add `PurePathTest` as a superclass of `PathTest`, and therefore also `PathSubclassTest`. This adds coverage of pure functionality in user subclasses of `pathlib.Path`. Remove `PosixPathAsPureTest` and `WindowsPathAsPureTest`, as they now duplicate `PosixPathTest` and `WindowsPathTest`. This makes the MROs of test unit classes match the MROs of pathlib classes.
* GH-110109: Fix misplaced tests for `pathlib.WindowsPath.owner()` and ↵Barney Gale2023-11-18
| | | | | | `group()` (#112239) Move test methods from `WindowsPathAsPureTest` to `WindowsPathTest` unit. The former test unit is intended to exercise only pure path functionality.
* GH-110109: Churn `pathlib.PurePath` methods (#112012)Barney Gale2023-11-17
| | | | | | | | | | | | | | Re-arrange `pathlib.PurePath` methods in source code. No other changes. The `PurePath` implementations of certain special methods, such as `__eq__()` and `__hash__()`, are not usually applicable to user subclasses of `_PathBase`. To facilitate their removal, another patch will split the `PurePath` class into `_PurePathBase` and `PurePath`, with the latter providing these special methods. This patch prepares the ground for splitting `PurePath`. It's similar to e8d77b0, which preceded splitting `Path`. By churning the methods here, subsequent patches will be easier to review and less likely to break things.
* GH-110109: Move tests for `pathlib.Path.walk()` into main test classes. ↵Barney Gale2023-10-28
| | | | (#110655)
* GH-110488: Fix two small issues in `pathlib.PurePath.with_name()` (#110651)Barney Gale2023-10-11
| | | | | | Ensure that `PurePath('foo/a').with_name('.')` raises `ValueError` Ensure that `PureWindowsPath('foo/a').with_name('a:b')` does not raise `ValueError`.
* GH-110109: Simplify pathlib test setup (#110308)Barney Gale2023-10-11
| | | Use the class under test to create files, directories and symlinks.
* GH-107465: Add `pathlib.Path.from_uri()` classmethod. (#107640)Barney Gale2023-10-01
| | | | | | | This method supports file URIs (including variants) as described in RFC 8089, such as URIs generated by `pathlib.Path.as_uri()` and `urllib.request.pathname2url()`. The method is added to `Path` rather than `PurePath` because it uses `os.fsdecode()`, and so its results vary from system to system. I intend to deprecate `PurePath.as_uri()` and move it to `Path` for the same reason. Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
* GH-89812: Add `pathlib._PathBase` (#106337)Barney Gale2023-09-30
| | | | | Add private `pathlib._PathBase` class. This will be used by an experimental PyPI package to incubate a `tarfile.TarPath` class. Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
* GH-109187: Improve symlink loop handling in `pathlib.Path.resolve()` (GH-109192)Barney Gale2023-09-26
| | | | Treat symlink loops like other errors: in strict mode, raise `OSError`, and in non-strict mode, do not raise any exception.
* GH-78722: Raise exceptions from `pathlib.Path.iterdir()` without delay. ↵Barney Gale2023-09-02
| | | | | | | (#107320) `pathlib.Path.iterdir()` now immediately raises any `OSError` exception from `os.listdir()`, rather than waiting until its result is iterated over.
* GH-70303: Emit FutureWarning when pathlib glob pattern ends with `**` ↵Barney Gale2023-08-04
| | | | | | (GH-105413) In a future Python release, patterns with this ending will match both files and directories. Users may add a trailing slash to remove the warning.
* gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." (#107014)János Kukovecz2023-07-26
| | | | It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
* GH-100502: Add `pathlib.PurePath.pathmod` attribute (GH-106533)Barney Gale2023-07-19
| | | | This instance attribute stores the implementation of `os.path` used for low-level path operations: either `posixpath` or `ntpath`.
* GH-106330: Fix matching of empty path in `pathlib.PurePath.match()` (GH-106331)Barney Gale2023-07-03
| | | | | We match paths using the `_lines` attribute, which is derived from the path's string representation. The bug arises because an empty path's string representation is `'.'` (not `''`), which is matched by the `'*'` wildcard.
* GH-89812: Improve test for `pathlib.Path.stat()` (GH-106064)Barney Gale2023-07-01
| | | | | Make assertions about the `st_mode`, `st_ino` and `st_dev` attributes of the stat results from two files and a directory, rather than checking if `chmod()` affects `st_mode` (which is already tested elsewhere).
* GH-89812: Miscellaneous pathlib test improvements (GH-106063)Barney Gale2023-07-01
| | | | | | | - Split out dedicated test for unbuffered `open()` - Split out dedicated test for `is_mount()` at the filesystem root - Avoid `os.stat()` when checking that empty paths point to '.' - Remove unnecessary `rmtree()` call - Remove unused `assertSame()` method
* GH-89812: Test that `pathlib.Path.is_junction()` returns false (GH-106062)Barney Gale2023-07-01
| | | | Slightly expand the test coverage of `is_junction()`. The existing test only checks that `os.path.isjunction()` is called under-the-hood.
* GH-89812: Make symlink support configurable in pathlib tests. (GH-106060)Barney Gale2023-07-01
| | | | | | | Adjust the pathlib tests to add a new `PathTest.can_symlink` class attribute, which allows us to enable or disable symlink support in tests. A (near-)future commit will add an `AbstractPath` class; its tests will hard-code the value to `True` or `False` depending on a stub subclass's capabilities.
* GH-89812: Simplify creation of symlinks in pathlib tests. (GH-106061)Barney Gale2023-07-01
| | | | | | Remove `PathTest.dirlink()` function. Symlinks in `PathTest.setUp()` are created using `os.symlink()` directly; symlinks in test functions use `Path.symlink_to()` in order to make the tests applicable to a (near-)future `AbstractPath` class.
* GH-105793: Add follow_symlinks argument to `pathlib.Path.is_dir()` and ↵Barney Gale2023-06-26
| | | | | | | `is_file()` (GH-105794) Brings `pathlib.Path.is_dir()` and `in line with `os.DirEntry.is_dir()`, which will be important for implementing generic path walking and globbing. Likewise `is_file()`.
* GH-89812: Add `pathlib.UnsupportedOperation` (GH-105926)Barney Gale2023-06-22
| | | | | | | This new exception type is raised instead of `NotImplementedError` when a path operation is not supported. It can be raised from `Path.readlink()`, `symlink_to()`, `hardlink_to()`, `owner()` and `group()`. In a future version of pathlib, it will be raised by `AbstractPath` for these methods and others, such as `AbstractPath.mkdir()` and `unlink()`.
* GH-89812: Churn `pathlib.Path` test methods (#105807)Barney Gale2023-06-15
| | | | | | | | | | | | | | Re-arrange `pathlib.Path` test methods in source code. No other changes. The test methods are arranged in two groups. The first group checks `stat()`, `open()`, `iterdir()`, `readlink()`, and derived methods like `exists()`, `read_text()`, `glob()` and `resolve()`. The second group checks all other `Path` methods. To minimise the diff I've maintained the method order within groups where possible. This patch prepares the ground for a new `_AbstractPath` class, which will support methods in the first group above. By churning the test methods here, subsequent patches will be easier to review and less likely to break things.
* GH-89812: Clean up pathlib tests. (#104829)Barney Gale2023-06-14
| | | | | | Clean up pathlib tests. Merge `PurePathTest` into `_BasePurePathTest`, and `PathTest` into `_BasePathTest`.
* gh-102613: Bump recursion limit to fix running test_pathlib under Coverage ↵Łukasz Langa2023-06-13
| | | | (#105744)
* GH-102613: Fast recursive globbing in `pathlib.Path.glob()` (GH-104512)Barney Gale2023-06-06
| | | | | | | | | | | | | | This commit introduces a 'walk-and-match' strategy for handling glob patterns that include a non-terminal `**` wildcard, such as `**/*.py`. For this example, the previous implementation recursively walked directories using `os.scandir()` when it expanded the `**` component, and then **scanned those same directories again** when expanded the `*.py` component. This is wasteful. In the new implementation, any components following a `**` wildcard are used to build a `re.Pattern` object, which is used to filter the results of the recursive walk. A pattern like `**/*.py` uses half the number of `os.scandir()` calls; a pattern like `**/*/*.py` a third, etc. This new algorithm does not apply if either: 1. The *follow_symlinks* argument is set to `None` (its default), or 2. The pattern contains `..` components. In these cases we fall back to the old implementation. This commit also replaces selector classes with selector functions. These generators directly yield results rather calling through to their successors. A new internal `Path._glob()` method takes care to chain these generators together, which simplifies the lazy algorithm and slightly improves performance. It should also be easier to understand and maintain.
* gh-105407: Remove unused imports in tests (#105408)Victor Stinner2023-06-06
|
* GH-73435: Implement recursive wildcards in `pathlib.PurePath.match()` (#101398)Barney Gale2023-05-30
| | | | | | | | `PurePath.match()` now handles the `**` wildcard as in `Path.glob()`, i.e. it matches any number of path segments. We now compile a `re.Pattern` object for the entire pattern. This is made more difficult by `fnmatch` not treating directory separators as special when evaluating wildcards (`*`, `?`, etc), and so we arrange the path parts onto separate *lines* in a string, and ensure we don't set `re.DOTALL`. Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
* GH-77609: Add follow_symlinks argument to `pathlib.Path.glob()` (GH-102616)Barney Gale2023-05-29
| | | | | Add a keyword-only *follow_symlinks* parameter to `pathlib.Path.glob()` and`rglob()`. When *follow_symlinks* is `None` (the default), these methods follow symlinks except when evaluating "`**`" wildcards. When set to true or false, symlinks are always or never followed, respectively.
* GH-103631: Fix `PurePosixPath(PureWindowsPath(...))` separator handling ↵Barney Gale2023-05-26
| | | | | | | | | (GH-104949) For backwards compatibility, accept backslashes as path separators in `PurePosixPath` if an instance of `PureWindowsPath` is supplied. This restores behaviour from Python 3.11. Co-authored-by: Gregory P. Smith <greg@krypto.org>
* GH-104947: Make pathlib.PureWindowsPath comparisons consistent across ↵Barney Gale2023-05-26
| | | | | | | | platforms (GH-104948) Use `str.lower()` rather than `ntpath.normcase()` to normalize case of Windows paths. This restores behaviour from Python 3.11. Co-authored-by: Gregory P. Smith <greg@krypto.org>
* GH-83863: Drop support for using `pathlib.Path` objects as context managers ↵Barney Gale2023-05-23
| | | | | | | | | | (GH-104807) In Python 3.8 and prior, `pathlib.Path.__exit__()` marked a path as closed; some subsequent attempts to perform I/O would raise an IOError. This functionality was never documented, and had the effect of making `Path` objects mutable, contrary to PEP 428. In Python 3.9 we made `__exit__()` a no-op, and in 3.11 `__enter__()` began raising deprecation warnings. Here we remove both methods.
* GH-104484: Add case_sensitive argument to `pathlib.PurePath.match()` (GH-104565)thirumurugan2023-05-18
| | | Co-authored-by: Barney Gale <barney.gale@gmail.com>
* GH-102613: Fix recursion error from `pathlib.Path.glob()` (GH-104373)Barney Gale2023-05-15
| | | | Use `Path.walk()` to implement the recursive wildcard `**`. This method uses an iterative (rather than recursive) walk - see GH-100282.
* GH-90208: Suppress OSError exceptions from `pathlib.Path.glob()` (GH-104141)Barney Gale2023-05-11
| | | | | | | | | | | `pathlib.Path.glob()` now suppresses all OSError exceptions, except those raised from calling `is_dir()` on the top-level path. Previously, `glob()` suppressed ENOENT, ENOTDIR, EBADF and ELOOP errors and their Windows equivalents. PermissionError was also suppressed unless it occurred when calling `is_dir()` on the top-level path. However, the selector would abort prematurely if a PermissionError was raised, and so `glob()` could return incomplete results.
* GH-87695: Fix OSError from `pathlib.Path.glob()` (GH-104292)Barney Gale2023-05-10
| | | | Fix issue where `pathlib.Path.glob()` raised `OSError` when it encountered a symlink to an overly long path.
* GH-102613: Improve performance of `pathlib.Path.rglob()` (GH-104244)Barney Gale2023-05-07
| | | | | | | | | | Stop de-duplicating results in `_RecursiveWildcardSelector`. A new `_DoubleRecursiveWildcardSelector` class is introduced which performs de-duplication, but this is used _only_ for patterns with multiple non-adjacent `**` segments, such as `path.glob('**/foo/**')`. By avoiding the use of a set, `PurePath.__hash__()` is not called, and so paths do not need to be stringified and case-normalised. Also merge adjacent '**' segments in patterns.
* GH-100479: Fix pathlib test failure on WASI (#104215)Barney Gale2023-05-07
|
* GH-100479: Add `pathlib.PurePath.with_segments()` (GH-103975)Barney Gale2023-05-05
| | | | | Add `pathlib.PurePath.with_segments()`, which creates a path object from arguments. This method is called whenever a derivative path is created, such as from `pathlib.PurePath.parent`. Subclasses may override this method to share information between path objects. Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
* GH-81079: Add case_sensitive argument to `pathlib.Path.glob()` (GH-102710)Barney Gale2023-05-04
| | | | | | This argument allows case-sensitive matching to be enabled on Windows, and case-insensitive matching to be enabled on Posix. Co-authored-by: Steve Dower <steve.dower@microsoft.com>
* GH-104114: Fix `pathlib.WindowsPath.glob()` use of literal pattern segment ↵Barney Gale2023-05-03
| | | | | | | | | case (GH-104116) We now use `_WildcardSelector` to evaluate literal pattern segments, which allows us to retrieve the real filesystem case. This change is necessary in order to implement a *case_sensitive* argument (see GH-81079) and a *follow_symlinks* argument (see GH-77609).
* GH-89769: `pathlib.Path.glob()`: do not follow symlinks when checking for ↵andrei kulakov2023-05-03
| | | | | precise match (GH-29655) Co-authored-by: Barney Gale <barney.gale@gmail.com>
* GH-104102: Optimize `pathlib.Path.glob()` handling of `../` pattern segments ↵Barney Gale2023-05-02
| | | | | | | | (GH-104103) These segments do not require a `stat()` call, as the selector's `_select_from()` method is called after we've established that the parent is a directory.