Author pitrou
Recipients Robin.Schreiber, belopolsky, loewis, pitrou
Date 2013-08-13.09:04:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1376384691.24.0.303522762494.issue15849@psf.upfronthosting.co.za>
In-reply-to
Content
> In the example Martin gave in his PEP 3121, the PyInit does not perform any INCREFs on
> the Variables that are referenced from inside the module state.
> He therefore left out m_free completely as there was nothing to DECREF within the module
> state.
> Back when I did my GSoC together with Martin, we decided that the Module state itself can 
> be considered a valid container object, an therefore has to INCREF and in the end of its
> lifecycle (that is within m_free) also DECREF every object reference it holds. I therefore 
> decided to include that into every module I refactored, and consequently also the xxmodule.

I agree with that, but then PEP 3121's example code should be fixed.

> I am yet undecided regarding the NULL-check for FindModule.

Not checking for NULL makes it dangerous to implement unloading of extension modules: see issue18674.

If checking for NULL makes extension code too complicated, then please take a look at the helper API I've suggested in issue18710.

Also, it would be nice if you could also read the following python-dev thread, since it discusses concrete issues and possible solutions:
http://mail.python.org/pipermail/python-dev/2013-August/127862.html

> This means we can not test for these leaks from within Python, but need some C-Code

You can use _testcapi.run_in_subinterp() to run custom code in a sub-interpreter.

Here is an example of a non-leaking extension module, and then a (presumably) leaking one:

$ ./python -Xshowrefcount
Python 3.4.0a1+ (default:9e61563edb67, Aug 12 2013, 14:52:25) 
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import _testcapi
[64175 refs, 19937 blocks]
>>> _testcapi.run_in_subinterp("import resource")
0
[68839 refs, 20969 blocks]
>>> _testcapi.run_in_subinterp("import resource")
0
[68852 refs, 20980 blocks]
>>> _testcapi.run_in_subinterp("import resource")
0
[68850 refs, 20979 blocks]
>>> _testcapi.run_in_subinterp("import resource")
0
[68852 refs, 20980 blocks]
>>> _testcapi.run_in_subinterp("import resource")
0
[68850 refs, 20979 blocks]
>>> _testcapi.run_in_subinterp("import _socket")
0
[71840 refs, 22059 blocks]
>>> _testcapi.run_in_subinterp("import _socket")
0
[71911 refs, 22083 blocks]
>>> _testcapi.run_in_subinterp("import _socket")
0
[71981 refs, 22107 blocks]
>>> _testcapi.run_in_subinterp("import _socket")
0
[72051 refs, 22131 blocks]
History
Date User Action Args
2013-08-13 09:04:51pitrousetrecipients: + pitrou, loewis, belopolsky, Robin.Schreiber
2013-08-13 09:04:51pitrousetmessageid: <1376384691.24.0.303522762494.issue15849@psf.upfronthosting.co.za>
2013-08-13 09:04:51pitroulinkissue15849 messages
2013-08-13 09:04:50pitroucreate