1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt for Python.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "helper.h"
41 #include "sbkstring.h"
42 #include "sbkstaticstrings.h"
43
44 #include <iomanip>
45 #include <iostream>
46
47 #include <stdarg.h>
48
49 #ifdef _WIN32
50 # ifndef NOMINMAX
51 # define NOMINMAX
52 # endif
53 # include <windows.h>
54 #else
55 # include <pthread.h>
56 #endif
57
58 #include <algorithm>
59
formatPyTypeObject(const PyTypeObject * obj,std::ostream & str)60 static void formatPyTypeObject(const PyTypeObject *obj, std::ostream &str)
61 {
62 if (obj) {
63 str << '"' << obj->tp_name << "\", 0x" << std::hex
64 << obj->tp_flags << std::dec;
65 if (obj->tp_flags & Py_TPFLAGS_HEAPTYPE)
66 str << " [heaptype]";
67 if (obj->tp_flags & Py_TPFLAGS_BASETYPE)
68 str << " [base]";
69 if (obj->tp_flags & Py_TPFLAGS_HAVE_GC)
70 str << " [gc]";
71 if (obj->tp_flags & Py_TPFLAGS_LONG_SUBCLASS)
72 str << " [long]";
73 if (obj->tp_flags & Py_TPFLAGS_LIST_SUBCLASS)
74 str << " [list]";
75 if (obj->tp_flags & Py_TPFLAGS_TUPLE_SUBCLASS)
76 str << " [tuple]";
77 if (obj->tp_flags & Py_TPFLAGS_BYTES_SUBCLASS)
78 str << " [bytes]";
79 if (obj->tp_flags & Py_TPFLAGS_UNICODE_SUBCLASS)
80 str << " [unicode]";
81 if (obj->tp_flags & Py_TPFLAGS_DICT_SUBCLASS)
82 str << " [dict]";
83 if (obj->tp_flags & Py_TPFLAGS_TYPE_SUBCLASS)
84 str << " [type]";
85 if (obj->tp_flags & Py_TPFLAGS_IS_ABSTRACT)
86 str << " [abstract]";
87 } else {
88 str << '0';
89 }
90 }
91
92 static void formatPyObject(PyObject *obj, std::ostream &str);
93
formatPySequence(PyObject * obj,std::ostream & str)94 static void formatPySequence(PyObject *obj, std::ostream &str)
95 {
96 const Py_ssize_t size = PySequence_Size(obj);
97 const Py_ssize_t printSize = std::min(size, Py_ssize_t(5));
98 str << size << " <";
99 for (Py_ssize_t i = 0; i < printSize; ++i) {
100 if (i)
101 str << ", ";
102 str << '(';
103 PyObject *item = PySequence_GetItem(obj, i);
104 formatPyObject(item, str);
105 str << ')';
106 Py_XDECREF(item);
107 }
108 if (printSize < size)
109 str << ",...";
110 str << '>';
111 }
112
formatPyObject(PyObject * obj,std::ostream & str)113 static void formatPyObject(PyObject *obj, std::ostream &str)
114 {
115 if (obj) {
116 formatPyTypeObject(obj->ob_type, str);
117 str << ", ";
118 if (PyLong_Check(obj))
119 str << PyLong_AsLong(obj);
120 else if (PyFloat_Check(obj))
121 str << PyFloat_AsDouble(obj);
122 #ifdef IS_PY3K
123 else if (PyUnicode_Check(obj))
124 str << '"' << _PepUnicode_AsString(obj) << '"';
125 #else
126 else if (PyString_Check(obj))
127 str << '"' << PyString_AsString(obj) << '"';
128 #endif
129 else if (PySequence_Check(obj))
130 formatPySequence(obj, str);
131 else
132 str << "<unknown>";
133 } else {
134 str << '0';
135 }
136 }
137
138 namespace Shiboken
139 {
140
debugPyObject(PyObject * o)141 debugPyObject::debugPyObject(PyObject *o) : m_object(o)
142 {
143 }
144
debugPyTypeObject(const PyTypeObject * o)145 debugPyTypeObject::debugPyTypeObject(const PyTypeObject *o) : m_object(o)
146 {
147 }
148
operator <<(std::ostream & str,const debugPyTypeObject & o)149 std::ostream &operator<<(std::ostream &str, const debugPyTypeObject &o)
150 {
151 str << "PyTypeObject(";
152 formatPyTypeObject(o.m_object, str);
153 str << ')';
154 return str;
155 }
156
operator <<(std::ostream & str,const debugPyObject & o)157 std::ostream &operator<<(std::ostream &str, const debugPyObject &o)
158 {
159 str << "PyObject(";
160 formatPyObject(o.m_object, str);
161 str << ')';
162 return str;
163 }
164
165 // PySide-510: Changed from PySequence to PyList, which is correct.
listToArgcArgv(PyObject * argList,int * argc,char *** argv,const char * defaultAppName)166 bool listToArgcArgv(PyObject *argList, int *argc, char ***argv, const char *defaultAppName)
167 {
168 if (!PyList_Check(argList))
169 return false;
170
171 if (!defaultAppName)
172 defaultAppName = "PySideApplication";
173
174 // Check all items
175 Shiboken::AutoDecRef args(PySequence_Fast(argList, nullptr));
176 int numArgs = int(PySequence_Fast_GET_SIZE(argList));
177 for (int i = 0; i < numArgs; ++i) {
178 PyObject *item = PyList_GET_ITEM(args.object(), i);
179 if (!PyBytes_Check(item) && !PyUnicode_Check(item))
180 return false;
181 }
182
183 bool hasEmptyArgList = numArgs == 0;
184 if (hasEmptyArgList)
185 numArgs = 1;
186
187 *argc = numArgs;
188 *argv = new char *[*argc];
189
190 if (hasEmptyArgList) {
191 // Try to get the script name
192 PyObject *globals = PyEval_GetGlobals();
193 PyObject *appName = PyDict_GetItem(globals, Shiboken::PyMagicName::file());
194 (*argv)[0] = strdup(appName ? Shiboken::String::toCString(appName) : defaultAppName);
195 } else {
196 for (int i = 0; i < numArgs; ++i) {
197 PyObject *item = PyList_GET_ITEM(args.object(), i);
198 char *string = nullptr;
199 if (Shiboken::String::check(item)) {
200 string = strdup(Shiboken::String::toCString(item));
201 }
202 (*argv)[i] = string;
203 }
204 }
205
206 return true;
207 }
208
sequenceToIntArray(PyObject * obj,bool zeroTerminated)209 int *sequenceToIntArray(PyObject *obj, bool zeroTerminated)
210 {
211 AutoDecRef seq(PySequence_Fast(obj, "Sequence of ints expected"));
212 if (seq.isNull())
213 return nullptr;
214
215 Py_ssize_t size = PySequence_Fast_GET_SIZE(seq.object());
216 int *array = new int[size + (zeroTerminated ? 1 : 0)];
217
218 for (int i = 0; i < size; i++) {
219 PyObject *item = PySequence_Fast_GET_ITEM(seq.object(), i);
220 if (!PyInt_Check(item)) {
221 PyErr_SetString(PyExc_TypeError, "Sequence of ints expected");
222 delete[] array;
223 return nullptr;
224 }
225 array[i] = PyInt_AsLong(item);
226 }
227
228 if (zeroTerminated)
229 array[size] = 0;
230
231 return array;
232 }
233
234
warning(PyObject * category,int stacklevel,const char * format,...)235 int warning(PyObject *category, int stacklevel, const char *format, ...)
236 {
237 va_list args;
238 va_start(args, format);
239 #ifdef _WIN32
240 va_list args2 = args;
241 #else
242 va_list args2;
243 va_copy(args2, args);
244 #endif
245
246 // check the necessary memory
247 int size = vsnprintf(nullptr, 0, format, args) + 1;
248 auto message = new char[size];
249 int result = 0;
250 if (message) {
251 // format the message
252 vsnprintf(message, size, format, args2);
253 result = PyErr_WarnEx(category, message, stacklevel);
254 delete [] message;
255 }
256 va_end(args2);
257 va_end(args);
258 return result;
259 }
260
currentThreadId()261 ThreadId currentThreadId()
262 {
263 #if defined(_WIN32)
264 return GetCurrentThreadId();
265 #elif defined(__APPLE_CC__)
266 return reinterpret_cast<ThreadId>(pthread_self());
267 #else
268 return pthread_self();
269 #endif
270 }
271
272 // Internal, used by init() from main thread
273 static ThreadId _mainThreadId{0};
_initMainThreadId()274 void _initMainThreadId() { _mainThreadId = currentThreadId(); }
275
mainThreadId()276 ThreadId mainThreadId()
277 {
278 return _mainThreadId;
279 }
280
281 } // namespace Shiboken
282