Path: csiph.com!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'repository': 0.05; 'url:pipermail': 0.05; 'cc:addr:python-list': 0.09; 'excitement': 0.09; 'mercurial': 0.09; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'subject:Does': 0.09; 'subject:don': 0.09; 'subject:files': 0.09; "system's": 0.09; 'thread': 0.10; 'assume': 0.11; 'files.': 0.13; 'wed,': 0.15; '>on': 0.16; 'cc:addr:lac': 0.16; 'cc:addr:openend.se': 0.16; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'inserting': 0.16; 'message- id:@fido.openend.se': 0.16; 'presume': 0.16; 'received:fido': 0.16; 'received:fido.openend.se': 0.16; 'subject:already': 0.16; 'temp': 0.16; 'url:linux': 0.16; 'wrote:': 0.16; 'conjunction': 0.18; 'laura': 0.18; 'written,': 0.18; 'windows': 0.20; '2015': 0.20; 'cc:addr:python.org': 0.20; 'handles': 0.20; 'cc:2**1': 0.22; 'see:': 0.22; 'sep': 0.22; 'cc:no real name:2**0': 0.22; 'am,': 0.23; 'seems': 0.23; 'forgot': 0.23; 'discussion': 0.24; "doesn't": 0.26; 'chris': 0.26; 'complain': 0.29; 'directly,': 0.29; 'exists,': 0.29; 'received:se': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'somebody': 0.30; 'another': 0.32; 'problem': 0.33; 'url:python': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'url:listinfo': 0.34; 'file': 0.34; 'that,': 0.34; 'list': 0.34; 'skip:> 10': 0.35; 'but': 0.36; 'instead': 0.36; 'url:org': 0.36; 'depends': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'expect': 0.37; 'support,': 0.37; 'thought': 0.37; 'charset :us-ascii': 0.37; 'wanted': 0.37; 'things': 0.38; 'doing': 0.38; 'mailing': 0.38; 'sure': 0.39; "didn't": 0.39; 'url:mail': 0.40; 'details,': 0.60; 'url:3': 0.60; 'chance': 0.60; 'your': 0.60; 'header:Message-Id:1': 0.61; 'back': 0.62; 'different': 0.63; 'safe': 0.63; 'here:': 0.63; 'you.': 0.64; 'due': 0.65; 'guaranteed': 0.67; 'virus': 0.68; '+1000,': 0.84; 'flag.': 0.84; 'header:In-reply-to:1': 0.84 To: Chris Angelico cc: "python-list@python.org" , lac@openend.se From: Laura Creighton Subject: Re: Does mkstemp open files only if they don't already exist? In-reply-to: References: <55e5b9ec$0$1649$c3e8da3$5496439d@news.astraweb.com> Comments: In-reply-to Chris Angelico message dated "Wed, 02 Sep 2015 00:57:45 +1000." MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <10936.1441127350.1@fido> Date: Tue, 01 Sep 2015 19:09:10 +0200 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [82.96.5.2]); Tue, 01 Sep 2015 19:09:17 +0200 (CEST) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441127373 news.xs4all.nl 23735 [2001:888:2000:d::a6]:48802 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95838 In a message of Wed, 02 Sep 2015 00:57:45 +1000, Chris Angelico writes: >On Wed, Sep 2, 2015 at 12:45 AM, Steven D'Aprano wrote: >> I assume the answer is "Yes", but is it safe to expect that >> tempfile.mkstemp() will only create a file that doesn't already exist? I >> presume that there's no chance of it over-writing an existing file (say, >> due to a race-condition). > >It depends on OS support, but with that, yes, it is guaranteed to be >safe; the file is opened with an exclusivity flag. Check your system's >man pages for details, or here: > >http://linux.die.net/man/3/open > >O_EXCL|O_CREATE makes an "atomic file creation" operation which will >fail if another process is doing the same thing. I'm not sure how >mkstemp() handles that failure, but my guess/expectation is that it >would pick a different file name and try again. > >ChrisA >-- >https://mail.python.org/mailman/listinfo/python-list I remember discussion on the mercurial mailing list about somebody who had a problem with this in conjunction with a virus scanner that really wanted to get in and do things to the poor temp files. see: https://selenic.com/pipermail/mercurial-devel/2009-February/010197.html for a very, very long thread about it. Apparantly, you can get windows to complain that you cannot create a file because it already exists, and it doesn't go back and try this again for you. But at the time I found the discussion puzzling, as my thought was 'why are these people using mkstemp directly, instead of tempfile.NamedTemporaryFile which seems to be what they want. But I found this thread looking for a different problem with a mercurial repository that we had that was corrupted, a year or so after the thread was written, so I didn't want to go back and ask them about it _then_. Then in the general excitement --"Your raid system is buggy! It is inserting sludge in your files!" I forgot about this puzzlement. Until now. Laura