Path: csiph.com!usenet.pasdenom.info!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.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; 'example:': 0.03; 'context': 0.07; 'constructor': 0.09; 'correct,': 0.09; 'executed': 0.09; 'skip:/ 10': 0.09; 'api': 0.11; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'caveat': 0.16; 'corresponds': 0.16; 'foo()': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'skip:{ 30': 0.16; 'subject:type': 0.16; 'subject:which': 0.16; 'true:': 0.16; 'url:html)': 0.16; 'subject:python': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; "python's": 0.19; 'not,': 0.20; 'feb': 0.22; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'manager.': 0.24; 'subject:like': 0.24; 'cc:2**0': 0.24; 'equivalent': 0.26; 'performing': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'statement': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'figure': 0.32; 'url:python': 0.33; 'call.': 0.33; 'subject: (': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'c++': 0.36; 'skip:~ 10': 0.36; 'subject:data': 0.36; 'yield': 0.36; 'url:org': 0.36; 'pm,': 0.38; 'skip:p 20': 0.39; 'release': 0.40; 'most': 0.60; 'tell': 0.60; 'url:5': 0.61; 'url:3': 0.61; 'first': 0.61; 'guarantee': 0.63; 'kind': 0.63; 'here': 0.66; 'qualified': 0.72; 'heavy': 0.81; 'truth': 0.81; '2015': 0.84; 'bar:': 0.84; 'to:none': 0.92 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=uLvUyTPGx9fPe0Z5Na4gYr0rDDC2Uo0Kih7jF8baJpU=; b=VeXtpc3AGrN855gZYNUlFIFEIlMfoXHix29nlCnjSmC/CmWiGuImv8R0LsASVxRCht 6WtaFHQQogOdR0Xr55kra0w/EElrUyOy6SbMVVdjaqvaWpV1raq2SOMGA0Oirj0HIqI5 m9CZzdLvgHPd33/IqLVbs0CWpHifxnkVHfs1ue9MIJtVLesJkk0mXTDUxFguN4l5NZYz MIfdabjd4KWvzSa7JdOdx2cNxL/t4gNS+VN2EmKM/YOPabA9bII1TUA07EFlYmn8SARp nPsgDsl5nWtsq2IZDonGhPHz9L2njtljdAJiFXKYbmUNMy16nVvVMwpK3MmGQ/rxZ+t7 Ep/w== MIME-Version: 1.0 X-Received: by 10.50.18.108 with SMTP id v12mr4976554igd.34.1422760882990; Sat, 31 Jan 2015 19:21:22 -0800 (PST) In-Reply-To: <54cd96b5$0$12994$c3e8da3$5496439d@news.astraweb.com> References: <0b8db0f6-75c5-4a8f-a9a5-51fa936b306e@googlegroups.com> <8ff4c8aa-a858-481f-8964-7c794f848de2@googlegroups.com> <54cd96b5$0$12994$c3e8da3$5496439d@news.astraweb.com> Date: Sun, 1 Feb 2015 14:21:22 +1100 Subject: Re: RAII vs gc (was fortran lib which provide python like data type) 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.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: 75 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422760885 news.xs4all.nl 2870 [2001:888:2000:d::a6]:44239 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84976 On Sun, Feb 1, 2015 at 2:00 PM, Steven D'Aprano wrote: >> A C++ statement with RAII like >> >> { >> Foo bar(); >> // suite >> } >> >> is not equivalent to >> >> bar = Foo() >> >> in Python. It actually corresponds to >> >> with Foo() as bar: >> > > > Nice answer! I'm not qualified to tell whether you are right or not, but > what you say has the ring of truth about it. I would say that this is indeed correct, with the caveat that RAII is most often used for memory allocation, in which case the correct transformation into Python _would_ be a constructor call. But when you use this kind of thing to set up state and clean up afterwards, then yes, Python's equivalent would be a context manager. So a C++ way to release and reacquire the GIL might look like this (borrowing from https://docs.python.org/3.5/c-api/init.html): class ReleaseGIL { //Internal state PyThreadState *_save; public: //Constructor ReleaseGIL() {_save = PyEval_SaveThread();} //Destructor ~ReleaseGIL() {PyEval_RestoreThread(_save);} }; //Usage example: int do_work() { while (1) { //use the Python C API to figure out what we need to do ReleaseGIL heavy_processing_coming; //do the heavy computational work } //GIL is reacquired here } The Python equivalent would be something like: @contextlib.contextmanager def release_gil(): """Why the are we manipulating the GIL from Python code anyway???""" _save = PyEval_SaveThread() yield PyEval_RestoreThread(_save) def do_work() while True: # get some work with release_gil() as heavy_processing_coming: # do the heavy computational work # GIL is reacquired here In each case, you have a guarantee that code following the suite will not be executed without first performing the appropriate cleanup. ChrisA