Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; '21,': 0.07; 'context': 0.07; 'finally:': 0.07; 'lawrence': 0.09; 'subject:method': 0.09; 'try:': 0.09; 'api': 0.11; 'clauses.': 0.16; 'connect.': 0.16; 'corrupt': 0.16; 'lite': 0.16; 'oserror': 0.16; 'exception': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'bit': 0.19; 'feb': 0.22; 'import': 0.22; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'raise': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'away.': 0.31; 'raised': 0.31; 'way?': 0.31; 'file': 0.32; 'level.': 0.33; 'subject:the': 0.34; 'could': 0.34; 'subject:with': 0.35; 'connection': 0.35; 'except': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'being': 0.38; 'manager': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'soon': 0.63; 'note:': 0.66; 'due': 0.66; 'close': 0.67; '2015': 0.84; 'needed:': 0.84; 'subject:try': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=AWuMtWUrUbe4q9dxjXmVpPT17n/qKsvPipoW+5kUhx8=; b=hYYmR/ixMp4BRVCLk8eWmKl9cDTW9M194Z7UzN24jMVZ88sDtJnh6udG6gbOzYd2Fc I34N5NdZqYppV7OLyedxx2D8A62y+spQwsBGswxAPUiVTp6NMCGUxj+pr7hlJ36eF2zF kNY7h9NZTeMaAeWmGMkvsgn/c2cb4Ud/whviYaefWhg7SAWgEcRSCgK/qiUG1pxrMFaF 9XaMkaYbRc+Afd4jC0BZDT4FEAE6Ul/TFIEqT8Hz/xBgh7+wHbXkkLwJLZytZVdet9yf guCxz8kp5thfYDCvjAed3V5/Ras+3dgoAq2hEXbox9+Q8aACf1JZil7dliS7vDcKCEwt NolA== X-Received: by 10.70.30.3 with SMTP id o3mr5018998pdh.114.1424531828677; Sat, 21 Feb 2015 07:17:08 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> From: Ian Kelly Date: Sat, 21 Feb 2015 08:16:28 -0700 Subject: Re: try pattern for database connection with the close method To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424531832 news.xs4all.nl 2855 [2001:888:2000:d::a6]:46095 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86046 On Sat, Feb 21, 2015 at 5:22 AM, Mark Lawrence wrote: > On 21/02/2015 02:42, Mario Figueiredo wrote: >> >> Hello all, >> >> I'm using the following pattern for db access that requires me to >> close the connection as soon as it is not needed: >> >> import sqlite3 as lite >> >> try: >> db = lite.connect('data.db') >> except lite.DatabaseError: >> raise OSError('database file corrupt or not found.') >> else: >> try: >> with db: >> db.execute(sql, parms) >> except lite.IntegrityError: >> raise ValueError('invalid data') >> finally: >> db.close() >> >> Since it's a bit verbose, is there a better way? >> >> Note: The user of this API has the whole database functionality >> abstracted away. Hence the exception channeling in the except clauses. >> > > Use your context manager at the outer level. > > import sqlite3 as lite > > try: > with lite.connect('data.db') as db: > try: > db.execute(sql, parms) > except lite.IntegrityError: > raise ValueError('invalid data') > except lite.DatabaseError: > raise OSError('database file corrupt or not found.') This could result in the OSError being misleadingly raised due to some DatabaseError raised by the execute rather than the connect.