[Python-Dev] Defining a path protocol (original) (raw)

Paul Moore p.f.moore at gmail.com
Thu Apr 7 03:59:14 EDT 2016


On 6 April 2016 at 23:46, Brett Cannon <brett at python.org> wrote:

str(path) will definitely work, path.path will work if you're running the next set of bugfix releases. fspath(path) will only work in Python 3.6 and newer.

Ah, that was something I hadn't appreciated, that the builtin would be 3.6+ whereas the protocol would be added to current bugfix releases.

Maybe a compatibility library could add

try: fspath except NameError: try: import pathlib def fspath(p): if isinstance(p, pathlib.Path): return str(p) return p except ImportError: def fspath(p): return p It's messy, like all compatibility code, but it allows code to use fspath(p) in older versions. I would tweak it to check for fspath before it resorted to calling str(), but yes, that could be something people use.

Yeah, the above code assumes that if the builtin isn't available, nor will the protocol be (see my misunderstanding above).

Paul



More information about the Python-Dev mailing list