Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Modern recommended exception handling practices? Date: Tue, 3 Nov 2015 18:52:27 +1100 Lines: 60 Message-ID: References: <52739457-5f7a-48ea-8835-9fc8934174f9@googlegroups.com> <56385887$0$1598$c3e8da3$5496439d@news.astraweb.com> <563860b9$0$1589$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de fd016j1ed7cllGTEUW37QQhTPDvEFoiDKIXIJd4E53dA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'context': 0.05; 'exception.': 0.07; 'method,': 0.07; 'cc:addr:python-list': 0.09; 'garbage': 0.09; 'script,': 0.09; 'way:': 0.09; 'python': 0.10; 'def': 0.13; "'if',": 0.16; '__del__': 0.16; 'broken:': 0.16; 'cheap.': 0.16; 'exceptions.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'gc.collect()': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'renamed': 0.16; 'subject:exception': 0.16; 'subject:handling': 0.16; 'threads': 0.16; "{'foo':": 0.16; 'wrote:': 0.16; 'deleted.': 0.18; 'try:': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'keyerror:': 0.22; 'suppose': 0.22; 'pass': 0.22; 'code,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'checking': 0.27; 'switch': 0.27; 'message-id:@mail.gmail.com': 0.27; 'too.': 0.30; 'code': 0.30; "can't": 0.32; 'skip:_ 10': 0.32; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'execution': 0.35; 'nov': 0.35; 'comment': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'expect': 0.37; "won't": 0.38; 'received:209': 0.38; 'names': 0.38; 'skip:p 20': 0.38; 'sure': 0.39; 'rather': 0.39; 'called': 0.40; 'collection': 0.60; 'your': 0.60; "you'll": 0.61; 'between': 0.65; 'benefit': 0.66; 'worth': 0.67; '100%': 0.72; 'chrisa': 0.84; 'dict,': 0.84; 'here!': 0.84; 'subject:recommended': 0.84; 'to:none': 0.91 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 :content-type; bh=t9dWZn5VF9TqtMr+V9vrWG1MgoR5XUJdALsS03faOlE=; b=Jp9LLbNEc1pFz4QnrpRtPKhC5aNCYx1gDrLDhlCWfATn+InLtC0iM5rEtC02YGuPjc fnjJS5uZymO/HTnex2tAC1lJU5UShwhTmnirLkHKzWBamigQt7w5feMBdOdtm+browFv JpAA9rjXl+Rk8U+5rpaA2oHYzzqMsecpKQUaAzWfQvqAc58HvPWAAj9TQHZqY65oPRPz KDq40rPxCZM0ZOrnX5xCpwiFpgpyyQSd09VrmC3wLKpVipvHXwLHKrkWSTr1CCdsveFH 557+1InsRifXYg/i2TxyW2aRFDBteGjkdSgkDIiRc3CKWL7E1TSuecyWm+vPfBETOt0W qW/w== X-Received: by 10.107.10.210 with SMTP id 79mr26560103iok.31.1446537147555; Mon, 02 Nov 2015 23:52:27 -0800 (PST) In-Reply-To: <563860b9$0$1589$c3e8da3$5496439d@news.astraweb.com> 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: , Xref: csiph.com comp.lang.python:98139 On Tue, Nov 3, 2015 at 6:22 PM, Steven D'Aprano wrote: > A lot can happen in the few microseconds between > checking for the existence of the file and actually opening it -- the file > could be renamed or deleted. And a lot of microseconds can happen between two opcodes, too. Even inside a Python script, it's possible for threads or other arbitrary code execution to get in your way: if "foo" in counters: # context switch here! process(counters["foo"]) Garbage collection can happen at any time. Here's an (admittedly arbitrary) example of how the above could be broken: >>> class X: ... def __init__(self, name, dict): ... self.dict = dict; self.name = name ... self.dict[self.name] = 0 ... def frob(self): ... self.dict[self.name] += 1 ... def __del__(self): ... del self.dict[self.name] ... >>> counters = {} >>> x = X("foo", counters) >>> x.refcycle = x >>> counters {'foo': 0} >>> del x >>> counters {'foo': 0} >>> gc.collect() 10 >>> counters {} If the cycle-detecting garbage collector happens to be called immediately after the 'if', you'll get an exception. So I suppose what you might do is this: try: # Optimization: Since a lot of these names won't be # in the dict, we check first rather than relying on the # exception. Since counters get removed in the __del__ # method, we can't depend 100% on the 'in' check, # but an unnecessary try block is cheap. if "foo" in counters: process(counters["foo"]) except KeyError: pass But any time you need a block comment to justify your code, you'd better be REALLY sure the performance benefit is worth the complexity. For reliability, expect exceptions. ChrisA