This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "python_interpreter.h" | ||
#include "notebook.h" | ||
#include "config.h" | ||
#include <iostream> | ||
#include <pygobject.h> | ||
|
||
inline pybind11::module pyobject_from_gobj(gpointer ptr){ | ||
auto obj=G_OBJECT(ptr); | ||
if(obj) | ||
return pybind11::module(pygobject_new(obj), false); | ||
return pybind11::module(Py_None, false); | ||
} | ||
|
||
PythonInterpreter::PythonInterpreter(){ | ||
auto init_juci_api=[](){ | ||
pybind11::module(pygobject_init(-1,-1,-1), false); | ||
pybind11::module api("jucpp", "Python bindings for juCi++"); | ||
api.def("get_current_text_buffer", [](){ | ||
auto view=Notebook::get().get_current_view(); | ||
if(view) | ||
return pyobject_from_gobj(view->gobj()); | ||
return pybind11::module(Py_None, false); | ||
}); | ||
return api.ptr(); | ||
}; | ||
PyImport_AppendInittab("jucipp", init_juci_api); | ||
Py_Initialize(); | ||
Config::get().load(); | ||
} | ||
|
||
pybind11::module PythonInterpreter::get_loaded_module(const std::string &module_name){ | ||
return pybind11::module(PyImport_AddModule(module_name.c_str()), true); | ||
} | ||
|
||
pybind11::module PythonInterpreter::import(const std::string &module_name){ | ||
return pybind11::module(PyImport_ImportModule(module_name.c_str()), false); | ||
} | ||
|
||
void PythonInterpreter::add_path(const boost::filesystem::path &path){ | ||
std::wstring sys_path(Py_GetPath()); | ||
if(!sys_path.empty()) | ||
#ifdef _WIN32 | ||
sys_path += ';'; | ||
#else | ||
sys_path += ':'; | ||
#endif | ||
sys_path += path.generic_wstring(); | ||
Py_SetPath(sys_path.c_str()); | ||
} | ||
|
||
PythonInterpreter::~PythonInterpreter(){ | ||
auto err=PythonError(); | ||
if(Py_IsInitialized()) | ||
Py_Finalize(); | ||
if(err) | ||
std::cerr << std::string(err) << std::endl; | ||
} | ||
|
||
PythonError::PythonError(){ | ||
pybind11::object error(PyErr_Occurred(), false); | ||
if(error){ | ||
PyObject *exception,*value,*traceback; | ||
PyErr_Fetch(&exception,&value,&traceback); | ||
PyErr_NormalizeException(&exception,&value,&traceback); | ||
try{ | ||
exp=std::string(pybind11::object(exception,false).str()); | ||
val=std::string(pybind11::object(value,false).str()); | ||
trace=std::string(pybind11::object(traceback,false).str()); | ||
} catch (const std::runtime_error &e){ | ||
exp=e.what(); | ||
} | ||
} | ||
} | ||
|
||
PythonError::operator std::string(){ | ||
return exp + "\n" + val + "\n" + trace; | ||
} | ||
|
||
PythonError::operator bool(){ | ||
return !exp.empty(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef JUCI_PYTHON_INTERPRETER_H_ | ||
#define JUCI_PYTHON_INTERPRETER_H_ | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <boost/filesystem/path.hpp> | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
class PythonInterpreter { | ||
private: | ||
PythonInterpreter(); | ||
~PythonInterpreter(); | ||
wchar_t *argv; | ||
public: | ||
static PythonInterpreter& get(){ | ||
static PythonInterpreter singleton; | ||
return singleton; | ||
} | ||
pybind11::module get_loaded_module(const std::string &module_name); | ||
pybind11::module import(const std::string &module_name); | ||
void add_path(const boost::filesystem::path &path); | ||
}; | ||
|
||
class PythonError { | ||
public: | ||
PythonError(); | ||
operator std::string(); | ||
operator bool(); | ||
std::string exp, val, trace; | ||
}; | ||
|
||
#endif // JUCI_PYTHON_INTERPRETER_H_ |