Repository navigation
Expand file tree
/
Copy pathQtPython.cpp
More file actions
283 lines (221 loc) · 7.8 KB
/
Copy pathQtPython.cpp
File metadata and controls
283 lines (221 loc) · 7.8 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "QtPython3/QtPython.h"
#include <TkUtil/Exception.h>
#include <TkUtil/Locale.h>
#include <qglobal.h>
#include <Python.h>
#include <pystate.h> // PyTrace_LINE ...
#include <frameobject.h>// PyFrameObject
#include <assert.h>
using namespace TkUtil;
using namespace std;
const Version QtPython::_version (QT_PYTHON_VERSION);
Charset QtPython::_consoleCharset (Locale::detectCharset ("")); // v 3.3.0
UTF8String QtPython::outputsString (Charset::UTF_8);
typedef struct { PyObject_HEAD } redirector_RedirectorObject;
static PyObject* Redirector_check (redirector_RedirectorObject* r); // forward declaration
static PyObject* Redirector_write (PyObject*, PyObject* args); // forward declaration
static PyObject* Redirector_flush (redirector_RedirectorObject* r); // forward declaration
static PyMethodDef redirector_RedirectorMethods [] =
{
{"check", (PyCFunction)Redirector_check, METH_NOARGS,"print a message"},
{"write", (PyCFunction)Redirector_write, METH_VARARGS, "implement the write method to redirect stdout/err"},
{"flush", (PyCFunction)Redirector_flush, METH_NOARGS,"flush the buffer"},
NULL // Sentinel
}; // redirector_RedirectorMethods
static PyObject* Redirector_check (redirector_RedirectorObject* r)
{
return PyUnicode_FromString ("My name is toto");
} // Redirector_check
static PyObject* Redirector_write (PyObject* th, PyObject* args)
{
char* output = 0;
PyObject* selfi = 0;
//cout << "================== TH = " << PyUnicode_AsUTF8 (PyObject_Str(th)) << " ARG = " << PyUnicode_AsUTF8 (PyObject_Str(args)) << endl;
if (!PyArg_ParseTuple (args, "s", &output))
{
return NULL;
}
try
{
UTF8String lines (QtPython::getConsoleCharset ( ));
lines << output;
QtPython::outputsString << lines;
}
catch (...)
{ // Remarque : si QtPython::getConsoleCharset ( ) vaut ASCII ou UNKNOWN
// ça risque de mal se passer => on assure ici un service minimum :
UTF8String lines (Charset::UTF_8);
lines << output;
QtPython::outputsString << lines;
}
Py_INCREF(Py_None);
return Py_None;
} // Redirector_write
static PyObject* Redirector_flush (redirector_RedirectorObject* r)
{
Py_INCREF(Py_None);
return Py_None;
} // Redirector_check
static PyTypeObject redirector_RedirectorType =
{
PyVarObject_HEAD_INIT(NULL, 0)
"redirector.Redirector", // tp_name
sizeof (redirector_RedirectorObject), // tp_basicsize
0, // tp_itemsize
0, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_reserved
0, // tp_repr
0, // tp_as_number
nullptr, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
"Redirector objects", // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
redirector_RedirectorMethods, // tp_methods
0, // tp_members
0, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
0, // tp_init
0, // tp_alloc
0, // tp_new
}; // redirector_RedirectorType
static PyMethodDef ModuleMethods[] = { {NULL,NULL,0,NULL} };
static struct PyModuleDef redirectorModule =
{
PyModuleDef_HEAD_INIT,
"redirector", //
"usage: bla bla bla", // module documentation, may be NULL
-1, // size of per-interpreter state of the module, or -1 if the module keeps state in global variables.
NULL, NULL, NULL, NULL, NULL//ModuleMethods
}; // redirectorModule
PyMODINIT_FUNC PyInit_redirector ( )
{
redirector_RedirectorType.tp_new = PyType_GenericNew;
if (PyType_Ready (&redirector_RedirectorType) < 0)
return NULL;
PyObject* module = PyModule_Create (&redirectorModule);
if (NULL == module)
return module;
Py_INCREF (&redirector_RedirectorType);
PyModule_AddObject (module, "Redirector", (PyObject*)&redirector_RedirectorType);
/* PyObject* module = PyModule_Create (&redirectorModule);
PyObject* classDict = PyDict_New ( );
PyObject* className = PyUnicode_FromString ("redirector");
Py_DECREF(classDict);
Py_DECREF(className);
// add methods to class
for (PyMethodDef* rm = redirectorMethods; rm->ml_name != NULL; rm++)
{
PyObject* func = PyCFunction_New (rm, NULL);
PyObject* method = PyInstanceMethod_New (func);
PyDict_SetItemString (classDict, rm->ml_name, method);
Py_DECREF(func);
Py_DECREF(method);
} // for (PyMethodDef* rm = redirectorMethods; rm->ml_name != NULL; rm++)
PyObject* fooClass = PyObject_CallFunctionObjArgs ((PyObject*)&PyType_Type, className, PyTuple_New (0), classDict, 0);
PyModule_AddObject (module, "redirector", fooClass);*/
return module;
} // PyInit_redirector
// ===============================================================================================================================
// LA CLASSE QtPython
// ===============================================================================================================================
QtPython::QtPython ( )
{
assert (0 && "QtPython is not instanciable.");
} // QtPython::QtPython
QtPython::QtPython (const QtPython&)
{
assert (0 && "QtPython is not instanciable.");
} // QtPython::QtPython
QtPython& QtPython::operator = (const QtPython&)
{
assert (0 && "QtPython assignment operator is not allowed.");
return *this;
} // QtPython::operator =
QtPython::~QtPython ( )
{
} // QtPython::~QtPython
void QtPython::preInitialize ( ) // CP v 5.1.0
{
static bool preInitialized = false; // v 6.4.4
if (false == preInitialized)
{
// A faire avant Py_Initialize :
if (false == Py_IsInitialized ( ))
{
if (-1 == PyImport_AppendInittab ("redirector", PyInit_redirector))
throw Exception ("Echec de l'importation du module redirector par la console Python.");
}
else
// v 6.4.4. Eviter (python 3.12.5) : Fatal Python error: PyImport_AppendInittab: PyImport_AppendInittab() may not be called after Py_Initialize()
// Python runtime state: initialized
throw Exception ("QtPython::preInitialize doit être appelé avant Py_Initialize. PyImport_AppendInittab non appelé car l'application planterait. La console python ne devrait pas fonctionner correctement.");
preInitialized = true;
} // if (false == preInitialized)
} // QtPython::preInitialize
void QtPython::initialize (const Charset& charset)
{
_consoleCharset = charset;
Q_INIT_RESOURCE (QtPython);
} // QtPython::initialize
void QtPython::finalize ( )
{
} // QtPython::finalize
const Version& QtPython::getVersion ( )
{
return _version;
} // QtPython::getVersion
UTF8String QtPython::getNamedVersion ( )
{
return UTF8String (_version.getVersion ( ) + " (console v1)", _consoleCharset);
} // QtPython::getNamedVersion
const Charset& QtPython::getConsoleCharset ( ) // v 3.3.0
{
return _consoleCharset;
} // QtPython::getConsoleCharset
void QtPython::setConsoleCharset (const Charset& charset) // v 3.3.0
{
_consoleCharset = charset;
} // QtPython::setConsoleCharset
string QtPython::slashify (const string& s)
{
string str (s);
string::size_type pos;
pos = str.find ('"');
while (string::npos != pos)
{
str.replace (pos, 1, "\\\"");
pos = str.find ('"', pos + 2);
} // while (string::npos != pos)
pos = str.find ('\t');
while (string::npos != pos)
{
str.replace (pos, 1, "\\t");
pos = str.find ('\t', pos + 2);
} // while (string::npos != pos)
pos = str.find ('\n');
while (string::npos != pos)
{
str.replace (pos, 1, "\\n");
pos = str.find ('\n', pos + 2);
} // while (string::npos != pos)
return str;
} // QtPython::slashify