Example #1
0
void exec_test()
{
  // Register the module with the interpreter
  if (PyImport_AppendInittab("embedded_hello", initembedded_hello) == -1)
    throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
                 "builtin modules");
  // Retrieve the main module
  python::object main = python::import("__main__");
  
  // Retrieve the main module's namespace
  python::object global(main.attr("__dict__"));

  // Define the derived class in Python.
  python::object result = python::exec(
    "from embedded_hello import *        \n"
    "class PythonDerived(Base):          \n"
    "    def hello(self):                \n"
    "        return 'Hello from Python!' \n",
    global, global);

  python::object PythonDerived = global["PythonDerived"];

  // Creating and using instances of the C++ class is as easy as always.
  CppDerived cpp;
  BOOST_TEST(cpp.hello() == "Hello from C++!");

  // But now creating and using instances of the Python class is almost
  // as easy!
  python::object py_base = PythonDerived();
  Base& py = python::extract<Base&>(py_base) BOOST_EXTRACT_WORKAROUND;

  // Make sure the right 'hello' method is called.
  BOOST_TEST(py.hello() == "Hello from Python!");
}
void test()
{
//- INITIALIZATION -----------------------------------------------------------//

    // Register the module with the interpreter
    if (PyImport_AppendInittab("embedded_hello", initembedded_hello) == -1)
        throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
                                 "builtin modules");

    // Initialize the interpreter
    Py_Initialize();

    // Retrieve the main module
    python::object main_module((
        python::handle<>(python::borrowed(PyImport_AddModule("__main__")))));

    // Retrieve the main module's namespace
    python::object main_namespace((main_module.attr("__dict__")));

    // Define the derived class in Python.
    // (You'll normally want to put this in a .py file.)
    python::handle<> result(
        PyRun_String(
        "from embedded_hello import *        \n"
        "class PythonDerived(Base):          \n"
        "    def hello(self):                \n"
        "        return 'Hello from Python!' \n",
        Py_file_input, main_namespace.ptr(), main_namespace.ptr())
        );
    // Result is not needed
    result.reset();

    // Extract the raw Python object representing the just defined derived class
    python::handle<> class_ptr(
        PyRun_String("PythonDerived\n", Py_eval_input,
                     main_namespace.ptr(), main_namespace.ptr()) );

    // Wrap the raw Python object in a Boost.Python object
    python::object PythonDerived(class_ptr);

//- MAIN PROGRAM -------------------------------------------------------------//

    // Creating and using instances of the C++ class is as easy as always.
    CppDerived cpp;
    std::cout << cpp.hello() << std::endl;

    // But now creating and using instances of the Python class is almost
    // as easy!
    python::object py_base = PythonDerived();
    Base& py = python::extract<Base&>(py_base)
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_WIN, <= 810)
        ()
#endif 
        ;
    std::cout << py.hello() << std::endl;
}