stack smashing calling python function importing tensorflow c++ -
i new tensorflow including python code in c++, therefore apprechiate tips/comments on following weird behaviour: have c++ class pythoninterface headerfile pythoninterface.h:
#include <string> #include <iostream> class pythoninterface{ private: const char* file; const char* funct; const char* filepath; public: pythoninterface(); ~pythoninterface(); void callfunction(); }; the sourcefile pythoninterface.cpp:
#include <python.h> #include <string> #include <sstream> #include <vector> #include "pythoninterface.h" pythoninterface::pythoninterface(){ file = "tensorflowincludepy"; funct = "mytestfunction"; filepath = "/path/to/tensorflowincludepy.py"; } void pythoninterface::callfunction(){ pyobject *pname, *pmodule, *pdict, *pfunc, *pvalue, *presult; // initialize python interpreter py_initialize(); //set in path find custom python module other path python's system modules/packages found. std::stringstream changepath; changepath << "import sys; sys.path.insert(0, '" << filepath << "')"; const std::string tmp = changepath.str(); filepath = tmp.c_str(); pyrun_simplestring (this->filepath); // build name object pname = pystring_fromstring(this->file); // load module object pmodule = pyimport_import(pname); if(pmodule != null) { // pdict borrowed reference pdict = pymodule_getdict(pmodule); // pfunc borrowed reference pfunc = pydict_getitemstring(pdict, this->funct); if (pycallable_check(pfunc)) { pvalue=py_buildvalue("()"); printf("pvalue empty!\n"); pyerr_print(); presult=pyobject_callobject(pfunc,pvalue); pyerr_print(); } else { pyerr_print(); } printf("result %d!\n",pyint_aslong(presult)); py_decref(pvalue); // clean py_decref(pmodule); py_decref(pname); } else{ std::cout << "python retuned null pointer, no file!" << std::endl; } // finish python interpreter py_finalize(); } and python file function should included (tensorflowincludepy.py):
def mytestfunction(): print 'i function without input!' gettingstartedtf() return 42 def gettingstartedtf(): import tensorflow tf #at point error occurs! hello = tf.constant('hello, tensorflow!') sess = tf.session() print(sess.run(hello)) return 42 finally in main function create pythoninterface object p , call function p.callfunction(). communication between c++ , python code works right, when (at runtime) line import tensorflow tf reached, *** stack smashing detected *** error message , program finishes. can guess problem might or had similar issue before?
i know there c++ tensorflow api, feel more comfortable using tensorflow in python thought might perfect solution me (apparently not...:p)
Comments
Post a Comment