Skip to content

Commit cc2e43d

Browse files
author
florianlink
committed
further PY3K port
git-svn-id: http://svn.code.sf.net/p/pythonqt/code/trunk@275 ea8d5007-eb21-0410-b261-ccb3ea6e24a9
1 parent 8b063bd commit cc2e43d

9 files changed

+84
-32
lines changed

generator/shellimplgenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_cla
124124
Option typeOptions = Option(OriginalName | UnderscoreSpaces | SkipName);
125125
AbstractMetaArgumentList args = fun->arguments();
126126

127-
s << "if (_wrapper && (_wrapper->ob_refcnt > 0)) {" << endl;
127+
s << "if (_wrapper && (((PyObject*)_wrapper)->ob_refcnt > 0)) {" << endl;
128128
s << " static PyObject* name = PyString_FromString(\"" << fun->name() << "\");" << endl;
129129
s << " PyObject* obj = PyBaseObject_Type.tp_getattro((PyObject*)_wrapper, name);" << endl;
130130
s << " if (obj) {" << endl;

src/PythonQt.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ PythonQt::PythonQt(int flags, const QByteArray& pythonQtModuleName)
166166
_p->_PythonQtObjectPtr_metaId = qRegisterMetaType<PythonQtObjectPtr>("PythonQtObjectPtr");
167167

