Xmipp  v3.23.11-Nereus
Functions
Python Namespace Reference

Functions

std::string print_traceback ()
 
std::string whichPython ()
 
void initPython (const std::string &path)
 
void initPython ()
 
void initPythonAndNumpy ()
 
PyObject * getClassRef (const std::string &moduleName, const std::string &className)
 
PyObject * getFunctionRef (const std::string &moduleName, const std::string &funcName)
 
template<typename T >
PyObject * convertToNumpy (const MultidimArray< T > &array)
 
void initNumpy ()
 
template PyObject * convertToNumpy< double > (MultidimArray< double > const &)
 
template PyObject * convertToNumpy< int > (MultidimArray< int > const &)
 

Function Documentation

◆ convertToNumpy()

template<typename T >
PyObject * Python::convertToNumpy ( const MultidimArray< T > &  array)

Create an Numpy reference array wrapper around data. This reference should be release using Py_DECREF() or Py_XDECREF()

Parameters
arrayto be wrapped
Returns
a reference to the wrapper

Definition at line 112 of file python_utils.cpp.

112  {
113  npy_intp dim[3];
114  dim[0]=XSIZE(array);
115  dim[1]=YSIZE(array);
116  dim[2]=ZSIZE(array);
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);
121  } else {
122  REPORT_ERROR(ERR_TYPE_INCORRECT, "Not implemented");
123  }
124 }
#define YSIZE(v)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define XSIZE(v)
#define ZSIZE(v)
Incorrect type received.
Definition: xmipp_error.h:190

◆ convertToNumpy< double >()

template PyObject* Python::convertToNumpy< double > ( MultidimArray< double > const &  )

◆ convertToNumpy< int >()

template PyObject* Python::convertToNumpy< int > ( MultidimArray< int > const &  )

◆ getClassRef()

PyObject * Python::getClassRef ( const std::string &  moduleName,
const std::string &  className 
)

Get a borrowed reference to a Python class This reference should be release using Py_DECREF() or Py_XDECREF()

Parameters
moduleNamename of the module (script file name)
classNamename of the class within the module
Returns
a borrowed reference to the class on success or nullptr on error

Definition at line 91 of file python_utils.cpp.

91  {
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());
96  Py_DECREF(pName);
97  Py_DECREF(pModule);
98  Py_DECREF(pDict);
99  return pClass;
100 }

◆ getFunctionRef()

PyObject * Python::getFunctionRef ( const std::string &  moduleName,
const std::string &  funcName 
)

Get a new reference to a Python function This reference should be release using Py_DECREF() or Py_XDECREF()

Parameters
moduleNamename of the module (script file name)
funcNamename of the class within the module
Returns
a new reference to the function on success or nullptr on error

Definition at line 102 of file python_utils.cpp.

102  {
103  PyObject * pName = PyUnicode_FromString(moduleName.c_str());
104  PyObject * pModule = PyImport_Import(pName);
105  PyObject * pFunc = PyObject_GetAttrString(pModule, funcName.c_str());
106  Py_DECREF(pName);
107  Py_DECREF(pModule);
108  return pFunc;
109 }

◆ initNumpy()

void Python::initNumpy ( )

Initialize Numpy arrays. Needs to be called from each compile unit

See also
https://stackoverflow.com/questions/31971185/segfault-when-import-array-not-in-same-translation-unit

Definition at line 126 of file python_utils.cpp.

126  {
127 #ifndef NUMPY_IMPORT_ARRAY_RETVAL
128 #define NUMPY_IMPORT_ARRAY_RETVAL NULL
129 #endif
130  auto init = []() -> std::nullptr_t { // explicit on purpose
131  import_array();
132  return nullptr;
133  };
134  init();
135 }

◆ initPython() [1/2]

void Python::initPython ( const std::string &  path)

Initialize default Python path will point to the python which has been initialized

Definition at line 64 of file python_utils.cpp.

64  {
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) {
76  REPORT_ERROR(ERR_VALUE_INCORRECT, "Cannot decode the python path\n");
77  }
78  Py_SetProgramName(program);
79  Py_Initialize();
80 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Incorrect value received.
Definition: xmipp_error.h:195

◆ initPython() [2/2]

void Python::initPython ( )

Initialize default Python

Definition at line 82 of file python_utils.cpp.

82  {
84 }
std::string whichPython()
void initPython()

◆ initPythonAndNumpy()

void Python::initPythonAndNumpy ( )

Initialize default Python and Numpy

Definition at line 86 of file python_utils.cpp.

86  {
87  initPython();
88  initNumpy();
89 }
void initPython()
void initNumpy()

◆ print_traceback()

std::string Python::print_traceback ( )

This function outputs traceback of the last exception to the standard output

Definition at line 29 of file python_utils.cpp.

29  {
30  PyObject* type;
31  PyObject* value;
32  PyObject* traceback;
33 
34  PyErr_Fetch(&type, &value, &traceback);
35  PyErr_NormalizeException(&type, &value, &traceback);
36 
37  std::string fcn = "";
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";
44 
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); //Now a unicode
50  return PyUnicode_AsUTF8(str_exc_type);
51 }
viol type
void mod(const MultidimArray< T > &x, MultidimArray< T > &m, double y)

◆ whichPython()

std::string Python::whichPython ( )

Returns path to the default python intrepreter Equivalent to 'which python'

Definition at line 53 of file python_utils.cpp.

53  {
54  char path[1035];
55  FILE *fp = popen("which python", "r");
56  if (fp == NULL)
57  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot execute which python");
58  if (fgets(path, sizeof(path)-1, fp) == NULL)
59  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot find python");
60  pclose(fp);
61  return path;
62 }
Just to locate unclassified errors.
Definition: xmipp_error.h:192
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211