Skip to content
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

bpo-14243: Optionally delete NamedTemporaryFile on content manager exit but not on file close #22431

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

Conversation

Ev2geny
Copy link

@Ev2geny Ev2geny commented Sep 27, 2020

This fixes this issue: https://bugs.python.org/issue14243

tempfile.NamedTemporaryFile is too hard to use portably when you need to open the file by name after writing it. To do that, you need to close the file first (on Windows), which means you have to pass delete=False, which in turn means that you get no help in cleaning up the actual file resource,

Hence at the moment there is no out of the box solution to use tempfile.NamedTemporaryFile on Windows in such scenario (which is often used in unit testing):

  • in test module:
  1. create and open temporary file
  2. write data to it
  3. pass name of the temporary file to the operational code
  • In operational code, being tested
  1. open file, using name of the temporary file
  2. read data from this temporary file

In this Pull Request the issue is solved by adding an additional optional argument to NamedTemporaryFile() 'delete_on_close' (default is True). It works in combination with already existing argument 'delete', and determines whether created temporary file gets deleted on close (which until this change was the only functionality available and it remains default now).

If 'delete' = True and 'delete_on_close' = False then temporary file gets deleted on context manager exit only (which is a new functionality).

If 'delete' is not true, then value of 'delete_on_close' is ignored.

So, the change shall be fully backwards compatible.

In the bug discussion such option was discussed by r.david.murray but mainly by eryksun

I have also added several unit tests to test new functionality in different combinations

https://bugs.python.org/issue14243

@the-knights-who-say-ni
Copy link

@the-knights-who-say-ni the-knights-who-say-ni commented Sep 27, 2020

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@Ev2geny

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@Ev2geny Ev2geny changed the title bro-14243: Optionally close NamedTemporaryFile on content manager exit and not on close bro-14243: Optionally close NamedTemporaryFile on content manager exit and not on file close Sep 27, 2020
@Ev2geny Ev2geny changed the title bro-14243: Optionally close NamedTemporaryFile on content manager exit and not on file close bro-14243: Optionally delete NamedTemporaryFile on content manager exit and not on file close Sep 28, 2020
@Ev2geny Ev2geny changed the title bro-14243: Optionally delete NamedTemporaryFile on content manager exit and not on file close bro-14243: tempfile.NamedTemporaryFile not particularly useful on Windows Sep 28, 2020
@Ev2geny
Copy link
Author

@Ev2geny Ev2geny commented Sep 28, 2020

I just can't figure out why bedevere/issue-number check is failing. Where do I have to mention it?

@Ev2geny Ev2geny changed the title bro-14243: tempfile.NamedTemporaryFile not particularly useful on Windows bro-14243: Optionally delete NamedTemporaryFile on content manager exit but not on file close Sep 28, 2020
@blais
Copy link
Contributor

@blais blais commented Oct 11, 2020

Hmm, I'm not sure I like the API in this solution. If I want to write portable code (Linux and Windows), I would then have to use delete=True and delete_on_close=False in every since call, right? This is going to look at bit convoluted. I'd much rather this was a distinct type of NamedTemporaryFile.

@Ev2geny
Copy link
Author

@Ev2geny Ev2geny commented Oct 12, 2020

Hmm, I'm not sure I like the API in this solution. If I want to write portable code (Linux and Windows), I would then have to use delete=True and delete_on_close=False in every since call, right? This is going to look at bit convoluted. I'd much rather this was a distinct type of NamedTemporaryFile.

@blais , yes, this PR assumes, you would need to use delete_on_close=False (delete=True is already a default), if you want to write a portable code (Linux and Windows), but only if you need to refer to a created temporary file by its name, not by a file object. And this is a new functionality all together, as current functionality of the NamedTemporaryFile implicitly assumes, that you need to refer to a file by its file object (at least on Windows). Though I agree with you, that in test modules, one needs to often refer to temporary file by its name (hence the PR).

There were a lot of suggestions on how to fix this in the original issue14243 discussion with no clear conclusion on what is the best approach. I have chosen this one as it is backwards compatible and does not change philosophy of existing code. I will wait for code reviews to provide their feedback. There must be someone, who takes a final decision on this.

@blais
Copy link
Contributor

@blais blais commented Oct 13, 2020

It would be wiser to create an alternative interface for this that is safe to use between platforms by default, even if it is more constrained, even if that means providing a completely different context manager.

