• Advertisement
Sign in to follow this  

Boost Python - derived class in python's constructor

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Another basic question to help my understanding of Boost::Python. Say I have a class in C++ CEntity that is exported to Python using:
void export_entity()
{
	class_ <CObject,boost::noncopyable>("Object",no_init)
		;

	class_<CEntity,boost::noncopyable,bases<CObject>>("Entity", init<const CString&>())
		;
}
If I derive from CEntity in Python:
class MyEntity(Entity):
	def __init__(self, name):
		Entity.__init__(self, name)
		print "MyEntity - " + name
I get a traceback when calling the super class' __init__ function.
Boost.Python.ArgumentError: Python argument types in
    Entity.__init__(MyEntity, str)
did not match C++ signature:
    __init__(struct _object *, class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > >)
Do I need to wrap (wrapper.h?) the constructor in Boost? I presumed this would "just work" as it's fairly standard/basic OO/Python stuff. Unfortunately I'm trying to add scripting to an MFC app of mine, so joyous days are ahead! Thanks

Share this post


Link to post
Share on other sites
Advertisement
It looks like you didn't register CString with Python.

Share this post


Link to post
Share on other sites
I did, I just forgot to paste it in here:

	class_ <CString>("CString")
;

class_ <CObject,boost::noncopyable>("Object",no_init)
;

class_<CEntity,boost::noncopyable,bases<CObject>>("Entity", init<const CString&>())
;

Share this post


Link to post
Share on other sites
Given your class registration why would Python know that passing a string would have anything to do with the CString class?

Share this post


Link to post
Share on other sites
Yeah I just realised that when I finished responding to your last post. So I'm reading up on converters. Guess I made a huge presumption!

Share this post


Link to post
Share on other sites
For anyone reading, I followed the example at the bottom of this page, replace custom_string with CString.

http://www.boost.org/doc/libs/1_35_0/libs/python/doc/v2/faq.html

Share this post


Link to post
Share on other sites
That works nicely except I have memory leaks when I exit the app in debug mode (VC2005). VC2005 gives no indication where they are. Ugh....

Share this post


Link to post
Share on other sites
It works until I try to access an exported data member of type CString:

TypeError: No Python class registered for C++ class class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTra
itsCRT<char> > >


If I add this to the BOOST::Python exports:


class_ <CString>("CString")
;


I get an assertion line 160 in registry.cpp, Expression: slot==0

Any ideas? Thanks

Share this post


Link to post
Share on other sites
Insufficient information. What does your exported data member look like? What does the class that contains that data member look like? What version of boost::python are you using? What does the code that accesses that data member look like?

Share this post


Link to post
Share on other sites
Boost 1.34.1, Win32 installer from the Boost Consulting site.

Data member:


public:
CString mType; // class name



I can't post the class - proprietary code etc.... But it's basically:


class CEntity :public CObject
...



I don't have any code that accesses this member yet - I'm just trying to export it first. The assert is from Boost trying to register the CString class twice I think.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement