[Python-Dev] file system path protocol PEP
Koos Zevenhoven
k7hoven at gmail.com
Thu May 12 14:11:18 EDT 2016
On Thu, May 12, 2016 at 7:24 PM, Guido van Rossum <guido at python.org> wrote:
> I am glad this is finally happening. There's quite a bit of noise in the
> thread which I have to ignore. The two issues that I want to respond to are
> speed and whether os.fspath() can return bytes.
>
> - Speed: We should trust our ability to optimize the implementations where
> necessary. First the API issues need to be settled.
>
> - Bytes: I strongly believe that os.fspath() should be a thin wrapper around
> the __fspath__ protocol, like next() wraps the .__next__ protocol. It should
> not get into bytes vs. string politics. If your app really needs strings,
> call os.fsdecode(). So this is my version (unoptimized):
>
:)
Thank you for this. I can breathe now.
Some questions remain:
> def fspath(p: Union[str, bytes, PathLike]) -> Union[str, bytes]:
> if isinstance(p, (str, bytes)):
> return p
> try:
> return p.__fspath__
> except AttributeError:
> raise TypeError(...)
>
(I know Brett already posted this question, but somehow it did not
show up in my mailbox before I had written this. I'm (re)posting
because there is some stuff here that is not in Brett's email )
You might be suggesting that __fspath__ should be an attribute, not a
method, or did you mean something like:
def fspath(p):
if isinstance(p, (str, bytes)):
return p
try:
p.__fspath__
except AttributeError:
raise TypeError(...)
return p.__fspath__()
IMO, either is fine, I suppose. As you know, it's mostly a question of
whether __fspath__ will be a property or a method (on PurePath for
instance). But if you meant the former, that would change also the ABC
and the protocol description.
-- Koos
More information about the Python-Dev
mailing list