classification
Title: Add/check os.PathLike support for the tempfile module's 'dir' arguments
Type: enhancement Stage:
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, louielu, serhiy.storchaka, svelankar
Priority: normal Keywords:

Created on 2017-02-05 05:39 by brett.cannon, last changed 2017-05-08 18:39 by serhiy.storchaka.

Pull Requests
URL Status Linked Edit
PR 1411 closed svelankar, 2017-05-03 03:08
PR 1496 open louielu, 2017-05-08 04:17
Messages (10)
msg287036 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-02-05 05:39
The various classes in the tempfile module could implement os.PathLike.
msg292837 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-03 04:59
TemporaryDirectory and _TemporaryFileWrapper are *not* paths, as well as ordinal files are not paths. Adding __fspath__() to them looks wrong to me.

I think this isn't what Brett meant.
msg292919 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-05-03 16:34
Without looking at the PR there are two ways to interpret my unfortunately vague comment. One is to say that the objects returned by tempfile code should support os.PathLike; that's what Serhiy is objecting to as those are objects representing concrete things on the file system instead of a general concept of a path (i.e. open() returns a concrete file while pathlib returns a path). You could potentially argue that TemporaryDirectory can implement os.PathLike since it does represent a path on the file system, but I see where Serhiy is coming from about how this can be viewed as inappropriate.

The other way to interpret what I said was to make sure things like the 'dir' argument accepted path-like objects.

What I probably meant back in February was the first interpretation, but after hearing Serhiy's argument, I agree that the second interpretation is best. (My apologies to svelankar if they implemented the first idea.)
msg293191 - (view) Author: Louie Lu (louielu) * Date: 2017-05-07 13:30
Since tempfile is relay on `os`,

e.g. `file = _os.join.path(dir, pre+name+suf)`, 


it can directly accept dir as PathLike type, this should need to add test case for it.
msg293196 - (view) Author: Louie Lu (louielu) * Date: 2017-05-07 14:33
Regards my words, some place need to changed to support PathLike, I'll test it tomorrow.
msg293223 - (view) Author: Louie Lu (louielu) * Date: 2017-05-08 03:29
@Brett, do you think if given a path-like dir, it should only be treated as `str`, or it could be `str` and `bytes`?

My PR is now treated path-like dir as `str`, not `bytes`.

This will affect at this places:

    tempfile.mkdtemp(dir=pathlike.Path(''), pre=b'', suf=b'')

Should it raise a TypeError (since we can't mix str and bytes), or it will convert path-like to bytes.
msg293225 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-08 04:53
I don't see any changes in tempfile.py. If the path-like protocol already is supported for the dir argument, no change in the documentation is needed.

tempfile.mkdtemp(dir=pathlike.Path(''), pre=b'', suf=b'') should raise a TypeError, but add also tests for path-like objects returning bytes path.
msg293227 - (view) Author: Louie Lu (louielu) * Date: 2017-05-08 06:39
Serhiy, though this no need to add versionchanged, should this need to explicit note in doc, that tempfile support os.PathLike?

also, I didn't get "add also tests for path-like objects returning bytes path", if `tempfile.mkdtemp(dir=pathlike.Path(''), pre=b'', suf=b'')` will raise TypeError, then we don't need to deal with path-like objects returning bytes. because tempfile._infer_return_type will treat path-like objects as str.
msg293245 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-05-08 17:40
The key thing with the docs is that it doesn't say anywhere "takes a string path" or a "path as a string" or something else that suggests path-like objects don't work. If you want to clearly state that path-like objects are acceptable that is fine as well.

As for the bytes/str parts, path-like objects that return bytes should work, but only if everything is str or bytes as passed into the function (e.g. mixing the types should not be expected to work).
msg293247 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-08 18:39
If tempfile doesn't have special code for supporting path-like objects, and nothing in the documentation points that path-like objects don't work, then the documentation doesn't need changes. This is just a consequence of implementing PEP 519 in low-level functions.

If tempfile._infer_return_type will treat *all* path-like objects as str, this is a bug.
History
Date User Action Args
2017-05-08 18:39:25serhiy.storchakasetmessages: + msg293247
2017-05-08 17:40:48brett.cannonsetmessages: + msg293245
2017-05-08 06:39:28louielusetmessages: + msg293227
2017-05-08 04:53:15serhiy.storchakasetmessages: + msg293225
2017-05-08 04:17:16louielusetpull_requests: + pull_request1599
2017-05-08 03:29:54louielusetmessages: + msg293223
2017-05-07 14:33:14louielusetmessages: + msg293196
2017-05-07 13:30:37louielusetnosy: + louielu
messages: + msg293191
2017-05-03 16:35:56brett.cannonsettitle: Add os.PathLike support to the tempfile module's 'dir' arguments -> Add/check os.PathLike support for the tempfile module's 'dir' arguments
2017-05-03 16:34:14brett.cannonsetmessages: + msg292919
title: Add os.PathLike support to the tempfile module -> Add os.PathLike support to the tempfile module's 'dir' arguments
2017-05-03 04:59:45serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg292837
2017-05-03 03:09:27svelankarsetnosy: + svelankar
2017-05-03 03:08:49svelankarsetpull_requests: + pull_request1519
2017-02-05 05:39:42brett.cannoncreate