]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
GH-128520: pathlib ABCs: improve protocol for 'openable' objects (#134101)
authorBarney Gale <barney.gale@gmail.com>
Fri, 12 Sep 2025 21:25:18 +0000 (22:25 +0100)
committerGitHub <noreply@github.com>
Fri, 12 Sep 2025 21:25:18 +0000 (22:25 +0100)
commit805e3368d6d07e58430654d1365283924fdf4143
tree365ccc7c15793fe5541713e125a4f89c72a35c94
parent2e8f64c9317a327a2be1ab8031844ef3967c1040
GH-128520: pathlib ABCs: improve protocol for 'openable' objects (#134101)

Rename `pathlib._os.magic_open()` to `vfsopen()`. The new name is a bit
less abstract, and it aligns with the `vfspath()` method added in 5dbd27d.

Per discussion on discourse[^1], adjust `vfsopen()` so that the following
methods may be called:

- `__open_reader__()`
- `__open_writer__(mode)`
- `__open_updater__(mode)`

These three methods return readable, writable, and full duplex file objects
respectively. In the 'writer' method, *mode* is either 'a', 'w' or 'x'. In
the 'updater' method, *mode* is either 'r' or 'w'.

In the pathlib ABCs, replace `ReadablePath.__open_rb__()` with
`__open_reader__()`, and replace `WritablePath.__open_wb__()` with
`__open_writer__()`.

[^1]: https://discuss.python.org/t/open-able-objects/90238

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Lib/pathlib/__init__.py
Lib/pathlib/_os.py
Lib/pathlib/types.py
Lib/test/test_pathlib/support/local_path.py
Lib/test/test_pathlib/support/zip_path.py
Lib/test/test_pathlib/test_read.py
Lib/test/test_pathlib/test_write.py