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

option to leave tempfile.NamedTemporaryFile around on close #43804

Closed
djmdjm mannequin opened this issue Aug 10, 2006 · 8 comments
Closed

option to leave tempfile.NamedTemporaryFile around on close #43804

djmdjm mannequin opened this issue Aug 10, 2006 · 8 comments
Labels
stdlib Python modules in the Lib dir

Comments

@djmdjm
Copy link
Mannequin

djmdjm mannequin commented Aug 10, 2006

BPO 1537850
Nosy @brettcannon, @birkenfeld
Files
  • tempfile.diff: Add optional 'delete' argument to NamedTemporaryFile constructor
  • python-tempfile.diff: Diff with regress test
  • 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 2007-03-13.18:32:06.000>
    created_at = <Date 2006-08-10.06:57:31.000>
    labels = ['library']
    title = 'option to leave tempfile.NamedTemporaryFile around on close'
    updated_at = <Date 2007-03-13.18:32:06.000>
    user = 'https://bugs.python.org/djmdjm'

    bugs.python.org fields:

    activity = <Date 2007-03-13.18:32:06.000>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2006-08-10.06:57:31.000>
    creator = 'djmdjm'
    dependencies = []
    files = ['7456', '7457']
    hgrepos = []
    issue_num = 1537850
    keywords = ['patch']
    message_count = 8.0
    messages = ['50852', '50853', '50854', '50855', '50856', '50857', '50858', '50859']
    nosy_count = 3.0
    nosy_names = ['brett.cannon', 'georg.brandl', 'djmdjm']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1537850'
    versions = ['Python 2.5']

    @djmdjm
    Copy link
    Mannequin Author

    djmdjm mannequin commented Aug 10, 2006

    Hi,

    tempfile.NamedTemporaryFile provides a good interface
    to creating temporary files, but its insistence on
    deleting the file upon close is limiting. The attached
    patch adds an optional parameter to NamedTemporaryFile
    that allows persistence of the temp file after it has
    been closed.

    One use-case that demonstrates where keeping the
    temporary file around is handy would be when you need
    to safely create and fill a temp file before it is
    atomically moved into place with os.rename(). E.g.

    def update_conf(conf_path):
        old = open(conf_path)
        tmp = tempfile.NamedTemporaryFile(prefix =
    os.basename(conf_path), \
            dir = os.dirname(conf_path), delete = False)
        for l in old:
            tmp.write(l.replace('war', 'peace'))
        close(old)
        close(tmp)
        os.link(conf_path, conf_path + ".old")
        os.rename(tmp.name, conf_path)

    @djmdjm djmdjm mannequin closed this as completed Aug 10, 2006
    @djmdjm djmdjm mannequin added the stdlib Python modules in the Lib dir label Aug 10, 2006
    @djmdjm djmdjm mannequin closed this as completed Aug 10, 2006
    @djmdjm djmdjm mannequin added the stdlib Python modules in the Lib dir label Aug 10, 2006
    @djmdjm
    Copy link
    Mannequin Author

    djmdjm mannequin commented Aug 10, 2006

    Logged In: YES
    user_id=1359232

    oops, wrong Category: this should be Lib and not Modules

    @brettcannon
    Copy link
    Member

    Logged In: YES
    user_id=357491

    Why can't you store into an instance of StringIO instead of
    a temp file?

    @djmdjm
    Copy link
    Mannequin Author

    djmdjm mannequin commented Aug 25, 2006

    Logged In: YES
    user_id=1359232

    As far as I can tell, StringIO doesn't actually create a
    filesystem object that can be manipulated.

    @djmdjm
    Copy link
    Mannequin Author

    djmdjm mannequin commented Aug 25, 2006

    Logged In: YES
    user_id=1359232

    Here is an diff that includes a regress test

    @brettcannon
    Copy link
    Member

    Logged In: YES
    user_id=357491

    Right, it doesn't create a filesystem file. But that is the
    point. You work in memory and then write to your final
    destination as needed. Your code you have pasted in the
    description does nothing special that requires the use of a
    temporary file. You can just write into a StringIO object,
    skip the os.link call, and then just write out to the final
    file location.

    @djmdjm
    Copy link
    Mannequin Author

    djmdjm mannequin commented Aug 26, 2006

    Logged In: YES
    user_id=1359232

    Well, that would a) not be an atomic replacement and b) you
    miss (or would have to reimplement) the mkstemp() like
    behaviour.

    @birkenfeld
    Copy link
    Member

    Thanks for the patch, committed with doc changes as rev. 54344.

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

    No branches or pull requests

    2 participants