static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name) { const char *attributeName; PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj; if ((attributeName = PyString_AsString(name)) == NULL) { return NULL; } if (obj == (PyObject*)&PythonQtInstanceWrapper_Type) { return NULL; } if (qstrcmp(attributeName, "__dict__")==0) { PyObject* objectDict = ((PyTypeObject *)wrapper)->tp_dict; if (!wrapper->classInfo()) { Py_INCREF(objectDict); return objectDict; } PyObject* dict = PyDict_New(); QStringList l = wrapper->classInfo()->memberList(); foreach (QString name, l) { PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data()); if (o) { PyDict_SetItemString(dict, name.toLatin1().data(), o); Py_DECREF(o); } else { // it must have been a property or child, which we do not know as a class object... PyErr_Clear(); } }
static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name) { const char *attributeName; PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj; #ifdef PY3K if ((attributeName = PyUnicode_AsUTF8(name)) == NULL) { #else if ((attributeName = PyString_AsString(name)) == NULL) { #endif return NULL; } if (obj == (PyObject*)&PythonQtInstanceWrapper_Type) { return NULL; } if (qstrcmp(attributeName, "__dict__")==0) { PyObject* objectDict = ((PyTypeObject *)wrapper)->tp_dict; if (!wrapper->classInfo()) { Py_INCREF(objectDict); return objectDict; } PyObject* dict = PyDict_New(); QStringList l = wrapper->classInfo()->memberList(); Q_FOREACH (QString name, l) { PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data()); if (o) { PyDict_SetItemString(dict, name.toLatin1().data(), o); Py_DECREF(o); } else { // it must have been a property or child, which we do not know as a class object... PyErr_Clear(); } } if (wrapper->classInfo()->constructors()) { #ifdef PY3K PyObject* initName = PyUnicode_FromString("__init__"); #else PyObject* initName = PyString_FromString("__init__"); #endif PyObject* func = PyType_Type.tp_getattro(obj, initName); Py_DECREF(initName); PyDict_SetItemString(dict, "__init__", func); Py_DECREF(func); } for (int i = 0; PythonQtClassWrapper_methods[i].ml_name != NULL; i++) { PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[i], obj); PyDict_SetItemString(dict, PythonQtClassWrapper_methods[i].ml_name, func); Py_DECREF(func); } PyDict_Update(dict, objectDict); return dict; }
PyObject *PythonQtMemberFunction_Call(PythonQtSlotInfo* info, PyObject* m_self, PyObject *args, PyObject *kw) { if (PyObject_TypeCheck(m_self, &PythonQtInstanceWrapper_Type)) { PythonQtInstanceWrapper* self = (PythonQtInstanceWrapper*) m_self; if (!info->isClassDecorator() && (self->_obj==NULL && self->_wrappedPtr==NULL)) { QString error = QString("Trying to call '") + info->slotName() + "' on a destroyed " + self->classInfo()->className() + " object"; PyErr_SetString(PyExc_ValueError, error.toLatin1().data()); return NULL; } else { return PythonQtSlotFunction_CallImpl(self->classInfo(), self->_obj, info, args, kw, self->_wrappedPtr); } } else if (m_self->ob_type == &PythonQtClassWrapper_Type) { PythonQtClassWrapper* type = (PythonQtClassWrapper*) m_self; if (info->isClassDecorator()) { return PythonQtSlotFunction_CallImpl(type->classInfo(), NULL, info, args, kw); } else { // otherwise, it is an unbound call and we have an instanceDecorator or normal slot... Py_ssize_t argc = PyTuple_Size(args); if (argc>0) { PyObject* firstArg = PyTuple_GET_ITEM(args, 0); if (PyObject_TypeCheck(firstArg, (PyTypeObject*)&PythonQtInstanceWrapper_Type) && ((PythonQtInstanceWrapper*)firstArg)->classInfo()->inherits(type->classInfo())) { PythonQtInstanceWrapper* self = (PythonQtInstanceWrapper*)firstArg; if (!info->isClassDecorator() && (self->_obj==NULL && self->_wrappedPtr==NULL)) { QString error = QString("Trying to call '") + info->slotName() + "' on a destroyed " + self->classInfo()->className() + " object"; PyErr_SetString(PyExc_ValueError, error.toLatin1().data()); return NULL; } // strip the first argument... PyObject* newargs = PyTuple_GetSlice(args, 1, argc); PyObject* result = PythonQtSlotFunction_CallImpl(self->classInfo(), self->_obj, info, newargs, kw, self->_wrappedPtr); Py_DECREF(newargs); return result; } else { // first arg is not of correct type! QString error = "slot " + info->fullSignature() + " requires " + type->classInfo()->className() + " instance as first argument, got " + firstArg->ob_type->tp_name; PyErr_SetString(PyExc_ValueError, error.toLatin1().data()); return NULL; } } else { // wrong number of args QString error = "slot " + info->fullSignature() + " requires " + type->classInfo()->className() + " instance as first argument."; PyErr_SetString(PyExc_ValueError, error.toLatin1().data()); return NULL; } } } return NULL; }
static PyObject * meth_repr(PythonQtSlotFunctionObject *f) { if (f->m_self->ob_type == &PythonQtClassWrapper_Type) { PythonQtClassWrapper* self = (PythonQtClassWrapper*) f->m_self; #ifdef PY3K return PyUnicode_FromFormat("<unbound qt slot %s of %s type>", #else return PyString_FromFormat("<unbound qt slot %s of %s type>", #endif f->m_ml->slotName().data(), self->classInfo()->className()); } else {
static PyObject * meth_repr(PythonQtSignalFunctionObject *f) { if (f->m_self->ob_type == &PythonQtClassWrapper_Type) { PythonQtClassWrapper* self = (PythonQtClassWrapper*) f->m_self; return PyString_FromFormat("<unbound qt signal %s of %s type>", f->m_ml->slotName().data(), self->classInfo()->className()); } else { return PyString_FromFormat("<qt signal %s of %s instance at %p>", f->m_ml->slotName().data(), f->m_self->ob_type->tp_name, f->m_self); } }