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>
This commit is contained in:
Barney Gale 2025-09-12 22:25:18 +01:00 committed by GitHub
parent 2e8f64c931
commit 805e3368d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 114 additions and 64 deletions

View file

@ -145,7 +145,7 @@ def __init__(self, *pathsegments):
super().__init__(*pathsegments)
self.info = LocalPathInfo(self)
def __open_rb__(self, buffering=-1):
def __open_reader__(self):
return open(self, 'rb')
def iterdir(self):
@ -163,8 +163,8 @@ class WritableLocalPath(_WritablePath, LexicalPath):
__slots__ = ()
__fspath__ = LexicalPath.__vfspath__
def __open_wb__(self, buffering=-1):
return open(self, 'wb')
def __open_writer__(self, mode):
return open(self, f'{mode}b')
def mkdir(self, mode=0o777):
os.mkdir(self, mode)