By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

extending Python base class in C

P: n/a
Hi all,

I once read that it is possible to use a python base class for a C
extension class. To be precise, I want to achieve the following
behavior:

class PythonClass :
pass

class CClass(PythonClass) :
"this class should be implemented as C extension"
pass
Unfortunately, google could not find me the right reference. Does
anyone know how to do it, or where I can find relevant information?

Thanks,

- harold -
--
Je me suis enfermé dans mon amour -- je rève.
-- Paul Eluard
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
There was good article of how to do this on DDJ home page www.ddj.com
"harold fellermann" <ha***************@upf.edu> wrote in message
news:ma**************************************@pyth on.org...
Hi all,

I once read that it is possible to use a python base class for a C
extension class. To be precise, I want to achieve the following
behavior:

class PythonClass :
pass

class CClass(PythonClass) :
"this class should be implemented as C extension"
pass
Unfortunately, google could not find me the right reference. Does
anyone know how to do it, or where I can find relevant information?

Thanks,

- harold -
--
Je me suis enfermé dans mon amour -- je rève.
-- Paul Eluard
Jul 19 '05 #2

P: n/a

"harold fellermann" <ha***************@upf.edu> wrote in message
news:ma**************************************@pyth on.org...
Hi all,

I once read that it is possible to use a python base class for a C
extension class.
On 14.06.2005, at 09:29, Grigoris Tsolakidis wrote:
There was good article of how to do this on DDJ home page www.ddj.com


unfortunately, I could not find it yet. I managed to do the following:
I the initmodule function of my extension, I import the module and look
for the class I want to use as a base class:

PyObject *base_module = PyImport_ImportModule("module_name");
if (!base_module) return;

PyObject *base_class = PyMapping_GetItemString(
PyModule_GetDict(base_module,"BaseClass")
);
if (!base_class) return;
This gives me a pointer to the class I want to use as base class.
Does anyone know how to assign this to the extension class? I tried
to build a tuple (BaseClass,) and assign it (after the respective
PyType_Ready call) to CClass.__bases__ like this:

PyObject_SetAttrString(
&cclassType,
"__bases__",
Py_BuildValue("(O,)",base_class)
);

but this raises a TypeError when executed during import:

TypeError: can't set attributes of built-in/extension type
'ext_module.CClass'

Any ideas how to proceed?

- harold -

--
You can imagine the opposite
-- Maurizio Nannucci

Jul 19 '05 #3

P: n/a
harold fellermann wrote:
...
This gives me a pointer to the class I want to use as base class.
Does anyone know how to assign this to the extension class? I tried
to build a tuple (BaseClass,) and assign it (after the respective
PyType_Ready call) ...

I think this is your problem -- call PyType_Ready _after_ setting up
the base class, not before.

--Scott David Daniels
Sc***********@Acm.Org
Jul 19 '05 #4

P: n/a
Yahoooo! Finally, I got the thing to work. Here is how I did it.
/* import the module that holds the base class */
PyObject *base_module = PyImport_ImportModule("module_name");
if (!base_module) return;

/* get a pointer to the base class */
PyObject *base_class = PyMapping_GetItemString(
PyModule_GetDict(base_module,"BaseClass")
);
if (!base_class) return;

/* assign it as base class */
cclassType.tp_bases = Py_BuildValue("(O)",BaseClass);

I am doing all this before the call to PyType_Ready() -- as
Scoot Daniels suggested. Using the python API's SetAttrString()
did not work (I suppose for the same reason, why assigning
cls.__bases__ does not work in pure python either).
I also checked

cclassType.tp_base = (PyTypeObject *) base_class;

which also works and I am not aware of the difference of these two
slots.
Anyway, for my simple purposes it works just fine.

- harold -

--
"Mr Ghandi, what do you think of western civilization?"
"Oh, this would be a great idea."

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.