Skip to content
This repository was archived by the owner on Jun 25, 2020. It is now read-only.

Commit bb6b4aa

Browse files
Jørgen Lien Sellægzalox
Jørgen Lien Sellæg
authored andcommitted
feature: Update pybind11 to latest version
1 parent 868f4c6 commit bb6b4aa

File tree

9 files changed

+111
-175
lines changed

9 files changed

+111
-175
lines changed

ci/update_ci.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ function windows () {
3232
if [ "$PLATFORM" == "x86" ]; then
3333
arch=i686
3434
fi
35-
sh -c "pacman -S --noconfirm git mingw-w64-${arch}-cmake make mingw-w64-${arch}-toolchain mingw-w64-${arch}-clang mingw-w64-${arch}-gtkmm3 mingw-w64-${arch}-gtksourceviewmm3 mingw-w64-${arch}-boost mingw-w64-${arch}-aspell mingw-w64-${arch}-aspell-en mingw-w64-${arch}-libgit2"
35+
sh -c "pacman -S --noconfirm git mingw-w64-${arch}-pygobject-devel mingw-w64-${arch}-cmake make mingw-w64-${arch}-toolchain mingw-w64-${arch}-clang mingw-w64-${arch}-gtkmm3 mingw-w64-${arch}-gtksourceviewmm3 mingw-w64-${arch}-boost mingw-w64-${arch}-aspell mingw-w64-${arch}-aspell-en mingw-w64-${arch}-libgit2"
3636
}
3737

3838
if [ "$TRAVIS_OS_NAME" == "" ]; then
3939
TRAVIS_OS_NAME=windows
4040
fi
4141

42-
$TRAVIS_OS_NAME
42+
$TRAVIS_OS_NAME

docs/install.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ make install
138138

139139
Install dependencies (replace `x86_64` with `i686` for 32-bit MSYS2 installs):
140140
```sh
141-
pacman -S git mingw-w64-x86_64-cmake make mingw-w64-x86_64-toolchain mingw-w64-x86_64-clang mingw-w64-x86_64-gtkmm3 mingw-w64-x86_64-gtksourceviewmm3 mingw-w64-x86_64-boost mingw-w64-x86_64-aspell mingw-w64-x86_64-aspell-en mingw-w64-x86_64-libgit2 mingw-w64-x86_64-universal-ctags-git
141+
pacman -S git mingw-w64-x86_64-cmake make mingw-w64-x86_64-toolchain mingw-w64-x86_64-clang mingw-w64-x86_64-gtkmm3 mingw-w64-x86_64-gtksourceviewmm3 mingw-w64-x86_64-boost mingw-w64-x86_64-aspell mingw-w64-x86_64-aspell-en mingw-w64-x86_64-gobject-introspection mingw-w64-x86_64-pygobject-devel mingw-w64-x86_64-python3-gobject mingw-w64-x86_64-libgit2 mingw-w64-x86_64-universal-ctags-git
142142
```
143143

144144
Note that juCi++ must be built and run in a MinGW Shell (for instance MinGW-w64 Win64 Shell).

pybind11

Submodule pybind11 updated 184 files

src/config.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void Config::find_or_create_config_files() {
5656
auto config_json = config_dir/"config.json";
5757

5858
boost::filesystem::create_directories(config_dir); // io exp captured by calling method
59-
boost::filesystem::create_directories(home/"plugins");
59+
boost::filesystem::create_directories(home_juci_path/"plugins");
6060

6161
if (!boost::filesystem::exists(config_json))
6262
filesystem::write(config_json, default_config_file);
@@ -118,8 +118,8 @@ void Config::retrieve_config() {
118118
}
119119
}
120120
#endif
121-
python.plugin_directory=cfg.get<std::string>("python.plugin_directory",(home/"plugins").string());
122-
python.site_packages=cfg.get<std::string>("python.site_packages","/usr/lib/python3.5/site-packages");
121+
python.plugin_directory=cfg.get<std::string>("python.plugin_directory",(home_juci_path/"plugins").string());
122+
python.site_packages=cfg.get<std::string>("python.site_packages","");
123123
}
124124

125125
bool Config::add_missing_nodes(const boost::property_tree::ptree &default_cfg, std::string parent_path) {

src/menu.cc

+2
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ void Menu::set_keys() {
478478
}
479479
}
480480

