Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:p 40': 0.04; 'wed,': 0.04; 'python.': 0.05; 'instance': 0.05; 'exception,': 0.07; 'something,': 0.07; 'python': 0.07; '+0200,': 0.09; 'exceptions': 0.09; 'null);': 0.09; 'null,': 0.09; 'question:': 0.09; 'stating': 0.09; 'this:': 0.11; 'wrote:': 0.14; 'defined': 0.15; '"can\'t': 0.16; '"error",': 0.16; '-1:': 0.16; 'behaviour...': 0.16; 'pyobject': 0.16; 'rachel': 0.16; 'segfault.': 0.16; 'subject:() ': 0.16; '27,': 0.19; 'modules': 0.20; 'maybe': 0.21; 'header:In- Reply-To:1': 0.22; 'right,': 0.22; '(and': 0.22; 'example.': 0.23; 'module,': 0.23; 'objects': 0.24; 'memory': 0.24; "didn't": 0.25; 'checked': 0.25; 'ps:': 0.25; "wasn't": 0.25; 'says': 0.25; "i'm": 0.26; 'instead': 0.26; '(in': 0.27; 'object': 0.27; 'pass': 0.27; 'skip:p 30': 0.29; 'missed': 0.29; 'error': 0.29; 'skip:* 20': 0.29; '>and': 0.31; 'break;': 0.31; 'does': 0.31; 'it.': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; "i've": 0.33; 'url:docs': 0.33; 'pure': 0.33; 'someone': 0.33; 'module': 0.33; 'reference': 0.34; 'using': 0.34; 'skip:" 10': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'put': 0.35; 'module.': 0.35; 'ownership': 0.35; 'doing': 0.36; 'hello,': 0.36; 'think': 0.36; 'case,': 0.36; 'program,': 0.36; 'two': 0.37; 'some': 0.37; 'case': 0.37; 'url:python': 0.37; 'exactly': 0.37; 'apr': 0.38; 'but': 0.38; 'url:org': 0.38; 'so,': 0.38; 'here,': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'might': 0.40; '2011': 0.62; 'here': 0.65; 'hand,': 0.72; 'claims': 0.83; 'destroyed,': 0.84; 'received:hu': 0.84; 'schrieb': 0.84 X-Spam-Score: -1.315 X-Spam-Level: X-Spam-Status: No, score=-1.315 tagged_above=-20 required=4 tests=[AWL=0.054, BAYES_00=-2.599, DNS_FROM_OPENWHOIS=1.13, RDNS_DYNAMIC=0.1] Date: Sun, 1 May 2011 22:00:36 +0200 From: =?utf-8?Q?Heged=C3=BCs?= Ervin To: python-list@python.org Subject: Re: Py_INCREF() incomprehension References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 108 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304280052 news.xs4all.nl 81473 [::ffff:82.94.164.166]:40368 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4412 hello, On Wed, Apr 27, 2011 at 11:58:18AM +0200, Thomas Rachel wrote: > Am 26.04.2011 20:44, schrieb Hegedüs Ervin: > > >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...) so, I've checked it - there wasn't any Py_INCREF(), I just calmed down. But. :) My module contains just 4 functions (in C), which translate 3rd party lib to Python. The name would be _mycrypt.so example. I wrapped it a pure Python module, its name is mycrypt.py. Then, I've import pure Python module in a main program, like this: =%= mycrypt.py: import _mycrypt ... =%= =%= userapp.py: import mycrypt ... =%= I've missed out something, and then I didn't get exception, instead there were a segfault. :( I've put it a Py_INCREF() after every PyModule_AddObject(), eg.: PyModule_AddObject(o, "error", cibcrypt_error_nokey); Py_INCREF(cibcrypt_error_nokey); and now if there is some expected exception, I get it. Any explanation? Thanks: a. ps: this is just for my passion, but I would like to understand it very-very much :)