472,988 Members | 2,296 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,988 software developers and data experts.

Embedding Python in C/C++ using Python/C API

Hello,
I've been researching how to embed python into C/C++. And, I learned how to write c/c++ code that, in threory, should do its job in embedding Python module into itself.
Here is what I have..

Expand|Select|Wrap|Line Numbers
  1. #include <python2.4/Python.h>
  2. void SomeStuff::someMethod  (void) {
  3.         std::cout << "ErrVisitor" << std::endl;
  4.  
  5.         PyObject *pName, *pModule, *pDict, *pFunc;
  6.  
  7.         Py_Initialize();
  8.         std::cout << "1" << std::endl;
  9.         pName = PyString_FromString ("mailSender");
  10.         std::cout << "2" << std::endl;
  11.         pModule = PyImport_Import (pName);
  12.         std::cout << "3" << std::endl;
  13.         if (PyModule_Check(pModule)) {
  14.                 std::cout << "Module" << std::endl;
  15.         } else {
  16.                 std::cout << "Not a Module" << std::endl;
  17.                 exit(2);
  18.         }
  19.         std::cout << "4" << std::endl;
  20.         pDict = PyModule_GetDict (pModule);
  21.         pFunc = PyDict_GetItemString(pDict, "send_mail");
  22.  
  23.         if (PyCallable_Check(pFunc)) {
  24.                 PyObject_CallObject(pFunc, NULL);
  25.         } else {
  26.                 std::cout << "Not Callable" << std::endl;
  27.                 PyErr_Print ();
  28.         }
  29. Py_DECREF (pModule);
  30.         Py_DECREF (pName);
  31.  
  32.         Py_Finalize();
  33. }
with the following python code: mailSender.py

Expand|Select|Wrap|Line Numbers
  1. import smtplib
  2. import os
  3. from email.MIMEMultipart import MIMEMultipart
  4. from email.MIMEBase import MIMEBase
  5. from email.MIMEText import MIMEText
  6. from email.Utils import COMMASPACE, formatdate
  7. from email import Encoders
  8.  
  9. def send_mail ():
  10.     send_from = "jin@sohovfx.com";
  11.     send_to = []
  12.     send_to.append ("jin@sohovfx.com")
  13.     subject = "hihihi"
  14.     text = "hihihihihi"
  15.     files = [];
  16.     files.append ('/home/jin/Workspace/scabs/old_split.py');
  17.     server = "smtp.beanfield.com"
  18.  
  19.     assert type(send_to)==list
  20.     assert type(files)==list
  21.  
  22.     msg = MIMEMultipart()
  23.     msg['From'] = send_from
  24.     msg['To'] = COMMASPACE.join(send_to)
  25.     msg['Date'] = formatdate(localtime=True)
  26.     msg['Subject'] = subject
  27.  
  28.     msg.attach( MIMEText(text) )
  29.  
  30.     for file in files:
  31.         part = MIMEBase('application', "octet-stream")
  32.         part.set_payload( open(file,"rb").read() )
  33.         Encoders.encode_base64(part)
  34.         part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
  35.         msg.attach(part)
  36.  
  37.     smtp = smtplib.SMTP(server)
  38.     smtp.sendmail(send_from, send_to, msg.as_string())
  39.     smtp.close()
  40.  
Both Codes are in the same directory, and I use
"g++ SomeCode.h SomeCode.cc -lpython2.4"
to compile my code...

I run with ./a.out,
and the output is the following:

ErrVisitor
1
2
3
Segmentation fault

..
I think the module didnt get imported? How do I make sure that the module DOES get imported? Do I need to do something else at the time of compilation to link these two codes?
Why am I getting a Seg fault?

Any help or insight would be greatly appreciated..

Thank you.
Nov 1 '07 #1
1 5448
Solved.

Please look at the bottom of this page:
Embedding Python in C/C++: Part I
Nov 1 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: jordi | last post by:
Hi, I'm starting to use Python embedded in a C program. I'm using Python to execute several scripts using as a variables information retrieved for several multithread "agents" written in C. ...
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...
1
by: Martin | last post by:
Greetings, I am new to python and wish to embed python in an 3D graphics application to provide application automation. The high level goal is to be able to drive my app from a script for batch...
2
by: Roose | last post by:
With some googling I have found these resources: http://docs.python.org/ext/win-dlls.html http://www.python.org/doc/faq/windows.html I have a large Win32/MFC/C/C++ application that has an...
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...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
0
by: Kenneth McDonald | last post by:
Sorry for crossposting to several lists, but from what I can tell, what I want to do may involve several different areas of expertise. (None of which I have :-( ) I'd like to use Gecko as the UI...
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
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
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
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...
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...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.