Open
Description
Inspired by gh-3484 that tried to fix this for wave.open()
only but failed.
We need a centralized and universal approach instead so everything accepting str
paths could automatically accept PathLike
for free.
For this, we can amend stdlib functions to wrap their path arguments into pathlib.PurePath
introduced in Python 3.4. Unlike os.fspath
, it throws a nicely formatted exception for us:
-
os.fspath
from the mentioned bpo-31412: wave.open takes a path-like object #3484:Lines 912 to 918 in 4a82d65
-
PurePath
:>>> import pathlib >>> pathlib.PurePath(16) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\Oleg\cpython\Lib\pathlib.py", line 267, in __new__ return cls._from_parts(args) ^^^^^^^^^^^^^^^^^^^^^ File "D:\Oleg\cpython\Lib\pathlib.py", line 316, in _from_parts drv, root, parts = self._parse_args(args) ^^^^^^^^^^^^^^^^^^^^^^ File "D:\Oleg\cpython\Lib\pathlib.py", line 300, in _parse_args a = os.fspath(a) ^^^^^^^^^^^^ TypeError: expected str, bytes or os.PathLike object, not int