bpo-29447: tempfile: Add pathlike object support for dir argument#1496
bpo-29447: tempfile: Add pathlike object support for dir argument#1496louisom wants to merge 11 commits into
Conversation
|
@lulouie, thanks for your PR! By analyzing the history of the files in this pull request, we identified @ezio-melotti, @birkenfeld and @serhiy-storchaka to be potential reviewers. |
brettcannon
left a comment
There was a problem hiding this comment.
Don't forget to add yourself to Misc/ACKS.
|
|
||
| The :py:data:`os.O_TMPFILE` flag is now used if available. | ||
|
|
||
| .. versionchanged:: 3.7 |
There was a problem hiding this comment.
So this isn't entirely true since you didn't need to change anything for support, so this really has been supported since Python 3.6.
|
|
||
| .. versionchanged:: 3.7 | ||
|
|
||
| The ``dir`` argument is now accept a :term:`path-like object`. |
There was a problem hiding this comment.
The *dir* argument now accepts a :term:`path-like object`.
There was a problem hiding this comment.
I think no documentation changes is needed. Otherwise we should add "versionchanged" directives for every function that got an implicit support of path-like objects (this is perhaps many tens of functions). This just distracts the attention.
There was a problem hiding this comment.
Revert the docs change for now.
| def test_choose_pathlike_directory(self): | ||
| # _mkstemp_inner can create files in a user-selected pathlike directory | ||
| dir = pathlib.Path(tempfile.mkdtemp()) | ||
| try: |
There was a problem hiding this comment.
Instead of a try/finally, use unittest.TestCase.addCleanup() (basically you shouldn't have to use any try/finally blocks in your test code thanks to that method).
| - bpo-30068: _io._IOBase.readlines will check if it's closed first when | ||
| hint is present. | ||
|
|
||
| - bpo-29447: tempfile's dir argument is now accept path-like objects. |
There was a problem hiding this comment.
This isn't accurate since the support was already there implicitly, so just drop the changes to this file or add a comment that tests for path-like objects in tempfile were added to the "Tests" section.
There was a problem hiding this comment.
Usually we don't add entries about changes to tests unless this is very large change or a change that affects the testing infrastructure (test.support, test.libregrtest).
There was a problem hiding this comment.
Remove the NEWS entry.
| class TestLowLevelInternals(unittest.TestCase): | ||
| def test_infer_return_type_singles(self): | ||
| self.assertIs(str, tempfile._infer_return_type('')) | ||
| self.assertIs(str, tempfile._infer_return_type(pathlib.Path(''))) |
There was a problem hiding this comment.
It wouldn't hurt to add a test for a path-like object that returns bytes (you will have to make a test object yourself).
There was a problem hiding this comment.
There are test path-like classes in other test files. The code can be shared.
There was a problem hiding this comment.
I couldn't find other place test path-like with bytes. Add some test here, and at low-level internals test
There was a problem hiding this comment.
Search def __fspath__ in tests.
| self.do_create(dir=".") | ||
| self.do_create(dir=pathlib.Path(".")) | ||
|
|
||
| def test_basic_with_bytes_names(self): |
There was a problem hiding this comment.
Add a test with a path-like object that returns bytes.
brettcannon
left a comment
There was a problem hiding this comment.
If those changes you made to tempfile are necessary then the doc changes you reverted will need to come back as this is no longer a patch to just add some tests.
| return_type = None | ||
| for arg in args: | ||
| if arg is None: | ||
| if arg is None or isinstance(arg, _os.PathLike): |
There was a problem hiding this comment.
Why is this necessary? Can't you simply call _os.fspath(arg) just before if isinstance(arg, bytes)?
There was a problem hiding this comment.
I think dir = _os.fspath(dir) should be called before calling _infer_return_type(). It would be not correct to call _os.fspath() for non-path arguments.
There was a problem hiding this comment.
We can't do dir = _os.fspath(dir) too fast, otherwise path-like objects will only be treat as an str, not str or bytes.
There was a problem hiding this comment.
os.fspath() returns str or bytes.
There was a problem hiding this comment.
ah, I get it. Is my misunderstand about path-like objects, I thought path-like object should return string only. because stdlib implement __fspath__ with return str only. And there should be some 3rd-party class implement __fspath__ with return bytes.
There was a problem hiding this comment.
For example DirEntry.__fspath__ can return bytes.
| dir = gettempdir() | ||
| else: | ||
| dir = gettempdirb() | ||
| if isinstance(dir, _os.PathLike): |
There was a problem hiding this comment.
Why is the encoding call suddenly necessary when it wasn't needed for dir previously?
There was a problem hiding this comment.
We should fist checkout the output type, to respect other arguments (prefix and suffix), then decide how to convert dir when it is a path-like objects.
| for arg in args: | ||
| if arg is None: | ||
| continue | ||
| elif isinstance(arg, _os.PathLike): |
There was a problem hiding this comment.
Why this explicit test is needed? Aren't high-level function fail in any case when pass path-like object as prefix or suffix?
There was a problem hiding this comment.
Should we allow path-like objects for prefix and suffix? it will failed when path-like objects concat with string:
file = _os.path.join(dir, pre + name + suf)
There was a problem hiding this comment.
@lulouie No, path-like objects are only useful for things that represent paths. Both prefix and suffix are not paths themselves.
|
To try and help move older pull requests forward, we are going through and backfilling 'awaiting' labels on pull requests that are lacking the label. Based on the current reviews, the best we can tell in an automated fashion is that a core developer requested changes to be made to this pull request. If/when the requested changes have been made, please leave a comment that says, |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Path-like objects shouldn't be accepted as prefix or suffix. There should be tests for this.
| TEST_FILES = 100 | ||
|
|
||
|
|
||
| class _PathLikeObj(object): |
There was a problem hiding this comment.
Use test.support.FakePath.
|
As this pull request is from an 'unknown repository', I'm going to close it. A new pull request can be opened with this change from a valid repository. |
No description provided.