471,350 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Question about embedding python in C++

When extending python in c/c++ after you register a module is there a
way in c/c++ to check if they were correctly registered?

Cause I'm having the problem where when I execute the the python
script it finds the module but none of the functions.

Aug 23 '07 #1
4 1086
TheShadow wrote:
When extending python in c/c++ after you register a module is there a
way in c/c++ to check if they were correctly registered?

Cause I'm having the problem where when I execute the the python
script it finds the module but none of the functions.
Are you calling Py_InitModule(...) in your initmodule function? If so,
Py_InitModule(...) returns a reference to the module object. You can use
this object to double check that your function names exist in the
modules dictionary. Most likely, there is an error in your code. If you
post it, it would be easier to tell what the problem is.

-Farshid
Aug 23 '07 #2
On Aug 23, 11:57 am, Farshid Lashkari <n...@spam.comwrote:
TheShadow wrote:
When extending python in c/c++ after you register a module is there a
way in c/c++ to check if they were correctly registered?
Cause I'm having the problem where when I execute the the python
script it finds the module but none of the functions.

Are you calling Py_InitModule(...) in your initmodule function? If so,
Py_InitModule(...) returns a reference to the module object. You can use
this object to double check that your function names exist in the
modules dictionary. Most likely, there is an error in your code. If you
post it, it would be easier to tell what the problem is.

-Farshid
Hrmm I forgot about the module init function... I was just calling
Py_initModule()

Aug 23 '07 #3
On Aug 23, 11:57 am, Farshid Lashkari <n...@spam.comwrote:
TheShadow wrote:
When extending python in c/c++ after you register a module is there a
way in c/c++ to check if they were correctly registered?
Cause I'm having the problem where when I execute the the python
script it finds the module but none of the functions.

Are you calling Py_InitModule(...) in your initmodule function? If so,
Py_InitModule(...) returns a reference to the module object. You can use
this object to double check that your function names exist in the
modules dictionary. Most likely, there is an error in your code. If you
post it, it would be easier to tell what the problem is.

-Farshid
Alright that was a dead end. Here is the source code.

bool CPythonModule::Register()
{
bool bReturn = false;
unsigned int uiSize = static_cast<unsigned
int>(m_LPyMethodDefs.size());
if (m_LPyMethodDefs.size() 0)
{
m_pEmbMethods = new PyMethodDef[m_LPyMethodDefs.size()+1];
unsigned int uiCounter = 0;

IterQPyMethodDefs iterMethods = m_LPyMethodDefs.begin();

while (m_LPyMethodDefs.end() != iterMethods)
{
m_pEmbMethods[uiCounter].ml_name =
m_LPyMethodDefs.front().sName.c_str();
m_pEmbMethods[uiCounter].ml_meth =
m_LPyMethodDefs.front().pFunction;
m_pEmbMethods[uiCounter].ml_flags = m_LPyMethodDefs.front().iFlags;
m_pEmbMethods[uiCounter].ml_doc =
m_LPyMethodDefs.front().sDoc.c_str();

++uiCounter;
++iterMethods;
}

PyMethodDef PyMethodNullDef = {NULL, NULL, 0, NULL};

m_pEmbMethods[uiSize] = PyMethodNullDef;

Py_InitModule(m_sModuleName.c_str(),m_pEmbMethods) ;

//PythonInterfaceInst->RegisterModule(this);

m_pInitFunc();

bReturn = true;
}
return bReturn;
}

Aug 23 '07 #4
On Aug 23, 1:17 pm, TheShadow <theshad...@gmail.comwrote:
On Aug 23, 11:57 am, Farshid Lashkari <n...@spam.comwrote:
TheShadow wrote:
When extending python in c/c++ after you register a module is there a
way in c/c++ to check if they were correctly registered?
Cause I'm having the problem where when I execute the the python
script it finds the module but none of the functions.
Are you calling Py_InitModule(...) in your initmodule function? If so,
Py_InitModule(...) returns a reference to the module object. You can use
this object to double check that your function names exist in the
modules dictionary. Most likely, there is an error in your code. If you
post it, it would be easier to tell what the problem is.
-Farshid

Alright that was a dead end. Here is the source code.

bool CPythonModule::Register()
{
bool bReturn = false;
unsigned int uiSize = static_cast<unsigned
int>(m_LPyMethodDefs.size());
if (m_LPyMethodDefs.size() 0)
{
m_pEmbMethods = new PyMethodDef[m_LPyMethodDefs.size()+1];
unsigned int uiCounter = 0;

IterQPyMethodDefs iterMethods = m_LPyMethodDefs.begin();

while (m_LPyMethodDefs.end() != iterMethods)
{
m_pEmbMethods[uiCounter].ml_name =
m_LPyMethodDefs.front().sName.c_str();
m_pEmbMethods[uiCounter].ml_meth =
m_LPyMethodDefs.front().pFunction;
m_pEmbMethods[uiCounter].ml_flags = m_LPyMethodDefs.front().iFlags;
m_pEmbMethods[uiCounter].ml_doc =
m_LPyMethodDefs.front().sDoc.c_str();

++uiCounter;
++iterMethods;
}

PyMethodDef PyMethodNullDef = {NULL, NULL, 0, NULL};

m_pEmbMethods[uiSize] = PyMethodNullDef;

Py_InitModule(m_sModuleName.c_str(),m_pEmbMethods) ;

//PythonInterfaceInst->RegisterModule(this);

m_pInitFunc();

bReturn = true;
}
return bReturn;

}
Forget I said anything I found my bug... I was using m_LPyMethodDefs
instead of my iterator... hurray for writing code at 12am :P

Aug 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Robey Holderith | last post: by
4 posts views Thread by Alicia Haumann | last post: by
reply views Thread by smilechaser | last post: by
reply views Thread by adsheehan | last post: by
1 post views Thread by amit | last post: by
4 posts views Thread by Martitza | last post: by
1 post views Thread by Thomas Troeger | last post: by

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.