File: pluginloader.cpp

package info (click to toggle)
python-qt4 4.11.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 40,148 kB
  • ctags: 6,150
  • sloc: python: 125,936; cpp: 12,628; xml: 292; makefile: 259; php: 27; sh: 2
file content (355 lines) | stat: -rw-r--r-- 10,043 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
/*
 * This is the Qt Designer plugin that collects all the Python plugins it can
 * find as a widget collection to Designer.
 *
 * Copyright (c) 2015 Riverbank Computing Limited <info@riverbankcomputing.com>
 * 
 * This file is part of PyQt4.
 * 
 * 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 "pluginloader.h"

#include <stdlib.h>

#include <QtGlobal>
#include <QtPlugin>
#include <QCoreApplication>
#include <QDesignerCustomWidgetInterface>
#include <QDir>
#include <QLibrary>
#include <QStringList>
#include <QVector>

#include "../qpy/QtDesigner/qpydesignercustomwidgetplugin.h"


// Construct the collection of Python widgets.
PyCustomWidgets::PyCustomWidgets(QObject *parent) : QObject(parent)
{
    // Get the default list of directories to search.  These correspond to a
    // standard "python" subdirectory of all the places that Designer looks for
    // its own plugins.
    QStringList default_dirs;

    QStringList path_list = QCoreApplication::libraryPaths();
    foreach (const QString &path, path_list)
        default_dirs.append(path + QDir::separator() +
                QLatin1String("designer") + QDir::separator() +
                QLatin1String("python"));

    default_dirs.append(QDir::homePath() + QDir::separator() +
            QLatin1String(".designer") + QDir::separator() +
            QLatin1String("plugins") + QDir::separator() +
            QLatin1String("python"));

    // Get the list of directories to search.
    QStringList dirs;
    char *pyqt_path = getenv("PYQTDESIGNERPATH");

    if (pyqt_path)
    {
#if defined(Q_OS_WIN)
        QLatin1Char sep(';');
#else
        QLatin1Char sep(':');
#endif

        QStringList pyqt_dirs = QString::fromLatin1(pyqt_path).split(sep);

        for (QStringList::const_iterator it = pyqt_dirs.constBegin(); it != pyqt_dirs.constEnd(); ++it)
            if ((*it).isEmpty())
                dirs << default_dirs;
            else
                dirs.append(QDir(*it).canonicalPath());
    }
    else
        dirs = default_dirs;

    // The sys.path object if we need it.
    PyObject *sys_path = 0;

    // The sip.unwrapinstance object if we need it.
    PyObject *sip_unwrapinstance = 0;

    // The PyQt4.QtDesigner.QPyDesignerCustomWidgetPlugin object if we need it.
    PyObject *qtdesigner_custom = 0;

    // Go through each directory.
    for (int i = 0; i < dirs.size(); ++i)
    {
        QString dir = dirs.at(i);

        // Get a list of all candidate plugin modules.  We sort by name to
        // provide control over the order they are imported.
        QStringList candidates = QDir(dir).entryList(QDir::Files, QDir::Name);
        QStringList plugins;

        for (int p = 0; p < candidates.size(); ++p)
        {
            QStringList parts = candidates.at(p).split('.');

            if (parts.size() != 2)
                continue;

            if (!parts.at(1).startsWith("py"))
                continue;

            const QString &plugin = parts.at(0);

            if (!plugin.endsWith("plugin"))
                continue;

            if (plugins.contains(plugin))
                continue;

            plugins.append(plugin);
        }

        // Skip if there is nothing of interest in this directory.
        if (!plugins.size())
            continue;

        // Make sure the interpreter is loaded and initialised.  Leave this as
        // late as possible.
        if (!Py_IsInitialized())
        {
            QLibrary library(PYTHON_LIB);

            library.setLoadHints(QLibrary::ExportExternalSymbolsHint);

            if (!library.load())
                return;

            Py_Initialize();
        }

        // Make sure we have sys.path.
        if (!sys_path)
        {
            sys_path = getModuleAttr("sys", "path");

            if (!sys_path)
                return;
        }

        // Make sure we have sip.unwrapinstance.
        if (!sip_unwrapinstance)
        {
            sip_unwrapinstance = getModuleAttr("sip", "unwrapinstance");

            if (!sip_unwrapinstance)
                return;
        }

        // Convert the directory to a Python object with native separators.
#if QT_VERSION >= 0x040200
        dir = QDir::toNativeSeparators(dir);
#else
        dir = QDir::convertSeparators(dir);
#endif

#if PY_VERSION_HEX >= 0x03030000
        PyObject *dobj = PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, dir.constData(), dir.length());

        if (!dobj)
        {
            PyErr_Print();
            continue;
        }
#elif PY_VERSION_HEX >= 0x03000000
#if defined(Py_UNICODE_WIDE)
#if QT_VERSION >= 0x040200
        QVector<uint> ucs4 = dir.toUcs4();

        PyObject *dobj = PyUnicode_FromUnicode(0, ucs4.size());

        if (!dobj)
        {
            PyErr_Print();
            continue;
        }

        memcpy(PyUnicode_AS_UNICODE(dobj), ucs4.constData(),
                ucs4.size() * sizeof (Py_UNICODE));
#else
        PyObject *dobj = PyUnicode_FromUnicode(0, dir.length());

        if (!dobj)
        {
            PyErr_Print();
            continue;
        }

        Py_UNICODE *pyu = PyUnicode_AS_UNICODE(dobj);

        for (int i = 0; i < dir.length(); ++i)
            *pyu++ = (dir.at(i)).unicode();
#endif
#else
        PyObject *dobj = PyUnicode_FromUnicode(0, dir.length());

        if (!dobj)
        {
            PyErr_Print();
            continue;
        }

        memcpy(PyUnicode_AS_UNICODE(dobj), dir.utf16(),
                dir.length() * sizeof (Py_UNICODE));
#endif
#else
        PyObject *dobj = PyString_FromString(dir.toLatin1().constData());

        if (!dobj)
        {
            PyErr_Print();
            continue;
        }
#endif

        // Add the directory to sys.path.
        int rc = PyList_Append(sys_path, dobj);
        Py_DECREF(dobj);

        if (rc < 0)
        {
            PyErr_Print();
            continue;
        }

        // Import each plugin.
        for (int plug = 0; plug < plugins.size(); ++plug)
        {
            PyObject *plug_mod = PyImport_ImportModule(plugins.at(plug).toLatin1().data());

            if (!plug_mod)
            {
                PyErr_Print();
                continue;
            }

            // Make sure we have QPyDesignerCustomWidgetPlugin.  We make sure
            // this is after the import of the first plugin to allow that
            // plugin to change any API versions.
            if (!qtdesigner_custom)
            {
                qtdesigner_custom = getModuleAttr("PyQt4.QtDesigner", "QPyDesignerCustomWidgetPlugin");

                if (!qtdesigner_custom)
                    return;
            }

            // Go through the module looking for types that implement
            // QDesignerCustomWidgetInterface (ie. by deriving from
            // QPyDesignerCustomWidgetPlugin).
            PyObject *mod_dict = PyModule_GetDict(plug_mod);
            PyObject *key, *value;
#if PY_VERSION_HEX >= 0x02050000
            Py_ssize_t pos = 0;
#else
            int pos = 0;
#endif

            while (PyDict_Next(mod_dict, &pos, &key, &value))
            {
                if (!PyType_Check(value))
                    continue;

                if (value == qtdesigner_custom)
                    continue;

                if (!PyType_IsSubtype((PyTypeObject *)value, (PyTypeObject *)qtdesigner_custom))
                    continue;

                // Create the plugin instance.  Note that we don't give it a
                // parent, which make things easier.  It also means that Python
                // owns the instance so we don't decrement the reference count
                // so that it doesn't get garbage collected.
                PyObject *plugobj = PyObject_CallObject(value, NULL);

                if (!plugobj)
                {
                    PyErr_Print();
                    continue;
                }

                // Get the address of the C++ instance.
                PyObject *plugaddr = PyObject_CallFunctionObjArgs(sip_unwrapinstance, plugobj, NULL);

                if (!plugaddr)
                {
                    Py_DECREF(plugobj);
                    PyErr_Print();
                    continue;
                }

                void *addr = PyLong_AsVoidPtr(plugaddr);
                Py_DECREF(plugaddr);

                widgets.append(reinterpret_cast<QPyDesignerCustomWidgetPlugin *>(addr));
            }

            Py_DECREF(plug_mod);
        }
    }
}


// Return the list of custom widgets.
QList<QDesignerCustomWidgetInterface *> PyCustomWidgets::customWidgets() const
{
    return widgets;
}


// Return the named attribute object from the named module.
PyObject *PyCustomWidgets::getModuleAttr(const char *module, const char *attr)
{
#if PY_VERSION_HEX >= 0x02050000
    PyObject *mod = PyImport_ImportModule(module);
#else
    PyObject *mod = PyImport_ImportModule(const_cast<char *>(module));
#endif

    if (!mod)
    {
        PyErr_Print();
        return 0;
    }

#if PY_VERSION_HEX >= 0x02050000
    PyObject *obj = PyObject_GetAttrString(mod, attr);
#else
    PyObject *obj = PyObject_GetAttrString(mod, const_cast<char *>(attr));
#endif

    Py_DECREF(mod);

    if (!obj)
    {
        PyErr_Print();
        return 0;
    }

    return obj;
}


#if QT_VERSION < 0x050000
// Register the plugin.
Q_EXPORT_PLUGIN2(MODULE_NAME, PyCustomWidgets)
#endif