Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.straub-nv.de!noris.net!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: Wed, 27 Apr 2011 11:58:18 +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 (Windows; U; Windows NT 5.1; de; rv:1.9.2.15) Gecko/20110303 Lightning/1.0b2 Thunderbird/3.1.9 Hamster/2.1.0.11 In-Reply-To: Lines: 54 NNTP-Posting-Date: 27 Apr 2011 12:00:02 CEST NNTP-Posting-Host: 1c65d337.newsspool2.arcor-online.net X-Trace: DXC=a?InW65_@E[5TOT9_N5i and (maybe) final question: :) > > I defined many exceptions: > > static PyObject *cibcrypt_error_nokey; > static PyObject *cibcrypt_error_nofile; > static PyObject *cibcrypt_error_badpad; > ... > > void handle_err(int errcode) { > switch(errcode) { > case -1: PyErr_SetString(cibcrypt_error_nokey, "Can't find key."); > break; > ... > } > ... > cibcrypt_error_nokey = PyErr_NewException("cibcrypt.error_nokey", NULL, NULL); > ... > PyModule_AddObject(o, "error", cibcrypt_error_nokey); Then I would not use the name "error" here, but maybe "error_nokey" or even better "NoKeyException". Oops: there is an inconsistency in the docu: on the one hand, it says There are exactly two important exceptions to this rule: PyTuple_SetItem() and PyList_SetItem(). stating these are the only ones who take over ownership. But PyModule_AddObject() claims to "steal" a reference as well... > I am right, here also no need any Py_INCREF()/Py_DECREF() action, > based on this doc: > http://docs.python.org/c-api/arg.html I'm not sure: On one hand, you pass ownership of the error objects to the module. There - one could think - they are until the module is unloaded. But what if someone does "del module.NoKeyException"? In this case, the object could have been destroyed, and you are using it -> BANG. On the other hand, if you keep one instance internally, it is not possible any longer to unload the module without a memory leak... As already stated - you might want to have a look at some other C modules and mimic their behaviour... (and hope they are doing it right...) Thomas