471,337 Members | 810 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 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 5187
Solved.

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

Post your reply

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

Similar topics

23 posts views Thread by Robey Holderith | last post: by
4 posts views Thread by Alicia Haumann | last post: by
1 post views Thread by Martin | last post: by
2 posts views Thread by Roose | last post: by
1 post views Thread by amit | last post: by
reply views Thread by rosydwin | 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.