summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pathlib
Commit message (Collapse)AuthorAgeFilesLines
* [3.14] gh-127146: Emscripten: Fix pathlib glob_dotdot test (GH-135624) (#135653)Miss Islington (bot)2025-06-181-1/+7
| | | | | | | | | | | | The Emscripten path resolver uses the same mechanism for resolving `..` at a file system root as for resolving symlinks. This is because roots don't store their mountpoints. If the parent of a node is itself, it is a root but it might be a mountpoint in some other file system. If a path has enough `..`'s at the root, it will return ELOOP. Enough turns out to be 49. (cherry picked from commit e4ccd46bf75fff2938d7c21c7284e49b0ab795b0) Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
* [3.14] gh-71339: Use new assertion methods in tests (GH-129046) (GH-134498)Miss Islington (bot)2025-05-221-4/+4
| | | | | (cherry picked from commit 2602d8ae981c4bae1cada2c174b367d97f712efb) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [3.14] gh-133677: Fix tests when running in non-UTF-8 locale (GH-133865) ↵Miss Islington (bot)2025-05-121-4/+8
| | | | | | | (GH-133938) (cherry picked from commit 14305a83d381ccdcae814abd9e2c28dda066720b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* gh-118761: Add test_lazy_import for more modules (#133057)Daniel Hollas2025-05-051-0/+7
| | | Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
* GH-123599: `url2pathname()`: don't call `gethostbyname()` by default (#132610)Barney Gale2025-05-051-1/+0
| | | | | | | | | | | Follow-up to 66cdb2bd8ab93a4691bead7f5d1e54e5ca6895b4. Add *resolve_host* keyword-only argument to `url2pathname()`, defaulting to false. When set to true, we call `socket.gethostbyname()` to resolve the URL hostname. Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Steve Dower <steve.dower@microsoft.com>
* GH-128520: pathlib ABCs tests: use explicit text encoding (#133105)Barney Gale2025-04-284-13/+13
| | | | | Follow-up to fbffd70. Set `encoding='utf-8'` when reading and writing text in the tests for the private pathlib ABCs, which allows the tests to run with `-W error -X warn_default_encoding`
* GH-128520: pathlib ABCs: raise text encoding warnings at correct stack level ↵Barney Gale2025-04-282-0/+44
| | | | | | | (#133051) Ensure that warnings about unspecified text encodings are emitted from `ReadablePath.read_text()`, `WritablePath.write_text()` and `magic_open()` with the correct stack level set.
* GH-125866: Support complete "file:" URLs in urllib (#132378)Barney Gale2025-04-141-2/+2
| | | | | | | | Add optional *add_scheme* argument to `urllib.request.pathname2url()`; when set to true, a complete URL is returned. Likewise add optional *require_scheme* argument to `url2pathname()`; when set to true, a complete URL is accepted. Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
* gh-132356: Find the correct group name in test_group_no_follow_symlinks ↵Karolina Surma2025-04-111-1/+1
| | | | | (#132357) Find the correct group name in test_group_no_follow_symlinks
* GH-123599: `url2pathname()`: handle authority section in file URL (#126844)Barney Gale2025-04-101-1/+5
| | | | | | | | | | | In `urllib.request.url2pathname()`, if the authority resolves to the current host, discard it. If an authority is present but resolves somewhere else, then on Windows we return a UNC path (as before), and on other platforms we raise `URLError`. Affects `pathlib.Path.from_uri()` in the same way. Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
* GH-128520: pathlib ABCs: validate `magic_open()` arguments (#131617)Barney Gale2025-03-242-0/+6
| | | | | When `pathlib._os.magic_open()` is called to open a path in binary mode, raise `ValueError` if any of the *encoding*, *errors* or *newline* arguments are given. This matches the `open()` built-in.
* GH-128520: pathlib ABCs: reject empty pattern in `ReadablePath.glob()` (#127343)Barney Gale2025-03-241-0/+2
| | | | | For compatibility with `Path.glob()`, raise `ValueError` if an empty pattern is given to `ReadablePath.glob()`.
* GH-128520: pathlib ABCs: allow tests to be run externally (#131315)Barney Gale2025-03-2110-72/+127
| | | | | | | | | | | | Adjust the tests for the `pathlib.types` module so that they can be run against the `pathlib-abc` PyPI package, which is a backport of the module for older Python versions. Specifically, we add a `.support.is_pypi` switch that is false in the stdlib and true in the pathlib-abc package. This controls which package we import, and whether or not we run tests against `PurePath` and `Path`. For compatibility with older Python versions, we stop using `zipfile.ZipFile.mkdir()` and `zipfile.ZipInfo._for_archive()`.
* GH-123599: Deprecate duplicate `pathname2url()` implementation (#127380)Barney Gale2025-03-201-15/+21
| | | | | | Call `urllib.request.pathname2url()` from `pathlib.Path.as_uri()`, and deprecate the duplicate implementation in `PurePath`. Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
* gh-131277: allow `EnvironmentVarGuard` to unset more than one environment ↵Bénédikt Tran2025-03-161-7/+3
| | | | | variable at once (#131280) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
* GH-130614: pathlib ABCs: delete vestigial `test_pathlib_abc` module (#131215)Barney Gale2025-03-142-395/+115
| | | | | Remove the `test.test_pathlib.test_pathlib_abc` test module, which was hollowed out in previous commits. Its few remaining tests are most relevant to `PurePath` and `Path`, so we move them into `test_pathlib`.
* GH-130614: pathlib ABCs: parametrize test suite for path copying (#131168)Barney Gale2025-03-132-138/+172
| | | | Test copying from `Path` and `ReadableZipPath` (types of `_ReadablePath`) to `Path` and `WritableZipPath` (types of `_WritablePath`).
* gh-131152: Remove unused imports from tests (part 2) (#131154)Victor Stinner2025-03-132-4/+1
|
* GH-130614: pathlib ABCs: revise test suite for writable paths (#131112)Barney Gale2025-03-124-43/+178
| | | | | Test `pathlib.types._WritablePath` in a dedicated test module. These tests cover `WritableZipPath`, `WritableLocalPath` and `Path`, where the former two classes are implementations of `_WritablePath` for use in tests.
* GH-130614: pathlib ABCs: revise test suite for readable paths (#131018)Barney Gale2025-03-115-268/+759
| | | | | Test `pathlib.types._ReadablePath` in a dedicated test module. These tests cover `ReadableZipPath`, `ReadableLocalPath` and `Path`, where the former two classes are implementations of `_ReadablePath` for use in tests.
* GH-130614: pathlib ABCs: revise test suite for Windows path joining (#131016)Barney Gale2025-03-114-279/+320
| | | | | | Test Windows-flavoured `pathlib.types._JoinablePath` in a dedicated test module. These tests cover `LexicalWindowsPath`, `PureWindowsPath` and `WindowsPath`, where `LexicalWindowsPath` is a simple implementation of `_JoinablePath` for use in tests.
* GH-130614: pathlib ABCs: revise test suite for Posix path joining (#131017)Barney Gale2025-03-103-23/+55
| | | | | | Test Posix-flavoured `pathlib.types._JoinablePath` in a dedicated test module. These tests cover `LexicalPosixPath`, `PurePosixPath` and `PosixPath`, where `LexicalPosixPath` is a simple implementation of `_JoinablePath` for use in tests.
* GH-127381: pathlib ABCs: remove `case_sensitive` argument (#131024)Barney Gale2025-03-103-17/+19
| | | | | | | | | | | | | | | | Remove the *case_sensitive* argument from `_JoinablePath.full_match()` and `_ReadablePath.glob()`. Using a non-native case sensitivity forces the use of "case-pedantic" globbing, where we `iterdir()` even for non-wildcard pattern segments. But it's hard to know when to enable this mode, as case-sensitivity can vary by directory, so `_PathParser.normcase()` doesn't always give the full picture. The `Path.glob()` implementation is forced to make an educated guess, but we can avoid the issue in the ABCs by dropping the *case_sensitive* argument. (I probably shouldn't have added these arguments in `PurePath` and `Path` in the first place!) Also drop support for `_ReadablePath.glob(recurse_symlinks=False)`, which makes recursive globbing much slower.
* GH-130614: pathlib ABCs: revise test suite for path joining (#130988)Barney Gale2025-03-094-381/+405
| | | | | | Test `pathlib.types._JoinablePath` in a dedicated test module. These tests cover `LexicalPath`, `PurePath` and `Path`, where `LexicalPath` is defined in a new `test.test_pathlib.support` package.
* GH-128520: Merge `pathlib._abc` into `pathlib.types` (#130747)Barney Gale2025-03-032-13/+13
| | | | | | | | | There used to be a meaningful distinction between these modules: `pathlib` imported `pathlib._abc` but not `pathlib.types`. This is no longer the case (neither module is imported), so we move the ABCs as follows: - `pathlib._abc.JoinablePath` --> `pathlib.types._JoinablePath` - `pathlib._abc.ReadablePath` --> `pathlib.types._ReadablePath` - `pathlib._abc.WritablePath` --> `pathlib.types._WritablePath`
* GH-127381: pathlib ABCs: remove `WritablePath.mkdir()` arguments (#130611)Barney Gale2025-03-011-9/+3
| | | | | | | | Remove the *mode*, *parents* and *exist_ok* arguments from `WritablePath.mkdir()`. These arguments imply support for POSIX permissions and checking for preexistence of the path or its parents, but subclasses of `WritablePath` may not have these capabilities. The public `Path.mkdir()` method retains these arguments.
* GH-127381: pathlib ABCs: remove `ReadablePath.exists()` and `is_*()` (#130520)Barney Gale2025-03-012-98/+89
| | | | | | Remove `ReadablePath` methods duplicated by `ReadablePath.info`. To be specific, we remove `exists()`, `is_dir()`, `is_file()` and `is_symlink()`. The public `Path` class retains these methods.
* GH-130608: Remove `dirs_exist_ok` argument from `pathlib.Path.copy()` (#130610)Barney Gale2025-02-281-17/+0
| | | This feature isn't sufficiently motivated.
* GH-125413: Fix stale metadata from `pathlib.Path.copy()` and `move()` (#130424)Barney Gale2025-02-241-24/+24
| | | | | In `pathlib.Path.copy()` and `move()`, return a fresh `Path` object with an unpopulated `info` attribute, rather than a `Path` object with information recorded *prior* to the path's creation.
* GH-125413: pathlib ABCs: use caching `path.info.exists()` when globbing ↵Barney Gale2025-02-241-1/+1
| | | | | | | (#130422) Call `ReadablePath.info.exists()` rather than `ReadablePath.exists()` when globbing so that we use (or populate) the `info` cache.
* GH-127381: pathlib ABCs: remove `ReadablePath.rglob()` (#130207)Barney Gale2025-02-172-58/+58
| | | | Remove `ReadablePath.rglob()` from the private pathlib ABCs. This method is a trivial wrapper around `glob()` and easily replaced.
* GH-125413: Add private metadata methods to `pathlib.Path.info` (#129897)Barney Gale2025-02-171-3/+5
| | | | | | | | | | | | | | | | | | | | | | | | Add the following private methods to `pathlib.Path.info`: - `_posix_permissions()`: the POSIX file permissions (`S_IMODE(st_mode)`) - `_file_id()`: the file ID (`(st_dev, st_ino)`) - `_access_time_ns()`: the access time in nanoseconds (`st_atime_ns`) - `_mod_time_ns()`: the modify time in nanoseconds (`st_mtime_ns`) - `_bsd_flags()`: the BSD file flags (`st_flags`) - `_xattrs()`: the file extended attributes as a list of key, value pairs, or an empty list if `listxattr()` or `getxattr()` fail in an ignorable way. These methods replace `LocalCopyReader.read_metadata()`, and so we can delete the `CopyReader` and `LocalCopyReader` classes. Rather than reading metadata via `source._copy_reader.read_metadata()`, we instead call `source.info._posix_permissions()`, `_access_time_ns()`, etc. Preserving metadata is only supported for local-to-local copies at the moment. To support copying metadata between arbitrary `ReadablePath` and `WritablePath` objects, we'd need to make the new methods public and documented. Co-authored-by: Petr Viktorin <encukou@gmail.com>
* GH-128520: Subclass `abc.ABC` in `pathlib._abc` (#128745)Barney Gale2025-02-162-29/+30
| | | | | | | | | | | Convert `JoinablePath`, `ReadablePath` and `WritablePath` to real ABCs derived from `abc.ABC`. Make `JoinablePath.parser` abstract, rather than defaulting to `posixpath`. Register `PurePath` and `Path` as virtual subclasses of the ABCs rather than deriving. This avoids a hit to path object instantiation performance. No change of behaviour in the public (non-abstract) classes.
* GH-129835: Yield path with trailing slash from `ReadablePath.glob('')` (#129836)Barney Gale2025-02-081-1/+1
| | | | | | | | | | In the private pathlib ABCs, make `ReadablePath.glob('')` yield a path with a trailing slash (if it yields anything at all). As a result, `glob()` works similarly to `joinpath()` when given a non-magic pattern. In the globbing implementation, we preemptively add trailing slashes to intermediate paths if there are pattern parts remaining; this removes the need to check for existing trailing slashes (in the removed `add_slash()` method) at subsequent steps.
* GH-125413: Add `pathlib.Path.info` attribute (#127730)Barney Gale2025-02-082-27/+172
| | | | | Add `pathlib.Path.info` attribute, which stores an object implementing the `pathlib.types.PathInfo` protocol (also new). The object supports querying the file type and internally caching `os.stat()` results. Path objects generated by `Path.iterdir()` are initialised with status information from `os.DirEntry` objects, which is gleaned from scanning the parent directory. The `PathInfo` protocol has four methods: `exists()`, `is_dir()`, `is_file()` and `is_symlink()`.
* GH-127381: pathlib ABCs: remove `JoinablePath.match()` (#129147)Barney Gale2025-01-282-82/+78
| | | | | | | Unlike `ReadablePath.[r]glob()` and `JoinablePath.full_match()`, the `JoinablePath.match()` method doesn't support the recursive wildcard `**`, and matches from the right when a fully relative pattern is given. These quirks means its probably unsuitable for inclusion in the pathlib ABCs, especially given `full_match()` handles the same use case.
* GH-128520: Make `pathlib._abc.WritablePath` a sibling of `ReadablePath` ↵Barney Gale2025-01-212-56/+43
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (#129014) In the private pathlib ABCs, support write-only virtual filesystems by making `WritablePath` inherit directly from `JoinablePath`, rather than subclassing `ReadablePath`. There are two complications: - `ReadablePath.open()` applies to both reading and writing - `ReadablePath.copy` is secretly an object that supports the *read* side of copying, whereas `WritablePath.copy` is a different kind of object supporting the *write* side We untangle these as follow: - A new `pathlib._abc.magic_open()` function replaces the `open()` method, which is dropped from the ABCs but remains in `pathlib.Path`. The function works like `io.open()`, but additionally accepts objects with `__open_rb__()` or `__open_wb__()` methods as appropriate for the mode. These new dunders are made abstract methods of `ReadablePath` and `WritablePath` respectively. If the pathlib ABCs are made public, we could consider blessing an "openable" protocol and supporting it in `io.open()`, removing the need for `pathlib._abc.magic_open()`. - `ReadablePath.copy` becomes a true method, whereas `WritablePath.copy` is deleted. A new `ReadablePath._copy_reader` property provides a `CopyReader` object, and similarly `WritablePath._copy_writer` is a `CopyWriter` object. Once GH-125413 is resolved, we'll be able to move the `CopyReader` functionality into `ReadablePath.info` and eliminate `ReadablePath._copy_reader`.
* GH-128520: Divide pathlib ABCs into three classes (#128523)Barney Gale2025-01-112-247/+243
| | | | | | | | | | | | In the private pathlib ABCs, rename `PurePathBase` to `JoinablePath`, and split `PathBase` into `ReadablePath` and `WritablePath`. This improves the API fit for read-only virtual filesystems. The split of `PathBase` entails a similar split of `CopyWorker` (implements copying) and the test cases in `test_pathlib_abc`. In a later patch, we'll make `WritablePath` inherit directly from `JoinablePath` rather than `ReadablePath`. For a couple of reasons, this isn't quite possible yet.
* pathlib tests: create `walk()` test hierarchy without using class under test ↵Barney Gale2025-01-042-39/+58
| | | | | | | | (#128338) In the tests for `pathlib.Path.walk()`, avoid using the path class under test (`self.cls`) in test setup. Instead we use `os` functions in `test_pathlib`, and direct manipulation of `DummyPath` internal data in `test_pathlib_abc`.
* GH-127381: pathlib ABCs: remove `PathBase.move()` and `move_into()` (#128337)Barney Gale2025-01-042-118/+118
| | | | | These methods combine `_delete()` and `copy()`, but `_delete()` isn't part of the public interface, and it's unlikely to be added until the pathlib ABCs are made official, or perhaps even later.
* GH-127381: pathlib ABCs: remove uncommon `PurePathBase` methods (#127853)Barney Gale2024-12-292-366/+360
| | | | | | | | Remove `PurePathBase.relative_to()` and `is_relative_to()` because they don't account for *other* being an entirely different kind of path, and they can't use `__eq__()` because it's not on the `PurePathBase` interface. Remove `PurePathBase.drive`, `root`, `is_absolute()` and `as_posix()`. These are all too specific to local filesystems.
* GH-127381: pathlib ABCs: remove `PathBase.stat()` (#128334)Barney Gale2024-12-292-53/+49
| | | | | | | Remove the `PathBase.stat()` method. Its use of the `os.stat_result` API, with its 10 mandatory fields and low-level types, makes it an awkward fit for virtual filesystems. We'll look to add a `PathBase.info` attribute later - see GH-125413.
* pathlib tests: create test hierarchy without using class under test (#128200)Barney Gale2024-12-232-29/+55
| | | | | In the pathlib tests, avoid using the path class under test (`self.cls`) in test setup. Instead we use `os` functions in `test_pathlib`, and direct manipulation of `DummyPath` internal data in `test_pathlib_abc`.
* GH-127807: pathlib ABCs: remove `PurePathBase._raw_paths` (#127883)Barney Gale2024-12-222-56/+54
| | | | | Remove the `PurePathBase` initializer, and make `with_segments()` and `__str__()` abstract. This allows us to drop the `_raw_paths` attribute, and also the `Parser.join()` protocol method.
* gh-127146: Emscripten clean up test suite (#127984)Hood Chatham2024-12-171-6/+1
| | | Removed test skips that are no longer required as a result of Emscripten updates.
* GH-127807: pathlib ABCs: remove `PathBase._unsupported_msg()` (#127855)Barney Gale2024-12-122-9/+11
| | | | | This method helped us customise the `UnsupportedOperation` message depending on the type. But we're aiming to make `PathBase` a proper ABC soon, so `NotImplementedError` is the right exception to raise there.
* GH-127381: pathlib ABCs: remove remaining uncommon `PathBase` methods (#127714)Barney Gale2024-12-121-12/+0
| | | | | | | | | | | | | | | | | | Remove the following methods from `pathlib._abc.PathBase`: - `expanduser()` - `hardlink_to()` - `touch()` - `chmod()` - `lchmod()` - `owner()` - `group()` - `from_uri()` - `as_uri()` These operations aren't regularly supported in virtual filesystems, so they don't win a place in the `PathBase` interface. (Some of them probably don't deserve a place in `Path` :P.) They're quasi-abstract (except `lchmod()`), and they're not called by other `PathBase` methods.
* GH-127381: pathlib ABCs: remove `PathBase.samefile()` and rarer `is_*()` ↵Barney Gale2024-12-112-83/+75
| | | | | | | | | | (#127709) Remove `PathBase.samefile()`, which is fairly specific to the local FS, and relies on `stat()`, which we're aiming to remove from `PathBase`. Also remove `PathBase.is_mount()`, `is_junction()`, `is_block_device()`, `is_char_device()`, `is_fifo()` and `is_socket()`. These rely on POSIX file type numbers that we're aiming to remove from the `PathBase` API.
* GH-127456: pathlib ABCs: add protocol for path parser (#127494)Barney Gale2024-12-091-53/+8
| | | | | | | | | | Change the default value of `PurePathBase.parser` from `ParserBase()` to `posixpath`. As a result, user subclasses of `PurePathBase` and `PathBase` use POSIX path syntax by default, which is very often desirable. Move `pathlib._abc.ParserBase` to `pathlib._types.Parser`, and convert it to a runtime-checkable protocol. Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
* GH-127381: pathlib ABCs: remove `PathBase.unlink()` and `rmdir()` (#127736)Barney Gale2024-12-082-45/+30
| | | | | | | Virtual filesystems don't always make a distinction between deleting files and empty directories, and sometimes support deleting non-empty directories in a single operation. Here we remove `PathBase.unlink()` and `rmdir()`, leaving `_delete()` as the sole deletion method, now made abstract. I hope to drop the underscore prefix later on.