Skip to content

bpo-29447: tempfile: Add pathlike object support for dir argument#1496

Closed
louisom wants to merge 11 commits into
python:masterfrom
louisom:bpo-29447
Closed

bpo-29447: tempfile: Add pathlike object support for dir argument#1496
louisom wants to merge 11 commits into
python:masterfrom
louisom:bpo-29447

Conversation

@louisom

@louisom louisom commented May 8, 2017

Copy link
Copy Markdown
Contributor

No description provided.

@mention-bot

Copy link
Copy Markdown

@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.

@louisom louisom changed the title Add pathlike object support bpo-29947: Add pathlike object support May 8, 2017
@louisom louisom changed the title bpo-29947: Add pathlike object support bpo-29447: Add pathlike object support May 8, 2017

@brettcannon brettcannon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add yourself to Misc/ACKS.

Comment thread Doc/library/tempfile.rst Outdated

The :py:data:`os.O_TMPFILE` flag is now used if available.

.. versionchanged:: 3.7

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Doc/library/tempfile.rst Outdated

.. versionchanged:: 3.7

The ``dir`` argument is now accept a :term:`path-like object`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The *dir* argument now accepts a :term:`path-like object`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert the docs change for now.

Comment thread Lib/test/test_tempfile.py Outdated
def test_choose_pathlike_directory(self):
# _mkstemp_inner can create files in a user-selected pathlike directory
dir = pathlib.Path(tempfile.mkdtemp())
try:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread Misc/NEWS Outdated
- 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the NEWS entry.

Comment thread Lib/test/test_tempfile.py Outdated
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('')))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are test path-like classes in other test files. The code can be shared.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find other place test path-like with bytes. Add some test here, and at low-level internals test

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Search def __fspath__ in tests.

Comment thread Lib/test/test_tempfile.py
self.do_create(dir=".")
self.do_create(dir=pathlib.Path("."))

def test_basic_with_bytes_names(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test with a path-like object that returns bytes.

@brettcannon brettcannon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Lib/tempfile.py Outdated
return_type = None
for arg in args:
if arg is None:
if arg is None or isinstance(arg, _os.PathLike):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? Can't you simply call _os.fspath(arg) just before if isinstance(arg, bytes)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@serhiy-storchaka sounds reasonable to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.fspath() returns str or bytes.

@louisom louisom May 10, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example DirEntry.__fspath__ can return bytes.

Comment thread Lib/tempfile.py Outdated
dir = gettempdir()
else:
dir = gettempdirb()
if isinstance(dir, _os.PathLike):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the encoding call suddenly necessary when it wasn't needed for dir previously?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Lib/tempfile.py
for arg in args:
if arg is None:
continue
elif isinstance(arg, _os.PathLike):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this explicit test is needed? Aren't high-level function fail in any case when pass path-like object as prefix or suffix?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lulouie No, path-like objects are only useful for things that represent paths. Both prefix and suffix are not paths themselves.

@louisom louisom changed the title bpo-29447: Add pathlike object support bpo-29447: tempfile: Add pathlike object support for dir argument May 10, 2017
@brettcannon

Copy link
Copy Markdown
Member

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, I have made the requested changes; please review again. That will trigger a bot to flag this pull request as ready for a follow-up review.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path-like objects shouldn't be accepted as prefix or suffix. There should be tests for this.

Comment thread Lib/test/test_tempfile.py
TEST_FILES = 100


class _PathLikeObj(object):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use test.support.FakePath.

@csabella

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants