Wrapping C++ Class Heirachy in Python

A

Andrew Wilkinson

Hi,

I'm trying to wrap a C++ class hierarchy with Python types and I'd like to
maintain the hierarchy in the types. I'm fairly sure this is possible,
isn't it?

Are there any documents explaining how to do this, the standard Python
manual doesn't go into enough detail about creating types for this.

I've found the tp_base and tp_bases elements and I've set them to the base
type object (and a tuple containing the base type object) before I call
PyType_Ready but in Python the base class isn't recognised. Is there
anything obvious I'm missing?

Thanks in advance for any help,
Andrew Wilkinson
 
D

Diez B. Roggisch

PyType_Ready but in Python the base class isn't recognised. Is there
anything obvious I'm missing?

Maybe - I'd go for one of the several available python wrapper generators. I
personally had good experiences with SIP. But you might also try boost
python or SWIG.

The python C-API is great - for C. For C++, use one of the above.
 
S

Steve Juranich

I'm trying to wrap a C++ class hierarchy with Python types and I'd like to
maintain the hierarchy in the types. I'm fairly sure this is possible,
isn't it?
Yes.

Are there any documents explaining how to do this, the standard Python
manual doesn't go into enough detail about creating types for this.
Amen.

I've found the tp_base and tp_bases elements and I've set them to the base
type object (and a tuple containing the base type object) before I call
PyType_Ready but in Python the base class isn't recognised. Is there
anything obvious I'm missing?

Well, I can't really recommend without seeing what you did (did you
remember the preceding "&" for the type struct?

Otherwise, I'd have to recommend Objects/unicodeobject.c in the Python
source as a reference for how to do this.

I've had a couple of questions about how to implement new types that I
posted to the list, and never got much help, so I started using the
Python source as a reference. I've gotten most of what I needed to
know from there.

HTH
 
A

Andrew Wilkinson

Steve said:
Well, I can't really recommend without seeing what you did (did you
remember the preceding "&" for the type struct?

I realised it will be a little difficult to help without seeing my source,
but I was hoping someone would point me to some documentation so I wouldn't
have to cut my source down to an example suitable for a usenet post.
 
Otherwise, I'd have to recommend Objects/unicodeobject.c in the Python
source as a reference for how to do this.

Thanks! I was trying to think of a built in object hierarchy in Python, I
didn't think of unicode strings.

Cheers!

Andrew

Simple distributed computing with Python...
PyLinda - http://www-users.cs.york.ac.uk/~aw/pylinda
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top