Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.051 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'model,': 0.05; 'happens.': 0.09; 'postgresql,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'changes': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'occurs.': 0.16; 'storing': 0.16; 'throw': 0.16; 'undo': 0.16; 'applies': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'all,': 0.19; 'thu,': 0.19; 'work,': 0.20; 'cc:addr:python.org': 0.22; 'fairly': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'leave': 0.29; 'database,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'block,': 0.31; 'second,': 0.31; 'there.': 0.32; 'probably': 0.32; 'stuff': 0.32; "we're": 0.32; "can't": 0.35; 'connection': 0.35; 'case,': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'done': 0.36; 'pm,': 0.38; 'sure': 0.39; 'even': 0.60; 'easy': 0.60; 'tell': 0.60; 'affect': 0.61; "you're": 0.61; 'first': 0.61; 'back': 0.62; "you've": 0.63; 'here': 0.66; 'facilities': 0.69; 'you:': 0.81; '"hey,': 0.84; "'with'": 0.84; '2015': 0.84; 'are)': 0.84; 'itself?': 0.84; 'to:none': 0.92; 'serious': 0.97 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=LvZbQlinvHcw6cPnBAi7V/SMernARgJvbjXBgYvrzqc=; b=ntVE72KARqp/wPzZ/lOzOkYLwe2wrODhMnc1LTX/hUfO0VXNaSrxROdyr9RNgrGKAL cbp/gi1hykUBcslwdFV5hhGn3NoY5vYFbuCwYfadQDBwuH7P8Rl5Xq/wkVojN8zOfZQc ZMFvXw1tFeDPkohTrQac2S5uIhhwsaGCA6lOt5WcbpDki8B61JTvZ+RkPRB/eWxgW30d w2lGDU24F8fGZidW2hrRSCPfvmps4437WOve77HTsDOoSKm5J4Vm+2fpJF++/h4GYV75 JRhUyWeqL4a4p9XIGe/BPzJbEpMlCB2pE7pG3ansx8f+PYYuBRWD+iGZxgvnBptiGqtQ b03g== MIME-Version: 1.0 X-Received: by 10.42.90.208 with SMTP id l16mr38014507icm.59.1428575811278; Thu, 09 Apr 2015 03:36:51 -0700 (PDT) In-Reply-To: References: Date: Thu, 9 Apr 2015 20:36:51 +1000 Subject: Re: Exception Handling From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428575820 news.xs4all.nl 2937 [2001:888:2000:d::a6]:50547 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88706 On Thu, Apr 9, 2015 at 7:31 PM, Palpandi wrote: > Hi all, > > Is there any way to roll back or undo changes which are all done before exception occurs. In Python itself? Not directly; there are no facilities for undoing Python code. But if you're serious about integrity, you probably want to be (or already are) using a database, eg PostgreSQL, and storing data in there. In that case, you can follow a fairly straight-forward model, and one that some database connection modules even make easy for you: with dbconnection: # do stuff that might affect the database # and might throw an exception As you leave the 'with' block, Python will tell the database connection "hey, we got out without a problem", or "hey, we're leaving here because of an exception". In the first case, the database will commit, so everything you've done really happens. In the second, it'll roll back, which will undo everything perfectly. But this applies only to the database. For this to work, you have to organize your code around that; make sure that everything that matters is in the database, and local objects can't ever need to be "undone". ChrisA