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

Making tempfile.NamedTemporaryFile a class #66581

Open
anntzer mannequin opened this issue Sep 11, 2014 · 5 comments
Open

Making tempfile.NamedTemporaryFile a class #66581

anntzer mannequin opened this issue Sep 11, 2014 · 5 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@anntzer
Copy link
Mannequin

anntzer mannequin commented Sep 11, 2014

BPO 22387
Nosy @birkenfeld, @ncoghlan, @ericvsmith, @serhiy-storchaka, @anntzer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2014-09-11.08:15:45.285>
labels = ['type-feature', 'library']
title = 'Making tempfile.NamedTemporaryFile a class'
updated_at = <Date 2014-09-11.15:49:10.704>
user = 'https://github.com/anntzer'

bugs.python.org fields:

activity = <Date 2014-09-11.15:49:10.704>
actor = 'Antony.Lee'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2014-09-11.08:15:45.285>
creator = 'Antony.Lee'
dependencies = []
files = []
hgrepos = []
issue_num = 22387
keywords = []
message_count = 5.0
messages = ['226752', '226756', '226758', '226766', '226783']
nosy_count = 5.0
nosy_names = ['georg.brandl', 'ncoghlan', 'eric.smith', 'serhiy.storchaka', 'Antony.Lee']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue22387'
versions = ['Python 3.5']

@anntzer
Copy link
Mannequin Author

anntzer mannequin commented Sep 11, 2014

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

@anntzer anntzer mannequin added the stdlib Python modules in the Lib dir label Sep 11, 2014
@ericvsmith
Copy link
Member

Can you explain why you want to subclass them?

@ericvsmith ericvsmith added the type-feature A feature request or enhancement label Sep 11, 2014
@anntzer
Copy link
Mannequin Author

anntzer mannequin commented Sep 11, 2014

The initial idea was to solve bpo-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).

@serhiy-storchaka
Copy link
Member

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

@anntzer
Copy link
Mannequin Author

anntzer mannequin commented Sep 11, 2014

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

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: No status
Development

No branches or pull requests

2 participants