/* * Implement asstring() for the type. */ static PyObject *sipVoidPtr_asstring(sipVoidPtrObject *v, PyObject *args, PyObject *kw) { static char *kwlist[] = {"size", NULL}; SIP_SSIZE_T size = -1; if (!PyArg_ParseTupleAndKeywords(args, kw, #if PY_VERSION_HEX >= 0x02050000 "|n:asstring", #else "|i:asstring", #endif kwlist, &size)) return NULL; /* Use the current size if one wasn't explicitly given. */ if (size < 0) size = v->size; if (size < 0) { PyErr_SetString(PyExc_ValueError, "a size must be given or the sip.voidptr object must have a size"); return NULL; } return SIPBytes_FromStringAndSize(v->voidptr, size); }
/* * Implement sequence item sub-script for the type. */ static PyObject *sipVoidPtr_item(PyObject *self, SIP_SSIZE_T idx) { if (check_size(self) < 0 || check_index(self, idx) < 0) return NULL; return SIPBytes_FromStringAndSize( (char *)((sipVoidPtrObject *)self)->voidptr + idx, 1); }
static PyObject *meth_QLatin1Char_0_toLatin1(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { const QLatin1Char *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QLatin1Char, &sipCpp)) { char sipRes; Py_BEGIN_ALLOW_THREADS sipRes = sipCpp->toLatin1(); Py_END_ALLOW_THREADS return SIPBytes_FromStringAndSize(&sipRes,1); } }
/* * Implement asstring() for the type. */ static PyObject *sipVoidPtr_asstring(sipVoidPtrObject *v, PyObject *args, PyObject *kw) { static char *kwlist[] = {"size", NULL}; SIP_SSIZE_T size = -1; if (!PyArg_ParseTupleAndKeywords(args, kw, #if PY_VERSION_HEX >= 0x02050000 "|n:asstring", #else "|i:asstring", #endif kwlist, &size)) return NULL; if ((size = get_size_from_arg(v, size)) < 0) return NULL; return SIPBytes_FromStringAndSize(v->voidptr, size); }
// Convert a C++ object at an arbitary address to Python. PyObject *Chimera::toPyObject(void *cpp) const { if (_metatype == PyQt_PyObject::metatype) { if (_type) { // SIP knows the type (therefore it isn't really wrapped in a // PyQt_PyObject) but it's not registered with Qt. if (_name.endsWith('*')) cpp = *reinterpret_cast<void **>(cpp); return sipConvertFromType(cpp, _type, 0); } else { // Otherwise unwrap the Python object. PyQt_PyObject *pyobj_wrapper = reinterpret_cast<PyQt_PyObject *>(cpp); if (!pyobj_wrapper->pyobject) { PyErr_SetString(PyExc_TypeError, "unable to convert a QVariant back to a Python object"); return 0; } Py_INCREF(pyobj_wrapper->pyobject); return pyobj_wrapper->pyobject; } } PyObject *py = 0; switch (_metatype) { case QMetaType::Bool: py = PyBool_FromLong(*reinterpret_cast<bool *>(cpp)); break; case QMetaType::Int: if (isFlag()) { py = sipConvertFromType(cpp, _type, 0); } else if (isEnum()) { py = sipConvertFromEnum(*reinterpret_cast<int *>(cpp), _type); } else { py = SIPLong_FromLong(*reinterpret_cast<int *>(cpp)); } break; case QMetaType::UInt: { long ui = *reinterpret_cast<unsigned int *>(cpp); if (ui < 0) py = PyLong_FromUnsignedLong((unsigned long)ui); else py = SIPLong_FromLong(ui); break; } case QMetaType::Double: py = PyFloat_FromDouble(*reinterpret_cast<double *>(cpp)); break; case QMetaType::VoidStar: py = sipConvertFromVoidPtr(*reinterpret_cast<void **>(cpp)); break; case QMetaType::Long: py = SIPLong_FromLong(*reinterpret_cast<long *>(cpp)); break; case QMetaType::LongLong: py = PyLong_FromLongLong(*reinterpret_cast<qlonglong *>(cpp)); break; case QMetaType::Short: py = SIPLong_FromLong(*reinterpret_cast<short *>(cpp)); break; case QMetaType::Char: case QMetaType::UChar: py = SIPBytes_FromStringAndSize(reinterpret_cast<char *>(cpp), 1); break; case QMetaType::ULong: py = PyLong_FromUnsignedLong(*reinterpret_cast<unsigned long *>(cpp)); break; case QMetaType::ULongLong: py = PyLong_FromUnsignedLongLong(*reinterpret_cast<qulonglong *>(cpp)); break; case QMetaType::UShort: py = SIPLong_FromLong(*reinterpret_cast<unsigned short *>(cpp)); break; case QMetaType::Float: py = PyFloat_FromDouble(*reinterpret_cast<float *>(cpp)); break; case QMetaType::QObjectStar: py = sipConvertFromType(*reinterpret_cast<void **>(cpp), sipType_QObject, 0); break; case QMetaType::QWidgetStar: if (sipType_QWidget) py = sipConvertFromType(*reinterpret_cast<void **>(cpp), sipType_QWidget, 0); break; case QMetaType::QVariantList: { QVariantList *ql = reinterpret_cast<QVariantList *>(cpp); py = PyList_New(ql->size()); if (py) { for (int i = 0; i < ql->size(); ++i) { PyObject *val_obj = toAnyPyObject(ql->at(i)); if (!val_obj) { Py_DECREF(py); py = 0; break; } PyList_SET_ITEM(py, i, val_obj); } } break; } case QMetaType::QVariantMap: { py = PyDict_New(); if (py) { QVariantMap *qm = reinterpret_cast<QVariantMap *>(cpp); for (QVariantMap::const_iterator it = qm->constBegin(); it != qm->constEnd(); ++it) if (!add_variant_to_dict(py, it.key(), it.value())) { Py_DECREF(py); py = 0; break; } } break; } #if QT_VERSION >= 0x040500 case QMetaType::QVariantHash: { py = PyDict_New(); if (py) { QVariantHash *qh = reinterpret_cast<QVariantHash *>(cpp); for (QVariantHash::const_iterator it = qh->constBegin(); it != qh->constEnd(); ++it) if (!add_variant_to_dict(py, it.key(), it.value())) { Py_DECREF(py); py = 0; break; } } break; } #endif case -1: { char *s = *reinterpret_cast<char **>(cpp); if (s) { py = SIPBytes_FromString(s); } else { Py_INCREF(Py_None); py = Py_None; } break; } default: if (_type) { if (_name.endsWith('*')) { py = sipConvertFromType(*reinterpret_cast<void **>(cpp), _type, 0); } else { // Make a copy as it is a value type. void *copy = QMetaType::construct(_metatype, cpp); py = sipConvertFromNewType(copy, _type, 0); if (!py) QMetaType::destroy(_metatype, copy); } } } if (!py) PyErr_Format(PyExc_TypeError, "unable to convert a C++ '%s' instance to a Python object", _name.constData()); return py; }