15 #ifdef BORNAGAIN_PYTHON
27 PyObject* pyStr = PyUnicode_AsEncodedString(obj,
"utf-8",
"Error ~");
28 result = std::string(PyBytes_AsString(pyStr));
35 std::vector<std::string> result;
37 if (PyTuple_Check(obj)) {
38 for (Py_ssize_t i = 0; i < PyTuple_Size(obj); i++) {
39 PyObject* value = PyTuple_GetItem(obj, i);
43 }
else if (PyList_Check(obj)) {
44 for (Py_ssize_t i = 0; i < PyList_Size(obj); i++) {
45 PyObject* value = PyList_GetItem(obj, i);
50 throw std::runtime_error(
"PyUtils::toVectorString() -> Error. Unexpected object.");
68 return std::string(wstr.begin(), wstr.end());
73 if (!Py_IsInitialized()) {
77 PyObject* sysPath = PySys_GetObject((
char*)
"path");
78 PyList_Append(sysPath, PyString_FromString(path.c_str()));
84 PyOS_sighandler_t sighandler;
85 sighandler = PyOS_getsig(SIGINT);
87 PyObject* pmod = PyImport_ImportModule(
"bornagain");
90 throw std::runtime_error(
"Can't load bornagain");
95 PyOS_setsig(SIGINT, sighandler);
104 std::stringstream result;
107 result << std::string(60,
'=') <<
"\n";
113 result <<
"Py_GetProgramName(): " <<
PyUtils::toString(Py_GetProgramName()) <<
"\n";
114 result <<
"Py_GetProgramFullPath(): " <<
PyUtils::toString(Py_GetProgramFullPath()) <<
"\n";
116 result <<
"Py_GetPythonHome(): " <<
PyUtils::toString(Py_GetPythonHome()) <<
"\n";
119 PyObject* sysPath = PySys_GetObject((
char*)
"path");
121 result <<
"sys.path: ";
122 for (
auto s : content)
134 std::stringstream result;
136 if (PyErr_Occurred()) {
137 PyObject *ptype, *pvalue, *ptraceback, *pystr;
139 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
140 pystr = PyObject_Str(pvalue);
141 if (
char* str = PyString_AsString(pystr))
142 result << std::string(str) <<
"\n";
144 PyObject* module_name = PyString_FromString(
"traceback");
145 PyObject* pyth_module = PyImport_Import(module_name);
146 Py_DecRef(module_name);
150 PyObject* pyth_func = PyObject_GetAttrString(pyth_module,
"format_exception");
151 if (pyth_func && PyCallable_Check(pyth_func)) {
153 PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
155 pystr = PyObject_Str(pyth_val);
156 if (
char* str = PyString_AsString(pystr))
157 result << std::string(str);
174 const size_t ndim(1);
175 npy_int ndim_numpy = ndim;
176 npy_intp* ndimsizes_numpy =
new npy_intp[ndim];
177 ndimsizes_numpy[0] = data.size();
180 PyObject* pyarray = PyArray_SimpleNew(ndim_numpy, ndimsizes_numpy, NPY_DOUBLE);
181 delete[] ndimsizes_numpy;
182 if (pyarray ==
nullptr)
183 throw std::runtime_error(
"ExportOutputData() -> Panic in PyArray_SimpleNew");
186 double* array_buffer = (
double*)PyArray_DATA((PyArrayObject*)pyarray);
188 for (
size_t index = 0; index < data.size(); ++index)
189 *array_buffer++ = data[index];
Includes python header and takes care of warnings.
Defines PyUtils namespace.
Defines various stuff in namespace Utils.
std::vector< std::string > toVectorString(PyObject *obj)
Converts PyObject into vector of strings, if possible, or throws exception.
PyObject * createNumpyArray(const std::vector< double > &data)
std::string toString(PyObject *obj)
Converts PyObject into string, if possible, or throws exception.
std::string pythonStackTrace()
Returns string representing python stack trace.
void import_bornagain(const std::string &path="")
Imports BornAgain from given location. If path is empty, tries to rely on PYTHONPATH.
std::string pythonRuntimeInfo()
Returns multi-line string representing PATH, PYTHONPATH, sys.path and other info.
std::string getenv(const std::string &name)
Returns environment variable.