[Python-Dev] Pathlib enhancements - acceptable inputs and outputs for __fspath__ and os.fspath()
ncoghlan at gmail.com
Thu Apr 14 02:00:22 EDT 2016
On 14 April 2016 at 13:54, Random832 <random832 at fastmail.com> wrote:
> On Wed, Apr 13, 2016, at 23:17, Nick Coghlan wrote:
>> - os.fspath -> str (no coercion)
>> - os.fsdecode -> str (with coercion from bytes)
>> - os.fsencode -> bytes (with coercion from str)
>> - os._raw_fspath -> str-or-bytes (no coercion)
>> (with "coercion" referring to how the result of __fspath__ and any
>> directly passed in str or bytes objects are handled)
>> The leading underscore on _raw_fspath would be of the "this is a
>> documented and stable API, but you probably don't want to use it
>> unless you really know what you're doing" variety, rather than the
>> "this is an undocumented and potentially unstable private API"
> In this scenario could the protocol return bytes?
Yes, that's desirable to handle DirEntry transparently regardless of type.
> If the protocol can return bytes, then that means that types (DirEntry?
> someone had an alternate path library with a bPath?) which return bytes
> via the protocol will proliferate, and cannot be safely passed to
> anything that uses os.fspath. Numerous copies of "def myfspath(x):
> return os.fsdecode(os._raw_fspath(x))" will proliferate (or they'll just
> monkey-patch os.fspath), and no-one actually uses os.fspath except toy
If folks want coercion, they can just use os.fsdecode(x), as that
already has a str -> str passthrough from the input to the output
(unlike codecs.decode) and will presumably be updated to include an
implicit call to os._raw_fspath() on the passed in object.
> Why is it so objectionable for os.fspath to do coercion?
The first problem is that binary paths on Windows basically don't
work, so it's preferable for them to fail fast regardless of platform,
rather than to have them implicitly work on *nix, only to fail for
Windows users using non-ASCII paths later.
The second is that it would make os.fspath and os.fsdecode
functionally equivalent, so we'd have two different spellings for the
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-Dev