LeonidPodosinnikov

pytest.cpp

Mar 8th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1.  /* pytest.cpp */
  2.  /* build: g++ -std=c++1y -fPIC -I/usr/include/python3.4 -lboost_python -lpython3.4m pytest.cpp */
  3.  /* bould: g++ -std=c++1y -fPIC -I/usr/include/python3.4 -lboost_python-py34 -lpython3.4m pytest.cpp */
  4.  
  5. #include <string>
  6. #include <iostream>
  7. #include <boost/python.hpp>
  8.  
  9. using namespace boost::python;
  10.  
  11. int main() {
  12.   try {
  13.     Py_Initialize();
  14.     object main_module = import("__main__");
  15.     object main_namespace = main_module.attr("__dict__");
  16.  
  17.     /* Тут import os нормально отработал */
  18.     object tstObj = exec("print('This is Python code...')\n"
  19.                          "import os\n"
  20.                          "print(os.getcwd())\n",
  21.                          main_namespace);
  22.  
  23.     /* А тут import mymodule не импортирует */
  24.     object testTwo = exec_file("test.py", main_namespace);
  25.  
  26.     /* Тоже не катит */
  27.     object mymodObj = import("mymodule");
  28.  
  29.     /* Note that at this time you must not call Py_Finalize() to stop the interpreter. This may be fixed in a future version of boost.python. */
  30.     /* http://www.boost.org/doc/libs/master/libs/python/doc/html/tutorial/tutorial/embedding.html */
  31.     /* Py_Finalize(); */
  32.  
  33.   } catch(error_already_set const &) {
  34.     if (PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
  35.       /* handle ZeroDivisionError specially */
  36.     } else {
  37.       /* print all other errors to stderr */
  38.       PyErr_Print();
  39.     }
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment