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

Equivalent to tempfile.NamedTemporaryFile that deletes file at context exit #58719

Closed
bitdancer opened this issue Apr 6, 2012 · 4 comments
Closed
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@bitdancer
Copy link
Member

BPO 14514
Nosy @ncoghlan, @ericvsmith, @bitdancer
Superseder
  • bpo-14243: tempfile.NamedTemporaryFile not particularly useful on Windows
  • 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 = <Date 2012-04-10.01:31:35.402>
    created_at = <Date 2012-04-06.02:00:28.526>
    labels = ['type-feature', 'library']
    title = 'Equivalent to tempfile.NamedTemporaryFile that deletes file at context exit'
    updated_at = <Date 2012-04-10.01:31:35.396>
    user = 'https://github.com/bitdancer'

    bugs.python.org fields:

    activity = <Date 2012-04-10.01:31:35.396>
    actor = 'ncoghlan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-04-10.01:31:35.402>
    closer = 'ncoghlan'
    components = ['Library (Lib)']
    creation = <Date 2012-04-06.02:00:28.526>
    creator = 'r.david.murray'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 14514
    keywords = []
    message_count = 4.0
    messages = ['157634', '157637', '157638', '157926']
    nosy_count = 3.0
    nosy_names = ['ncoghlan', 'eric.smith', 'r.david.murray']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '14243'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue14514'
    versions = ['Python 3.3']

    @bitdancer
    Copy link
    Member Author

    A common pattern (especially in writing tests) is to create a named temporary file, operate on it with tools that take the filename, and then delete the file. This pattern would be facilitated by a version of NamedTemporaryFile that deleted the named file at the end of the context, but allowed the file to continue to exist when closed. This latter requirement arises because the file cannot be opened a second time on Windows, and so needs to be closed before other operations that open it can be performed in order for the code to work cross platform.

    It's tempting just to change delete=True to mean delete on exit (not close) if and only if NamedTemporaryFile is being used as a context manager, but there might be backward compatibility issues with that. It might be acceptable as a feature change, though, since the likelyhood of someone closing such a tempfile inside the context and then depending on it *not* existing between then and the end of the context seems low.

    If that it deemed unacceptable, this could be implemented as a new flag on NamedTemporaryFile, or by changing the delete flag to take additional values (True/False == current behavior, but add delete='onclose', delete='onexit', delete='no', and document True/False as deprecated.)

    @bitdancer bitdancer added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Apr 6, 2012
    @ericvsmith
    Copy link
    Member

    See also bpo-14243.

    @bitdancer
    Copy link
    Member Author

    Grr. I did a search first, and even used google because I know search is currently broken, and I did not find that issue.

    @ncoghlan
    Copy link
    Contributor

    I converted issue bpo-14243 to a feature request, so this is now a duplicate.

    @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
    None yet
    Development

    No branches or pull requests

    3 participants