Advertisement
Guest User

Automatically executing a Python script in C++

a guest
Jan 27th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. int main(int argc, char** argv) {
  2.   Py_Initialize();
  3.   init_my_modules_and_stuff();
  4.  
  5.   // Load up the auto execution script and then open up the command prompt
  6.   object main_module((handle<>(borrowed(PyImport_AddModule("__main__")))));
  7.   object main_namespace = main_module.attr("__dict__");
  8.   try
  9.   {
  10.     handle<> ignored(( PyRun_String( "execfile('autoexec.py')",
  11.                                        Py_file_input,
  12.                                        main_namespace.ptr(),
  13.                                        main_namespace.ptr() ) ));
  14.   }
  15.   catch(...)
  16.   {
  17.     // If an exception was thrown, translate it to Python
  18.     boost::python::handle_exception();
  19.   }
  20.  
  21.   Py_Main(argc, argv);
  22.  
  23.   Py_Finalize();   // According to old Boost documentation, we don't want to do this. (???)
  24.   return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement