-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyrun.cpp
More file actions
125 lines (120 loc) · 3.84 KB
/
pyrun.cpp
File metadata and controls
125 lines (120 loc) · 3.84 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "Python.h"
#include "./pyrun.h"
#include <QByteArray>
#include <QCoreApplication>
#include <QDebug>
namespace PyRun
{
static State state = PythonUninitialized;
static void cleanup()
{
if (state > PythonUninitialized) {
Py_Finalize();
state = PythonUninitialized;
}
}
QPair<int,QVariant> loadModule(const QString &modulePath, const QString &moduleName, const QString &functionName, const QStringList& args)
{
qDebug()<<"modulepath"<<modulePath;
QPair <int,QVariant>resultado;
QVariant valor;
qputenv("PYTHONPATH", modulePath.toLocal8Bit());
if (init() != AppModuleLoaded)
{
resultado.first=CannotLoadModule;
resultado.second=QVariant();
return resultado;
}
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue;
pName = PyUnicode_DecodeFSDefault(moduleName.toLocal8Bit().constData());
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule)
{
pFunc = PyObject_GetAttrString(pModule, functionName.toLocal8Bit().constData());
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc))
{
/* pValue reference stolen here: */
pArgs = PyTuple_New(args.size());
for (int i = 0;i<args.size();i++)
{
PyObject* arg_valor = Py_BuildValue("s",args.at(i).toLocal8Bit().constData());
if (!arg_valor)
{
Py_DECREF(pArgs);
Py_DECREF(pModule);
qWarning("Cannot convert argument\n");
resultado.first=CannotConvertArgument;
resultado.second=QVariant();
return resultado;
}
PyTuple_SetItem(pArgs, i, arg_valor);//meto los datos de conexion
}
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue){
qWarning("Result of call: %ld\n", PyLong_AsLong(pValue));
PyObject* repr = PyObject_Repr(pValue);
PyObject* str = PyUnicode_AsEncodedString(repr, "utf-8", "strict");
char* raw_result = PyBytes_AsString(str);
QString result = QString(raw_result).remove('\'');
qDebug()<<"result "<<result;
valor = QVariant(result);
Py_DECREF(pValue);
}
else
{
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
qWarning("Call failed\n");
resultado.first=CallFailed;
resultado.second=QVariant();
return resultado;
}
}
else
{
if (PyErr_Occurred())
PyErr_Print();
qWarning("Cannot find function \"%s\"\n", functionName.toLocal8Bit().constData());
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else
{
PyErr_Print();
qWarning("Failed to load \"%s\"\n", moduleName.toLocal8Bit().constData());
resultado.first=FailedToLoad;
resultado.second=QVariant();
return resultado;
}
resultado.first=Success;
resultado.second=valor;
return resultado;
}
State init()
{
if (state > PythonUninitialized)
return state;
QByteArray virtualEnvPath = qgetenv("VIRTUAL_ENV");
if (!virtualEnvPath.isEmpty())
qputenv("PYTHONHOME", virtualEnvPath);
Py_Initialize();
qAddPostRoutine(cleanup);
state = PythonInitialized;
const bool pyErrorOccurred = PyErr_Occurred() != nullptr;
if (!pyErrorOccurred) {
state = AppModuleLoaded;
} else {
if (pyErrorOccurred)
PyErr_Print();
qWarning("Failed to initialize the module.");
}
return state;
}
}