Issue 29447: Add/check os.PathLike support for the tempfile module's 'dir' arguments (original) (raw)

Issue29447

Created on 2017-02-05 05:39 by brett.cannon, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 1411 closed svelankar,2017-05-03 03:08
PR 1496 closed louielu,2017-05-08 04:17
Messages (11)
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.
msg344073 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-05-31 13:05
The pull request attached to this issue has been closed as the repository was marked as unknown. This issue is now available for a new pull request.
History
Date User Action Args
2022-04-11 14:58:42 admin set github: 73633
2020-07-18 03:39:52 calestyo set nosy: + calestyo
2019-05-31 13:05:20 cheryl.sabella set versions: + Python 3.9nosy: + cheryl.sabellamessages: + stage: needs patch
2017-05-08 18:39:25 serhiy.storchaka set messages: +
2017-05-08 17:40:48 brett.cannon set messages: +
2017-05-08 06:39:28 louielu set messages: +
2017-05-08 04:53:15 serhiy.storchaka set messages: +
2017-05-08 04:17:16 louielu set pull_requests: + <pull%5Frequest1599>
2017-05-08 03:29:54 louielu set messages: +
2017-05-07 14:33:14 louielu set messages: +
2017-05-07 13:30:37 louielu set nosy: + louielumessages: +
2017-05-03 16:35:56 brett.cannon set title: 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:14 brett.cannon set messages: + 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:45 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2017-05-03 03:09:27 svelankar set nosy: + svelankar
2017-05-03 03:08:49 svelankar set pull_requests: + <pull%5Frequest1519>
2017-02-05 05:39:42 brett.cannon create