Skip to content

gh-87646: Make tempfile.NamedTemporaryFile and TemporaryDirectory path-like #114765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Jan 30, 2024

Add __fspath__() methods to tempfile.NamedTemporaryFile and TemporaryDirectory.


📚 Documentation preview 📚: https://cpython-previews--114765.org.readthedocs.build/

…ctory` path-like.

Add `__fspath__()` methods to `tempfile.NamedTemporaryFile` and
`TemporaryDirectory`.
@barneygale barneygale added the type-feature A feature request or enhancement label Jan 30, 2024
Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

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

Some stylistic suggestions and test simplifications.

Comment on lines 1335 to 1339
if hasattr(file, 'read') or hasattr(file, 'write'):
self._filePassed = 1
self.fp = file
self.filename = getattr(file, 'name', None)
else:
Copy link
Member

Choose a reason for hiding this comment

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

What does this have to to with tempfile?

Copy link
Contributor Author

@barneygale barneygale Feb 21, 2024

Choose a reason for hiding this comment

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

When given an argument that is both file-like and path-like, previously zipfile would treat the argument as path-like and attempt to re-open() it. On Windows that caused PermissionError to be raised because the NamedTemporaryFile was already open. This change makes zipfile use the file-like interface in preference to the path-like interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suspect some third-party code will fall into the same trap as zipfile did here, and so perhaps it's not viable to make NamedTemporaryFile path-like.

@bedevere-app
Copy link

bedevere-app bot commented Feb 17, 2024

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@barneygale
Copy link
Contributor Author

I have made the requested changes; please review again.

@bedevere-app
Copy link

bedevere-app bot commented Jun 29, 2024

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from brettcannon June 29, 2024 20:19
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

Maybe this was forgotten about, ready to merge before the freeze in a fortnight?

@hugovk hugovk changed the title GH-87646: Make tempfile.NamedTemporaryFile and TemporaryDirectory path-like gh-87646: Make tempfile.NamedTemporaryFile and TemporaryDirectory path-like Apr 29, 2025
@hugovk hugovk added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label May 2, 2025
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @hugovk for commit fc0ff04 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F114765%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting merge type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants