34 PyErr_Fetch(&type, &value, &traceback);
35 PyErr_NormalizeException(&type, &value, &traceback);
38 fcn +=
"def get_pretty_traceback(exc_type, exc_value, exc_tb):\n";
39 fcn +=
" import sys, traceback\n";
40 fcn +=
" lines = []\n";
41 fcn +=
" lines = traceback.format_exception(exc_type, exc_value, exc_tb)\n";
42 fcn +=
" output = '\\n'.join(lines)\n";
43 fcn +=
" return output\n";
45 PyRun_SimpleString(fcn.c_str());
46 PyObject*
mod = PyImport_ImportModule(
"__main__");
47 PyObject* method = PyObject_GetAttrString(mod,
"get_pretty_traceback");
48 PyObject* outStr = PyObject_CallObject(method, Py_BuildValue(
"OOO", type, value, traceback));
49 PyObject* str_exc_type = PyObject_Str(outStr);
50 return PyUnicode_AsUTF8(str_exc_type);
55 FILE *fp = popen(
"which python",
"r");
58 if (fgets(path,
sizeof(path)-1, fp) == NULL)
65 #if PY_MAJOR_VERSION >= 3 66 #if PY_MINOR_VERSION > 4 67 auto program = Py_DecodeLocale(path.c_str(), NULL);
68 #else // PY_MINOR_VERSION > 4 69 std::vector<wchar_t> tmp(path.begin(), path.end());
70 auto program = tmp.data();
71 #endif // PY_MINOR_VERSION > 4 72 #else // PY_MAJOR_VERSION >= 3 73 #error Python version >= 3 expected 74 #endif // PY_MAJOR_VERSION >= 3 75 if (
nullptr == program) {
78 Py_SetProgramName(program);
91 PyObject *
getClassRef(
const std::string &moduleName,
const std::string &className) {
92 PyObject * pName = PyUnicode_FromString(moduleName.c_str());
93 PyObject * pModule = PyImport_Import(pName);
94 PyObject * pDict = PyModule_GetDict(pModule);
95 PyObject * pClass = PyDict_GetItemString(pDict, className.c_str());
102 PyObject *
getFunctionRef(
const std::string &moduleName,
const std::string &funcName) {
103 PyObject * pName = PyUnicode_FromString(moduleName.c_str());
104 PyObject * pModule = PyImport_Import(pName);
105 PyObject * pFunc = PyObject_GetAttrString(pModule, funcName.c_str());
117 if (std::is_same<T, int>::value) {
118 return PyArray_SimpleNewFromData(3, dim, NPY_INT, array.
data);
119 }
else if (std::is_same<T, double>::value){
120 return PyArray_SimpleNewFromData(3, dim, NPY_DOUBLE, array.
data);
127 #ifndef NUMPY_IMPORT_ARRAY_RETVAL 128 #define NUMPY_IMPORT_ARRAY_RETVAL NULL 130 auto init = []() -> std::nullptr_t {
Just to locate unclassified errors.
std::string whichPython()
#define REPORT_ERROR(nerr, ErrormMsg)
void initPythonAndNumpy()
template PyObject * convertToNumpy< int >(MultidimArray< int > const &)
PyObject * getFunctionRef(const std::string &moduleName, const std::string &funcName)
void mod(const MultidimArray< T > &x, MultidimArray< T > &m, double y)
PyObject * getClassRef(const std::string &moduleName, const std::string &className)
PyObject * convertToNumpy(const MultidimArray< T > &array)
template PyObject * convertToNumpy< double >(MultidimArray< double > const &)
std::string print_traceback()
Incorrect value received.
void initPython(const std::string &path)