Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: Thomas Rachel Newsgroups: comp.lang.python Subject: Re: Py_INCREF() incomprehension Date: Mon, 02 May 2011 02:59:55 +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: 67 NNTP-Posting-Date: 02 May 2011 03:00:01 CEST NNTP-Posting-Host: 1f0dd568.newsspool2.arcor-online.net X-Trace: DXC=1@o==KX3lWM0YVY]kmLTlDA9EHlD;3YcB4Fo<]lROoRA8kF 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 > ... > =%= AFAICS, it looks ok. > I've missed out something, and then I didn't get exception, > instead there were a segfault. :( I guess this is the point where yo should start printf programing. * What happens during module initialization? * What happens n the functions? * Where does the stuff fail? * What are the reference counts of the involved objects? etc. > 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? I don't have one - I would think that if the module object exists for all the time, it would be enough to have one reference there. But obviously it is not enough - did you at any time del something related to here? The module or one of its attributes? Anyway, it seems safer to do INCREF here - so do it. (As Gregory already stated - it looks cleaner if you do INCREF before AddObject.) > ps: this is just for my passion, but I would like to understand > it very-very much :) Understandable. That's that the printf debugging of the refcounts can be good for - even if you don't really have a problem. Thomas