Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: urlopen, six, and py2 Date: Thu, 3 Mar 2016 01:53:05 +1100 Lines: 48 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de fnuIZrNU9XeqYHZvIpQWKAhKW+apeD4OiwlCUDded3OQ== 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; 'context': 0.05; 'finally:': 0.05; 'raises': 0.07; 'resp': 0.07; 'cc:addr:python- list': 0.09; 'absent': 0.09; 'cleaned': 0.09; 'exception,': 0.09; 'python': 0.10; 'exception': 0.13; 'def': 0.13; 'properly': 0.15; 'thu,': 0.15; '(before': 0.16; '*always*': 0.16; '2016': 0.16; 'clear.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'resp.close()': 0.16; 'resp:': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fairly': 0.22; "aren't": 0.22; 'am,': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'cases.': 0.29; 'code': 0.30; 'normally': 0.30; 'agreed': 0.31; 'except': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'closing': 0.36; 'depends': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'received:209.85.213': 0.37; "won't": 0.38; 'received:209': 0.38; 'why': 0.39; 'goes': 0.39; 'sure': 0.39; 'resources': 0.39; 'skip:n 10': 0.62; 'leaving': 0.63; 'here:': 0.63; 'mar': 0.65; 'chrisa': 0.84; 'compact,': 0.84; 'lying': 0.84; 'subject:six': 0.84; 'to:none': 0.91; 'disposal': 0.91; 'anymore,': 0.95 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=nj9RfUZdZLi5IVgghFbzPFhXgxub48DHgOC3PUlm//A=; b=UOQOM+sIqckxSZqnTZlt31kbL/PTVU6DNcnJSg5FoKbcTe59RtNi9PCg20gzAY1GCS 2Z7fMP4/trAQ7RJShQB6ENLGTQiFgsgj3KBuz/+5FIhrDpCieQnk5fDEY4RcJaDDZ21r bWx35fKuum5z+tWbiJS5Ok4DeFBqlu41VLSTZ+GxoS0/GdsoJONBNLwwl6dpySXh9dJa WWTT/l5z4cXKf0r+fYKh/FH2h7xuahCXuHbVhmFuvHWHKpQtWTUaviGbdRD5RJ971Cy8 wJ15fusJWqMaXq+gm1KjLyktdzEP1z8rvq93r+1QgYkKOpc2CbiSwc+pkz6QS6fzLhdT ZXIw== 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=nj9RfUZdZLi5IVgghFbzPFhXgxub48DHgOC3PUlm//A=; b=NY2VrEzvX6kiF/uhPPjbUCyEpmtRDfX7UNiK2GKl1PAQxTbtgRIF0zPtYCJQrDP3vR SARcSKm0rLmKl36AFSvM2rRI1UZ0aFwae9Oeaf5D3tMN9u7y+KY9ZoEyHxbUXHZgJGqh xVYe4gQQeVkf7e7tBwUqYDsdiOoKaeRVBHzOIcLMtxo/HZ1bHmtOGkDSvJ3JAm22K3u0 IdX7w/MXBM7KtJMm5YN+didoPhfC8oU2C8PxAHO2axtq96loCnNr8cvvDTuo/BEAWQLY vJYktlGAgi1eJwcX/wI2JP8N1iso/s3hx2sRMzcQVc2Y9I8KtTMcDs0xoaz0TYpAW+N4 t7Fw== X-Gm-Message-State: AD7BkJKfz6KZxjSh6kXHgqoJawUMIhf6oPjFHJCQyuGh9ZBwqx9fWjl+AqgcBTZWRKwzvrHzdfMZRxgZn43ssg== X-Received: by 10.50.57.11 with SMTP id e11mr330177igq.13.1456930385306; Wed, 02 Mar 2016 06:53:05 -0800 (PST) In-Reply-To: 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:103873 On Thu, Mar 3, 2016 at 1:35 AM, Matt Wheeler wrote: >> from six.moves.urllib.request import urlopen >> >> try: >> with urlopen('http://www.google.com') as resp: >> _ = resp.read() >> except AttributeError: >> # python 2 >> resp = urlopen('http://www.google.com') >> _ = resp.read() > > This is poor practise as you aren't closing "resp". > This leaves state lying around that you don't need anymore, which is > the whole purpose of the context manager that 3 provides. > It will *usually* be cleaned up when leaving the current scope, but > won't if there is an exception thrown. Using the context manager > ensures resp is *always* cleaned up properly even if an exception is > thrown. Not sure why you say it won't if there's an exception thrown. This code will do the same thing on both versions: def get_data(): resp = urlopen('http://www.google.com') return resp.read() Absent the context manager, this depends on object disposal for its cleanup. But whether this function returns normally or raises an exception, the response object goes out of scope at the same time. There's no guarantee that it'll be cleaned up immediately when the function exits, but it's the same for the exceptional and non-exceptional cases. Agreed that try/finally is the best way to do cross-platform code here: def get_data(): resp = urlopen('http://www.google.com') try: return resp.read() finally: resp.close() It's reasonably compact, and fairly clear. And it has the exact guarantee that the context manager has (before the function exits, the 'finally' block will be performed, and resources will be properly released). ChrisA