keeping local state in an C extension module

Stefan Behnel stefan_ml at behnel.de
Thu Jun 30 09:10:22 EDT 2011


Daniel Franke, 30.06.2011 13:37:
> 2011/6/30 Stefan Behnel:
>> There's a PEP for Py3 that enables this through a general framework. In
>> general, to achieve this, you may want to allocate the module content
>> (including types etc.) on the heap rather than statically.
>>
>> http://www.python.org/dev/peps/pep-3121/
>
> Perfect! Unfortunately, it seems not to be available in 2.x, so
> unusable for now. For the foreseeable future the project can not
> depend on 3.x as many (internal) target systems are "stable", i.e. are
> not updated besides security stuff.

Yes, it's Py3 only.


>> Take a look at PyCapsule, it may (or may not) be enough for your use case.
>
> Also a perfectly good solution - unfortunately the same comment about
> 3.0 applies here.
>
>
>> However, note the comment about Py_InitModule4() in the docs, where it says
>> that the 'self' passing feature isn't used very often. You may or may not
>> want to (or need to) use it.
>
> If there are too many hoops to jump through to get a reasonable
> solution working for 2.[567], I might as well go back to
> Py_InitModule() and static variables. It's somewhat ugly, but it
> works.

Py2.x has a PyCObject, basically the predecessor to PyCapsule. Writing your 
own little type is another option.

Stefan




More information about the Python-list mailing list