[Python-Dev] file system path protocol PEP
Nick Coghlan
ncoghlan at gmail.com
Thu May 12 08:04:02 EDT 2016
On 12 May 2016 at 02:43, Brett Cannon <brett at python.org> wrote:
> **deep, calming breath**
>
> Here is the PEP for __fspath__(). The draft lives at
> https://github.com/brettcannon/path-pep so feel free to send me PRs for
> spelling mistakes, grammatical errors, etc.
Thanks for putting this together :)
> C API
> '''''
>
> The C API will gain an equivalent function to ``os.fspath()`` that
> also allows bytes objects through::
>
> /*
> Return the file system path of the object.
>
> If the object is str or bytes, then allow it to pass through with
> an incremented refcount. All other types raise a TypeError.
> */
> PyObject *
> PyOS_RawFSPath(PyObject *path)
> {
> if (PyObject_HasAttrString(path, "__fspath__")) {
> path = PyObject_CallMethodObjArgs(path, "__fspath__", NULL);
> if (path == NULL) {
> return NULL;
> }
> }
> else {
> Py_INCREF(path);
> }
>
> if (!PyUnicode_Check(path) && !PyBytes_Check(path)) {
> Py_DECREF(path);
> return PyErr_Format(PyExc_TypeError,
> "expected a string, bytes, or path object,
> not %S",
> path->ob_type);
> }
>
> return path;
> }
I'd still like to see this exposed to Python code as os._raw_fspath()
(with the leading underscore just meaning "this probably isn't the API
you want" rather than indicating a private or unstable API), and then
fspath() defined as a wrapper around it which disallows bytes as
output.
However, I don't have a specific use case, and it would be
straightforward to add later, so the overall PEP gets a +1 from me.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-Dev
mailing list