##// END OF EJS Templates
Start Python 3.x Port...
Orochimarufan -
r206:316b4928f53f
parent child
Show More
@@ -44,11 +44,11 endif()
44
44
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46 # Generator
46 # Generator
47 #if(PythonQt_Qt5)
47 if(PythonQt_Qt5)
48 # add_subdirectory(generator_50 EXCLUDE_FROM_ALL)
48 add_subdirectory(generator_50 EXCLUDE_FROM_ALL)
49 # add_custom_target(generator)
49 add_custom_target(generator)
50 # add_dependencies(generator pythonqt_generator)
50 add_dependencies(generator pythonqt_generator)
51 #endif()
51 endif()
52
52
53 # TODO
53 # TODO
54
54
@@ -107,7 +107,6 endif()
107 option(PythonQt_Python3 "Use Python 3.x (3.3+)" OFF)
107 option(PythonQt_Python3 "Use Python 3.x (3.3+)" OFF)
108 if(PythonQt_Python3)
108 if(PythonQt_Python3)
109 set(PythonQt_PythonMin 3.3)
109 set(PythonQt_PythonMin 3.3)
110 add_definitions(PYTHON3K)
111 else(PythonQt_Python3)
110 else(PythonQt_Python3)
112 set(PythonQt_PythonMin 2.6)
111 set(PythonQt_PythonMin 2.6)
113 endif()
112 endif()
@@ -174,7 +174,11 void delete_QByteArray(QByteArray* obj) { delete obj; }
174
174
175 PyObject* data(QByteArray* b) {
175 PyObject* data(QByteArray* b) {
176 if (b->data()) {
176 if (b->data()) {
177 #ifdef PY3K
178 return PyUnicode_FromStringAndSize(b->data(), b->size());
179 #else
177 return PyString_FromStringAndSize(b->data(), b->size());
180 return PyString_FromStringAndSize(b->data(), b->size());
181 #endif
178 } else {
182 } else {
179 Py_INCREF(Py_None);
183 Py_INCREF(Py_None);
180 return Py_None;
184 return Py_None;
@@ -1426,7 +1426,7
1426 <replace-type modified-type="QString"/>
1426 <replace-type modified-type="QString"/>
1427 <conversion-rule class="native">
1427 <conversion-rule class="native">
1428 <insert-template name="core.convert_string_arg_to_latin1"/>
1428 <insert-template name="core.convert_string_arg_to_latin1"/>
1429 </conversion-rule>
1429 </conversion-rule>
1430 </modify-argument>
1430 </modify-argument>
1431 </modify-function>
1431 </modify-function>
1432 </object-type>
1432 </object-type>
@@ -1439,7 +1439,7
1439 <replace-type modified-type="QString"/>
1439 <replace-type modified-type="QString"/>
1440 <conversion-rule class="native">
1440 <conversion-rule class="native">
1441 <insert-template name="core.convert_string_arg_to_char*"/>
1441 <insert-template name="core.convert_string_arg_to_char*"/>
1442 </conversion-rule>
1442 </conversion-rule>
1443 </modify-argument>
1443 </modify-argument>
1444 </modify-function>
1444 </modify-function>
1445 <modify-function signature="assignProperty(QObject*,const char*,QVariant)">
1445 <modify-function signature="assignProperty(QObject*,const char*,QVariant)">
@@ -1447,7 +1447,7
1447 <replace-type modified-type="QString"/>
1447 <replace-type modified-type="QString"/>
1448 <conversion-rule class="native">
1448 <conversion-rule class="native">
1449 <insert-template name="core.convert_string_arg_to_char*"/>
1449 <insert-template name="core.convert_string_arg_to_char*"/>
1450 </conversion-rule>
1450 </conversion-rule>
1451 </modify-argument>
1451 </modify-argument>
1452 </modify-function>
1452 </modify-function>
1453 </object-type>
1453 </object-type>
@@ -1505,14 +1505,6
1505 </conversion-rule>
1505 </conversion-rule>
1506 </modify-argument>
1506 </modify-argument>
1507 </modify-function>
1507 </modify-function>
1508 <modify-function signature="createNativeFile(QFile&amp;)">
1509 <modify-argument index="1">
1510 <replace-type modified-type="QFile*"/>
1511 <conversion-rule class="native">
1512 QFile &amp; %out% = *qscriptvalue_cast&lt;QFile*&gt;(%in%);
1513 </conversion-rule>
1514 </modify-argument>
1515 </modify-function>
1516 </object-type>
1508 </object-type>
1517 <object-type name="QMimeData">
1509 <object-type name="QMimeData">
1518 <extra-includes>
1510 <extra-includes>
@@ -1200,14 +1200,6
1200 </modify-argument>
1200 </modify-argument>
1201 </modify-function>
1201 </modify-function>
1202
1202
1203 <inject-code class="pywrap-h">
1204 QImage* new_QImage( const uchar * data, int width, int height, QImage::Format format )
1205 {
1206 QImage* image = new QImage(width, height, format);
1207 memcpy(image-&gt;bits(), data, image-&gt;byteCount());
1208 return image;
1209 }
1210 </inject-code>
1211 </value-type>
1203 </value-type>
1212
1204
1213 <value-type name="QColormap" delete-in-main-thread="yes">
1205 <value-type name="QColormap" delete-in-main-thread="yes">
@@ -2101,23 +2093,31 QImage* new_QImage( const uchar * data, int width, int height, QImage::Format fo
2101 <access modifier="private"/>
2093 <access modifier="private"/>
2102 </modify-function>
2094 </modify-function>
2103
2095
2104 <inject-code class="pywrap-h">
2096 <modify-function signature="addAction(QString,const QObject*,const char*,QKeySequence)">
2105 QAction* addAction (QMenu* menu, const QString &amp; text, PyObject* callable, const QKeySequence &amp; shortcut = 0) {
2097 <remove/>
2106 QAction* a = menu-&gt;addAction(text);
2098 </modify-function>
2107 a-&gt;setShortcut(shortcut);
2108 PythonQt::self()-&gt;addSignalHandler(a, SIGNAL(triggered(bool)), callable);
2109 return a;
2110 }
2111
2099
2112 QAction* addAction (QMenu* menu, const QIcon&amp; icon, const QString&amp; text, PyObject* callable, const QKeySequence&amp; shortcut = 0)
2100 <modify-function signature="addAction(QIcon,QString,const QObject*,const char*,QKeySequence)">
2113 {
2101 <remove/>
2114 QAction* a = menu-&gt;addAction(text);
2102 </modify-function>
2115 a-&gt;setIcon(icon);
2103
2116 a-&gt;setShortcut(shortcut);
2104 <inject-code class="pywrap-h">
2117 PythonQt::self()-&gt;addSignalHandler(a, SIGNAL(triggered(bool)), callable);
2105 QAction* addAction (QMenu* menu, const QString &amp; text, PyObject* callable, const QKeySequence &amp; shortcut = 0) {
2118 return a;
2106 QAction* a = menu-&gt;addAction(text);
2119 }
2107 a-&gt;setShortcut(shortcut);
2120 </inject-code>
2108 PythonQt::self()-&gt;addSignalHandler(a, SIGNAL(triggered(bool)), callable);
2109 return a;
2110 }
2111
2112 QAction* addAction (QMenu* menu, const QIcon&amp; icon, const QString&amp; text, PyObject* callable, const QKeySequence&amp; shortcut = 0)
2113 {
2114 QAction* a = menu-&gt;addAction(text);
2115 a-&gt;setIcon(icon);
2116 a-&gt;setShortcut(shortcut);
2117 PythonQt::self()-&gt;addSignalHandler(a, SIGNAL(triggered(bool)), callable);
2118 return a;
2119 }
2120 </inject-code>
2121 </object-type>
2121 </object-type>
2122
2122
2123 <object-type name="QMenuBar">
2123 <object-type name="QMenuBar">
@@ -2158,14 +2158,15 QImage* new_QImage( const uchar * data, int width, int height, QImage::Format fo
2158 <reference-count action="ignore"/>
2158 <reference-count action="ignore"/>
2159 </modify-argument>
2159 </modify-argument>
2160 </modify-function>
2160 </modify-function>
2161 <inject-code class="pywrap-h">
2161
2162 QAction* addAction (QMenuBar* menu, const QString &amp; text, PyObject* callable)
2162 <inject-code class="pywrap-h">
2163 {
2163 QAction* addAction (QMenuBar* menu, const QString &amp; text, PyObject* callable)
2164 QAction* a = menu-&gt;addAction(text);
2164 {
2165 PythonQt::self()-&gt;addSignalHandler(a, SIGNAL(triggered(bool)), callable);
2165 QAction* a = menu-&gt;addAction(text);
2166 return a;
2166 PythonQt::self()-&gt;addSignalHandler(a, SIGNAL(triggered(bool)), callable);
2167 }
2167 return a;
2168 </inject-code>
2168 }
2169 </inject-code>
2169 </object-type>
2170 </object-type>
2170 <object-type name="QMotifStyle">
2171 <object-type name="QMotifStyle">
2171 <modify-function signature="standardPixmap(QStyle::StandardPixmap, const QStyleOption*, const QWidget*)const" remove="all"/> <!--### Obsolete in 4.3-->
2172 <modify-function signature="standardPixmap(QStyle::StandardPixmap, const QStyleOption*, const QWidget*)const" remove="all"/> <!--### Obsolete in 4.3-->
@@ -4887,22 +4888,23 QImage* new_QImage( const uchar * data, int width, int height, QImage::Format fo
4887 <reference-count action="ignore"/>
4888 <reference-count action="ignore"/>
4888 </modify-argument>
4889 </modify-argument>
4889 </modify-function>
4890 </modify-function>
4890 <inject-code class="pywrap-h">
4891
4891 QAction* addAction (QToolBar* menu, const QString &amp; text, PyObject* callable)
4892 <inject-code class="pywrap-h">
4892 {
4893 QAction* addAction (QToolBar* menu, const QString &amp; text, PyObject* callable)
4893 QAction* a = menu-&gt;addAction(text);
4894 {
4894 PythonQt::self()-&gt;addSignalHandler(a, SIGNAL(triggered(bool)), callable);
4895 QAction* a = menu-&gt;addAction(text);
4895 return a;
4896 PythonQt::self()-&gt;addSignalHandler(a, SIGNAL(triggered(bool)), callable);
4896 }
4897 return a;
4897
4898 }
4898 QAction* addAction (QToolBar* menu, const QIcon&amp; icon, const QString&amp; text, PyObject* callable)
4899
4899 {
4900 QAction* addAction (QToolBar* menu, const QIcon&amp; icon, const QString&amp; text, PyObject* callable)
4900 QAction* a = menu-&gt;addAction(text);
4901 {
4901 a-&gt;setIcon(icon);
4902 QAction* a = menu-&gt;addAction(text);
4902 PythonQt::self()-&gt;addSignalHandler(a, SIGNAL(triggered(bool)), callable);
4903 a-&gt;setIcon(icon);
4903 return a;
4904 PythonQt::self()-&gt;addSignalHandler(a, SIGNAL(triggered(bool)), callable);
4904 }
4905 return a;
4905 </inject-code>
4906 }
4907 </inject-code>
4906 </object-type>
4908 </object-type>
4907
4909
4908 <object-type name="QPaintEngine">
4910 <object-type name="QPaintEngine">
@@ -5665,6 +5667,7 QImage* new_QImage( const uchar * data, int width, int height, QImage::Format fo
5665 <suppress-warning text="WARNING(JavaGenerator) :: either add or remove specified for reference count variable '__rcMenus' in 'com.trolltech.qt.gui.QMenuBar' but not both"/>
5667 <suppress-warning text="WARNING(JavaGenerator) :: either add or remove specified for reference count variable '__rcMenus' in 'com.trolltech.qt.gui.QMenuBar' but not both"/>
5666
5668
5667 <suppress-warning text="WARNING(MetaJavaBuilder) :: skipping function 'QPixmap::pixmapData', unmatched return type 'QPixmapData*'"/>
5669 <suppress-warning text="WARNING(MetaJavaBuilder) :: skipping function 'QPixmap::pixmapData', unmatched return type 'QPixmapData*'"/>
5670 <suppress-warning text="WARNING(MetaJavaBuilder) :: object type 'QAccessible' extended by interface type 'QAbstractAccessibleFactory'. The resulting API will be less expressive than the original."/>
5668
5671
5669 <suppress-warning text="WARNING(MetaJavaBuilder) :: Rejected enum has no alternative...: QPalette::NColorRoles"/>
5672 <suppress-warning text="WARNING(MetaJavaBuilder) :: Rejected enum has no alternative...: QPalette::NColorRoles"/>
5670
5673
@@ -9,11 +9,6
9 <enum-type name="QGLContext::BindOption" flags="QGLContext::BindOptions"/>
9 <enum-type name="QGLContext::BindOption" flags="QGLContext::BindOptions"/>
10 <enum-type name="QGLShader::ShaderTypeBit" flags="QGLShader::ShaderType"/>
10 <enum-type name="QGLShader::ShaderTypeBit" flags="QGLShader::ShaderType"/>
11
11
12 <enum-type name="QGLBuffer::Access"/>
13 <enum-type name="QGLBuffer::Type"/>
14 <enum-type name="QGLBuffer::UsagePattern"/>
15 <enum-type name="QGLFunctions::OpenGLFeature"/>
16 <enum-type name="QGLFormat::OpenGLContextProfile"/>
17
12
18 <namespace-type name="QGL">
13 <namespace-type name="QGL">
19 <include file-name="qgl.h" location="global"/>
14 <include file-name="qgl.h" location="global"/>
@@ -32,8 +27,6
32 </value-type>
27 </value-type>
33
28
34 <value-type name="QGLFramebufferObjectFormat"/>
29 <value-type name="QGLFramebufferObjectFormat"/>
35 <object-type name="QGLFunctions"/>
36 <object-type name="QGLBuffer"/>
37 <object-type name="QGLShader"/>
30 <object-type name="QGLShader"/>
38 <object-type name="QGLShaderProgram">
31 <object-type name="QGLShaderProgram">
39 <!-- Should be disambiguated later by fixing the native pointer API -->
32 <!-- Should be disambiguated later by fixing the native pointer API -->
@@ -87,9 +87,9 qt_add_resources(GEN_QRC ${SOURCES_QRC})
87
87
88 include_directories(${CMAKE_CURRENT_SOURCE_DIR})
88 include_directories(${CMAKE_CURRENT_SOURCE_DIR})
89
89
90 add_definitions(-DQT_NO_KEYWORDS) # recent python versions use them :(
90 #add_definitions(-DQT_NO_KEYWORDS) # recent python versions use them :(
91
91
92 add_library(PythonQt SHARED ${SOURCES} ${GEN_MOC} ${GEN_QRC})
92 add_library(PythonQt SHARED ${SOURCES} ${GEN_MOC} ${GEN_QRC} ${HEADERS})
93 if(PythonQt_Qt5)
93 if(PythonQt_Qt5)
94 qt_use_modules(PythonQt Core Gui Widgets)
94 qt_use_modules(PythonQt Core Gui Widgets)
95 else()
95 else()
@@ -166,7 +166,11 PythonQt::PythonQt(int flags, const QByteArray& pythonQtModuleName)
166 _p->_PythonQtObjectPtr_metaId = qRegisterMetaType<PythonQtObjectPtr>("PythonQtObjectPtr");
166 _p->_PythonQtObjectPtr_metaId = qRegisterMetaType<PythonQtObjectPtr>("PythonQtObjectPtr");
167
167
168 if ((flags & PythonAlreadyInitialized) == 0) {
168 if ((flags & PythonAlreadyInitialized) == 0) {
169 #ifdef PY3K
170 Py_SetProgramName(const_cast<wchar_t*>(L"PythonQt"));
171 #else
169 Py_SetProgramName(const_cast<char*>("PythonQt"));
172 Py_SetProgramName(const_cast<char*>("PythonQt"));
173 #endif
170 if (flags & IgnoreSiteModule) {
174 if (flags & IgnoreSiteModule) {
171 // this prevents the automatic importing of Python site files
175 // this prevents the automatic importing of Python site files
172 Py_NoSiteFlag = 1;
176 Py_NoSiteFlag = 1;
@@ -496,7 +500,11 PyObject* PythonQtPrivate::dummyTuple() {
496 static PyObject* dummyTuple = NULL;
500 static PyObject* dummyTuple = NULL;
497 if (dummyTuple==NULL) {
501 if (dummyTuple==NULL) {
498 dummyTuple = PyTuple_New(1);
502 dummyTuple = PyTuple_New(1);
503 #ifdef PY3K
504 PyTuple_SET_ITEM(dummyTuple, 0, PyUnicode_FromString("dummy"));
505 #else
499 PyTuple_SET_ITEM(dummyTuple, 0 , PyString_FromString("dummy"));
506 PyTuple_SET_ITEM(dummyTuple, 0 , PyString_FromString("dummy"));
507 #endif
500 }
508 }
501 return dummyTuple;
509 return dummyTuple;
502 }
510 }
@@ -526,7 +534,11 PythonQtInstanceWrapper* PythonQtPrivate::createNewPythonQtInstanceWrapper(QObje
526 PythonQtClassWrapper* PythonQtPrivate::createNewPythonQtClassWrapper(PythonQtClassInfo* info, PyObject* parentModule) {
534 PythonQtClassWrapper* PythonQtPrivate::createNewPythonQtClassWrapper(PythonQtClassInfo* info, PyObject* parentModule) {
527 PythonQtClassWrapper* result;
535 PythonQtClassWrapper* result;
528
536
537 #ifdef PY3K
538 PyObject* className = PyUnicode_FromString(info->className());
539 #else
529 PyObject* className = PyString_FromString(info->className());
540 PyObject* className = PyString_FromString(info->className());
541 #endif
530
542
531 PyObject* baseClasses = PyTuple_New(1);
543 PyObject* baseClasses = PyTuple_New(1);
532 PyTuple_SET_ITEM(baseClasses, 0, (PyObject*)&PythonQtInstanceWrapper_Type);
544 PyTuple_SET_ITEM(baseClasses, 0, (PyObject*)&PythonQtInstanceWrapper_Type);
@@ -561,10 +573,18 PyObject* PythonQtPrivate::createEnumValueInstance(PyObject* enumType, unsigned
561 PyObject* PythonQtPrivate::createNewPythonQtEnumWrapper(const char* enumName, PyObject* parentObject) {
573 PyObject* PythonQtPrivate::createNewPythonQtEnumWrapper(const char* enumName, PyObject* parentObject) {
562 PyObject* result;
574 PyObject* result;
563
575
576 #ifdef PY3K
577 PyObject* className = PyUnicode_FromString(enumName);
578 #else
564 PyObject* className = PyString_FromString(enumName);
579 PyObject* className = PyString_FromString(enumName);
580 #endif
565
581
566 PyObject* baseClasses = PyTuple_New(1);
582 PyObject* baseClasses = PyTuple_New(1);
583 #ifdef PY3K
584 PyTuple_SET_ITEM(baseClasses, 0, (PyObject*)&PyLong_Type);
585 #else
567 PyTuple_SET_ITEM(baseClasses, 0, (PyObject*)&PyInt_Type);
586 PyTuple_SET_ITEM(baseClasses, 0, (PyObject*)&PyInt_Type);
587 #endif
568
588
569 PyObject* module = PyObject_GetAttrString(parentObject, "__module__");
589 PyObject* module = PyObject_GetAttrString(parentObject, "__module__");
570 PyObject* typeDict = PyDict_New();
590 PyObject* typeDict = PyDict_New();
@@ -703,11 +723,21 QVariant PythonQt::evalCode(PyObject* object, PyObject* pycode) {
703 globals = dict;
723 globals = dict;
704 } else {
724 } else {
705 dict = PyObject_GetAttrString(object, "__dict__");
725 dict = PyObject_GetAttrString(object, "__dict__");
706 globals = PyObject_GetAttrString(PyImport_ImportModule(PyString_AS_STRING(PyObject_GetAttrString(object, "__module__"))),"__dict__");
726 globals = PyObject_GetAttrString(PyImport_ImportModule(
727 #ifdef PY3K
728 PyUnicode_AsUTF8(
729 #else
730 PyString_AS_STRING(
731 #endif
732 PyObject_GetAttrString(object, "__module__"))),"__dict__");
707 }
733 }
708 PyObject* r = NULL;
734 PyObject* r = NULL;
709 if (dict) {
735 if (dict) {
736 #ifdef PY3K
737 r = PyEval_EvalCode(pycode, globals, dict);
738 #else
710 r = PyEval_EvalCode((PyCodeObject*)pycode, globals , dict);
739 r = PyEval_EvalCode((PyCodeObject*)pycode, globals , dict);
740 #endif
711 }
741 }
712 if (r) {
742 if (r) {
713 result = PythonQtConv::PyObjToQVariant(r);
743 result = PythonQtConv::PyObjToQVariant(r);
@@ -894,7 +924,11 QStringList PythonQt::introspectObject(PyObject* object, ObjectType type)
894 } else {
924 } else {
895 PyObject* doc = PyObject_GetAttrString(object, "__doc__");
925 PyObject* doc = PyObject_GetAttrString(object, "__doc__");
896 if (doc) {
926 if (doc) {
927 #ifdef PY3K
928 results << PyUnicode_AsUTF8(doc);
929 #else
897 results << PyString_AsString(doc);
930 results << PyString_AsString(doc);
931 #endif
898 Py_DECREF(doc);
932 Py_DECREF(doc);
899 }
933 }
900 }
934 }
@@ -922,7 +956,11 QStringList PythonQt::introspectObject(PyObject* object, ObjectType type)
922 value = PyObject_GetAttr(object, key);
956 value = PyObject_GetAttr(object, key);
923 }
957 }
924 if (!value) continue;
958 if (!value) continue;
959 #ifdef PY3K
960 keystr = PyUnicode_AsUTF8(key);
961 #else
925 keystr = PyString_AsString(key);
962 keystr = PyString_AsString(key);
963 #endif
926 static const QString underscoreStr("__tmp");
964 static const QString underscoreStr("__tmp");
927 if (!keystr.startsWith(underscoreStr)) {
965 if (!keystr.startsWith(underscoreStr)) {
928 switch (type) {
966 switch (type) {
@@ -930,18 +968,25 QStringList PythonQt::introspectObject(PyObject* object, ObjectType type)
930 results << keystr;
968 results << keystr;
931 break;
969 break;
932 case Class:
970 case Class:
971 #ifdef PY3K
972 if(PyType_Check(value)) {
973 #else
933 if (value->ob_type == &PyClass_Type || value->ob_type == &PyType_Type) {
974 if (value->ob_type == &PyClass_Type || value->ob_type == &PyType_Type) {
975 #endif
934 results << keystr;
976 results << keystr;
935 }
977 }
936 break;
978 break;
937 case Variable:
979 case Variable:
938 if (value->ob_type != &PyClass_Type
980 if (
939 && value->ob_type != &PyCFunction_Type
981 #ifndef PY3K
940 && value->ob_type != &PyFunction_Type
982 value->ob_type != &PyClass_Type &&
941 && value->ob_type != &PyMethod_Type
983 #endif
942 && value->ob_type != &PyModule_Type
984 value->ob_type != &PyCFunction_Type &&
943 && value->ob_type != &PyType_Type
985 value->ob_type != &PyFunction_Type &&
944 && value->ob_type != &PythonQtSlotFunction_Type
986 value->ob_type != &PyMethod_Type &&
987 value->ob_type != &PyModule_Type &&
988 value->ob_type != &PyType_Type &&
989 value->ob_type != &PythonQtSlotFunction_Type
945 ) {
990 ) {
946 results << keystr;
991 results << keystr;
947 }
992 }
@@ -1282,8 +1327,12 int custom_system_exit_exception_handler()
1282 // return exitcode;
1327 // return exitcode;
1283
1328
1284 PyErr_Fetch(&exception, &value, &tb);
1329 PyErr_Fetch(&exception, &value, &tb);
1330 #ifdef PY3K
1331 std::cout << std::endl;
1332 #else
1285 if (Py_FlushLine())
1333 if (Py_FlushLine())
1286 PyErr_Clear();
1334 PyErr_Clear();
1335 #endif
1287 fflush(stdout);
1336 fflush(stdout);
1288 if (value == NULL || value == Py_None)
1337 if (value == NULL || value == Py_None)
1289 goto done;
1338 goto done;
@@ -1299,8 +1348,13 int custom_system_exit_exception_handler()
1299 /* If we failed to dig out the 'code' attribute,
1348 /* If we failed to dig out the 'code' attribute,
1300 just let the else clause below print the error. */
1349 just let the else clause below print the error. */
1301 }
1350 }
1351 #ifdef PY3K
1352 if (PyLong_Check(value))
1353 exitcode = (int)PyLong_AsLong(value);
1354 #else
1302 if (PyInt_Check(value))
1355 if (PyInt_Check(value))
1303 exitcode = (int)PyInt_AsLong(value);
1356 exitcode = (int)PyInt_AsLong(value);
1357 #endif
1304 else {
1358 else {
1305 PyObject *sys_stderr = PySys_GetObject(const_cast<char*>("stderr"));
1359 PyObject *sys_stderr = PySys_GetObject(const_cast<char*>("stderr"));
1306 if (sys_stderr != NULL && sys_stderr != Py_None) {
1360 if (sys_stderr != NULL && sys_stderr != Py_None) {
@@ -1437,13 +1491,32 static PyMethodDef PythonQtMethods[] = {
1437 {NULL, NULL, 0, NULL}
1491 {NULL, NULL, 0, NULL}
1438 };
1492 };
1439
1493
1494 #ifdef PY3K
1495 static PyModuleDef PythonQtModule = {
1496 PyModuleDef_HEAD_INIT,
1497 "",
1498 NULL,
1499 -1,
1500 PythonQtMethods,
1501 NULL,
1502 NULL,
1503 NULL,
1504 NULL
1505 };
1506 #endif
1507
1440 void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQtModuleName)
1508 void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQtModuleName)
1441 {
1509 {
1442 QByteArray name = "PythonQt";
1510 QByteArray name = "PythonQt";
1443 if (!pythonQtModuleName.isEmpty()) {
1511 if (!pythonQtModuleName.isEmpty()) {
1444 name = pythonQtModuleName;
1512 name = pythonQtModuleName;
1445 }
1513 }
1514 #ifdef PY3K
1515 PythonQtModule.m_name = name.constData();
1516 _p->_pythonQtModule = PyModule_Create(&PythonQtModule);
1517 #else
1446 _p->_pythonQtModule = Py_InitModule(name.constData(), PythonQtMethods);
1518 _p->_pythonQtModule = Py_InitModule(name.constData(), PythonQtMethods);
1519 #endif
1447 _p->_pythonQtModuleName = name;
1520 _p->_pythonQtModuleName = name;
1448
1521
1449 if (redirectStdOut) {
1522 if (redirectStdOut) {
@@ -1469,7 +1542,11 void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
1469 PyObject *module_names = PyTuple_New(old_size+1);
1542 PyObject *module_names = PyTuple_New(old_size+1);
1470 for(Py_ssize_t i = 0; i < old_size; i++)
1543 for(Py_ssize_t i = 0; i < old_size; i++)
1471 PyTuple_SetItem(module_names, i, PyTuple_GetItem(old_module_names, i));
1544 PyTuple_SetItem(module_names, i, PyTuple_GetItem(old_module_names, i));
1545 #ifdef PY3K
1546 PyTuple_SetItem(module_names, old_size, PyUnicode_FromString(name.constData()));
1547 #else
1472 PyTuple_SetItem(module_names, old_size, PyString_FromString(name.constData()));
1548 PyTuple_SetItem(module_names, old_size, PyString_FromString(name.constData()));
1549 #endif
1473 PyModule_AddObject(sys.object(),"builtin_module_names",module_names);
1550 PyModule_AddObject(sys.object(),"builtin_module_names",module_names);
1474 Py_DecRef(old_module_names);
1551 Py_DecRef(old_module_names);
1475 }
1552 }
@@ -1512,7 +1589,11 QString PythonQt::getReturnTypeOfWrappedMethodHelper(const PythonQtObjectPtr& va
1512
1589
1513 QString type;
1590 QString type;
1514
1591
1592 #ifdef PY3K
1593 if (PyType_Check(methodObject)) {
1594 #else
1515 if (methodObject->ob_type == &PyClass_Type || methodObject->ob_type == &PyType_Type) {
1595 if (methodObject->ob_type == &PyClass_Type || methodObject->ob_type == &PyType_Type) {
1596 #endif
1516 // the methodObject is not a method, but the name of a type/class. This means
1597 // the methodObject is not a method, but the name of a type/class. This means
1517 // a constructor is called. Return the context.
1598 // a constructor is called. Return the context.
1518 type = context;
1599 type = context;
@@ -1525,8 +1606,13 QString PythonQt::getReturnTypeOfWrappedMethodHelper(const PythonQtObjectPtr& va
1525 } else {
1606 } else {
1526 PyObject* classNameObject = PyObject_GetAttrString(variableObject, "__name__");
1607 PyObject* classNameObject = PyObject_GetAttrString(variableObject, "__name__");
1527 if (classNameObject) {
1608 if (classNameObject) {
1609 #ifdef PY3K
1610 Q_ASSERT(PyUnicode_Check(classNameObject));
1611 className = PyUnicode_AsUTF8(classNameObject);
1612 #else
1528 Q_ASSERT(PyString_Check(classNameObject));
1613 Q_ASSERT(PyString_Check(classNameObject));
1529 className = PyString_AsString(classNameObject);
1614 className = PyString_AsString(classNameObject);
1615 #endif
1530 Py_DECREF(classNameObject);
1616 Py_DECREF(classNameObject);
1531 }
1617 }
1532 }
1618 }
@@ -1559,11 +1645,20 QString PythonQt::getReturnTypeOfWrappedMethodHelper(const PythonQtObjectPtr& va
1559 PythonQtClassInfo* typeInfo = _p->_knownClassInfos.value(type.toLatin1().constData());
1645 PythonQtClassInfo* typeInfo = _p->_knownClassInfos.value(type.toLatin1().constData());
1560 if (typeInfo && typeInfo->pythonQtClassWrapper()) {
1646 if (typeInfo && typeInfo->pythonQtClassWrapper()) {
1561 PyObject* s = PyObject_GetAttrString(typeInfo->pythonQtClassWrapper(), "__module__");
1647 PyObject* s = PyObject_GetAttrString(typeInfo->pythonQtClassWrapper(), "__module__");
1648 #ifdef PY3K
1649 Q_ASSERT(PyUnicode_Check(s));
1650 type = QString(PyUnicode_AsUTF8(s));
1651 #else
1562 Q_ASSERT(PyString_Check(s));
1652 Q_ASSERT(PyString_Check(s));
1563 type = QString(PyString_AsString(s)) + "." + type;
1653 type = QString(PyString_AsString(s)) + "." + type;
1654 #endif
1564 Py_DECREF(s);
1655 Py_DECREF(s);
1565 s = PyObject_GetAttrString(typeInfo->pythonQtClassWrapper(), "__name__");
1656 s = PyObject_GetAttrString(typeInfo->pythonQtClassWrapper(), "__name__");
1657 #ifdef PY3K
1658 Q_ASSERT(PyUnicode_Check(s));
1659 #else
1566 Q_ASSERT(PyString_Check(s));
1660 Q_ASSERT(PyString_Check(s));
1661 #endif
1567 Py_DECREF(s);
1662 Py_DECREF(s);
1568 }
1663 }
1569 }
1664 }
@@ -1668,7 +1763,11 PyObject* PythonQt::helpCalled(PythonQtClassInfo* info)
1668 Q_EMIT pythonHelpRequest(QByteArray(info->className()));
1763 Q_EMIT pythonHelpRequest(QByteArray(info->className()));
1669 return Py_BuildValue("");
1764 return Py_BuildValue("");
1670 } else {
1765 } else {
1766 #ifdef PY3K
1767 return PyUnicode_FromString(info->help().toLatin1().data());
1768 #else
1671 return PyString_FromString(info->help().toLatin1().data());
1769 return PyString_FromString(info->help().toLatin1().data());
1770 #endif
1672 }
1771 }
1673 }
1772 }
1674
1773
@@ -1743,8 +1842,11 bool PythonQtPrivate::isMethodDescriptor(PyObject* object) const
1743 if (PyObject_HasAttrString(object, "__get__") &&
1842 if (PyObject_HasAttrString(object, "__get__") &&
1744 !PyObject_HasAttrString(object, "__set__") &&
1843 !PyObject_HasAttrString(object, "__set__") &&
1745 !PyMethod_Check(object) &&
1844 !PyMethod_Check(object) &&
1746 !PyFunction_Check(object) &&
1845 !PyFunction_Check(object)
1747 !PyClass_Check(object)) {
1846 #ifndef PY3K
1847 && !PyClass_Check(object)
1848 #endif
1849 ) {
1748 return true;
1850 return true;
1749 }
1851 }
1750 return false;
1852 return false;
@@ -1760,7 +1862,11 QString PythonQtPrivate::getSignature(PyObject* object)
1760
1862
1761 bool decrefMethod = false;
1863 bool decrefMethod = false;
1762
1864
1865 #ifdef PY3K
1866 if (PyType_Check(object)) {
1867 #else
1763 if (object->ob_type == &PyClass_Type || object->ob_type == &PyType_Type) {
1868 if (object->ob_type == &PyClass_Type || object->ob_type == &PyType_Type) {
1869 #endif
1764 method = (PyMethodObject*)PyObject_GetAttrString(object, "__init__");
1870 method = (PyMethodObject*)PyObject_GetAttrString(object, "__init__");
1765 decrefMethod = true;
1871 decrefMethod = true;
1766 } else if (object->ob_type == &PyFunction_Type) {
1872 } else if (object->ob_type == &PyFunction_Type) {
@@ -1775,14 +1881,23 QString PythonQtPrivate::getSignature(PyObject* object)
1775 QString docstr;
1881 QString docstr;
1776 PyObject* doc = PyObject_GetAttrString(object, "__doc__");
1882 PyObject* doc = PyObject_GetAttrString(object, "__doc__");
1777 if (doc) {
1883 if (doc) {
1884 #ifdef PY3K
1885 docstr = PyUnicode_AsUTF8(doc);
1886 #else
1778 docstr = PyString_AsString(doc);
1887 docstr = PyString_AsString(doc);
1888 #endif
1779 Py_DECREF(doc);
1889 Py_DECREF(doc);
1780 }
1890 }
1781
1891
1782 PyObject* s = PyObject_GetAttrString(object, "__name__");
1892 PyObject* s = PyObject_GetAttrString(object, "__name__");
1783 if (s) {
1893 if (s) {
1894 #ifdef PY3K
1895 Q_ASSERT(PyUnicode_Check(s));
1896 signature = PyUnicode_AsUTF8(s);
1897 #else
1784 Q_ASSERT(PyString_Check(s));
1898 Q_ASSERT(PyString_Check(s));
1785 signature = PyString_AsString(s);
1899 signature = PyString_AsString(s);
1900 #endif
1786 if (docstr.startsWith(signature + "(")) {
1901 if (docstr.startsWith(signature + "(")) {
1787 signature = docstr;
1902 signature = docstr;
1788 } else {
1903 } else {
@@ -1800,15 +1915,25 QString PythonQtPrivate::getSignature(PyObject* object)
1800 QString funcName;
1915 QString funcName;
1801 PyObject* s = PyObject_GetAttrString((PyObject*)func, "__name__");
1916 PyObject* s = PyObject_GetAttrString((PyObject*)func, "__name__");
1802 if (s) {
1917 if (s) {
1918 #ifdef PY3K
1919 Q_ASSERT(PyUnicode_Check(s));
1920 funcName = PyUnicode_AsUTF8(s);
1921 #else
1803 Q_ASSERT(PyString_Check(s));
1922 Q_ASSERT(PyString_Check(s));
1804 funcName = PyString_AsString(s);
1923 funcName = PyString_AsString(s);
1924 #endif
1805 Py_DECREF(s);
1925 Py_DECREF(s);
1806 }
1926 }
1807 if (method && funcName == "__init__") {
1927 if (method && funcName == "__init__") {
1808 PyObject* s = PyObject_GetAttrString(object, "__name__");
1928 PyObject* s = PyObject_GetAttrString(object, "__name__");
1809 if (s) {
1929 if (s) {
1930 #ifdef PY3K
1931 Q_ASSERT(PyUnicode_Check(s));
1932 funcName = PyUnicode_AsUTF8(s);
1933 #else
1810 Q_ASSERT(PyString_Check(s));
1934 Q_ASSERT(PyString_Check(s));
1811 funcName = PyString_AsString(s);
1935 funcName = PyString_AsString(s);
1936 #endif
1812 Py_DECREF(s);
1937 Py_DECREF(s);
1813 }
1938 }
1814 }
1939 }
@@ -1826,19 +1951,34 QString PythonQtPrivate::getSignature(PyObject* object)
1826 Q_ASSERT(PyTuple_Check(code->co_varnames));
1951 Q_ASSERT(PyTuple_Check(code->co_varnames));
1827 for (int i=0; i<nargs; i++) {
1952 for (int i=0; i<nargs; i++) {
1828 PyObject* name = PyTuple_GetItem(code->co_varnames, i);
1953 PyObject* name = PyTuple_GetItem(code->co_varnames, i);
1954 #ifdef PY3K
1955 Q_ASSERT(PyUnicode_Check(name));
1956 arguments << PyUnicode_AsUTF8(name);
1957 #else
1829 Q_ASSERT(PyString_Check(name));
1958 Q_ASSERT(PyString_Check(name));
1830 arguments << PyString_AsString(name);
1959 arguments << PyString_AsString(name);
1960 #endif
1831 }
1961 }
1832 if (code->co_flags & CO_VARARGS) {
1962 if (code->co_flags & CO_VARARGS) {
1833 PyObject* s = PyTuple_GetItem(code->co_varnames, nargs);
1963 PyObject* s = PyTuple_GetItem(code->co_varnames, nargs);
1964 #ifdef PY3K
1965 Q_ASSERT(PyUnicode_Check(s));
1966 varargs = PyUnicode_AsUTF8(s);
1967 #else
1834 Q_ASSERT(PyString_Check(s));
1968 Q_ASSERT(PyString_Check(s));
1835 varargs = PyString_AsString(s);
1969 varargs = PyString_AsString(s);
1970 #endif
1836 nargs += 1;
1971 nargs += 1;
1837 }
1972 }
1838 if (code->co_flags & CO_VARKEYWORDS) {
1973 if (code->co_flags & CO_VARKEYWORDS) {
1839 PyObject* s = PyTuple_GetItem(code->co_varnames, nargs);
1974 PyObject* s = PyTuple_GetItem(code->co_varnames, nargs);
1975 #ifdef PY3K
1976 Q_ASSERT(PyUnicode_Check(s));
1977 varkeywords = PyUnicode_AsUTF8(s);
1978 #else
1840 Q_ASSERT(PyString_Check(s));
1979 Q_ASSERT(PyString_Check(s));
1841 varkeywords = PyString_AsString(s);
1980 varkeywords = PyString_AsString(s);
1981 #endif
1842 }
1982 }
1843 }
1983 }
1844
1984
@@ -1848,8 +1988,13 QString PythonQtPrivate::getSignature(PyObject* object)
1848 for (Py_ssize_t i=0; i<PyTuple_Size(defaultsTuple); i++) {
1988 for (Py_ssize_t i=0; i<PyTuple_Size(defaultsTuple); i++) {
1849 PyObject* d = PyTuple_GetItem(defaultsTuple, i);
1989 PyObject* d = PyTuple_GetItem(defaultsTuple, i);
1850 PyObject* s = PyObject_Repr(d);
1990 PyObject* s = PyObject_Repr(d);
1991 #ifdef PY3K
1992 Q_ASSERT(PyUnicode_Check(s));
1993 defaults << PyUnicode_AsUTF8(s);
1994 #else
1851 Q_ASSERT(PyString_Check(s));
1995 Q_ASSERT(PyString_Check(s));
1852 defaults << PyString_AsString(s);
1996 defaults << PyString_AsString(s);
1997 #endif
1853 Py_DECREF(s);
1998 Py_DECREF(s);
1854 }
1999 }
1855 }
2000 }
@@ -1900,12 +2045,20 void PythonQtPrivate::shellClassDeleted( void* shellClass )
1900
2045
1901 PyObject* PythonQtPrivate::wrapMemoryAsBuffer( const void* data, Py_ssize_t size )
2046 PyObject* PythonQtPrivate::wrapMemoryAsBuffer( const void* data, Py_ssize_t size )
1902 {
2047 {
1903 // P3K port needed later on!
2048 // P3K port needed later on! -- not anymore :D
1904 return PyBuffer_FromMemory((void*)data, size);
2049 #ifdef PY3K
2050 return PyMemoryView_FromMemory((char*)data, size, PyBUF_READ);
2051 #else
2052 return PyBuffer_FromMemory((char*)data, size);
2053 #endif
1905 }
2054 }
1906
2055
1907 PyObject* PythonQtPrivate::wrapMemoryAsBuffer( void* data, Py_ssize_t size )
2056 PyObject* PythonQtPrivate::wrapMemoryAsBuffer( void* data, Py_ssize_t size )
1908 {
2057 {
1909 // P3K port needed later on!
2058 // P3K port needed later on! -- not anymore :D
1910 return PyBuffer_FromReadWriteMemory(data, size);
2059 #ifdef PY3K
2060 return PyMemoryView_FromMemory((char*)data, size, PyBUF_READ | PyBUF_WRITE);
2061 #else
2062 return PyBuffer_FromReadWriteMemory((char*)data, size);
2063 #endif
1911 }
2064 }
@@ -156,7 +156,9 static void initializeSlots(PythonQtClassWrapper* wrap)
156 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_mul;
156 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_mul;
157 }
157 }
158 if (typeSlots & PythonQt::Type_Divide) {
158 if (typeSlots & PythonQt::Type_Divide) {
159 #ifndef PY3K
159 wrap->_base.as_number.nb_divide = (binaryfunc)PythonQtInstanceWrapper_div;
160 wrap->_base.as_number.nb_divide = (binaryfunc)PythonQtInstanceWrapper_div;
161 #endif
160 wrap->_base.as_number.nb_true_divide = (binaryfunc)PythonQtInstanceWrapper_div;
162 wrap->_base.as_number.nb_true_divide = (binaryfunc)PythonQtInstanceWrapper_div;
161 }
163 }
162 if (typeSlots & PythonQt::Type_And) {
164 if (typeSlots & PythonQt::Type_And) {
@@ -188,7 +190,9 static void initializeSlots(PythonQtClassWrapper* wrap)
188 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_imul;
190 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_imul;
189 }
191 }
190 if (typeSlots & PythonQt::Type_InplaceDivide) {
192 if (typeSlots & PythonQt::Type_InplaceDivide) {
193 #ifndef PY3K
191 wrap->_base.as_number.nb_inplace_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
194 wrap->_base.as_number.nb_inplace_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
195 #endif
192 wrap->_base.as_number.nb_inplace_true_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
196 wrap->_base.as_number.nb_inplace_true_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
193 }
197 }
194 if (typeSlots & PythonQt::Type_InplaceAnd) {
198 if (typeSlots & PythonQt::Type_InplaceAnd) {
@@ -213,7 +217,11 static void initializeSlots(PythonQtClassWrapper* wrap)
213 wrap->_base.as_number.nb_invert = (unaryfunc)PythonQtInstanceWrapper_invert;
217 wrap->_base.as_number.nb_invert = (unaryfunc)PythonQtInstanceWrapper_invert;
214 }
218 }
215 if (typeSlots & PythonQt::Type_NonZero) {
219 if (typeSlots & PythonQt::Type_NonZero) {
220 #ifdef PY3K
221 wrap->_base.as_number.nb_bool = (inquiry)PythonQtInstanceWrapper_nonzero;
222 #else
216 wrap->_base.as_number.nb_nonzero = (inquiry)PythonQtInstanceWrapper_nonzero;
223 wrap->_base.as_number.nb_nonzero = (inquiry)PythonQtInstanceWrapper_nonzero;
224 #endif
217 }
225 }
218 }
226 }
219 }
227 }
@@ -252,10 +260,10 static int PythonQtClassWrapper_init(PythonQtClassWrapper* self, PyObject* args,
252 // ...
260 // ...
253 // class MyWidget(MyWidgetBase):
261 // class MyWidget(MyWidgetBase):
254 // ...
262 // ...
255 while( superType && superType->ob_type != &PythonQtClassWrapper_Type )
263 while( superType && Py_TYPE(superType) != &PythonQtClassWrapper_Type )
256 superType = superType->tp_base;
264 superType = superType->tp_base;
257
265
258 if (!superType || (superType->ob_type != &PythonQtClassWrapper_Type)) {
266 if (!superType || (Py_TYPE(superType) != &PythonQtClassWrapper_Type)) {
259 PyErr_Format(PyExc_TypeError, "type %s is not derived from PythonQtClassWrapper", ((PyTypeObject*)self)->tp_name);
267 PyErr_Format(PyExc_TypeError, "type %s is not derived from PythonQtClassWrapper", ((PyTypeObject*)self)->tp_name);
260 return -1;
268 return -1;
261 }
269 }
@@ -269,7 +277,11 static int PythonQtClassWrapper_init(PythonQtClassWrapper* self, PyObject* args,
269
277
270 static PyObject *PythonQtClassWrapper_classname(PythonQtClassWrapper* type)
278 static PyObject *PythonQtClassWrapper_classname(PythonQtClassWrapper* type)
271 {
279 {
280 #ifdef PY3K
281 return PyUnicode_FromString((QString("Class_") + type->classInfo()->className()).toLatin1().data());
282 #else
272 return PyString_FromString((QString("Class_") + type->classInfo()->className()).toLatin1().data());
283 return PyString_FromString((QString("Class_") + type->classInfo()->className()).toLatin1().data());
284 #endif
273 }
285 }
274
286
275 static PyObject *PythonQtClassWrapper_help(PythonQtClassWrapper* type)
287 static PyObject *PythonQtClassWrapper_help(PythonQtClassWrapper* type)
@@ -325,7 +337,11 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
325 const char *attributeName;
337 const char *attributeName;
326 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
338 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
327
339
340 #ifdef PY3K
341 if ((attributeName = PyUnicode_AsUTF8(name)) == NULL) {
342 #else
328 if ((attributeName = PyString_AsString(name)) == NULL) {
343 if ((attributeName = PyString_AsString(name)) == NULL) {
344 #endif
329 return NULL;
345 return NULL;
330 }
346 }
331 if (obj == (PyObject*)&PythonQtInstanceWrapper_Type) {
347 if (obj == (PyObject*)&PythonQtInstanceWrapper_Type) {
@@ -352,7 +368,11 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
352 }
368 }
353 }
369 }
354 if (wrapper->classInfo()->constructors()) {
370 if (wrapper->classInfo()->constructors()) {
371 #ifdef PY3K
372 PyObject* initName = PyUnicode_FromString("__init__");
373 #else
355 PyObject* initName = PyString_FromString("__init__");
374 PyObject* initName = PyString_FromString("__init__");
375 #endif
356 PyObject* func = PyType_Type.tp_getattro(obj, initName);
376 PyObject* func = PyType_Type.tp_getattro(obj, initName);
357 Py_DECREF(initName);
377 Py_DECREF(initName);
358 PyDict_SetItemString(dict, "__init__", func);
378 PyDict_SetItemString(dict, "__init__", func);
@@ -369,6 +389,9 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
369 }
389 }
370
390
371 // look in Python to support derived Python classes
391 // look in Python to support derived Python classes
392 #ifdef PY3K
393 return PyObject_GenericGetAttr(obj, name);
394 #else
372 PyObject* superAttr = PyType_Type.tp_getattro(obj, name);
395 PyObject* superAttr = PyType_Type.tp_getattro(obj, name);
373 if (superAttr) {
396 if (superAttr) {
374 return superAttr;
397 return superAttr;
@@ -403,6 +426,7 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
403 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
426 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
404 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
427 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
405 return NULL;
428 return NULL;
429 #endif
406 }
430 }
407
431
408 static int PythonQtClassWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
432 static int PythonQtClassWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
@@ -435,8 +459,7 static PyObject * PythonQtClassWrapper_repr(PyObject * obj)
435 */
459 */
436
460
437 PyTypeObject PythonQtClassWrapper_Type = {
461 PyTypeObject PythonQtClassWrapper_Type = {
438 PyObject_HEAD_INIT(NULL)
462 PyVarObject_HEAD_INIT(NULL, 0)
439 0, /*ob_size*/
440 "PythonQt.PythonQtClassWrapper", /*tp_name*/
463 "PythonQt.PythonQtClassWrapper", /*tp_name*/
441 sizeof(PythonQtClassWrapper), /*tp_basicsize*/
464 sizeof(PythonQtClassWrapper), /*tp_basicsize*/
442 0, /*tp_itemsize*/
465 0, /*tp_itemsize*/
@@ -45,6 +45,10
45 #include <QTime>
45 #include <QTime>
46 #include <QDate>
46 #include <QDate>
47
47
48 #if PY_MAJOR_VERSION >= 3
49 #define PY3K
50 #endif
51
48 PythonQtValueStorage<qint64, 128> PythonQtConv::global_valueStorage;
52 PythonQtValueStorage<qint64, 128> PythonQtConv::global_valueStorage;
49 PythonQtValueStorage<void*, 128> PythonQtConv::global_ptrStorage;
53 PythonQtValueStorage<void*, 128> PythonQtConv::global_ptrStorage;
50 PythonQtValueStorageWithCleanup<QVariant, 128> PythonQtConv::global_variantStorage;
54 PythonQtValueStorageWithCleanup<QVariant, 128> PythonQtConv::global_variantStorage;
@@ -78,7 +82,11 PyObject* PythonQtConv::ConvertQtValueToPython(const PythonQtMethodInfo::Paramet
78 // a char ptr will probably be a null terminated string, so we support that:
82 // a char ptr will probably be a null terminated string, so we support that:
79 char* charPtr = *((char**)data);
83 char* charPtr = *((char**)data);
80 if (charPtr) {
84 if (charPtr) {
85 #ifdef PY3K
86 return PyUnicode_FromString(charPtr);
87 #else
81 return PyString_FromString(charPtr);
88 return PyString_FromString(charPtr);
89 #endif
82 } else {
90 } else {
83 Py_INCREF(Py_None);
91 Py_INCREF(Py_None);
84 return Py_None;
92 return Py_None;
@@ -129,27 +137,27 PyObject* PythonQtConv::ConvertQtValueToPythonInternal(int type, const void* dat
129 Py_INCREF(Py_None);
137 Py_INCREF(Py_None);
130 return Py_None;
138 return Py_None;
131 case QMetaType::Char:
139 case QMetaType::Char:
132 return PyInt_FromLong(*((char*)data));
140 return PyLong_FromLong(*((char*)data));
133 case QMetaType::UChar:
141 case QMetaType::UChar:
134 return PyInt_FromLong(*((unsigned char*)data));
142 return PyLong_FromLong(*((unsigned char*)data));
135 case QMetaType::Short:
143 case QMetaType::Short:
136 return PyInt_FromLong(*((short*)data));
144 return PyLong_FromLong(*((short*)data));
137 case QMetaType::UShort:
145 case QMetaType::UShort:
138 return PyInt_FromLong(*((unsigned short*)data));
146 return PyLong_FromLong(*((unsigned short*)data));
139 case QMetaType::Long:
147 case QMetaType::Long:
140 return PyInt_FromLong(*((long*)data));
148 return PyLong_FromLong(*((long*)data));
141 case QMetaType::ULong:
149 case QMetaType::ULong:
142 // does not fit into simple int of python
150 // does not fit into simple int of python
143 return PyLong_FromUnsignedLong(*((unsigned long*)data));
151 return PyLong_FromUnsignedLong(*((unsigned long*)data));
144 case QMetaType::Bool:
152 case QMetaType::Bool:
145 return PythonQtConv::GetPyBool(*((bool*)data));
153 return PythonQtConv::GetPyBool(*((bool*)data));
146 case QMetaType::Int:
154 case QMetaType::Int:
147 return PyInt_FromLong(*((int*)data));
155 return PyLong_FromLong(*((int*)data));
148 case QMetaType::UInt:
156 case QMetaType::UInt:
149 // does not fit into simple int of python
157 // does not fit into simple int of python
150 return PyLong_FromUnsignedLong(*((unsigned int*)data));
158 return PyLong_FromUnsignedLong(*((unsigned int*)data));
151 case QMetaType::QChar:
159 case QMetaType::QChar:
152 return PyInt_FromLong(*((short*)data));
160 return PyLong_FromLong(*((short*)data));
153 case QMetaType::Float:
161 case QMetaType::Float:
154 return PyFloat_FromDouble(*((float*)data));
162 return PyFloat_FromDouble(*((float*)data));
155 case QMetaType::Double:
163 case QMetaType::Double:
@@ -293,7 +301,7 void* PythonQtConv::handlePythonToQtAutoConversion(int typeId, PyObject* obj, vo
293 if (typeId == cursorId) {
301 if (typeId == cursorId) {
294 static PyObject* qtCursorShapeEnum = PythonQtClassInfo::findEnumWrapper("Qt::CursorShape", NULL);
302 static PyObject* qtCursorShapeEnum = PythonQtClassInfo::findEnumWrapper("Qt::CursorShape", NULL);
295 if ((PyObject*)obj->ob_type == qtCursorShapeEnum) {
303 if ((PyObject*)obj->ob_type == qtCursorShapeEnum) {
296 Qt::CursorShape val = (Qt::CursorShape)PyInt_AS_LONG(obj);
304 Qt::CursorShape val = (Qt::CursorShape)PyLong_AsLong(obj);
297 if (!ptr) {
305 if (!ptr) {
298 PythonQtValueStorage_ADD_VALUE(global_variantStorage, QVariant, QCursor(), ptr);
306 PythonQtValueStorage_ADD_VALUE(global_variantStorage, QVariant, QCursor(), ptr);
299 ptr = (void*)((QVariant*)ptr)->constData();
307 ptr = (void*)((QVariant*)ptr)->constData();
@@ -305,7 +313,7 void* PythonQtConv::handlePythonToQtAutoConversion(int typeId, PyObject* obj, vo
305 // brushes can be created from QColor and from Qt::GlobalColor (and from brushes, but that's the default)
313 // brushes can be created from QColor and from Qt::GlobalColor (and from brushes, but that's the default)
306 static PyObject* qtColorClass = PythonQt::priv()->getClassInfo("QColor")->pythonQtClassWrapper();
314 static PyObject* qtColorClass = PythonQt::priv()->getClassInfo("QColor")->pythonQtClassWrapper();
307 if ((PyObject*)obj->ob_type == qtGlobalColorEnum) {
315 if ((PyObject*)obj->ob_type == qtGlobalColorEnum) {
308 Qt::GlobalColor val = (Qt::GlobalColor)PyInt_AS_LONG(obj);
316 Qt::GlobalColor val = (Qt::GlobalColor)PyLong_AsLong(obj);
309 if (!ptr) {
317 if (!ptr) {
310 PythonQtValueStorage_ADD_VALUE(global_variantStorage, QVariant, QPen(), ptr);
318 PythonQtValueStorage_ADD_VALUE(global_variantStorage, QVariant, QPen(), ptr);
311 ptr = (void*)((QVariant*)ptr)->constData();
319 ptr = (void*)((QVariant*)ptr)->constData();
@@ -324,7 +332,7 void* PythonQtConv::handlePythonToQtAutoConversion(int typeId, PyObject* obj, vo
324 // brushes can be created from QColor and from Qt::GlobalColor (and from brushes, but that's the default)
332 // brushes can be created from QColor and from Qt::GlobalColor (and from brushes, but that's the default)
325 static PyObject* qtColorClass = PythonQt::priv()->getClassInfo("QColor")->pythonQtClassWrapper();
333 static PyObject* qtColorClass = PythonQt::priv()->getClassInfo("QColor")->pythonQtClassWrapper();
326 if ((PyObject*)obj->ob_type == qtGlobalColorEnum) {
334 if ((PyObject*)obj->ob_type == qtGlobalColorEnum) {
327 Qt::GlobalColor val = (Qt::GlobalColor)PyInt_AS_LONG(obj);
335 Qt::GlobalColor val = (Qt::GlobalColor)PyLong_AsLong(obj);
328 if (!ptr) {
336 if (!ptr) {
329 PythonQtValueStorage_ADD_VALUE(global_variantStorage, QVariant, QBrush(), ptr);
337 PythonQtValueStorage_ADD_VALUE(global_variantStorage, QVariant, QBrush(), ptr);
330 ptr = (void*)((QVariant*)ptr)->constData();
338 ptr = (void*)((QVariant*)ptr)->constData();
@@ -342,7 +350,7 void* PythonQtConv::handlePythonToQtAutoConversion(int typeId, PyObject* obj, vo
342 } else if (typeId == colorId) {
350 } else if (typeId == colorId) {
343 // colors can be created from Qt::GlobalColor (and from colors, but that's the default)
351 // colors can be created from Qt::GlobalColor (and from colors, but that's the default)
344 if ((PyObject*)obj->ob_type == qtGlobalColorEnum) {
352 if ((PyObject*)obj->ob_type == qtGlobalColorEnum) {
345 Qt::GlobalColor val = (Qt::GlobalColor)PyInt_AS_LONG(obj);
353 Qt::GlobalColor val = (Qt::GlobalColor)PyLong_AsLong(obj);
346 if (!ptr) {
354 if (!ptr) {
347 PythonQtValueStorage_ADD_VALUE(global_variantStorage, QVariant, QColor(), ptr);
355 PythonQtValueStorage_ADD_VALUE(global_variantStorage, QVariant, QColor(), ptr);
348 ptr = (void*)((QVariant*)ptr)->constData();
356 ptr = (void*)((QVariant*)ptr)->constData();
@@ -393,10 +401,18 void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
393 // a pointer
401 // a pointer
394 if (info.typeId == QMetaType::Char || info.typeId == QMetaType::UChar)
402 if (info.typeId == QMetaType::Char || info.typeId == QMetaType::UChar)
395 {
403 {
404 #ifdef PY3K
405 if (PyUnicode_Check(obj)) {
406 QByteArray bytes(PyUnicode_AsUTF8(obj));
407 void* ptr2 = NULL;
408 PythonQtValueStorage_ADD_VALUE_IF_NEEDED(NULL,global_variantStorage, QVariant, QVariant(bytes), ptr2);
409 PythonQtValueStorage_ADD_VALUE_IF_NEEDED(alreadyAllocatedCPPObject,global_ptrStorage, void*, (((QByteArray*)((QVariant*)ptr2)->constData())->data()), ptr);
410 #else
396 if (obj->ob_type == &PyString_Type) {
411 if (obj->ob_type == &PyString_Type) {
397 // take direct reference to string data
412 // take direct reference to string data
398 const char* data = PyString_AS_STRING(obj);
413 const char* data = PyString_AS_STRING(obj);
399 PythonQtValueStorage_ADD_VALUE_IF_NEEDED(alreadyAllocatedCPPObject,global_ptrStorage, void*, (void*)data, ptr);
414 PythonQtValueStorage_ADD_VALUE_IF_NEEDED(alreadyAllocatedCPPObject,global_ptrStorage, void*, (void*)data, ptr);
415 #endif
400 } else {
416 } else {
401 // convert to string
417 // convert to string
402 QString str = PyObjGetString(obj, strict, ok);
418 QString str = PyObjGetString(obj, strict, ok);
@@ -601,7 +617,7 void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
601 ok = false;
617 ok = false;
602 if ((PyObject*)obj->ob_type == info.enumWrapper) {
618 if ((PyObject*)obj->ob_type == info.enumWrapper) {
603 // we have a exact enum type match:
619 // we have a exact enum type match:
604 val = PyInt_AS_LONG(obj);
620 val = PyLong_AsLong(obj);
605 ok = true;
621 ok = true;
606 } else if (!strict) {
622 } else if (!strict) {
607 // we try to get any integer, when not being strict. If we are strict, integers are not wanted because
623 // we try to get any integer, when not being strict. If we are strict, integers are not wanted because
@@ -681,8 +697,10 QStringList PythonQtConv::PyObjToStringList(PyObject* val, bool strict, bool& ok
681 ok = false;
697 ok = false;
682 // if we are strict, we do not want to convert a string to a stringlist
698 // if we are strict, we do not want to convert a string to a stringlist
683 // (strings in python are detected to be sequences)
699 // (strings in python are detected to be sequences)
684 if (strict &&
700 if (strict && (
685 (val->ob_type == &PyString_Type ||
701 #ifndef PY3K
702 val->ob_type == &PyString_Type ||
703 #endif
686 PyUnicode_Check(val))) {
704 PyUnicode_Check(val))) {
687 ok = false;
705 ok = false;
688 return v;
706 return v;
@@ -703,7 +721,13 QString PythonQtConv::PyObjGetRepresentation(PyObject* val)
703 QString r;
721 QString r;
704 PyObject* str = PyObject_Repr(val);
722 PyObject* str = PyObject_Repr(val);
705 if (str) {
723 if (str) {
724 #ifdef PY3K
725 Py_UCS4 *x = PyUnicode_AsUCS4Copy(str);
726 r = QString::fromUcs4(x, PyUnicode_GetLength(str));
727 PyMem_Free(x);
728 #else
706 r = QString(PyString_AS_STRING(str));
729 r = QString(PyString_AS_STRING(str));
730 #endif
707 Py_DECREF(str);
731 Py_DECREF(str);
708 }
732 }
709 return r;
733 return r;
@@ -712,19 +736,34 QString PythonQtConv::PyObjGetRepresentation(PyObject* val)
712 QString PythonQtConv::PyObjGetString(PyObject* val, bool strict, bool& ok) {
736 QString PythonQtConv::PyObjGetString(PyObject* val, bool strict, bool& ok) {
713 QString r;
737 QString r;
714 ok = true;
738 ok = true;
715 if (val->ob_type == &PyString_Type) {
739 #ifndef PY3K
740 if (Py_TYPE(val) == &PyString_Type) {
716 r = QString(PyString_AS_STRING(val));
741 r = QString(PyString_AS_STRING(val));
717 } else if (PyUnicode_Check(val)) {
742 } else
743 #endif
744 if (PyUnicode_Check(val)) {
745 #ifdef PY3K
746 Py_UCS4 *x = PyUnicode_AsUCS4Copy(val);
747 r = QString::fromUcs4(x, PyUnicode_GetLength(val));
748 PyMem_Free(x);
749 #else
718 PyObject *ptmp = PyUnicode_AsUTF8String(val);
750 PyObject *ptmp = PyUnicode_AsUTF8String(val);
719 if(ptmp) {
751 if(ptmp) {
720 r = QString::fromUtf8(PyString_AS_STRING(ptmp));
752 r = QString::fromUtf8(PyString_AS_STRING(ptmp));
721 Py_DECREF(ptmp);
753 Py_DECREF(ptmp);
722 }
754 }
755 #endif
723 } else if (!strict) {
756 } else if (!strict) {
724 // EXTRA: could also use _Unicode, but why should we?
757 // EXTRA: could also use _Unicode, but why should we?
725 PyObject* str = PyObject_Str(val);
758 PyObject* str = PyObject_Str(val);
726 if (str) {
759 if (str) {
760 #ifdef PY3K
761 Py_UCS4 *x = PyUnicode_AsUCS4Copy(str);
762 r = QString::fromUcs4(x, PyUnicode_GetLength(str));
763 PyMem_Free(x);
764 #else
727 r = QString(PyString_AS_STRING(str));
765 r = QString(PyString_AS_STRING(str));
766 #endif
728 Py_DECREF(str);
767 Py_DECREF(str);
729 } else {
768 } else {
730 ok = false;
769 ok = false;
@@ -739,9 +778,14 QByteArray PythonQtConv::PyObjGetBytes(PyObject* val, bool /*strict*/, bool& ok)
739 // TODO: support buffer objects in general
778 // TODO: support buffer objects in general
740 QByteArray r;
779 QByteArray r;
741 ok = true;
780 ok = true;
742 if (val->ob_type == &PyString_Type) {
781 #ifdef PY3K
782 if (PyBytes_Check(val)) {
783 r = QByteArray(PyBytes_AS_STRING(val), PyBytes_GET_SIZE(val));
784 #else
785 if (Py_TYPE(val) == &PyString_Type) {
743 long size = PyString_GET_SIZE(val);
786 long size = PyString_GET_SIZE(val);
744 r = QByteArray(PyString_AS_STRING(val), size);
787 r = QByteArray(PyString_AS_STRING(val), size);
788 #endif
745 } else {
789 } else {
746 ok = false;
790 ok = false;
747 }
791 }
@@ -769,17 +813,25 bool PythonQtConv::PyObjGetBool(PyObject* val, bool strict, bool &ok) {
769 int PythonQtConv::PyObjGetInt(PyObject* val, bool strict, bool &ok) {
813 int PythonQtConv::PyObjGetInt(PyObject* val, bool strict, bool &ok) {
770 int d = 0;
814 int d = 0;
771 ok = true;
815 ok = true;
816 #ifndef PY3K
772 if (val->ob_type == &PyInt_Type) {
817 if (val->ob_type == &PyInt_Type) {
773 d = PyInt_AS_LONG(val);
818 d = PyInt_AS_LONG(val);
819 } else
820 #endif
821 if (Py_TYPE(val) == &PyLong_Type) {
822 // handle error on overflow!
823 d = PyLong_AsLong(val);
774 } else if (!strict) {
824 } else if (!strict) {
825 #ifndef PY3K
775 if (PyObject_TypeCheck(val, &PyInt_Type)) {
826 if (PyObject_TypeCheck(val, &PyInt_Type)) {
776 // support for derived int classes, e.g. for our enums
827 // support for derived int classes, e.g. for our enums
777 d = PyInt_AS_LONG(val);
828 d = PyInt_AS_LONG(val);
778 } else if (val->ob_type == &PyFloat_Type) {
829 } else
779 d = floor(PyFloat_AS_DOUBLE(val));
830 #endif
780 } else if (val->ob_type == &PyLong_Type) {
831 if (PyObject_TypeCheck(val, &PyLong_Type)) {
781 // handle error on overflow!
782 d = PyLong_AsLong(val);
832 d = PyLong_AsLong(val);
833 } else if (Py_TYPE(val) == &PyFloat_Type) {
834 d = floor(PyFloat_AS_DOUBLE(val));
783 } else if (val == Py_False) {
835 } else if (val == Py_False) {
784 d = 0;
836 d = 0;
785 } else if (val == Py_True) {
837 } else if (val == Py_True) {
@@ -787,7 +839,7 int PythonQtConv::PyObjGetInt(PyObject* val, bool strict, bool &ok) {
787 } else {
839 } else {
788 PyErr_Clear();
840 PyErr_Clear();
789 // PyInt_AsLong will try conversion to an int if the object is not an int:
841 // PyInt_AsLong will try conversion to an int if the object is not an int:
790 d = PyInt_AsLong(val);
842 d = PyLong_AsLong(val);
791 if (PyErr_Occurred()) {
843 if (PyErr_Occurred()) {
792 ok = false;
844 ok = false;
793 PyErr_Clear();
845 PyErr_Clear();
@@ -802,15 +854,23 int PythonQtConv::PyObjGetInt(PyObject* val, bool strict, bool &ok) {
802 qint64 PythonQtConv::PyObjGetLongLong(PyObject* val, bool strict, bool &ok) {
854 qint64 PythonQtConv::PyObjGetLongLong(PyObject* val, bool strict, bool &ok) {
803 qint64 d = 0;
855 qint64 d = 0;
804 ok = true;
856 ok = true;
857 #ifndef PY3K
805 if (val->ob_type == &PyInt_Type) {
858 if (val->ob_type == &PyInt_Type) {
806 d = PyInt_AS_LONG(val);
859 d = PyInt_AS_LONG(val);
807 } else if (val->ob_type == &PyLong_Type) {
860 } else
861 #endif
862 if (Py_TYPE(val) == &PyLong_Type) {
808 d = PyLong_AsLongLong(val);
863 d = PyLong_AsLongLong(val);
809 } else if (!strict) {
864 } else if (!strict) {
865 #ifndef PY3K
810 if (PyObject_TypeCheck(val, &PyInt_Type)) {
866 if (PyObject_TypeCheck(val, &PyInt_Type)) {
811 // support for derived int classes, e.g. for our enums
867 // support for derived int classes, e.g. for our enums
812 d = PyInt_AS_LONG(val);
868 d = PyInt_AS_LONG(val);
813 } else if (val->ob_type == &PyFloat_Type) {
869 } else
870 #endif
871 if (PyObject_TypeCheck(val, &PyLong_Type)) {
872 d = PyLong_AsLong(val);
873 } else if (Py_TYPE(val) == &PyFloat_Type) {
814 d = floor(PyFloat_AS_DOUBLE(val));
874 d = floor(PyFloat_AS_DOUBLE(val));
815 } else if (val == Py_False) {
875 } else if (val == Py_False) {
816 d = 0;
876 d = 0;
@@ -834,15 +894,23 qint64 PythonQtConv::PyObjGetLongLong(PyObject* val, bool strict, bool &ok) {
834 quint64 PythonQtConv::PyObjGetULongLong(PyObject* val, bool strict, bool &ok) {
894 quint64 PythonQtConv::PyObjGetULongLong(PyObject* val, bool strict, bool &ok) {
835 quint64 d = 0;
895 quint64 d = 0;
836 ok = true;
896 ok = true;
837 if (PyObject_TypeCheck(val, &PyInt_Type)) {
897 #ifndef PY3K
898 if (Py_TYPE(val) == &PyInt_Type) {
838 d = PyInt_AS_LONG(val);
899 d = PyInt_AS_LONG(val);
839 } else if (val->ob_type == &PyLong_Type) {
900 } else
901 #endif
902 if (Py_TYPE(val) == &PyLong_Type) {
840 d = PyLong_AsLongLong(val);
903 d = PyLong_AsLongLong(val);
841 } else if (!strict) {
904 } else if (!strict) {
905 #ifndef PY3K
842 if (PyObject_TypeCheck(val, &PyInt_Type)) {
906 if (PyObject_TypeCheck(val, &PyInt_Type)) {
843 // support for derived int classes, e.g. for our enums
907 // support for derived int classes, e.g. for our enums
844 d = PyInt_AS_LONG(val);
908 d = PyInt_AS_LONG(val);
845 } else if (val->ob_type == &PyFloat_Type) {
909 } else
910 #endif
911 if (PyObject_TypeCheck(val, &PyLong_Type)) {
912 d = PyLong_AsLong(val);
913 } else if (Py_TYPE(val) == &PyFloat_Type) {
846 d = floor(PyFloat_AS_DOUBLE(val));
914 d = floor(PyFloat_AS_DOUBLE(val));
847 } else if (val == Py_False) {
915 } else if (val == Py_False) {
848 d = 0;
916 d = 0;
@@ -869,9 +937,12 double PythonQtConv::PyObjGetDouble(PyObject* val, bool strict, bool &ok) {
869 if (val->ob_type == &PyFloat_Type) {
937 if (val->ob_type == &PyFloat_Type) {
870 d = PyFloat_AS_DOUBLE(val);
938 d = PyFloat_AS_DOUBLE(val);
871 } else if (!strict) {
939 } else if (!strict) {
940 #ifndef PY3K
872 if (PyObject_TypeCheck(val, &PyInt_Type)) {
941 if (PyObject_TypeCheck(val, &PyInt_Type)) {
873 d = PyInt_AS_LONG(val);
942 d = PyInt_AS_LONG(val);
874 } else if (val->ob_type == &PyLong_Type) {
943 } else
944 #endif
945 if (PyLong_Check(val)) {
875 d = PyLong_AsLong(val);
946 d = PyLong_AsLong(val);
876 } else if (val == Py_False) {
947 } else if (val == Py_False) {
877 d = 0;
948 d = 0;
@@ -899,15 +970,23 QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
899
970
900 if (type==-1) {
971 if (type==-1) {
901 // no special type requested
972 // no special type requested
973 #ifdef PY3K
974 if (PyBytes_Check(val)) {
975 type = QVariant::ByteArray;
976 } else if (PyUnicode_Check(val)) {
977 #else
902 if (val->ob_type==&PyString_Type || val->ob_type==&PyUnicode_Type) {
978 if (val->ob_type==&PyString_Type || val->ob_type==&PyUnicode_Type) {
979 #endif
903 type = QVariant::String;
980 type = QVariant::String;
904 } else if (val == Py_False || val == Py_True) {
981 } else if (val == Py_False || val == Py_True) {
905 type = QVariant::Bool;
982 type = QVariant::Bool;
983 #ifndef PY3K
906 } else if (PyObject_TypeCheck(val, &PyInt_Type)) {
984 } else if (PyObject_TypeCheck(val, &PyInt_Type)) {
907 type = QVariant::Int;
985 type = QVariant::Int;
908 } else if (val->ob_type==&PyLong_Type) {
986 #endif
987 } else if (PyLong_Check(val)) {
909 type = QVariant::LongLong;
988 type = QVariant::LongLong;
910 } else if (val->ob_type==&PyFloat_Type) {
989 } else if (PyFloat_Check(val)) {
911 type = QVariant::Double;
990 type = QVariant::Double;
912 } else if (PyObject_TypeCheck(val, &PythonQtInstanceWrapper_Type)) {
991 } else if (PyObject_TypeCheck(val, &PythonQtInstanceWrapper_Type)) {
913 PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)val;
992 PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)val;
@@ -928,9 +1007,9 QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
928 v = qVariantFromValue(myObject);
1007 v = qVariantFromValue(myObject);
929 }
1008 }
930 return v;
1009 return v;
931 } else if (val->ob_type==&PyDict_Type) {
1010 } else if (PyDict_Check(val)) {
932 type = QVariant::Map;
1011 type = QVariant::Map;
933 } else if (val->ob_type==&PyList_Type || val->ob_type==&PyTuple_Type || PySequence_Check(val)) {
1012 } else if (PyList_Check(val) || PyTuple_Check(val) || PySequence_Check(val)) {
934 type = QVariant::List;
1013 type = QVariant::List;
935 } else if (val == Py_None) {
1014 } else if (val == Py_None) {
936 // none is invalid
1015 // none is invalid
@@ -1100,7 +1179,11 QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
1100 PyObject* PythonQtConv::QStringToPyObject(const QString& str)
1179 PyObject* PythonQtConv::QStringToPyObject(const QString& str)
1101 {
1180 {
1102 if (str.isNull()) {
1181 if (str.isNull()) {
1182 #ifdef PY3K
1183 return PyUnicode_New(0, 0);
1184 #else
1103 return PyString_FromString("");
1185 return PyString_FromString("");
1186 #endif
1104 } else {
1187 } else {
1105 return PyUnicode_DecodeUTF16((const char*)str.utf16(), str.length()*2, NULL, NULL);
1188 return PyUnicode_DecodeUTF16((const char*)str.utf16(), str.length()*2, NULL, NULL);
1106 }
1189 }
@@ -51,6 +51,10
51 #include <QFile>
51 #include <QFile>
52 #include <QFileInfo>
52 #include <QFileInfo>
53
53
54 #if PY_MAJOR_VERSION >= 3
55 #define PY3K
56 #endif
57
54 #define IS_SOURCE 0x0
58 #define IS_SOURCE 0x0
55 #define IS_BYTECODE 0x1
59 #define IS_BYTECODE 0x1
56 #define IS_PACKAGE 0x2
60 #define IS_PACKAGE 0x2
@@ -159,7 +163,7 PythonQtImporter_dealloc(PythonQtImporter *self)
159 // free the stored path
163 // free the stored path
160 if (self->_path) delete self->_path;
164 if (self->_path) delete self->_path;
161 // free ourself
165 // free ourself
162 self->ob_type->tp_free((PyObject *)self);
166 Py_TYPE(self)->tp_free((PyObject *)self);
163 }
167 }
164
168
165
169
@@ -230,7 +234,11 PythonQtImporter_load_module(PyObject *obj, PyObject *args)
230 QString subname = info.moduleName;
234 QString subname = info.moduleName;
231 int err;
235 int err;
232
236
237 #ifdef PY3K
238 fullpath = PyUnicode_FromFormat("%s%c%s",
239 #else
233 fullpath = PyString_FromFormat("%s%c%s",
240 fullpath = PyString_FromFormat("%s%c%s",
241 #endif
234 self->_path->toLatin1().constData(),
242 self->_path->toLatin1().constData(),
235 SEP,
243 SEP,
236 subname.toLatin1().constData());
244 subname.toLatin1().constData());
@@ -400,8 +408,7 Create a new PythonQtImporter instance. 'path' must be a valid path on disk/or i
400 #define DEFERRED_ADDRESS(ADDR) 0
408 #define DEFERRED_ADDRESS(ADDR) 0
401
409
402 PyTypeObject PythonQtImporter_Type = {
410 PyTypeObject PythonQtImporter_Type = {
403 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
411 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
404 0,
405 "PythonQtImport.PythonQtImporter",
412 "PythonQtImport.PythonQtImporter",
406 sizeof(PythonQtImporter),
413 sizeof(PythonQtImporter),
407 0, /* tp_itemsize */
414 0, /* tp_itemsize */
@@ -766,6 +773,20 PyObject* PythonQtImport::getCodeFromPyc(const QString& file)
766 PyDoc_STRVAR(mlabimport_doc,
773 PyDoc_STRVAR(mlabimport_doc,
767 "Imports python files into PythonQt, completely replaces internal python import");
774 "Imports python files into PythonQt, completely replaces internal python import");
768
775
776 #ifdef PY3K
777 static struct PyModuleDef PythonQtImport_def = {
778 PyModuleDef_HEAD_INIT,
779 "PythonQtImport", /* m_name */
780 mlabimport_doc, /* m_doc */
781 -1, /* m_size */
782 NULL, /* m_methods */
783 NULL, /* m_reload */
784 NULL, /* m_traverse */
785 NULL, /* m_clear */
786 NULL /* m_free */
787 };
788 #endif
789
769 void PythonQtImport::init()
790 void PythonQtImport::init()
770 {
791 {
771 static bool first = true;
792 static bool first = true;
@@ -794,8 +815,12 void PythonQtImport::init()
794 mlab_searchorder[4] = tmp;
815 mlab_searchorder[4] = tmp;
795 }
816 }
796
817
818 #ifdef PY3K
819 mod = PyModule_Create(&PythonQtImport_def);
820 #else
797 mod = Py_InitModule4("PythonQtImport", NULL, mlabimport_doc,
821 mod = Py_InitModule4("PythonQtImport", NULL, mlabimport_doc,
798 NULL, PYTHON_API_VERSION);
822 NULL, PYTHON_API_VERSION);
823 #endif
799
824
800 PythonQtImportError = PyErr_NewException(const_cast<char*>("PythonQtImport.PythonQtImportError"),
825 PythonQtImportError = PyErr_NewException(const_cast<char*>("PythonQtImport.PythonQtImportError"),
801 PyExc_ImportError, NULL);
826 PyExc_ImportError, NULL);
@@ -48,10 +48,14
48 #include "PythonQtConversion.h"
48 #include "PythonQtConversion.h"
49 #include "PythonQtClassWrapper.h"
49 #include "PythonQtClassWrapper.h"
50
50
51 #if PY_MAJOR_VERSION >= 3
52 #define PY3K
53 #endif
54
51 PythonQtClassInfo* PythonQtInstanceWrapperStruct::classInfo()
55 PythonQtClassInfo* PythonQtInstanceWrapperStruct::classInfo()
52 {
56 {
53 // take the class info from our type object
57 // take the class info from our type object
54 return ((PythonQtClassWrapper*)ob_type)->_classInfo;
58 return ((PythonQtClassWrapper*)Py_TYPE(this))->_classInfo;
55 }
59 }
56
60
57 static void PythonQtInstanceWrapper_deleteObject(PythonQtInstanceWrapper* self, bool force = false) {
61 static void PythonQtInstanceWrapper_deleteObject(PythonQtInstanceWrapper* self, bool force = false) {
@@ -112,7 +116,7 static void PythonQtInstanceWrapper_dealloc(PythonQtInstanceWrapper* self)
112 {
116 {
113 PythonQtInstanceWrapper_deleteObject(self);
117 PythonQtInstanceWrapper_deleteObject(self);
114 self->_obj.~QPointer<QObject>();
118 self->_obj.~QPointer<QObject>();
115 self->ob_type->tp_free((PyObject*)self);
119 Py_TYPE(self)->tp_free((PyObject*)self);
116 }
120 }
117
121
118 static PyObject* PythonQtInstanceWrapper_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/)
122 static PyObject* PythonQtInstanceWrapper_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/)
@@ -286,7 +290,11 static PyObject *PythonQtInstanceWrapper_richcompare(PythonQtInstanceWrapper* wr
286
290
287 static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* obj)
291 static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* obj)
288 {
292 {
289 return PyString_FromString(obj->ob_type->tp_name);
293 #ifdef PY3K
294 return PyUnicode_FromString(Py_TYPE(obj)->tp_name);
295 #else
296 return PyString_FromString(Py_TYPE(obj)->tp_name);
297 #endif
290 }
298 }
291
299
292 PyObject *PythonQtInstanceWrapper_inherits(PythonQtInstanceWrapper* obj, PyObject *args)
300 PyObject *PythonQtInstanceWrapper_inherits(PythonQtInstanceWrapper* obj, PyObject *args)
@@ -333,7 +341,11 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
333 const char *attributeName;
341 const char *attributeName;
334 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
342 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
335
343
344 #ifdef PY3K
345 if ((attributeName = PyUnicode_AsUTF8(name)) == NULL) {
346 #else
336 if ((attributeName = PyString_AsString(name)) == NULL) {
347 if ((attributeName = PyString_AsString(name)) == NULL) {
348 #endif
337 return NULL;
349 return NULL;
338 }
350 }
339
351
@@ -455,7 +467,11 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
455 }
467 }
456
468
457 // look for the internal methods (className(), help())
469 // look for the internal methods (className(), help())
470 #ifdef PY3K
471 PyObject* internalMethod = PyObject_GenericGetAttr(obj, name);
472 #else
458 PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
473 PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
474 #endif
459 if (internalMethod) {
475 if (internalMethod) {
460 return internalMethod;
476 return internalMethod;
461 }
477 }
@@ -483,7 +499,11 static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObjec
483 const char *attributeName;
499 const char *attributeName;
484 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
500 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
485
501
502 #ifdef PY3K
503 if ((attributeName = PyUnicode_AsUTF8(name)) == NULL)
504 #else
486 if ((attributeName = PyString_AsString(name)) == NULL)
505 if ((attributeName = PyString_AsString(name)) == NULL)
506 #endif
487 return -1;
507 return -1;
488
508
489 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
509 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
@@ -619,9 +639,17 static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
619 if (wrapper->classInfo()->metaTypeId()==QVariant::ByteArray) {
639 if (wrapper->classInfo()->metaTypeId()==QVariant::ByteArray) {
620 QByteArray* b = (QByteArray*) wrapper->_wrappedPtr;
640 QByteArray* b = (QByteArray*) wrapper->_wrappedPtr;
621 if (b->data()) {
641 if (b->data()) {
642 #ifdef PY3K
643 return PyUnicode_FromStringAndSize(b->data(), b->size());
644 #else
622 return PyString_FromStringAndSize(b->data(), b->size());
645 return PyString_FromStringAndSize(b->data(), b->size());
646 #endif
623 } else {
647 } else {
648 #ifdef PY3K
649 return PyUnicode_New(0, 0);
650 #else
624 return PyString_FromString("");
651 return PyString_FromString("");
652 #endif
625 }
653 }
626 }
654 }
627
655
@@ -629,16 +657,32 static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
629 QObject *qobj = wrapper->_obj;
657 QObject *qobj = wrapper->_obj;
630 QString str = getStringFromObject(wrapper);
658 QString str = getStringFromObject(wrapper);
631 if (!str.isEmpty()) {
659 if (!str.isEmpty()) {
660 #ifdef PY3K
661 return PyUnicode_FromFormat("%s", str.toLatin1().constData());
662 #else
632 return PyString_FromFormat("%s", str.toLatin1().constData());
663 return PyString_FromFormat("%s", str.toLatin1().constData());
664 #endif
633 }
665 }
634 if (wrapper->_wrappedPtr) {
666 if (wrapper->_wrappedPtr) {
635 if (wrapper->_obj) {
667 if (wrapper->_obj) {
636 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
668 #ifdef PY3K
669 return PyUnicode_FromFormat("%s (C++ Object %p wrapped by %s %p)", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
670 #else
671 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p)", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
672 #endif
637 } else {
673 } else {
674 #ifdef PY3K
675 return PyUnicode_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
676 #else
638 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
677 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
678 #endif
639 }
679 }
640 } else {
680 } else {
681 #ifdef PY3K
682 return PyUnicode_FromFormat("%s (QObject %p)", typeName, qobj);
683 #else
641 return PyString_FromFormat("%s (QObject %p)", typeName, qobj);
684 return PyString_FromFormat("%s (QObject %p)", typeName, qobj);
685 #endif
642 }
686 }
643 }
687 }
644
688
@@ -651,19 +695,39 static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj)
651 QString str = getStringFromObject(wrapper);
695 QString str = getStringFromObject(wrapper);
652 if (!str.isEmpty()) {
696 if (!str.isEmpty()) {
653 if (str.startsWith(typeName)) {
697 if (str.startsWith(typeName)) {
698 #ifdef PY3K
699 return PyUnicode_FromFormat("%s", str.toLatin1().constData());
700 #else
654 return PyString_FromFormat("%s", str.toLatin1().constData());
701 return PyString_FromFormat("%s", str.toLatin1().constData());
702 #endif
655 } else {
703 } else {
704 #ifdef PY3K
705 return PyUnicode_FromFormat("%s (%s, at: %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr ? wrapper->_wrappedPtr : qobj);
706 #else
656 return PyString_FromFormat("%s (%s, at: %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr ? wrapper->_wrappedPtr : qobj);
707 return PyString_FromFormat("%s (%s, at: %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr ? wrapper->_wrappedPtr : qobj);
708 #endif
657 }
709 }
658 }
710 }
659 if (wrapper->_wrappedPtr) {
711 if (wrapper->_wrappedPtr) {
660 if (wrapper->_obj) {
712 if (wrapper->_obj) {
713 #ifdef PY3K
714 return PyUnicode_FromFormat("%s (C++ object at: %p wrapped by %s at: %p)", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
715 #else
661 return PyString_FromFormat("%s (C++ object at: %p wrapped by %s at: %p)", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
716 return PyString_FromFormat("%s (C++ object at: %p wrapped by %s at: %p)", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
717 #endif
662 } else {
718 } else {
719 #ifdef PY3K
720 return PyUnicode_FromFormat("%s (C++ object at: %p)", typeName, wrapper->_wrappedPtr);
721 #else
663 return PyString_FromFormat("%s (C++ object at: %p)", typeName, wrapper->_wrappedPtr);
722 return PyString_FromFormat("%s (C++ object at: %p)", typeName, wrapper->_wrappedPtr);
723 #endif
664 }
724 }
665 } else {
725 } else {
726 #ifdef PY3K
727 return PyUnicode_FromFormat("%s (%s at: %p)", typeName, wrapper->classInfo()->className(), qobj);
728 #else
666 return PyString_FromFormat("%s (%s at: %p)", typeName, wrapper->classInfo()->className(), qobj);
729 return PyString_FromFormat("%s (%s at: %p)", typeName, wrapper->classInfo()->className(), qobj);
730 #endif
667 }
731 }
668 }
732 }
669
733
@@ -691,30 +755,38 static PyNumberMethods PythonQtInstanceWrapper_as_number = {
691 0, /* nb_add */
755 0, /* nb_add */
692 0, /* nb_subtract */
756 0, /* nb_subtract */
693 0, /* nb_multiply */
757 0, /* nb_multiply */
758 #ifndef PY3K
694 0, /* nb_divide */
759 0, /* nb_divide */
760 #endif
695 0, /* nb_remainder */
761 0, /* nb_remainder */
696 0, /* nb_divmod */
762 0, /* nb_divmod */
697 0, /* nb_power */
763 0, /* nb_power */
698 0, /* nb_negative */
764 0, /* nb_negative */
699 0, /* nb_positive */
765 0, /* nb_positive */
700 0, /* nb_absolute */
766 0, /* nb_absolute */
701 PythonQtInstanceWrapper_builtin_nonzero, /* nb_nonzero */
767 PythonQtInstanceWrapper_builtin_nonzero, /* nb_nonzero / nb_bool in Py3K */
702 0, /* nb_invert */
768 0, /* nb_invert */
703 0, /* nb_lshift */
769 0, /* nb_lshift */
704 0, /* nb_rshift */
770 0, /* nb_rshift */
705 0, /* nb_and */
771 0, /* nb_and */
706 0, /* nb_xor */
772 0, /* nb_xor */
707 0, /* nb_or */
773 0, /* nb_or */
774 #ifndef PY3K
708 0, /* nb_coerce */
775 0, /* nb_coerce */
776 #endif
709 0, /* nb_int */
777 0, /* nb_int */
710 0, /* nb_long */
778 0, /* nb_long / nb_reserved in Py3K */
711 0, /* nb_float */
779 0, /* nb_float */
780 #ifndef PY3K
712 0, /* nb_oct */
781 0, /* nb_oct */
713 0, /* nb_hex */
782 0, /* nb_hex */
783 #endif
714 0, /* nb_inplace_add */
784 0, /* nb_inplace_add */
715 0, /* nb_inplace_subtract */
785 0, /* nb_inplace_subtract */
716 0, /* nb_inplace_multiply */
786 0, /* nb_inplace_multiply */
787 #ifndef PY3K
717 0, /* nb_inplace_divide */
788 0, /* nb_inplace_divide */
789 #endif
718 0, /* nb_inplace_remainder */
790 0, /* nb_inplace_remainder */
719 0, /* nb_inplace_power */
791 0, /* nb_inplace_power */
720 0, /* nb_inplace_lshift */
792 0, /* nb_inplace_lshift */
@@ -726,11 +798,13 static PyNumberMethods PythonQtInstanceWrapper_as_number = {
726 0, /* nb_true_divide */
798 0, /* nb_true_divide */
727 0, /* nb_inplace_floor_divide */
799 0, /* nb_inplace_floor_divide */
728 0, /* nb_inplace_true_divide */
800 0, /* nb_inplace_true_divide */
801 #ifdef PY3K
802 0, /* nb_index in Py3K */
803 #endif
729 };
804 };
730
805
731 PyTypeObject PythonQtInstanceWrapper_Type = {
806 PyTypeObject PythonQtInstanceWrapper_Type = {
732 PyObject_HEAD_INIT(&PythonQtClassWrapper_Type)
807 PyVarObject_HEAD_INIT(&PythonQtClassWrapper_Type, 0)
733 0, /*ob_size*/
734 "PythonQt.PythonQtInstanceWrapper", /*tp_name*/
808 "PythonQt.PythonQtInstanceWrapper", /*tp_name*/
735 sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
809 sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
736 0, /*tp_itemsize*/
810 0, /*tp_itemsize*/
@@ -749,7 +823,7 PyTypeObject PythonQtInstanceWrapper_Type = {
749 PythonQtInstanceWrapper_getattro, /*tp_getattro*/
823 PythonQtInstanceWrapper_getattro, /*tp_getattro*/
750 PythonQtInstanceWrapper_setattro, /*tp_setattro*/
824 PythonQtInstanceWrapper_setattro, /*tp_setattro*/
751 0, /*tp_as_buffer*/
825 0, /*tp_as_buffer*/
752 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
826 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE /*| Py_TPFLAGS_CHECKTYPES FIXME Py_TPFLAGS_CHECKTYPES removal */, /*tp_flags*/
753 "PythonQtInstanceWrapper object", /* tp_doc */
827 "PythonQtInstanceWrapper object", /* tp_doc */
754 0, /* tp_traverse */
828 0, /* tp_traverse */
755 0, /* tp_clear */
829 0, /* tp_clear */
@@ -38,6 +38,12
38 #undef _POSIX_THREADS
38 #undef _POSIX_THREADS
39 #undef _XOPEN_SOURCE
39 #undef _XOPEN_SOURCE
40
40
41 // Undefine Qt keywords that conflict with Python headers
42 #ifdef slots
43 #undef slots
44 #define PYTHONQT_RESTORE_KEYWORDS
45 #endif
46
41 // If PYTHONQT_USE_RELEASE_PYTHON_FALLBACK is enabled, try to link
47 // If PYTHONQT_USE_RELEASE_PYTHON_FALLBACK is enabled, try to link
42 // release Python DLL if it is available by undefining _DEBUG while
48 // release Python DLL if it is available by undefining _DEBUG while
43 // including Python.h
49 // including Python.h
@@ -53,4 +59,14
53 #include <Python.h>
59 #include <Python.h>
54 #endif
60 #endif
55
61
62 // get Qt keywords back
63 #ifdef PYTHONQT_RESTORE_KEYWORDS
64 #define slots Q_SLOTS
65 #undef PYTHONQT_RESTORE_KEYWORDS
66 #endif
67
68 #if PY_MAJOR_VERSION >= 3
69 #define PY3K
70 #endif
71
56 #endif
72 #endif
@@ -110,10 +110,18 static PyObject *
110 meth_get__name__(PythonQtSignalFunctionObject *m, void * /*closure*/)
110 meth_get__name__(PythonQtSignalFunctionObject *m, void * /*closure*/)
111 {
111 {
112 #if( QT_VERSION >= QT_VERSION_CHECK(5,0,0) )
112 #if( QT_VERSION >= QT_VERSION_CHECK(5,0,0) )
113 #ifdef PY3K
114 return PyUnicode_FromString(m->m_ml->metaMethod()->methodSignature());
115 #else
113 return PyString_FromString(m->m_ml->metaMethod()->methodSignature());
116 return PyString_FromString(m->m_ml->metaMethod()->methodSignature());
117 #endif
118 #else
119 #ifdef PY3K
120 return PyUnicode_FromString(m->m_ml->metaMethod()->signature());
114 #else
121 #else
115 return PyString_FromString(m->m_ml->metaMethod()->signature());
122 return PyString_FromString(m->m_ml->metaMethod()->signature());
116 #endif
123 #endif
124 #endif
117 }
125 }
118
126
119 static int
127 static int
@@ -137,11 +145,13 static PyObject *
137 meth_get__self__(PythonQtSignalFunctionObject *m, void * /*closure*/)
145 meth_get__self__(PythonQtSignalFunctionObject *m, void * /*closure*/)
138 {
146 {
139 PyObject *self;
147 PyObject *self;
148 #ifndef PY3K
140 if (PyEval_GetRestricted()) {
149 if (PyEval_GetRestricted()) {
141 PyErr_SetString(PyExc_RuntimeError,
150 PyErr_SetString(PyExc_RuntimeError,
142 "method.__self__ not accessible in restricted mode");
151 "method.__self__ not accessible in restricted mode");
143 return NULL;
152 return NULL;
144 }
153 }
154 #endif
145 self = m->m_self;
155 self = m->m_self;
146 if (self == NULL)
156 if (self == NULL)
147 self = Py_None;
157 self = Py_None;
@@ -266,11 +276,19 meth_repr(PythonQtSignalFunctionObject *f)
266 {
276 {
267 if (f->m_self->ob_type == &PythonQtClassWrapper_Type) {
277 if (f->m_self->ob_type == &PythonQtClassWrapper_Type) {
268 PythonQtClassWrapper* self = (PythonQtClassWrapper*) f->m_self;
278 PythonQtClassWrapper* self = (PythonQtClassWrapper*) f->m_self;
279 #ifdef PY3K
280 return PyUnicode_FromFormat("<unbound qt signal %s of %s type>",
281 #else
269 return PyString_FromFormat("<unbound qt signal %s of %s type>",
282 return PyString_FromFormat("<unbound qt signal %s of %s type>",
283 #endif
270 f->m_ml->slotName().data(),
284 f->m_ml->slotName().data(),
271 self->classInfo()->className());
285 self->classInfo()->className());
272 } else {
286 } else {
287 #ifdef PY3K
288 return PyUnicode_FromFormat("<qt signal %s of %s instance at %p>",
289 #else
273 return PyString_FromFormat("<qt signal %s of %s instance at %p>",
290 return PyString_FromFormat("<qt signal %s of %s instance at %p>",
291 #endif
274 f->m_ml->slotName().data(),
292 f->m_ml->slotName().data(),
275 f->m_self->ob_type->tp_name,
293 f->m_self->ob_type->tp_name,
276 f->m_self);
294 f->m_self);
@@ -314,10 +332,32 meth_hash(PythonQtSignalFunctionObject *a)
314 return x;
332 return x;
315 }
333 }
316
334
335 // for python 3.x
336 static PyObject*
337 meth_richcompare(PythonQtSignalFunctionObject *a, PythonQtSignalFunctionObject *b, int op)
338 {
339 int x = meth_compare(a, b);
340 bool r;
341 if (op == Py_LT)
342 r = x < 0;
343 else if (op == Py_LE)
344 r = x < 1;
345 else if (op == Py_EQ)
346 r = x == 0;
347 else if (op == Py_NE)
348 r = x != 0;
349 else if (op == Py_GE)
350 r = x > -1;
351 else if (op == Py_GT)
352 r = x > 0;
353 if (r)
354 Py_RETURN_TRUE;
355 else
356 Py_RETURN_FALSE;
357 }
317
358
318 PyTypeObject PythonQtSignalFunction_Type = {
359 PyTypeObject PythonQtSignalFunction_Type = {
319 PyObject_HEAD_INIT(&PyType_Type)
360 PyVarObject_HEAD_INIT(&PyType_Type, 0)
320 0,
321 "builtin_qt_signal",
361 "builtin_qt_signal",
322 sizeof(PythonQtSignalFunctionObject),
362 sizeof(PythonQtSignalFunctionObject),
323 0,
363 0,
@@ -325,7 +365,11 PyTypeObject PythonQtSignalFunction_Type = {
325 0, /* tp_print */
365 0, /* tp_print */
326 0, /* tp_getattr */
366 0, /* tp_getattr */
327 0, /* tp_setattr */
367 0, /* tp_setattr */
368 #ifdef PY3K
369 0,
370 #else
328 (cmpfunc)meth_compare, /* tp_compare */
371 (cmpfunc)meth_compare, /* tp_compare */
372 #endif
329 (reprfunc)meth_repr, /* tp_repr */
373 (reprfunc)meth_repr, /* tp_repr */
330 0, /* tp_as_number */
374 0, /* tp_as_number */
331 0, /* tp_as_sequence */
375 0, /* tp_as_sequence */
@@ -341,7 +385,7 PyTypeObject PythonQtSignalFunction_Type = {
341 0, /* tp_doc */
385 0, /* tp_doc */
342 (traverseproc)meth_traverse, /* tp_traverse */
386 (traverseproc)meth_traverse, /* tp_traverse */
343 0, /* tp_clear */
387 0, /* tp_clear */
344 0, /* tp_richcompare */
388 (richcmpfunc)meth_richcompare, /* tp_richcompare */
345 0, /* tp_weaklistoffset */
389 0, /* tp_weaklistoffset */
346 0, /* tp_iter */
390 0, /* tp_iter */
347 0, /* tp_iternext */
391 0, /* tp_iternext */
@@ -138,7 +138,11 PyObject* PythonQtSignalTarget::call(PyObject* callable, const PythonQtMethodInf
138
138
139 bool PythonQtSignalTarget::isSame( int signalId, PyObject* callable ) const
139 bool PythonQtSignalTarget::isSame( int signalId, PyObject* callable ) const
140 {
140 {
141 #ifdef PY3K
142 return PyObject_RichCompareBool(callable, _callable, Py_EQ) && signalId == _signalId;
143 #else
141 return PyObject_Compare(callable, _callable) == 0 && signalId==_signalId;
144 return PyObject_Compare(callable, _callable) == 0 && signalId==_signalId;
145 #endif
142 }
146 }
143
147
144 //------------------------------------------------------------------------------
148 //------------------------------------------------------------------------------
@@ -52,6 +52,10
52
52
53 #include <QByteArray>
53 #include <QByteArray>
54
54
55 #if PY_MAJOR_VERSION >= 3
56 #define PY3K
57 #endif
58
55 #define PYTHONQT_MAX_ARGS 32
59 #define PYTHONQT_MAX_ARGS 32
56
60
57
61
@@ -192,7 +196,11 bool PythonQtCallSlot(PythonQtClassInfo* classInfo, QObject* objectToCall, PyObj
192 hadException = true;
196 hadException = true;
193 QByteArray what("std::exception: ");
197 QByteArray what("std::exception: ");
194 what += e.what();
198 what += e.what();
199 #ifdef PY3K
200 PyErr_SetString(PyExc_RuntimeError, what.constData());
201 #else
195 PyErr_SetString(PyExc_StandardError, what.constData());
202 PyErr_SetString(PyExc_StandardError, what.constData());
203 #endif
196 }
204 }
197 }
205 }
198
206
@@ -419,10 +427,18 static PyObject *
419 meth_get__name__(PythonQtSlotFunctionObject *m, void * /*closure*/)
427 meth_get__name__(PythonQtSlotFunctionObject *m, void * /*closure*/)
420 {
428 {
421 #if( QT_VERSION >= QT_VERSION_CHECK(5,0,0) )
429 #if( QT_VERSION >= QT_VERSION_CHECK(5,0,0) )
430 #ifdef PY3K
431 return PyUnicode_FromString(m->m_ml->metaMethod()->methodSignature());
432 #else
422 return PyString_FromString(m->m_ml->metaMethod()->methodSignature());
433 return PyString_FromString(m->m_ml->metaMethod()->methodSignature());
434 #endif
435 #else
436 #ifdef PY3K
437 return PyUnicode_FromString(m->m_ml->metaMethod()->signature());
423 #else
438 #else
424 return PyString_FromString(m->m_ml->metaMethod()->signature());
439 return PyString_FromString(m->m_ml->metaMethod()->signature());
425 #endif
440 #endif
441 #endif
426 }
442 }
427
443
428 static int
444 static int
@@ -446,11 +462,13 static PyObject *
446 meth_get__self__(PythonQtSlotFunctionObject *m, void * /*closure*/)
462 meth_get__self__(PythonQtSlotFunctionObject *m, void * /*closure*/)
447 {
463 {
448 PyObject *self;
464 PyObject *self;
465 #ifndef PY3K
449 if (PyEval_GetRestricted()) {
466 if (PyEval_GetRestricted()) {
450 PyErr_SetString(PyExc_RuntimeError,
467 PyErr_SetString(PyExc_RuntimeError,
451 "method.__self__ not accessible in restricted mode");
468 "method.__self__ not accessible in restricted mode");
452 return NULL;
469 return NULL;
453 }
470 }
471 #endif
454 self = m->m_self;
472 self = m->m_self;
455 if (self == NULL)
473 if (self == NULL)
456 self = Py_None;
474 self = Py_None;
@@ -505,7 +523,11 PyObject *PythonQtMemberFunction_parameterTypes(PythonQtSlotInfo* theInfo)
505 QList<QByteArray> types = info->metaMethod()->parameterTypes();
523 QList<QByteArray> types = info->metaMethod()->parameterTypes();
506 PyObject* tuple = PyTuple_New(types.count());
524 PyObject* tuple = PyTuple_New(types.count());
507 for (int i = 0; i<types.count();i++) {
525 for (int i = 0; i<types.count();i++) {
526 #ifdef PY3K
527 PyTuple_SET_ITEM(tuple, i, PyUnicode_FromString(types.at(i).constData()));
528 #else
508 PyTuple_SET_ITEM(tuple, i, PyString_FromString(types.at(i).constData()));
529 PyTuple_SET_ITEM(tuple, i, PyString_FromString(types.at(i).constData()));
530 #endif
509 }
531 }
510 info = info->nextInfo();
532 info = info->nextInfo();
511 PyTuple_SET_ITEM(result, j, tuple);
533 PyTuple_SET_ITEM(result, j, tuple);
@@ -527,7 +549,11 PyObject *PythonQtMemberFunction_parameterNames(PythonQtSlotInfo* theInfo)
527 QList<QByteArray> names = info->metaMethod()->parameterNames();
549 QList<QByteArray> names = info->metaMethod()->parameterNames();
528 PyObject* tuple = PyTuple_New(names.count());
550 PyObject* tuple = PyTuple_New(names.count());
529 for (int i = 0; i<names.count();i++) {
551 for (int i = 0; i<names.count();i++) {
552 #ifdef PY3K
553 PyTuple_SET_ITEM(tuple, i, PyUnicode_FromString(names.at(i).constData()));
554 #else
530 PyTuple_SET_ITEM(tuple, i, PyString_FromString(names.at(i).constData()));
555 PyTuple_SET_ITEM(tuple, i, PyString_FromString(names.at(i).constData()));
556 #endif
531 }
557 }
532 info = info->nextInfo();
558 info = info->nextInfo();
533 PyTuple_SET_ITEM(result, j, tuple);
559 PyTuple_SET_ITEM(result, j, tuple);
@@ -547,7 +573,11 PyObject *PythonQtMemberFunction_typeName(PythonQtSlotInfo* theInfo)
547 PyObject* result = PyTuple_New(count);
573 PyObject* result = PyTuple_New(count);
548 for (int j = 0;j<count;j++) {
574 for (int j = 0;j<count;j++) {
549 QByteArray name = info->metaMethod()->typeName();
575 QByteArray name = info->metaMethod()->typeName();
576 #ifdef PY3K
577 PyTuple_SET_ITEM(result, j, PyUnicode_FromString(name.constData()));
578 #else
550 PyTuple_SET_ITEM(result, j, PyString_FromString(name.constData()));
579 PyTuple_SET_ITEM(result, j, PyString_FromString(name.constData()));
580 #endif
551 info = info->nextInfo();
581 info = info->nextInfo();
552 }
582 }
553 return result;
583 return result;
@@ -571,11 +601,19 meth_repr(PythonQtSlotFunctionObject *f)
571 {
601 {
572 if (f->m_self->ob_type == &PythonQtClassWrapper_Type) {
602 if (f->m_self->ob_type == &PythonQtClassWrapper_Type) {
573 PythonQtClassWrapper* self = (PythonQtClassWrapper*) f->m_self;
603 PythonQtClassWrapper* self = (PythonQtClassWrapper*) f->m_self;
604 #ifdef PY3K
605 return PyUnicode_FromFormat("<unbound qt slot %s of %s type>",
606 #else
574 return PyString_FromFormat("<unbound qt slot %s of %s type>",
607 return PyString_FromFormat("<unbound qt slot %s of %s type>",
608 #endif
575 f->m_ml->slotName().data(),
609 f->m_ml->slotName().data(),
576 self->classInfo()->className());
610 self->classInfo()->className());
577 } else {
611 } else {
612 #ifdef PY3K
613 return PyUnicode_FromFormat("<qt slot %s of %s instance at %p",
614 #else
578 return PyString_FromFormat("<qt slot %s of %s instance at %p>",
615 return PyString_FromFormat("<qt slot %s of %s instance at %p>",
616 #endif
579 f->m_ml->slotName().data(),
617 f->m_ml->slotName().data(),
580 f->m_self->ob_type->tp_name,
618 f->m_self->ob_type->tp_name,
581 f->m_self);
619 f->m_self);
@@ -619,10 +657,33 meth_hash(PythonQtSlotFunctionObject *a)
619 return x;
657 return x;
620 }
658 }
621
659
660 // for python 3.x
661 static PyObject*
662 meth_richcompare(PythonQtSlotFunctionObject *a, PythonQtSlotFunctionObject *b, int op)
663 {
664 int x = meth_compare(a, b);
665 bool r;
666 if (op == Py_LT)
667 r = x < 0;
668 else if (op == Py_LE)
669 r = x < 1;
670 else if (op == Py_EQ)
671 r = x == 0;
672 else if (op == Py_NE)
673 r = x != 0;
674 else if (op == Py_GE)
675 r = x > -1;
676 else if (op == Py_GT)
677 r = x > 0;
678 if (r)
679 Py_RETURN_TRUE;
680 else
681 Py_RETURN_FALSE;
682 }
683
622
684
623 PyTypeObject PythonQtSlotFunction_Type = {
685 PyTypeObject PythonQtSlotFunction_Type = {
624 PyObject_HEAD_INIT(&PyType_Type)
686 PyVarObject_HEAD_INIT(&PyType_Type, 0)
625 0,
626 "builtin_qt_slot",
687 "builtin_qt_slot",
627 sizeof(PythonQtSlotFunctionObject),
688 sizeof(PythonQtSlotFunctionObject),
628 0,
689 0,
@@ -630,7 +691,11 PyTypeObject PythonQtSlotFunction_Type = {
630 0, /* tp_print */
691 0, /* tp_print */
631 0, /* tp_getattr */
692 0, /* tp_getattr */
632 0, /* tp_setattr */
693 0, /* tp_setattr */
694 #ifdef PY3K
695 0,
696 #else
633 (cmpfunc)meth_compare, /* tp_compare */
697 (cmpfunc)meth_compare, /* tp_compare */
698 #endif
634 (reprfunc)meth_repr, /* tp_repr */
699 (reprfunc)meth_repr, /* tp_repr */
635 0, /* tp_as_number */
700 0, /* tp_as_number */
636 0, /* tp_as_sequence */
701 0, /* tp_as_sequence */
@@ -645,7 +710,7 PyTypeObject PythonQtSlotFunction_Type = {
645 0, /* tp_doc */
710 0, /* tp_doc */
646 (traverseproc)meth_traverse, /* tp_traverse */
711 (traverseproc)meth_traverse, /* tp_traverse */
647 0, /* tp_clear */
712 0, /* tp_clear */
648 0, /* tp_richcompare */
713 (richcmpfunc)meth_richcompare, /* tp_richcompare */
649 0, /* tp_weaklistoffset */
714 0, /* tp_weaklistoffset */
650 0, /* tp_iter */
715 0, /* tp_iter */
651 0, /* tp_iternext */
716 0, /* tp_iternext */
@@ -181,8 +181,13 QObject* PythonQtStdDecorators::findChild(QObject* parent, PyObject* type, const
181 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
181 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
182 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
182 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
183 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
183 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
184 #ifdef PY3K
185 } else if (PyUnicode_Check(type)) {
186 typeName = PyUnicode_AsUTF8(type);
187 #else
184 } else if (PyString_Check(type)) {
188 } else if (PyString_Check(type)) {
185 typeName = PyString_AsString(type);
189 typeName = PyString_AsString(type);
190 #endif
186 }
191 }
187
192
188 if (!typeName && !meta)
193 if (!typeName && !meta)
@@ -200,8 +205,13 QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* t
200 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
205 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
201 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
206 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
202 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
207 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
208 #ifdef PY3K
209 } else if (PyUnicode_Check(type)) {
210 typeName = PyUnicode_AsUTF8(type);
211 #else
203 } else if (PyString_Check(type)) {
212 } else if (PyString_Check(type)) {
204 typeName = PyString_AsString(type);
213 typeName = PyString_AsString(type);
214 #endif
205 }
215 }
206
216
207 QList<QObject*> list;
217 QList<QObject*> list;
@@ -223,8 +233,13 QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* t
223 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
233 meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
224 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
234 } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
225 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
235 meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
236 #ifdef PY3K
237 } else if (PyUnicode_Check(type)) {
238 typeName = PyUnicode_AsUTF8(type);
239 #else
226 } else if (PyString_Check(type)) {
240 } else if (PyString_Check(type)) {
227 typeName = PyString_AsString(type);
241 typeName = PyString_AsString(type);
242 #endif
228 }
243 }
229
244
230 QList<QObject*> list;
245 QList<QObject*> list;
@@ -324,4 +339,4 int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, c
324 const QMetaObject* PythonQtStdDecorators::metaObject( QObject* obj )
339 const QMetaObject* PythonQtStdDecorators::metaObject( QObject* obj )
325 {
340 {
326 return obj->metaObject();
341 return obj->metaObject();
327 } No newline at end of file
342 }
@@ -76,8 +76,7 static PyMemberDef PythonQtStdInRedirect_members[] = {
76 };
76 };
77
77
78 PyTypeObject PythonQtStdInRedirectType = {
78 PyTypeObject PythonQtStdInRedirectType = {
79 PyObject_HEAD_INIT(NULL)
79 PyVarObject_HEAD_INIT(NULL, 0)
80 0, /*ob_size*/
81 "PythonQtStdInRedirect", /*tp_name*/
80 "PythonQtStdInRedirect", /*tp_name*/
82 sizeof(PythonQtStdInRedirect), /*tp_basicsize*/
81 sizeof(PythonQtStdInRedirect), /*tp_basicsize*/
83 0, /*tp_itemsize*/
82 0, /*tp_itemsize*/
@@ -60,6 +60,11 static PyObject *PythonQtStdOutRedirect_write(PyObject *self, PyObject *args)
60 if (PyTuple_GET_SIZE(args)>=1) {
60 if (PyTuple_GET_SIZE(args)>=1) {
61 PyObject* obj = PyTuple_GET_ITEM(args,0);
61 PyObject* obj = PyTuple_GET_ITEM(args,0);
62 if (PyUnicode_Check(obj)) {
62 if (PyUnicode_Check(obj)) {
63 #ifdef PY3K
64 Py_UCS4 *x = PyUnicode_AsUCS4Copy(obj);
65 output = QString::fromUcs4(x, PyUnicode_GetLength(obj));
66 PyMem_Free(x);
67 #else
63 PyObject *tmp = PyUnicode_AsUTF8String(obj);
68 PyObject *tmp = PyUnicode_AsUTF8String(obj);
64 if(tmp) {
69 if(tmp) {
65 output = QString::fromUtf8(PyString_AS_STRING(tmp));
70 output = QString::fromUtf8(PyString_AS_STRING(tmp));
@@ -67,6 +72,7 static PyObject *PythonQtStdOutRedirect_write(PyObject *self, PyObject *args)
67 } else {
72 } else {
68 return NULL;
73 return NULL;
69 }
74 }
75 #endif
70 } else {
76 } else {
71 char *string;
77 char *string;
72 if (!PyArg_ParseTuple(args, "s", &string)) {
78 if (!PyArg_ParseTuple(args, "s", &string)) {
@@ -117,8 +123,7 static PyMemberDef PythonQtStdOutRedirect_members[] = {
117 };
123 };
118
124
119 PyTypeObject PythonQtStdOutRedirectType = {
125 PyTypeObject PythonQtStdOutRedirectType = {
120 PyObject_HEAD_INIT(NULL)
126 PyVarObject_HEAD_INIT(NULL, 0)
121 0, /*ob_size*/
122 "PythonQtStdOutRedirect", /*tp_name*/
127 "PythonQtStdOutRedirect", /*tp_name*/
123 sizeof(PythonQtStdOutRedirect), /*tp_basicsize*/
128 sizeof(PythonQtStdOutRedirect), /*tp_basicsize*/
124 0, /*tp_itemsize*/
129 0, /*tp_itemsize*/
General Comments 0
You need to be logged in to leave comments. Login now