473,394 Members | 2,071 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,394 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 1152
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
23
by: Robey Holderith | last post by:
Anyone know a good way to embed python within python? Now before you tell me that's silly, let me explain what I'd like to do. I'd like to allow user-defined scriptable objects. I'd like to...
4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
0
by: smilechaser | last post by:
Hi All, A question about embedding: If I take the example code on embedding (Section 5.3 of Extending and Embedding the Python Interpreter 2.4) and add the lines: PyRun_SimpleString("import...
0
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application runnig on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
1
by: amit | last post by:
Hello, I am currently studying how to embedd python. I am developing a graphical C++ application. My goal is to embedd python script that will control some kind of animation. I have some...
4
by: Martitza | last post by:
Hi. I work for a small company (actually in process of forming) interested in embedding or extending python as part of our commercial non-open-source product. We have legal counsel, but are...
2
by: bappai | last post by:
Hello, I am trying to actually call a GUI from my C++ code which would have buttons and therefore can call functions from C++ again, ie extend the C++ code also. I am faced with a peculiar...
1
by: Thomas Troeger | last post by:
Dear all, I've successfully embedded the Python interpreter into a set of C/C++ application programs that use a larger library project with information from http://docs.python.org/api/api.html...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.