473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

extending Python base class in C

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
4 1689
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: henrikpierrou | last post by:
Hi, I am trying to extend an overridden base class method (printer) by printing some extra fields and then calling the base class method. Extract from the python tutorial: 'An overriding...
3
by: Marco Meoni | last post by:
Hi all! I've a problem with a C++ class that has to be included in a python application. One way to do it is Extending and Embedding the Python Interpreter Now i have 2 questions 1) Is there a...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
16
by: chosechu | last post by:
Hello Pythoneers: I need to pass a list of named arguments to a function in a given order, and make sure these named arguments are retrieved using keys() in the same order they were given....
0
by: Tom C | last post by:
In an N-tier app, what is the DOT NET approach for extending an application at all levels (i.e. DAL, Business Object, Client) for a customer or module specific deviation without changing the base...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
3
varuns
by: varuns | last post by:
hi i extended python to my own widgets inherited from gtk. But when i try to run command python2.5 -c "import mygtk" it displays following error "TypeError: mro() returned base with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.