How to use boost::python::dict or tuple in main function? -


when use boost::python::tuple or boost::python::dict in main function, program crash!

#define boost_python_static_lib #include <boost/python.hpp> #include <iostream> #include <boost/python/tuple.hpp> #include <boost/python/dict.hpp>  //using namespace std; using namespace boost::python;  int main() {     //tuple x;     //x = make_tuple(object(0),object(1));     dict x;     x["123"] = 3;     return 0; } 

but when use them in .dll, ok, what's wrong?

it's necessary call py_initialize() initialize interpreter before use of python object:

#define boost_python_static_lib #include <boost/python.hpp> #include <iostream> #include <boost/python/tuple.hpp> #include <boost/python/dict.hpp>  //using namespace std; using namespace boost::python;  int main() {     py_initialize();     dict x;     x["123"] = 3;     return 0; } 

boost python offers lot of functionalities interface c++ python, funcionalities create c extensions in higher level using c++. code above correspondent code bellow:

#include <python.h> #include <dictobject.h>  int main(int argc, char *argv[]) {     py_initialize();     pyobject *d = pydict_new();     pydict_setitemstring(d, "123", pylong_fromlong(3));     return 0; } 

inside pydict_setitemstring there call pyunicode_interninplace tries use string exists, otherwise creates new 1 (remember python strings immutable). segfault (when py_initilize not called) occurs inside function, because python needs query it's runtime environment check string, once envirnment not loaded, crashes.

it's not necessary explicit call py_initilize when creating .dll, because imported inside interpreter called during initialization.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -