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