Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'context': 0.07; 'except:': 0.09; 'f.close()': 0.09; 'statements': 0.09; 'try:': 0.09; 'whatever.': 0.09; 'wrong,': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '12)': 0.16; 'broken.': 0.16; 'corresponds': 0.16; 'foo()': 0.16; 'subject:type': 0.16; 'subject:which': 0.16; 'variables,': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'ownership': 0.19; 'stack': 0.19; 'cc:addr:python.org': 0.22; 'error': 0.23; 'case.': 0.24; 'subject:like': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'equivalent': 0.26; 'nearly': 0.26; 'right.': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'occurs': 0.31; 'file': 0.32; 'fri,': 0.33; 'problem': 0.35; 'subject: (': 0.35; "can't": 0.35; 'common': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'c++': 0.36; 'subject:data': 0.36; 'follows:': 0.38; 'handle': 0.38; 'whatever': 0.38; 'pm,': 0.38; 'easy': 0.60; 'then,': 0.60; 'love': 0.65; '30,': 0.65; 'to:addr:gmail.com': 0.65; 'transfer': 0.82; '2015': 0.84; 'bar:': 0.84; 'opens': 0.91 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 :cc:content-type; bh=X1SbnJ1AQxMZ4oRTcQ2dGfDKxlJe9ykYi7KwhLr2pZc=; b=PfGB/K0qkL3mOjJ/3eXtZ9e9pC+cNQV2Zk4DEjiWvvM+2STBJEYfgn75KiztlfvoYg K7y0dJmL4yoevS1AzOpRgOEBAsaXv1cmCQ9+Ax23VdejOGWKNbZhTzQZ6ggTerjNcWeY fdl2F1fKNkmniY/UszQtzt1l/9ZB0BJJBIvGdNHOGa6mgeqtlkn2crQL6hNnwHTPkaf0 6QoYcE5dq8eTO9FX7KwdTws/Qnn0lp3AMvdxhM9nYSXaLtZgPGhffngIQzjznVxEH2mv N1a44ccIMDE0yuqpaZ6WqKWxNW3iX11MBUOJdOKDNUtFbR2JO4HprYtSeM+Qb47jGs0E mntQ== X-Received: by 10.229.37.136 with SMTP id x8mr28772992qcd.30.1422761282416; Sat, 31 Jan 2015 19:28:02 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <1553594603444345533.649938sturla.molden-gmail.com@news.gmane.org> References: <0b8db0f6-75c5-4a8f-a9a5-51fa936b306e@googlegroups.com> <8ff4c8aa-a858-481f-8964-7c794f848de2@googlegroups.com> <1553594603444345533.649938sturla.molden-gmail.com@news.gmane.org> From: Devin Jeanpierre Date: Sat, 31 Jan 2015 19:27:22 -0800 Subject: Re: RAII vs gc (was fortran lib which provide python like data type) To: Sturla Molden Content-Type: text/plain; charset=UTF-8 Cc: "comp.lang.python" 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422761289 news.xs4all.nl 2941 [2001:888:2000:d::a6]:50552 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84978 On Fri, Jan 30, 2015 at 1:28 PM, Sturla Molden wrote: > in Python. It actually corresponds to > > with Foo() as bar: > The problem with with statements is that they only handle the case of RAII with stack allocated variables, and can't handle transfer of ownership cleanly. Consider the case of a function that opens a file and returns it: def myfunction(name, stuff): f = open(name) f.seek(stuff) # or whatever return f def blahblah(): with myfunction('hello', 12) as f: .... This code is wrong, because if an error occurs during seek in myfunction, the file is leaked. The correct myfunction is as follows: def myfunction(name, stuff) f = open(name) try: f.seek(stuff) except: f.close() raise Or whatever. (I would love a close_on_error context manager, BTW.) With RAII, the equivalent C++ looks nearly exactly like the original (bad) Python approach, except it uses unique_ptr to store the file, and isn't broken. ("Modern") C++ makes this easy to get right. But then, this isn't the common case. -- Devin