Skip to content

Commit b16c597

Browse files
committed
removed implicit conversion of QByteArray to python str, use str() to convert to Python string or QByteArray::data()
1 parent 95f53a0 commit b16c597

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/PythonQt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
8181
PythonQt_init_QtCoreBuiltin(NULL);
8282
PythonQt_init_QtGuiBuiltin(NULL);
8383

84+
PythonQtRegisterToolClassesTemplateConverter(QByteArray);
8485
PythonQtRegisterToolClassesTemplateConverter(QDate);
8586
PythonQtRegisterToolClassesTemplateConverter(QTime);
8687
PythonQtRegisterToolClassesTemplateConverter(QDateTime);

src/PythonQtConversion.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,11 @@ PyObject* PythonQtConv::ConvertQtValueToPythonInternal(int type, const void* dat
146146
return PyLong_FromLongLong(*((qint64*)data));
147147
case QMetaType::ULongLong:
148148
return PyLong_FromUnsignedLongLong(*((quint64*)data));
149-
case QMetaType::QByteArray: {
150-
QByteArray* v = (QByteArray*) data;
151-
return PyString_FromStringAndSize(*v, v->size());
152-
}
149+
// implicit conversion from QByteArray to str has been removed:
150+
//case QMetaType::QByteArray: {
151+
// QByteArray* v = (QByteArray*) data;
152+
// return PyString_FromStringAndSize(*v, v->size());
153+
// }
153154
case QMetaType::QVariantMap:
154155
return PythonQtConv::QVariantMapToPyObject(*((QVariantMap*)data));
155156
case QMetaType::QVariantList:
@@ -689,6 +690,7 @@ QString PythonQtConv::PyObjGetString(PyObject* val, bool strict, bool& ok) {
689690
}
690691

691692
QByteArray PythonQtConv::PyObjGetBytes(PyObject* val, bool /*strict*/, bool& ok) {
693+
// TODO: support buffer objects in general
692694
QByteArray r;
693695
ok = true;
694696
if (val->ob_type == &PyString_Type) {

src/PythonQtDoc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
- QObject.emit to emit Qt signals from Python is not yet implemented but PythonQt allows to just emit a signal by calling it
147147
- PythonQt does not (yet) offer to add new signals to Python/C++ objects
148148
- Ownership of objects is a bit different in PythonQt, currently Python classes derived from a C++ class need to be manually referenced in Python to not get deleted too early (this will be fixed)
149-
- QString and QBytearray are always converted to unicode and str Python objects (PyQt returns QString and QByteArray instead).
149+
- QStrings are always converted to unicode Python objects (PyQt returns QString instead), we prefered to return Python strings.
150150
- Probably there are lots of details that differ, I do not know PyQt that well to list them all.
151151
152152
\section Interface

src/PythonQtInstanceWrapper.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,17 @@ static QString getStringFromObject(PythonQtInstanceWrapper* wrapper) {
566566
static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
567567
{
568568
PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
569+
570+
// QByteArray should be directly returned as a str
571+
if (wrapper->classInfo()->metaTypeId()==QVariant::ByteArray) {
572+
QByteArray* b = (QByteArray*) wrapper->_wrappedPtr;
573+
if (b->data()) {
574+
return PyString_FromStringAndSize(b->data(), b->size());
575+
} else {
576+
return PyString_FromString("");
577+
}
578+
}
579+
569580
const char* typeName = obj->ob_type->tp_name;
570581
QObject *qobj = wrapper->_obj;
571582
QString str = getStringFromObject(wrapper);

0 commit comments

Comments
 (0)