File: qpycore_public_api.cpp

package info (click to toggle)
pyqt5 5.11.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 15,956 kB
  • sloc: python: 93,806; cpp: 21,390; xml: 285; makefile: 266; sh: 31
file content (424 lines) | stat: -rw-r--r-- 12,176 bytes parent folder | download
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// This implements the public API provided by PyQt to external packages.
//
// Copyright (c) 2018 Riverbank Computing Limited <info@riverbankcomputing.com>
// 
// This file is part of PyQt5.
// 
// This file may be used under the terms of the GNU General Public License
// version 3.0 as published by the Free Software Foundation and appearing in
// the file LICENSE included in the packaging of this file.  Please review the
// following information to ensure the GNU General Public License version 3.0
// requirements will be met: http://www.gnu.org/copyleft/gpl.html.
// 
// If you do not wish to use this file under the terms of the GPL version 3.0
// then you may purchase a commercial license.  For more information contact
// info@riverbankcomputing.com.
// 
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.


#include <QByteArray>
#include <QHash>

#include "qpycore_chimera.h"
#include "qpycore_objectified_strings.h"
#include "qpycore_public_api.h"
#include "qpycore_pyqtboundsignal.h"
#include "qpycore_pyqtsignal.h"
#include "qpycore_types.h"


// A replacement for PyErr_Print() that passes the exception to qFatal().
void pyqt5_err_print()
{
#if PY_MAJOR_VERSION >= 3
#define CONST_CAST(s)   s
#else
#define CONST_CAST(s)   const_cast<char *>(s)
#endif

    static bool recursing = false;

    if (recursing)
        return;

    recursing = true;

    // Save the exception in case of new exceptions raised here.
    PyObject *exception, *value, *traceback;

    PyErr_Fetch(&exception, &value, &traceback);

    // Get the standard exception hook.
    static PyObject *original_hook = 0;

    if (!original_hook)
        original_hook = PySys_GetObject(CONST_CAST("__excepthook__"));

    // See if the application has installed its own hook.
    PyObject *hook = PySys_GetObject(CONST_CAST("excepthook"));

    if (hook != original_hook)
    {
        // This will invoke the application's hook.
        PyErr_Restore(exception, value, traceback);
        PyErr_Print();
    }
    else
    {
        // Make sure we have the StringIO ctor.
        static PyObject *stringio_ctor = 0;

        if (!stringio_ctor)
        {
            PyObject *io_module;

#if PY_MAJOR_VERSION >= 3
            io_module = PyImport_ImportModule("io");
#else
            PyErr_Clear();
            io_module = PyImport_ImportModule("cStringIO");

            if (!io_module)
            {
                PyErr_Clear();
                io_module = PyImport_ImportModule("StringIO");
            }
#endif

            if (io_module)
            {
                stringio_ctor = PyObject_GetAttrString(io_module, "StringIO");
                Py_DECREF(io_module);
            }
        }

        // Create a StringIO object and replace sys.stderr with it.
        PyObject *new_stderr = 0, *old_stderr;

        if (stringio_ctor)
        {
            old_stderr = PySys_GetObject(CONST_CAST("stderr"));

            if (old_stderr)
            {
                new_stderr = PyObject_CallObject(stringio_ctor, NULL);

                if (new_stderr)
                {
                    // Make sure the old stderr doesn't get garbage collected
                    // when it is replaced.
                    Py_INCREF(old_stderr);

                    if (PySys_SetObject(CONST_CAST("stderr"), new_stderr) < 0)
                    {
                        Py_DECREF(old_stderr);
                        Py_DECREF(new_stderr);
                        new_stderr = 0;
                    }
                }
            }
        }

        // Restore the exception and print it.
        PyErr_Restore(exception, value, traceback);
        PyErr_Print();

        // This will be passed to qFatal() if we can't get the detailed text.
        QByteArray message("Unhandled Python exception");

        // Extract the detailed text if it was redirected.
        if (new_stderr)
        {
            // Restore sys.stderr.
            PySys_SetObject(CONST_CAST("stderr"), old_stderr);
            Py_DECREF(old_stderr);

            // Extract the text.
            PyObject *text = PyObject_CallMethod(new_stderr,
                    CONST_CAST("getvalue"), NULL);

            if (text)
            {
                // Strip the text as qFatal() likes to add a newline.
                PyObject *stripped = PyObject_CallMethod(text,
                        CONST_CAST("strip"), NULL);

                if (stripped)
                {
                    Py_DECREF(text);
                    text = stripped;
                }

                // Encode the text using the encoding of the original
                // sys.stderr.

#if PY_MAJOR_VERSION >= 3
                PyObject *encoding = PyObject_GetAttrString(old_stderr,
                        "encoding");

                if (encoding)
                {
                    PyObject *encoding_bytes = PyUnicode_AsUTF8String(
                            encoding);

                    if (encoding_bytes)
                    {
                        Q_ASSERT(PyBytes_Check(encoding_bytes));

                        PyObject *bytes = PyUnicode_AsEncodedString(text,
                                PyBytes_AsString(encoding_bytes), "strict");

                        if (bytes)
                        {
                            Q_ASSERT(PyBytes_Check(bytes));

                            message = QByteArray(PyBytes_AsString(bytes),
                                    PyBytes_Size(bytes));

                            Py_DECREF(bytes);
                        }

                        Py_DECREF(encoding_bytes);
                    }

                    Py_DECREF(encoding);
                }
#else
                char *buffer;
                Py_ssize_t length;

                if (PyString_AsStringAndSize(text, &buffer, &length) == 0)
                    message = QByteArray(buffer, length);
#endif

                Py_DECREF(text);
            }

            Py_DECREF(new_stderr);
        }

        // qFatal() may not call abort.
        Py_BEGIN_ALLOW_THREADS
        qFatal("%s", message.data());
        Py_END_ALLOW_THREADS
    }

    recursing = false;
}


