diff --git a/embedding.py b/embedding.py new file mode 100644 index 000000000..32977492f --- /dev/null +++ b/embedding.py @@ -0,0 +1 @@ +print("hello llama.cpp") diff --git a/plugin_python.cpp b/plugin_python.cpp new file mode 100644 index 000000000..324b18d56 --- /dev/null +++ b/plugin_python.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +class Base { +public: + Base() : mName("Base") {} + Base(const std::string& name) : mName(name) {} + virtual ~Base() {} + std::string name() const + { return mName; } +private: + std::string mName; +}; + + +using namespace boost::python; + +#if PY_MAJOR_VERSION >= 3 +# define INIT_MODULE PyInit_mymodule + extern "C" PyObject* INIT_MODULE(); +#else +# define INIT_MODULE initmymodule + extern "C" void INIT_MODULE(); +#endif + + +int call_python() +{ + try { + PyImport_AppendInittab((char*)"mymodule", INIT_MODULE); + Py_Initialize(); + object main_module = import("__main__"); + dict main_namespace = extract(main_module.attr("__dict__")); + object mymodule = import("mymodule"); + + main_namespace["precreated_object"] = Base("created on C++ side"); + exec_file("embedding.py", main_namespace, main_namespace); + } catch (error_already_set& e) { + PyErr_PrintEx(0); + return 1; + } + return 0; +} + + +using namespace boost::python; + +BOOST_PYTHON_MODULE(mymodule) +{ + class_("Base") + .def("__str__", &Base::name) + ; +} +