-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonobject.cpp
More file actions
102 lines (84 loc) · 2.45 KB
/
Copy pathpythonobject.cpp
File metadata and controls
102 lines (84 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "pythonobject.h"
#include "PythonBinding.h"
#include "generalHelpers.h"
using namespace zPyInt;
PythonObject::PythonObject()
{
}
PythonObject::~PythonObject()
{
Py_DECREF(m_pClass);
Py_DECREF(m_pInst);
}
PyObject* PythonObject::callFunction(const QString &p_name, QList<PyObject *> p_args)
{
int i=0;
int argSize = p_args.size();
if (m_pClass != NULL) {
m_pFunc = PyObject_GetAttrString(m_pInst, p_name.toUtf8());
if (m_pFunc && PyCallable_Check(m_pFunc)) {
m_pArgs = PyTuple_New(argSize);
for (i = 0; i < argSize ; ++i) {
m_pValue=p_args.at(i);
//m_pValue = PyLong_FromLong(5);
if (!m_pValue) {
Py_DECREF(m_pArgs);
fprintf(stderr, "Cannot convert argument\n");
return nullptr;
}
/* pValue reference stolen here: */
PyTuple_SetItem(m_pArgs, i, m_pValue);
}
m_pValue = PyObject_CallObject(m_pFunc, m_pArgs);
Py_DECREF(m_pArgs);
if (m_pValue != NULL) {
return m_pValue;
}
else {
Py_DECREF(m_pFunc);
Py_DECREF(m_pClass);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return nullptr;
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
qout << "Cannot find function" << p_name << Qt::endl;
}
Py_XDECREF(m_pFunc);
Py_DECREF(m_pClass);
}else{
qout << "Module not available" << Qt::endl;
}
return nullptr;
}
PyObject *PythonObject::getAttribute(const QString p_name)
{
m_pFunc = PyObject_GetAttrString(m_pInst, p_name.toUtf8());
if(m_pFunc == NULL){
throw std::runtime_error(QString("Calling of undefined python variable ").append(p_name).append(" from C").toUtf8());
}
return m_pFunc;
}
void PythonObject::setAttribute(const QString &p_name, PyObject *p_obj)
{
PyObject_SetAttrString(m_pInst, p_name.toUtf8(),p_obj);
}
PyObject *PythonObject::getPInst() const
{
return m_pInst;
}
void PythonObject::setPInst(PyObject *pInst)
{
m_pInst = pInst;
}
PythonObject &PythonObject::operator=(const PythonObject& obj)
{
this->m_pInst=obj.m_pInst;
this->m_pClass=obj.m_pInst;
Py_INCREF(this->m_pClass);
Py_INCREF(this->m_pInst);
return *this;
}