Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Wed, 23 Mar 2016 17:28:45 +1100 Lines: 41 Message-ID: References: <56e44258$0$1598$c3e8da3$5496439d@news.astraweb.com> <8737rvxs89.fsf@elektro.pacujo.net> <56e7483d$0$1608$c3e8da3$5496439d@news.astraweb.com> <56f09973$0$1601$c3e8da3$5496439d@news.astraweb.com> <56F1A071.1000506@gmail.com> <56f23287$0$1607$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de OwnH8VHAHdRW8GYhRfyCiAMUTHt4jojVqAYj5eqFAH8A== 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; 'else:': 0.03; 'received:209.85.223': 0.03; 'exception.': 0.07; 'option,': 0.07; 'rename': 0.07; 'cc:addr:python-list': 0.09; 'check.': 0.09; 'it;': 0.09; 'reliably': 0.09; 'subject:which': 0.09; 'underlying': 0.09; 'unhandled': 0.09; 'wed,': 0.15; '2016': 0.16; '23,': 0.16; 'bug:': 0.16; 'concurrency,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'temp': 0.16; 'wrote:': 0.16; 'exists': 0.18; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'prevent': 0.20; 'fix': 0.21; "aren't": 0.22; 'exceptions': 0.22; 'parameter': 0.22; 'seems': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; "doesn't": 0.26; 'command': 0.26; 'error': 0.27; 'handling': 0.27; 'message-id:@mail.gmail.com': 0.27; 'bad.': 0.29; 'sure,': 0.29; 'handled': 0.29; 'system,': 0.30; 'code': 0.30; 'probably': 0.31; 'generally': 0.32; "d'aprano": 0.33; 'flags': 0.33; 'steven': 0.33; 'case,': 0.34; 'equal': 0.34; 'file': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'best,': 0.35; 'done': 0.35; 'exist': 0.35; 'involving': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'things': 0.38; 'creation': 0.38; 'received:209': 0.38; 'anything': 0.38; 'shared': 0.38; 'skip:o 20': 0.38; 'end': 0.39; 'why': 0.39; 'does': 0.39; 'resources': 0.39; 'some': 0.40; 'easy': 0.60; 'your': 0.60; 'subject:The': 0.61; 'yes': 0.62; 'mar': 0.65; 'worth': 0.67; 'chrisa': 0.84; 'everywhere.': 0.84; 'fortunately,': 0.84; 'to:none': 0.91; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=csN4HDy6NCkbt+07HfuvaV/qBKqilSrqtcUu7WoawpQ=; b=posJnpYiWNmsuYl5hEziujky5eWdT89IZznJn6fqxR085IAV9i3Du0QiMuhBJPDyEf ahpuYdFj6LD0CP99+hiDNppi8w8RE0Cvm0P3Xw2mT4P7zXA4QDymbxhHUF2dsEiBqa29 efnWhsd54kJ1rsbRmtvcuJqeR5zX2hy10nHOLZRpIz7eROeb302QGAj+odWGttM1Qvia EuAAWrN1tBFKr3rn7XFFiT6boQyTpzonrlHGQS9fMqcvZxGnc9zB3Ie769ODe9XYy8ml WbRbgec0aIW6XV+PLRsJfMnG+V0hopI3pnQifHNs73LsfQFN2dg/DyW2SlzkxBSFgTnl LgcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=csN4HDy6NCkbt+07HfuvaV/qBKqilSrqtcUu7WoawpQ=; b=EigcHzX0AKEvco0Sdlp8gvEzWH+FDmtLO5DVuF+GAqorav90koQgesOEdZ7cbly40K hKcPM2u48tlhPeMdLJMWrL3zc97PmHHg1T4JMwtHUKNqfOh8yKENFWp6HTF4i9PFQggI 4PuymaxXqXi2THgx4DStC2iTq8wSHfSH5HMUrxzSzZHUavSe6LKEQk40BGjq7mMkzAyZ 2y8xMow7MssUJmCUzLkjri8uA+5eL7BAexMrrStCBvsV5wa4cpT6xFSfME5hDBy1WDn8 xg+er/Wgki9sOmoKqNNQDaOCK3WWV4mlqwSvaRz2MwRaB56FGw5kdLOeGlmDYjIUWKDT 1x9g== X-Gm-Message-State: AD7BkJI72nchd4fVMEBha6RyQ5Zxq5nwyxJ+s38wZKMeZHWFJ9NxztGspZmNCkhFrPxBJpBS+ftafObxFlZX4g== X-Received: by 10.107.63.139 with SMTP id m133mr1496747ioa.157.1458714525362; Tue, 22 Mar 2016 23:28:45 -0700 (PDT) In-Reply-To: <56f23287$0$1607$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105516 On Wed, Mar 23, 2016 at 5:07 PM, Steven D'Aprano wrote: > Fortunately, not all such "bugs" are of equal severity. In this case, there > are two failure modes. Consider a *false positive* bug: we think the file > exists when it actually doesn't. > > if os.path.exists(filename): > os.unlink(filename) # some other process does this > open(filename) > > > This is probably bad. At best, we get some sort of unhandled exception. Yes - and it's exactly why it's not worth bothering with the 'exists' check. Your code would end up looking like this: if os.path.exists(filename): try: open(filename) except FileNotFoundError: # file got deleted out from under you else: # file doesn't exist Which is exactly why error handling (whether with structured exceptions or error codes) is better than LBYL for anything involving the file system, or shared resources and concurrency, or anything with a potential TOCTTOU discrepancy. All the other situations (apart from renaming) are handled the same way - sure, the consequences aren't as serious, but it's just as easy to use the same technique everywhere. I don't know of a cross-platform way to rename without overwriting, but the "mv" command on my system has a --no-clobber option, and the underlying rename system call seems to have a non-standard flags parameter that can be set to RENAME_NOREPLACE to prevent overwriting. This is generally the way to fix it; it's only in the system call that this can actually be done reliably (hence things like temp file creation are done with flags to the file-open syscall). ChrisA