// Convert a Python argv list to a conventional C argc count and argv array.
char **pyqt5_from_argv_list(PyObject *argv_list, int &argc)
{
    argc = PyList_Size(argv_list);

    // Allocate space for two copies of the argument pointers, plus the
    // terminating NULL.
    char **argv = new char *[2 * (argc + 1)];

    // Convert the list.
    for (int a = 0; a < argc; ++a)
    {
        PyObject *arg_obj = PyList_GetItem(argv_list, a);
        char *arg;

        if (PyUnicode_Check(arg_obj))
        {
            QByteArray ba_arg = qpycore_PyObject_AsQString(arg_obj).toLocal8Bit();
            arg = qstrdup(ba_arg.constData());
        }
        else if (SIPBytes_Check(arg_obj))
        {
            arg = qstrdup(SIPBytes_AsString(arg_obj));
        }
        else
        {
            arg = const_cast<char *>("invalid");
        }

        argv[a] = argv[a + argc + 1] = arg;
    }

    argv[argc + argc + 1] = argv[argc] = NULL;

    return argv;
}


// Get the receiver object and slot signature for a connection.
sipErrorState pyqt5_get_connection_parts(PyObject *slot, QObject *transmitter,
        const char *signal_signature, bool single_shot, QObject **receiver,
        QByteArray &slot_signature)
{ 
    static QHash<QByteArray, const Chimera::Signature *> cache;

    QByteArray key(signal_signature);
    const Chimera::Signature *parsed_signal_signature = cache.value(key);

    if (!parsed_signal_signature)
    {
        parsed_signal_signature = Chimera::parse(key, "a signal argument");

        if (!parsed_signal_signature)
            return sipErrorFail;

        cache.insert(key, parsed_signal_signature);
    }

    return qpycore_get_receiver_slot_signature(slot, transmitter,
            parsed_signal_signature, single_shot, receiver, slot_signature);
}



// Get the transmitter object and signal signature from a bound signal.
sipErrorState pyqt5_get_pyqtsignal_parts(PyObject *signal,
        QObject **transmitter, QByteArray &signal_signature)
{
    if (PyObject_TypeCheck(signal, qpycore_pyqtBoundSignal_TypeObject))
    {
        qpycore_pyqtBoundSignal *bs = (qpycore_pyqtBoundSignal *)signal;

        *transmitter = bs->bound_qobject;
        signal_signature = bs->unbound_signal->parsed_signature->signature;

        return sipErrorNone;
    }

    return sipErrorContinue;
}


