Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.straub-nv.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: Thomas Rachel Newsgroups: comp.lang.python Subject: Re: Py_INCREF() incomprehension Date: Tue, 26 Apr 2011 18:08:58 +0200 Organization: A newly installed InterNetNews server Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit User-Agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.2.14) Gecko/20110221 SUSE/3.1.8 Thunderbird/3.1.8 In-Reply-To: Lines: 50 NNTP-Posting-Date: 26 Apr 2011 18:10:03 CEST NNTP-Posting-Host: 0997336c.newsspool2.arcor-online.net X-Trace: DXC=@biHU@3c7ocf1oJaJ0@dmgA9EHlD;3Ycb4Fo<]lROoRa8kF>DH I've read API doc (which you've included in another mail), but > that's not clear for me. :( No probem, I'll go in detail, now as I have read it again. (I didn't want to help from memory, as it is some time ago I worked with it, and didn't have time to read it.) >> The most critical parts are indeed >> >> * the input parameters The ownership rules say that the input parameter belongs to the caller who holds it at least until we return. (We just "borrow" it.) So no action needed. >> * Py_BuildValue() This function "transfers ownership", as it is none of (PyTuple_GetItem(), PyList_GetItem(), PyDict_GetItem(), PyDict_GetItemString()). So the value it returns belongs to us, for now. We do transfer ownership to our caller (implicitly), so no action is required as well here. > so, it means when I implicit allocate a new object (whit > Py_BuildValue()), Python's GC will free that pointer when it > doesn't require anymore? In a way, yes. But you have to obey ownership: whom belongs the current reference? If it is not ours, and we need it, we do Py_(X)INCREF(). If we got it, but don't need it, we do Py_(X)DECREF(). >> BTW: Is there any reason for using calloc()? malloc() would probably >> be faster... > > may be, I didn't measure it ever... but calloc() gives clear > space... :) Ok. (But as sizeof(char) is, by C standard definition, always 1, you can write it shorter.) Thomas