How to instantiate a custom Python class inside a C extension?

Rhodri James rhodri at kynesim.co.uk
Wed Apr 1 13:41:49 EDT 2020


On 01/04/2020 18:24, Musbur wrote:
> Am 01.04.2020 15:01 schrieb Rhodri James:
> 
>> I believe you do it in C as you would in Python: you call the Series 
>> class!
>>
>> pyseries = PyObject_CallObject((PyObject *)&series_type, NULL);
> 
> Well, that dumps core just as everything else I tried.
> 
> What does work, however, is calling PyType_Ready first:
> 
>      PyType_Ready(&series_type);
>      pyseries = PyObject_New(Series, &series_type);
>      PyObject_Init((PyObject *)pyseries, &series_type);o
> 
> I don't understand, though, why I have to do that and when. Didn't that 
> already happen when the module was imported? Do I need to do it whenever 
> I create a new instance in C?

It should have happened on your module being imported, the line was 
there in your code.  Stick a breakpoint on your module init function and 
see if it is being called.

(The "from thingy import *" always makes me nervous, but it shouldn't be 
responsible for this.)

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-list mailing list