168168
if ((flags & PythonAlreadyInitialized) == 0) {
169-
Py_SetProgramName(const_cast<char*>("PythonQt"));
170169
if (flags & IgnoreSiteModule) {
171170
// this prevents the automatic importing of Python site files
172171
Py_NoSiteFlag = 1;
@@ -706,7 +705,11 @@ QVariant PythonQt::evalCode(PyObject* object, PyObject* pycode) {
706705
}
707706
PyObject* r = NULL;
708707
if (dict) {
709-
r = PyEval_EvalCode((PyCodeObject*)pycode, globals , dict);
708+
#ifdef PY3K
709+
r = PyEval_EvalCode(pycode, globals, dict);
710+
#else
711+
r = PyEval_EvalCode((PyCodeObject*)pycode, globals, dict);
712+
#endif
710713
}
711714
if (r) {
712715
result = PythonQtConv::PyObjToQVariant(r);
@@ -1279,8 +1282,13 @@ int custom_system_exit_exception_handler()
12791282
// return exitcode;
12801283

12811284
PyErr_Fetch(&exception, &value, &tb);
1282-
if (Py_FlushLine())
1285+
#ifndef PY3K
1286+
if (Py_FlushLine()) {
12831287
PyErr_Clear();
1288+
}
1289+
#else
1290+
// TODO: unclear what to do, since Py_FlushLine is gone...
1291+
#endif
12841292
fflush(stdout);
12851293
if (value == NULL || value == Py_None)
12861294
goto done;
@@ -1740,8 +1748,11 @@ bool PythonQtPrivate::isMethodDescriptor(PyObject* object) const
17401748
if (PyObject_HasAttrString(object, "__get__") &&
17411749
!PyObject_HasAttrString(object, "__set__") &&
17421750
!PyMethod_Check(object) &&
1743-
!PyFunction_Check(object) &&
1744-
!PyClass_Check(object)) {
1751+
!PyFunction_Check(object)
1752+
#ifndef PY3K
1753+
&& !PyClass_Check(object)
1754+
#endif
1755+
) {
17451756
return true;
17461757
}
17471758
return false;
@@ -1897,12 +1908,18 @@ void PythonQtPrivate::shellClassDeleted( void* shellClass )
18971908

18981909
PyObject* PythonQtPrivate::wrapMemoryAsBuffer( const void* data, Py_ssize_t size )
18991910
{
1900-
// P3K port needed later on!
1911+
#ifdef PY3K
1912+
return PyMemoryView_FromMemory((char*)data, size, PyBUF_READ);
1913+
#else
19011914
return PyBuffer_FromMemory((void*)data, size);
1915+
#endif
19021916
}
19031917

19041918
PyObject* PythonQtPrivate::wrapMemoryAsBuffer( void* data, Py_ssize_t size )
19051919
{
1906-
// P3K port needed later on!
1920+
#ifdef PY3K
1921+
return PyMemoryView_FromMemory((char*)data, size, PyBUF_READ | PyBUF_WRITE);
1922+
#else
19071923
return PyBuffer_FromReadWriteMemory(data, size);
1924+
#endif
19081925
}

src/PythonQtConversion.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,9 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
396396
// a pointer
397397
if (info.typeId == QMetaType::Char || info.typeId == QMetaType::UChar)
398398
{
399-
if (obj->ob_type == &PyString_Type) {
399+
if (obj->ob_type == &PyBytes_Type) {
400400
// take direct reference to string data
401-
const char* data = PyString_AS_STRING(obj);
401+
const char* data = PyBytes_AS_STRING(obj);
402402
PythonQtValueStorage_ADD_VALUE_IF_NEEDED(alreadyAllocatedCPPObject,global_ptrStorage, void*, (void*)data, ptr);
403403
} else {
404404
// convert to string
@@ -685,7 +685,7 @@ QStringList PythonQtConv::PyObjToStringList(PyObject* val, bool strict, bool& ok
685685
// if we are strict, we do not want to convert a string to a stringlist
686686
// (strings in python are detected to be sequences)
687687
if (strict &&
688-
(val->ob_type == &PyString_Type ||
688+
(val->ob_type == &PyBytes_Type ||
689689
PyUnicode_Check(val))) {
690690
return v;
691691
}
@@ -716,14 +716,18 @@ QString PythonQtConv::PyObjGetRepresentation(PyObject* val)
716716
QString PythonQtConv::PyObjGetString(PyObject* val, bool strict, bool& ok) {
717717
QString r;
718718
ok = true;
719-
if (val->ob_type == &PyString_Type) {
720-
r = QString(PyString_AS_STRING(val));
719+
if (val->ob_type == &PyBytes_Type) {
720+
r = QString(PyBytes_AS_STRING(val));
721721
} else if (PyUnicode_Check(val)) {
722+
#ifdef PY3K
723+
r = QString::fromUtf8(PyUnicode_AsUTF8(val));
724+
#else
722725
PyObject *ptmp = PyUnicode_AsUTF8String(val);
723726
if(ptmp) {
724727
r = QString::fromUtf8(PyString_AS_STRING(ptmp));
725728
Py_DECREF(ptmp);
726729
}
730+
#endif
727731
} else if (!strict) {
728732
// EXTRA: could also use _Unicode, but why should we?
729733
PyObject* str = PyObject_Str(val);
@@ -743,9 +747,9 @@ QByteArray PythonQtConv::PyObjGetBytes(PyObject* val, bool /*strict*/, bool& ok)
743747
// TODO: support buffer objects in general
744748
QByteArray r;
745749
ok = true;
746-
if (val->ob_type == &PyString_Type) {
747-
long size = PyString_GET_SIZE(val);
748-
r = QByteArray(PyString_AS_STRING(val), size);
750+
if (val->ob_type == &PyBytes_Type) {
751+
long size = PyBytes_GET_SIZE(val);
752+
r = QByteArray(PyBytes_AS_STRING(val), size);
749753
} else {
750754
ok = false;
751755
}
@@ -927,7 +931,8 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
927931

928932
if (type==-1) {
929933
// no special type requested
930-
if (val->ob_type==&PyString_Type || val->ob_type==&PyUnicode_Type) {
934+
if (PyBytes_Check(val) || PyUnicode_Check(val)) {
935+
// NOTE: for compatibility reasons between Python 2/3 we don't use ByteArray for PyBytes_Type
931936
type = QVariant::String;
932937
} else if (val == Py_False || val == Py_True) {
933938
type = QVariant::Bool;

src/PythonQtInstanceWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static void PythonQtInstanceWrapper_dealloc(PythonQtInstanceWrapper* self)
119119
{
120120
PythonQtInstanceWrapper_deleteObject(self);
121121
self->_obj.~QPointer<QObject>();
122-
Py_TYPE(this)->tp_free((PyObject*)self);
122+
Py_TYPE(self)->tp_free((PyObject*)self);
123123
}
124124

125125
static PyObject* PythonQtInstanceWrapper_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/)

src/PythonQtPythonInclude.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@
6969
#define PY3K
7070
// Helper defines to facilitate porting
7171
#define PyString_FromString PyUnicode_FromString
72+
#define PyString_AS_STRING PyUnicode_AsUTF8
73+
#define PyString_AsString PyUnicode_AsUTF8
74+
#define PyString_FromFormat PyUnicode_FromFormat
75+
76+
#define PyInt_Type PyLong_Type
77+
#define PyInt_FromLong PyLong_FromLong
78+
#define PyInt_AS_LONG PyLong_AS_LONG
79+
#define PyInt_Check PyLong_Check
80+
#define PyInt_AsLong PyLong_AsLong
81+
82+
#else
83+
// Defines to use Python 3 names in Python 2 code
84+
#define PyBytes_Type PyString_Type
85+
#define PyBytes_Check PyString_Check
86+
#define PyBytes_AS_STRING PyString_AS_STRING
87+
#define PyBytes_AsString PyString_AsString
88+
#define PyBytes_GET_SIZE PyString_GET_SIZE
89+
#define PyBytes_FromStringAndSize PyString_FromStringAndSize
7290
#endif
7391

7492
#endif

src/PythonQtSignal.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,13 @@ static PyObject *
133133
meth_get__self__(PythonQtSignalFunctionObject *m, void * /*closure*/)
134134
{
135135
PyObject *self;
136+
#ifndef PY3K
136137
if (PyEval_GetRestricted()) {
137138
PyErr_SetString(PyExc_RuntimeError,
138139
"method.__self__ not accessible in restricted mode");
139140
return NULL;
140141
}
142+
#endif
141143
self = m->m_self;
142144
if (self == NULL)
143145
self = Py_None;

src/PythonQtSignalReceiver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ PyObject* PythonQtSignalTarget::call(PyObject* callable, const PythonQtMethodInf
138138

139139
bool PythonQtSignalTarget::isSame( int signalId, PyObject* callable ) const
140140
{
141-
return PyObject_Compare(callable, _callable) == 0 && signalId==_signalId;
141+
return PyObject_RichCompareBool(callable, _callable, Py_EQ) && (signalId == _signalId);
142142
}
143143

144144
//------------------------------------------------------------------------------

src/PythonQtSlot.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ bool PythonQtCallSlot(PythonQtClassInfo* classInfo, QObject* objectToCall, PyObj
193193
hadException = true;
194194
QByteArray what("std::exception: ");
195195
what += e.what();
196-
PyErr_SetString(PyExc_StandardError, what.constData());
196+
PyErr_SetString(PyExc_RuntimeError, what.constData());
197197
}
198198
}
199199

@@ -443,11 +443,13 @@ static PyObject *
443443
meth_get__self__(PythonQtSlotFunctionObject *m, void * /*closure*/)
444444
{
445445
PyObject *self;
446+
#ifndef PY3K
446447
if (PyEval_GetRestricted()) {
447448
PyErr_SetString(PyExc_RuntimeError,
448449
"method.__self__ not accessible in restricted mode");
449450
return NULL;
450451
}
452+
#endif
451453
self = m->m_self;
452454
if (self == NULL)
453455
self = Py_None;

src/PythonQtStdDecorators.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include "PythonQtStdDecorators.h"
4343
#include "PythonQt.h"
4444
#include "PythonQtClassInfo.h"
45+
#include "PythonQtConversion.h"
46+
4547
#include <QCoreApplication>
4648

4749
bool PythonQtStdDecorators::connect(QObject* sender, const QByteArray& signal, PyObject* callable)
@@ -175,39 +177,43 @@ QString PythonQtStdDecorators::tr(QObject* obj, const QByteArray& text, const QB
175177
QObject* PythonQtStdDecorators::findChild(QObject* parent, PyObject* type, const QString& name)
176178
{
177179
const QMetaObject* meta = NULL;
178-
const char* typeName = NULL;
180+
QByteArray typeName;
179181

180182
if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) {
181183
meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
182184
} else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
183185
meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
184-
} else if (PyString_Check(type)) {
185-
typeName = PyString_AsString(type);
186+
} else if (PyBytes_Check(type) || PyUnicode_Check(type)) {
187+
typeName = PythonQtConv::PyObjGetString(type).toUtf8();
186188
}
187189

188-
if (!typeName && !meta)
190+
if (typeName.isEmpty() && !meta) {
189191
return NULL;
192+
}
190193

191194
return findChild(parent, typeName, meta, name);
192195
}
193196

194197
QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QString& name)
195198
{
196199
const QMetaObject* meta = NULL;
197-
const char* typeName = NULL;
200+
QByteArray typeName;
198201

199202
if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) {
200203
meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
201204
} else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
202205
meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
203-
} else if (PyString_Check(type)) {
204-
typeName = PyString_AsString(type);
205206
}
207+
else if (PyBytes_Check(type) || PyUnicode_Check(type)) {
208+
typeName = PythonQtConv::PyObjGetString(type).toUtf8();
209+
}
210+
206211

207212
QList<QObject*> list;
208213

209-
if (!typeName && !meta)
214+
if (typeName.isEmpty() && !meta) {
210215
return list;
216+
}
211217

212218
findChildren(parent, typeName, meta, name, list);
213219

@@ -217,20 +223,22 @@ QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* t
217223
QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QRegExp& regExp)
218224
{
219225
const QMetaObject* meta = NULL;
220-
const char* typeName = NULL;
226+
QByteArray typeName;
221227

222228
if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) {
223229
meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject();
224230
} else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) {
225231
meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject();
226-
} else if (PyString_Check(type)) {
227-
typeName = PyString_AsString(type);
232+
}
233+
else if (PyBytes_Check(type) || PyUnicode_Check(type)) {
234+
typeName = PythonQtConv::PyObjGetString(type).toUtf8();
228235
}
229236

230237
QList<QObject*> list;
231238

232-
if (!typeName && !meta)
239+
if (typeName.isEmpty() && !meta) {
233240
return list;
241+
}
234242

235243
findChildren(parent, typeName, meta, regExp, list);
236244

0 commit comments

Comments
 (0)