@bozhinov
Copy link

@bozhinov bozhinov commented Oct 18, 2020

OK. So I read the comments in the bug and the workaround here.
Here is what I did though (on Windows) - Created a temporary directory - put my temp file in there - script ends - all is gone
Hope that helps

@encukou encukou changed the title bro-14243: Optionally delete NamedTemporaryFile on content manager exit but not on file close bpo-14243: Optionally delete NamedTemporaryFile on content manager exit but not on file close Dec 14, 2020
@willingc
Copy link
Sponsor Contributor

@willingc willingc commented Dec 15, 2020

@zooba Do you have time to take a look at this PR?

@Ev2geny
Copy link
Author

@Ev2geny Ev2geny commented Jan 13, 2021

@zooba , just checking whether you managed to look at this

@MaxwellDupre
Copy link
Contributor

@MaxwellDupre MaxwellDupre commented May 21, 2022

Can you re-target for 3.12 (Doc/whatsnew/3.10.rst) please?
Also, in NEWS I don't think you need double back ticks (``).

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented May 21, 2022

Also, in NEWS I don't think you need double back ticks (``).

@MaxwellDupre this is incorrect. Double backticks are usually preferred over single backticks in .rst files, unless a specific ReST role is being used.

Please read https://devguide.python.org/documenting/ for a full guide on ReST syntax.

@kumaraditya303 kumaraditya303 requested review from zooba and eryksun May 21, 2022
Copy link
Member

@zooba zooba left a comment

Code change looks good, but we need to get the docs in order.

Lib/test/test_tempfile.py Outdated Show resolved Hide resolved
@@ -0,0 +1,16 @@
At the moment ``NamedTemporaryFile`` is too hard to use portably when you
Copy link
Member

@zooba zooba May 23, 2022

Choose a reason for hiding this comment

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

This is way more details than we want here - this will end up in a list with 1000 other changes.

Most of this should move to specific documentation, and then this entry can link to it with :class:`NamedTemporaryFile`

Copy link
Author

@Ev2geny Ev2geny May 29, 2022

Choose a reason for hiding this comment

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

This is fixed. The spesific documentation is updated and linked to with :class:tempfile.NamedTemporaryFile``

@@ -93,6 +93,8 @@ Other Language Changes
:meth:`~object.__index__` method).
(Contributed by Serhiy Storchaka in :issue:`37999`.)

* XXX Describe changes to NamedTemporaryFile functionality. Re 2020-09-28-04-56-04.bpo-14243.YECnxv.rst
Copy link
Member

@zooba zooba May 23, 2022

Choose a reason for hiding this comment

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

Needs to move to 3.12.rst, and can be about half the length of what's currently in the NEWS file

Copy link
Author

@Ev2geny Ev2geny May 29, 2022

Choose a reason for hiding this comment

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

Implemented. Whats new is moved to 3.12.rst and only 2 lines now

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented May 23, 2022

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Commiting changes, suggested by @zooba

Co-authored-by: Steve Dower <steve.dower@microsoft.com>
@Ev2geny
Copy link
Author

@Ev2geny Ev2geny commented May 29, 2022

I have made the requested changes; please review again

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented May 29, 2022

Thanks for making the requested changes!

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

@bedevere-bot bedevere-bot requested a review from zooba May 29, 2022
Doc/library/tempfile.rst Outdated Show resolved Hide resolved
Doc/whatsnew/3.12.rst Outdated Show resolved Hide resolved
Ev2geny and others added 2 commits May 30, 2022
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
zooba
zooba approved these changes May 30, 2022
Doc/whatsnew/3.12.rst Outdated Show resolved Hide resolved
Doc/library/tempfile.rst Outdated Show resolved Hide resolved
Ev2geny and others added 4 commits May 31, 2022
changing issue number to github issue number in documentation

Co-authored-by: Steve Dower <steve.dower@microsoft.com>
@Ev2geny Ev2geny requested review from eryksun and AA-Turner May 31, 2022
@Ev2geny
Copy link
Author

@Ev2geny Ev2geny commented Jun 7, 2022

@eryksun , @AA-Turner , I have implemented your comments, thank you! Can you please review the final version once again?

Doc/whatsnew/3.12.rst Outdated Show resolved Hide resolved
Wrap lines at 70-80 chars in the Doc/whatsnew/3.12.rst

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
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.

None yet