// Get the receiver object and slot signature from a decorated callable.
sipErrorState pyqt5_get_pyqtslot_parts(PyObject *slot, QObject **receiver,
        QByteArray &slot_signature)
{
    PyObject *py_receiver, *decorations;
    int is_err;
    void *qobj;
    Chimera::Signature *sig;
    sipMethodDef slot_m;

    // Get the QObject.
    if (!sipGetMethod(slot, &slot_m))
        goto bad_callable;

    py_receiver = slot_m.pm_self;
    if (!py_receiver)
        goto bad_callable;

    is_err = 0;

    qobj = sipForceConvertToType(py_receiver, sipType_QObject, 0,
            SIP_NO_CONVERTORS, 0, &is_err);

    if (is_err)
        goto bad_callable;

    *receiver = reinterpret_cast<QObject *>(qobj);

    // Get the decoration.
    decorations = PyObject_GetAttr(slot, qpycore_dunder_pyqtsignature);

    if (!decorations)
        goto bad_callable;

    // Use the first one ignoring any others.
    sig = Chimera::Signature::fromPyObject(PyList_GetItem(decorations, 0));
    Py_DECREF(decorations);

    slot_signature = sig->signature;
    slot_signature.prepend('1');

    return sipErrorNone;

bad_callable:
    PyErr_SetString(PyExc_TypeError,
            "callable must be a method of a QtCore.QObject instance decorated "
            "by QtCore.pyqtSlot");

    return sipErrorFail;
}


// Get the signature string for a bound or unbound signal.
sipErrorState pyqt5_get_signal_signature(PyObject *signal,
        const QObject *transmitter, QByteArray &signal_signature)
{
    qpycore_pyqtSignal *ps;

    if (PyObject_TypeCheck(signal, qpycore_pyqtBoundSignal_TypeObject))
    {
        qpycore_pyqtBoundSignal *bs = (qpycore_pyqtBoundSignal *)signal;

        if (transmitter && bs->bound_qobject != transmitter)
        {
            PyErr_SetString(PyExc_ValueError,
                    "signal is bound to a different QObject");
            return sipErrorFail;
        }

        ps = bs->unbound_signal;
    }
    else if (PyObject_TypeCheck(signal, qpycore_pyqtSignal_TypeObject))
    {
        ps = (qpycore_pyqtSignal *)signal;
    }
    else
    {
        return sipErrorContinue;
    }

    signal_signature = ps->parsed_signature->signature;

    return sipErrorNone;
}


// Register a convertor function that converts a QVariant to a Python object.
void pyqt5_register_from_qvariant_convertor(
        bool (*convertor)(const QVariant &, PyObject **))
{
    Chimera::registeredFromQVariantConvertors.append(convertor);
}


// Register a convertor function that converts a Python object to a QVariant.
void pyqt5_register_to_qvariant_convertor(
        bool (*convertor)(PyObject *, QVariant &, bool *))
{
    Chimera::registeredToQVariantConvertors.append(convertor);
}


// Register a convertor function that converts a Python object to the
// pre-allocated data of a QVariant with a specific meta-type.
void pyqt5_register_to_qvariant_data_convertor(
        bool (*convertor)(PyObject *, void *, int, bool *))
{
    Chimera::registeredToQVariantDataConvertors.append(convertor);
}


// Remove arguments from the Python argv list that have been removed from the
// C argv array.
void pyqt5_update_argv_list(PyObject *argv_list, int argc, char **argv)
{
    for (int a = 0, na = 0; a < argc; ++a)
    {
        // See if it was removed.
        if (argv[na] == argv[a + argc + 1])
            ++na;
        else
            PyList_SetSlice(argv_list, na, na + 1, 0);
    }
}


// Get the QMetaObject instance for a Python type.
const QMetaObject *pyqt5_get_qmetaobject(PyTypeObject *type)
{
    return qpycore_get_qmetaobject((sipWrapperType *)type);
}