481+
Menu::Menu() : plugin_menu(nullptr) {}
482+
481483
void Menu::build() {
482484
try {
483485
builder = Gtk::Builder::create_from_string(menu_xml);

src/menu.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include <gtkmm.h>
77

88
class Menu {
9-
Menu() {}
9+
private:
10+
Menu();
1011
public:
1112
static Menu &get() {
1213
static Menu singleton;
@@ -21,11 +22,9 @@ class Menu {
2122

2223
Glib::RefPtr<Gio::Menu> juci_menu;
2324
Glib::RefPtr<Gio::Menu> window_menu;
24-
2525
std::unique_ptr<Gtk::Menu> right_click_line_menu;
2626
std::unique_ptr<Gtk::Menu> right_click_selected_menu;
2727
Glib::RefPtr<Gio::Menu> plugin_menu;
28-
2928
private:
3029
Glib::RefPtr<Gtk::Builder> builder;
3130
};

src/python_interpreter.cc

+65-115
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,25 @@ static wchar_t* DecodeLocale(const char* arg, size_t *size)
2020
inline pybind11::module pyobject_from_gobj(gpointer ptr){
2121
auto obj=G_OBJECT(ptr);
2222
if(obj)
23-
return pybind11::module(pygobject_new(obj), false);
24-
return pybind11::module(Py_None, false);
23+
return pybind11::reinterpret_steal<pybind11::module>(pygobject_new(obj));
24+
return pybind11::reinterpret_steal<pybind11::module>(Py_None);
2525
}
2626

2727
Python::Interpreter::Interpreter(){
28-
#ifdef _WIN32
29-
auto root_path=Config::get().terminal.msys2_mingw_path;
30-
append_path(root_path/"include/python3.5m");
31-
append_path(root_path/"lib/python3.5");
32-
long long unsigned size = 0L;
33-
#else
34-
long unsigned size = 0L;
35-
#endif
28+
3629
auto init_juci_api=[](){
37-
pybind11::module(pygobject_init(-1,-1,-1),false);
30+
auto module = pybind11::reinterpret_steal<pybind11::module>(pygobject_init(-1,-1,-1));
3831
pybind11::module api("jucpp","Python bindings for juCi++");
3932
api
40-
.def("get_juci_home",[](){return Config::get().juci_home_path().string();})
33+
.def("get_juci_home",[](){return Config::get().home_juci_path.string();})
4134
.def("get_plugin_folder",[](){return Config::get().python.plugin_directory;});
4235
api
4336
.def_submodule("editor")
4437
.def("get_current_gtk_source_view",[](){
4538
auto view=Notebook::get().get_current_view();
4639
if(view)
4740
return pyobject_from_gobj(view->gobj());
48-
return pybind11::module(Py_None,false);
41+
return pybind11::reinterpret_steal<pybind11::module>(Py_None);
4942
})
5043
.def("get_file_path",[](){
5144
auto view=Notebook::get().get_current_view();
@@ -55,13 +48,12 @@ Python::Interpreter::Interpreter(){
5548
});
5649
api
5750
.def("get_gio_plugin_menu",[](){
58-
auto &plugin_menu=Menu::get().plugin_menu;
59-
if(!plugin_menu){
60-
plugin_menu=Gio::Menu::create();
61-
plugin_menu->append("<empty>");
62-
Menu::get().window_menu->append_submenu("_Plugins",plugin_menu);
51+
if(!Menu::get().plugin_menu){
52+
Menu::get().plugin_menu=Gio::Menu::create();
53+
Menu::get().plugin_menu->append("<empty>");
54+
Menu::get().window_menu->append_submenu("_Plugins",Menu::get().plugin_menu);
6355
}
64-
return pyobject_from_gobj(plugin_menu->gobj());
56+
return pyobject_from_gobj(Menu::get().plugin_menu->gobj());
6557
})
6658
.def("get_gio_window_menu",[](){return pyobject_from_gobj(Menu::get().window_menu->gobj());})
6759
.def("get_gio_juci_menu",[](){return pyobject_from_gobj(Menu::get().juci_menu->gobj());})
@@ -76,129 +68,87 @@ Python::Interpreter::Interpreter(){
7668
return api.ptr();
7769
};
7870
PyImport_AppendInittab("jucipp", init_juci_api);
71+
7972
Config::get().load();
80-
auto plugin_path=Config::get().python.plugin_directory;
81-
add_path(Config::get().python.site_packages);
82-
add_path(plugin_path);
73+
configure_path();
8374
Py_Initialize();
75+
#ifdef _WIN32
76+
long long unsigned size = 0L;
77+
#else
78+
long unsigned size = 0L;
79+
#endif
8480
argv=DecodeLocale("",&size);
8581
PySys_SetArgv(0,&argv);
86-
auto sys=get_loaded_module("sys");
87-
auto exc_func=[](pybind11::object type,pybind11::object value,pybind11::object traceback){
88-
if(!given_exception_matches(type,PyExc_SyntaxError))
89-
Terminal::get().print(Error(type,value,traceback),true);
90-
else
91-
Terminal::get().print(SyntaxError(type,value,traceback),true);
92-
};
93-
sys.attr("excepthook")=pybind11::cpp_function(exc_func);
9482
boost::filesystem::directory_iterator end_it;
95-
for(boost::filesystem::directory_iterator it(plugin_path);it!=end_it;it++){
83+
for(boost::filesystem::directory_iterator it(Config::get().python.plugin_directory);it!=end_it;it++){
9684
auto module_name=it->path().stem().string();
9785
if(module_name.empty())
98-
break;
86+
continue;
9987
auto is_directory=boost::filesystem::is_directory(it->path());
10088
auto has_py_extension=it->path().extension()==".py";
10189
auto is_pycache=module_name=="__pycache__";
10290
if((is_directory && !is_pycache)||has_py_extension){
103-
auto module=import(module_name);
104-
if(!module){
105-
auto msg="Error loading plugin `"+module_name+"`:\n";
106-
auto err=std::string(Error());
107-
Terminal::get().print(msg+err+"\n");
91+
try {
92+
pybind11::module::import(module_name.c_str());
93+
} catch (pybind11::error_already_set &error) {
94+
Terminal::get().print("Error loading plugin `"+module_name+"`:\n"+error.what()+"\n");
10895
}
10996
}
11097
}
98+
auto sys=find_module("sys");
99+
if(sys){
100+
auto exc_func=[](pybind11::object type,pybind11::object value,pybind11::object traceback){
101+
std::cerr << "ERROR FUNCTION";
102+
};
103+
sys.attr("excepthook")=pybind11::cpp_function(exc_func);
104+
} else {
105+
std::cerr << "Failed to set exception hook\n";
106+
}
111107
}
112108

113-
pybind11::module Python::get_loaded_module(const std::string &module_name){
114-
return pybind11::module(PyImport_AddModule(module_name.c_str()), true);
115-
}
116-
117-
pybind11::module Python::import(const std::string &module_name){
118-
return pybind11::module(PyImport_ImportModule(module_name.c_str()), false);
119-
}
120-
121-
pybind11::module Python::reload(pybind11::module &module){
122-
return pybind11::module(PyImport_ReloadModule(module.ptr()),false);
109+
pybind11::module Python::Interpreter::find_module(const std::string &module_name){
110+
return pybind11::reinterpret_borrow<pybind11::module>(PyImport_AddModule(module_name.c_str()));
123111
}
124112

125-
Python::SyntaxError::SyntaxError(pybind11::object type,pybind11::object value,pybind11::object traceback)
126-
: Error(type,value,traceback){}
127-
128-
Python::Error::Error(pybind11::object type,pybind11::object value,pybind11::object traceback){
129-
exp=type;
130-
val=value;
131-
trace=traceback;
113+
pybind11::module Python::Interpreter::reload(pybind11::module &module){
114+
auto reload=pybind11::reinterpret_steal<pybind11::module>(PyImport_ReloadModule(module.ptr()));
115+
if(!reload)
116+
throw pybind11::error_already_set();
117+
return reload;
132118
}
133119

134-
void Python::Interpreter::add_path(const boost::filesystem::path &path){
135-
if(path.empty())
136-
return;
137-
std::wstring sys_path(Py_GetPath());
138-
if(!sys_path.empty())
139-
#ifdef _WIN32
140-
sys_path += ';';
141-
#else
142-
sys_path += ':';
143-
#endif
144-
sys_path += path.generic_wstring();
120+
void Python::Interpreter::configure_path(){
121+
const std::vector<boost::filesystem::path> python_path = {
122+
"/usr/lib/python3.6",
123+
"/usr/lib/python3.6/lib-dynload",
124+
"/usr/lib/python3.6/site-packages",
125+
Config::get().python.site_packages,
126+
Config::get().python.plugin_directory
127+
};
128+
std::wstring sys_path;
129+
for(auto &path:python_path){
130+
if(path.empty())
131+
continue;
132+
if(!sys_path.empty()){
133+
#ifdef _WIN32
134+
sys_path += ';';
135+
#else
136+
sys_path += ':';
137+
#endif
138+
}
139+
sys_path += path.generic_wstring();
140+
}
145141
Py_SetPath(sys_path.c_str());
146142
}
147143

148144
Python::Interpreter::~Interpreter(){
149-
auto err=Error();
150145
if(Py_IsInitialized())
151146
Py_Finalize();
152-
if(err)
153-
std::cerr << std::string(err) << std::endl;
147+
if(error())
148+
std::cerr << pybind11::error_already_set().what() << std::endl;
154149
}
155150

156-
pybind11::object Python::error_occured(){
157-
return pybind11::object(PyErr_Occurred(),true);
151+
pybind11::object Python::Interpreter::error(){
152+
return pybind11::reinterpret_borrow<pybind11::object>(PyErr_Occurred());
158153
}
159154

160-
bool Python::thrown_exception_matches(pybind11::handle exception_type){
161-
return PyErr_ExceptionMatches(exception_type.ptr());
162-
}
163-
164-
bool Python::given_exception_matches(const pybind11::object &exception, pybind11::handle exception_type){
165-
return PyErr_GivenExceptionMatches(exception.ptr(),exception_type.ptr());
166-
}
167-
168-
Python::Error::Error(){
169-
if(error_occured()){
170-
try{
171-
PyErr_Fetch(&exp.ptr(),&val.ptr(),&trace.ptr());
172-
PyErr_NormalizeException(&exp.ptr(),&val.ptr(),&trace.ptr());
173-
}catch(const std::exception &e) {
174-
Terminal::get().print(e.what(),true);
175-
}
176-
}
177-
}
178-
179-
Python::Error::operator std::string(){
180-
return std::string(exp.str())+"\n"+std::string(val.str())+"\n";
181-
}
182-
183-
Python::SyntaxError::SyntaxError():Error(){
184-
if(val){
185-
_Py_IDENTIFIER(msg);
186-
_Py_IDENTIFIER(lineno);
187-
_Py_IDENTIFIER(offset);
188-
_Py_IDENTIFIER(text);
189-
exp=std::string(pybind11::str(_PyObject_GetAttrId(val.ptr(),&PyId_msg),false));
190-
text=std::string(pybind11::str(_PyObject_GetAttrId(val.ptr(),&PyId_text),false));
191-
pybind11::object py_line_number(_PyObject_GetAttrId(val.ptr(),&PyId_lineno),false);
192-
pybind11::object py_line_offset(_PyObject_GetAttrId(val.ptr(),&PyId_offset),false);
193-
line_number=pybind11::cast<int>(py_line_number);
194-
line_offset=pybind11::cast<int>(py_line_offset);
195-
}
196-
}
197-
198-
Python::SyntaxError::operator std::string(){
199-
return exp+" ("+std::to_string(line_number)+":"+std::to_string(line_offset)+"):\n"+text;
200-
}
201-
202-
Python::Error::operator bool(){
203-
return exp || trace || val;
204-
}

src/python_interpreter.h

+6-31
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,23 @@
77
#include <iostream>
88
using namespace std;
99

10-
class Python {
11-
public:
10+
namespace Python {
1211
class Interpreter {
12+
public:
13+
pybind11::module static find_module(const std::string &module_name);
14+
pybind11::module static reload(pybind11::module &module);
15+
pybind11::object static error();
1316
private:
1417
Interpreter();
1518
~Interpreter();
1619
wchar_t *argv;
17-
void add_path(const boost::filesystem::path &path);
20+
void configure_path();
1821
public:
1922
static Interpreter& get(){
2023
static Interpreter singleton;
2124
return singleton;
2225
}
2326
};
24-
25-
pybind11::module static get_loaded_module(const std::string &module_name);
26-
pybind11::module static import(const std::string &module_name);
27-
pybind11::module static reload(pybind11::module &module);
28-
29-
class Error {
30-
public:
31-
Error();
32-
Error(pybind11::object type,pybind11::object value,pybind11::object traceback);
33-
operator std::string();
34-
operator bool();
35-
pybind11::object exp, val, trace;
36-
};
37-
38-
class SyntaxError : public Error{
39-
public:
40-
SyntaxError();
41-
SyntaxError(pybind11::object type,pybind11::object value,pybind11::object traceback);
42-
operator std::string();
43-
std::string exp, text;
44-
int line_number, line_offset;
45-
};
46-
47-
bool static thrown_exception_matches(pybind11::handle exception_type);
48-
bool static given_exception_matches(const pybind11::object &exception,pybind11::handle exception_type);
49-
50-
private:
51-
pybind11::object static error_occured();
5227
};
5328

5429
#endif // JUCI_PYTHON_INTERPRETER_H_

0 commit comments

Comments
 (0)