How do I call super() in c extention ?

U

umedoblock

Hi, nice to meet you, everyone.
I'm umedoblock.

I'd like to call super() in c extension.
I'd like to rewrite class Baa as c extension.
Please see below code.
Now, I can make super object.

But I cannot understand how to use super_init(), super_descr_get() and
super_getattro().

Please please help me...

------------------------------------------------------------
baa.py

class Baa(object):
def __init__(self):
print('call __init__() in Baa.')
def sample(self):
print('call sample() in Baa.')
class Foo(Baa):
def __init__(self):
print('call __init__() in Foo.')
super().sample()
super().__init__()
class Baz(Foo):
def __init__(self):
print('call __init__() in Baz.')
super().__init__()
--
pyfoo.c

static int
Foo_init(FooObject *self, PyObject *args, PyObject *kwds)
{
int ret = 0;
PyTypeObject *super_type = &PySuper_Type;
PyObject *super_obj;
PyObject *empty_tuple;

super_obj = super_type->tp_alloc(super_type, 0);

empty_tuple = Py_BuildValue("()");
ret = super_type->tp_init(super_obj, empty_tuple, NULL);

fprintf(stderr, "super_obj = %p\n", super_obj);
PyObject_Print((PyObject *)super_obj, stderr, 0); fprintf(stderr,
"\n\n");
fprintf(stderr, "super_type->tp_init(super_obj=%p, empty_tuple=%p,
NULL)=%d\n", super_obj, empty_tuple, ret);

return 0;
}

PyMODINIT_FUNC
PyInit__foo(void)
{
PyObject *m;
PyObject *import_baa = NULL, *Baa = NULL;
PyObject *Baa_str;

import_baa = PyImport_ImportModule("baa");
Baa_str = Py_BuildValue("s", "Baa");
Baa = PyObject_GetAttr(import_baa, Baa_str);
Foo_Type.tp_base = (struct _typeobject *)Baa;
Py_INCREF(Foo_Type.tp_base);

Foo_Type.tp_new = Foo_new;
if (PyType_Ready(&Foo_Type) < 0)
return NULL;
Py_INCREF(&Foo_Type);

m = PyModule_Create(&Foo_module);
if (m == NULL)
return NULL;
PyModule_AddObject(m, "Foo", (PyObject *)&Foo_Type);

PyModule_AddStringConstant(m, "CONST", "C_EXTENSION");

return m;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top