This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Making tempfile.NamedTemporaryFile a class
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Antony.Lee, eric.smith, georg.brandl, ncoghlan, serhiy.storchaka
Priority: normal Keywords:

Created on 2014-09-11 08:15 by Antony.Lee, last changed 2022-04-11 14:58 by admin.

Messages (5)
msg226752 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-09-11 08:15
Currently, tempfile.TemporaryFile and tempfile.NamedTemporaryFile are functions, not classes, despite what their names suggest, preventing subclassing.  It would arguably be not so easy to make TemporaryFile a class, as its return value is whatever "_io.open" returns, which can be of various types, but NamedTemporaryFile can trivially converted into a class by reusing the body of _TemporaryFileWrapper (which is not used elsewhere).
msg226756 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2014-09-11 08:35
Can you explain why you want to subclass them?
msg226758 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-09-11 09:24
The initial idea was to solve #14243 (NamedTemporaryFile would be more useful on Windows if you could close it without deleting) by adding a "closed" keyword argument to the constructor of a subclass, that would set "delete" to False and then close it, e.g.

class NTF(NamedTemporaryFile):
    def __init__(self, *args, closed=False, **kwargs):
        if closed: kwargs["delete"] = True
        super().__init__(*args, **kwargs)
        if closed: self.close()

Actually, there are some not-so-nice interactions with the context-manager protocol though, as the file cannot be reopened.  So it's not clear that this is such a good idea.

Still, it somewhat confusing that a CamelCase object is not actually a type (especially when TemporaryDirectory is one).
msg226766 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-09-11 12:19
You can do this with a function too:

    def NTF(*args, closed=False, **kwargs):
        if closed: kwargs["delete"] = True
        ntf = NamedTemporaryFile(*args, **kwargs)
        if closed: ntf.close()
        return ntf
msg226783 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-09-11 15:49
Yes, but this will make the context-manager approach (with NamedTemporaryFile(closed=True): <do stuff>) unusable, because the underlying file object will be closed before calling __enter__.  I think the only reasonable way to implement this would be to have __enter__ return the file name (as for TemporaryDirectory), instead of the file object (which is reasonable because if I pass closed=True, it probably means all I care is that a file exists here for some other process to use!).
But, with the function approach, I cannot override __enter__.
History
Date User Action Args
2022-04-11 14:58:07adminsetgithub: 66581
2014-09-11 15:49:10Antony.Leesetmessages: + msg226783
2014-09-11 12:19:39serhiy.storchakasetnosy: + serhiy.storchaka, georg.brandl, ncoghlan
messages: + msg226766
2014-09-11 09:24:08Antony.Leesetmessages: + msg226758
2014-09-11 08:35:46eric.smithsettype: enhancement

messages: + msg226756
nosy: + eric.smith
2014-09-11 08:15:45Antony.Leecreate