summaryrefslogtreecommitdiffstats
path: root/sources/pyside2/libpyside/pysideslot.cpp
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2019-05-03 09:44:52 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2019-05-06 11:44:20 +0000
commit06f97eca45ddadf4f04229cf14d5dc0bbd867316 (patch)
tree84c9daa0e963166e75663a370432feee911220f0 /sources/pyside2/libpyside/pysideslot.cpp
parent06e53faac0b1bcdffc966e0676a39a5f50f93592 (diff)
Port property/signal/slot handling of libpyside to use QByteArray
Introduce C++ structs with QByteArray to be used for signal and slot names and signatures, removing a lot of code dealing with char * pointers, strdup() and reallocating. Change-Id: I28acf727bc6cf468285b153c85b0a342fd79f7d8 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/pyside2/libpyside/pysideslot.cpp')
-rw-r--r--sources/pyside2/libpyside/pysideslot.cpp59
1 files changed, 27 insertions, 32 deletions
diff --git a/sources/pyside2/libpyside/pysideslot.cpp b/sources/pyside2/libpyside/pysideslot.cpp
index 694b5b59..4104c090 100644
--- a/sources/pyside2/libpyside/pysideslot.cpp
+++ b/sources/pyside2/libpyside/pysideslot.cpp
@@ -48,12 +48,17 @@
#define SLOT_DEC_NAME "Slot"
+struct SlotData
+{
+ QByteArray name;
+ QByteArray args;
+ QByteArray resultType;
+};
+
typedef struct
{
PyObject_HEAD
- char* slotName;
- char* args;
- char* resultType;
+ SlotData *slotData;
} PySideSlot;
extern "C"
@@ -103,31 +108,25 @@ int slotTpInit(PyObject *self, PyObject *args, PyObject *kw)
}
PySideSlot *data = reinterpret_cast<PySideSlot*>(self);
+ if (!data->slotData)
+ data->slotData = new SlotData;
for(Py_ssize_t i = 0, i_max = PyTuple_Size(args); i < i_max; i++) {
PyObject *argType = PyTuple_GET_ITEM(args, i);
- char *typeName = PySide::Signal::getTypeName(argType);
- if (typeName) {
- if (data->args) {
- data->args = reinterpret_cast<char*>(realloc(data->args, (strlen(data->args) + 1 + strlen(typeName)) * sizeof(char*)));
- data->args = strcat(data->args, ",");
- data->args = strcat(data->args, typeName);
- free(typeName);
- } else {
- data->args = typeName;
- }
- } else {
+ const auto typeName = PySide::Signal::getTypeName(argType);
+ if (typeName.isEmpty()) {
PyErr_Format(PyExc_TypeError, "Unknown signal argument type: %s", Py_TYPE(argType)->tp_name);
return -1;
}
+ if (!data->slotData->args.isEmpty())
+ data->slotData->args += ',';
+ data->slotData->args += typeName;
}
if (argName)
- data->slotName = strdup(argName);
+ data->slotData->name = argName;
- if (argResult)
- data->resultType = PySide::Signal::getTypeName(argResult);
- else
- data->resultType = strdup("void");
+ data->slotData->resultType = argResult
+ ? PySide::Signal::getTypeName(argResult) : PySide::Signal::voidType();
return 1;
}
@@ -142,15 +141,15 @@ PyObject *slotCall(PyObject *self, PyObject *args, PyObject * /* kw */)
if (PyFunction_Check(callback)) {
PySideSlot *data = reinterpret_cast<PySideSlot*>(self);
- if (!data->slotName) {
- PyObject *funcName = PepFunction_GetName(callback);
- data->slotName = strdup(Shiboken::String::toCString(funcName));
- }
+ if (!data->slotData)
+ data->slotData = new SlotData;
+
+ if (data->slotData->name.isEmpty())
+ data->slotData->name = Shiboken::String::toCString(PepFunction_GetName(callback));
- const QByteArray returnType = QMetaObject::normalizedType(data->resultType);
+ const QByteArray returnType = QMetaObject::normalizedType(data->slotData->resultType);
const QByteArray signature =
- returnType + ' ' + const_cast<const char *>(data->slotName)
- + '(' + const_cast<const char *>(data->args) + ')';
+ returnType + ' ' + data->slotData->name + '(' + data->slotData->args + ')';
if (!pySlotName)
pySlotName = Shiboken::String::fromCString(PYSIDE_SLOT_LIST_ATTR);
@@ -169,12 +168,8 @@ PyObject *slotCall(PyObject *self, PyObject *args, PyObject * /* kw */)
Py_DECREF(pySignature);
//clear data
- free(data->slotName);
- data->slotName = 0;
- free(data->resultType);
- data->resultType = 0;
- free(data->args);
- data->args = 0;
+ delete data->slotData;
+ data->slotData = nullptr;
return callback;
}
return callback;