Remoto - VFS: pythonUtils.cpp Source File
Remoto - VFS
pythonUtils.cpp
Go to the documentation of this file.
1 
2 #include "pythonUtils.h"
3 
4 #include "VFS.h"
5 
6 PyObject *py::loadModule(QString path)
7 {
8  VFS::LOG( QString("Loading module '%1'").arg(path), 9, "python");
9 
10  PyObject *module;
11 
12  module = PyImport_ImportModule(qUtf8Printable(path));
13 
14  if (module == nullptr)
15  {
16  if (PyErr_Occurred())
17  {
19 
20  VFS::ERROR( QString("Failed to load module '%1'").arg(path), 0, "python" );
21  }
22  //else
23  // printf("No ERROR!\n");
24  }
25 
26  return module;
27 }
28 
29 QString py::callObjectMethod(PyObject *object, QString method, PyObject *args)
30 {
31  //methodcall = PyObject_CallMethod(instance, (char *) "name", (char *) "(s)", (char *) "Bill");
32 
33  PyObject *pFunc = PyObject_GetAttrString(object, qUtf8Printable(method));
34 
35  QString ret;
36 
37  if (PyCallable_Check(pFunc))
38  {
39  PyObject *result = PyObject_CallObject(pFunc,args);
40  if (result)
41  {
42  if (PyString_Check(result))
43  ret = QString(PyString_AsString(result));
44  // if (PyUnicode_Check(result))
45  // //ret = QString(PyUnicode_AsUTF8String(result));
46  // ret = QString(PyBytes_AsString(result));
47  else
48  ret = "";
49 
50  Py_XDECREF(result);
51  }
52  if (PyErr_Occurred())
54  }
55  else
56  VFS::WARN( QString("Can't call '%1' method.").arg(method) );
57 
58  Py_XDECREF(pFunc);
59 
60  return ret;
61 }
62 
63 /*
64 PyObject *py::callObjectCreator(PyObject *object, QString method, PyObject *args)
65 {
66  PyObject *pFunc = PyObject_GetAttrString(object, qUtf8Printable(method));
67 
68  if (PyCallable_Check(pFunc))
69  {
70  PyObject *result = PyObject_CallObject(pFunc,args);
71  if (result)
72  { Py_XDECREF(pFunc);
73  return result;
74  }
75 
76  if (PyErr_Occurred())
77  py::printError();
78  }
79  else
80  VFS::WARN( QString("Can't call '%1' method.").arg(method) );
81 
82  Py_XDECREF(pFunc);
83 
84  return nullptr;
85 }
86 */
87 
89 {
90  //PyErr_Print();
91 
92  PyObject *ptype, *pvalue, *ptraceback;
93  PyObject *stype, *svalue;
94  PyErr_Fetch(&ptype, &pvalue, &ptraceback);
95  //pvalue contains error message
96  //ptraceback contains stack snapshot and many other information
97  //ptype contains error/exception type
98 
99  stype = PyObject_Str(ptype);
100  char *errorMessage = PyString_AsString(stype);
101  //char *errorMessage = PyBytes_AsString(stype);
102  Py_DECREF(stype);
103 
104  svalue = PyObject_Str(pvalue);
105  char *errorType = PyString_AsString(svalue);
106  //char *errorType = PyBytes_AsString(svalue);
107  Py_DECREF(svalue);
108 
109  if (ptype) Py_DECREF(ptype);
110  if (pvalue) Py_DECREF(pvalue);
111  if (ptraceback) Py_DECREF(ptraceback);
112 
113  VFS::ERROR( QString("%1").arg(errorMessage), 0, "python" );
114  VFS::ERROR( QString("%1").arg(errorType), 0, "python" );
115 }
static void LOG(QString message, int level=0, QString user="server")
Send a message to the VFS::_messages VFS_stream.
Definition: VFS.cpp:209
static void ERROR(QString message, int level=0, QString user="server")
Send a message to the VFS::_errors VFS_stream.
Definition: VFS.cpp:307
static void WARN(QString message, int level=0, QString user="server")
Send a message to the VFS::_warnings VFS_stream.
Definition: VFS.cpp:258
getter path
a getter DOCME
QString callObjectMethod(PyObject *object, QString method, PyObject *args=nullptr)
Definition: pythonUtils.cpp:29
PyObject * loadModule(QString path)
Definition: pythonUtils.cpp:6
void printError()
Definition: pythonUtils.cpp:88