Skip to content

Commit bc70661

Browse files
Allow wrapper's py_toString to return empty string
Don't fallback to the default ("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr) when py_toString returns empty string
1 parent 4da9fe4 commit bc70661

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/PythonQtConversion.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,8 +1385,9 @@ bool PythonQtConv::ConvertPythonListToQListOfPointerType(PyObject* obj, QList<vo
13851385
}
13861386

13871387

1388-
QString PythonQtConv::CPPObjectToString(int type, const void* data) {
1388+
QString PythonQtConv::CPPObjectToString(int type, const void* data, bool &ok) {
13891389
QString r;
1390+
ok = true;
13901391
switch (type) {
13911392
case QVariant::Size: {
13921393
const QSize* s = static_cast<const QSize*>(data);
@@ -1456,11 +1457,15 @@ QString PythonQtConv::CPPObjectToString(int type, const void* data) {
14561457
//TODO: add more printing for other variant types
14571458
default:
14581459
// this creates a copy, but that should not be expensive for typical simple variants
1459-
// (but we do not want to do this for our won user types!
1460+
// (but we do not want to do this for our own user types!
14601461
if (type>0 && type < (int)QVariant::UserType) {
14611462
QVariant v(type, data);
1463+
ok = v.canConvert<QString>();
14621464
r = v.toString();
1465+
} else {
1466+
ok = false;
14631467
}
1468+
break;
14641469
}
14651470
return r;
14661471
}
@@ -1595,4 +1600,4 @@ PyObject* PythonQtConv::convertFromPythonQtSafeObjectPtr(const void* /* PythonQt
15951600
// extra ref count, since we are supposed to return a newly refcounted object
15961601
Py_XINCREF(obj);
15971602
return obj;
1598-
}
1603+
}

src/PythonQtConversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class PYTHONQT_EXPORT PythonQtConv {
158158
static PyObject* QVariantListToPyObject(const QVariantList& l);
159159

160160
//! get human readable string from CPP object (when the metatype is known)
161-
static QString CPPObjectToString(int type, const void* data);
161+
static QString CPPObjectToString(int type, const void* data, bool &ok);
162162

163163
//! register a converter callback from python to cpp for given metatype
164164
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB* cb) { _pythonToMetaTypeConverters.insert(metaTypeId, cb); }

src/PythonQtInstanceWrapper.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -732,13 +732,14 @@ static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObjec
732732
return -1;
733733
}
734734

735-
static QString getStringFromObject(PythonQtInstanceWrapper* wrapper) {
735+
static QString getStringFromObject(PythonQtInstanceWrapper* wrapper, bool &ok) {
736736
QString result;
737+
ok = false;
737738
if (wrapper->_wrappedPtr) {
738739
// first try some manually string conversions for some variants
739740
int metaid = wrapper->classInfo()->metaTypeId();
740-
result = PythonQtConv::CPPObjectToString(metaid, wrapper->_wrappedPtr);
741-
if (!result.isEmpty()) {
741+
result = PythonQtConv::CPPObjectToString(metaid, wrapper->_wrappedPtr, ok);
742+
if (ok) {
742743
return result;
743744
}
744745
}
@@ -748,7 +749,7 @@ static QString getStringFromObject(PythonQtInstanceWrapper* wrapper) {
748749
if (info._type == PythonQtMemberInfo::Slot) {
749750
PyObject* resultObj = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, info._slot, NULL, NULL, wrapper->_wrappedPtr);
750751
if (resultObj) {
751-
result = PythonQtConv::PyObjGetString(resultObj);
752+
result = PythonQtConv::PyObjGetString(resultObj, false, ok);
752753
Py_DECREF(resultObj);
753754
}
754755
}
@@ -786,8 +787,9 @@ static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
786787

787788
const char* typeName = obj->ob_type->tp_name;
788789
QObject *qobj = wrapper->_obj;
789-
QString str = getStringFromObject(wrapper);
790-
if (!str.isEmpty()) {
790+
bool ok = false;
791+
QString str = getStringFromObject(wrapper, ok);
792+
if (ok) {
791793
return PyString_FromFormat("%s", QStringToPythonConstCharPointer(str));
792794
}
793795
if (wrapper->_wrappedPtr) {
@@ -807,8 +809,9 @@ static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj)
807809
const char* typeName = obj->ob_type->tp_name;
808810

809811
QObject *qobj = wrapper->_obj;
810-
QString str = getStringFromObject(wrapper);
811-
if (!str.isEmpty()) {
812+
bool ok = false;
813+
QString str = getStringFromObject(wrapper, ok);
814+
if (ok) {
812815
if (str.startsWith(typeName)) {
813816
return PyString_FromFormat("%s", QStringToPythonConstCharPointer(str));
814817
} else {

0 commit comments

Comments
 (0)