472,958 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1138
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.