[Python-Dev] pathlib - current status of discussions (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Thu Apr 14 09:40:33 EDT 2016


On 14 April 2016 at 22:16, Victor Stinner <victor.stinner at gmail.com> wrote:

2016-04-13 19:10 GMT+02:00 Brett Cannon <brett at python.org>:

https://gist.github.com/brettcannon/b3719f54715787d54a206bc011869aa1 has the four potential approaches implemented (although it doesn't follow the "separate functions" approach some are proposing and instead goes with the allowbytes approach I originally proposed). IMHO the best argument against the flavor 4 (fspath: str or bytes allowed) is the os.path.join() function. I consider that the final goal of the whole discussion is to support something like: path = os.path.join(pathlibpath, "strpath", direntry)

That's not a new problem though, it already exists if you pass in a mix of bytes and str:

import os.path os.path.join("str", b"bytes") Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.4/posixpath.py", line 89, in join "components") from None TypeError: Can't mix strings and bytes in path components

There's also already a solution (regardless of whether you want bytes or str as the result), which is to explicitly coerce all the arguments to the same type:

os.path.join(*map(os.fsdecode, ("str", b"bytes"))) 'str/bytes' os.path.join(*map(os.fsencode, ("str", b"bytes"))) b'str/bytes'

Assuming os.fsdecode and os.fsencode are updated to call os.fspath on their argument before continuing with the current logic, the latter two forms would both start automatically handling both DirEntry and pathlib objects, while the first form would continue to throw TypeError if handed an unexpected bytes value (whether directly or via an fspath call).

Cheers, Nick.

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia



More information about the Python-Dev mailing list