1
0
mirror of https://github.com/thp/pyotherside.git synced 2025-01-17 23:22:53 +08:00

Merge pull request #103 from uglide/add_qbytearray_support

Add support for QByteArray
This commit is contained in:
Thomas Perl 2019-06-06 13:48:24 +02:00 committed by GitHub
commit c67604ba4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 5 deletions

View File

@ -100,6 +100,7 @@ class Converter {
FLOATING,
BOOLEAN,
STRING,
BYTES,
LIST,
DICT,
DATE,
@ -114,6 +115,7 @@ class Converter {
virtual double floating(V&) = 0;
virtual bool boolean(V&) = 0;
virtual const char *string(V&) = 0;
virtual QByteArray bytes(V&) = 0;
virtual ListIterator<V> *list(V&) = 0;
virtual DictIterator<V> *dict(V&) = 0;
virtual ConverterDate date(V&) = 0;
@ -126,6 +128,7 @@ class Converter {
virtual V fromFloating(double v) = 0;
virtual V fromBoolean(bool v) = 0;
virtual V fromString(const char *v) = 0;
virtual V fromBytes(const QByteArray &v) = 0;
virtual V fromDate(ConverterDate date) = 0;
virtual V fromTime(ConverterTime time) = 0;
virtual V fromDateTime(ConverterDateTime dateTime) = 0;
@ -154,6 +157,8 @@ convert(F from)
return tconv.fromBoolean(fconv.boolean(from));
case FC::STRING:
return tconv.fromString(fconv.string(from));
case FC::BYTES:
return tconv.fromBytes(fconv.bytes(from));
case FC::LIST:
{
ListBuilder<T> *listBuilder = tconv.newList();

View File

@ -150,8 +150,10 @@ class PyObjectConverter : public Converter<PyObject *> {
return INTEGER;
} else if (PyFloat_Check(o)) {
return FLOATING;
} else if (PyUnicode_Check(o) || PyBytes_Check(o)) {
} else if (PyUnicode_Check(o)) {
return STRING;
} else if (PyBytes_Check(o)) {
return BYTES;
} else if (PyDateTime_Check(o)) {
// Need to check PyDateTime before PyDate, because
// it is a subclass of PyDate.
@ -175,10 +177,6 @@ class PyObjectConverter : public Converter<PyObject *> {
virtual double floating(PyObject *&o) { return PyFloat_AsDouble(o); }
virtual bool boolean(PyObject *&o) { return (o == Py_True); }
virtual const char *string(PyObject *&o) {
if (PyBytes_Check(o)) {
return PyBytes_AsString(o);
}
// XXX: In Python 3.3, we can use PyUnicode_UTF8()
if (stringcontainer != NULL) {
Py_DECREF(stringcontainer);
@ -186,6 +184,9 @@ class PyObjectConverter : public Converter<PyObject *> {
stringcontainer = PyUnicode_AsUTF8String(o);
return PyBytes_AsString(stringcontainer);
}
virtual QByteArray bytes(PyObject *&o) {
return QByteArray(PyBytes_AsString(o), PyBytes_Size(o));
}
virtual ListIterator<PyObject *> *list(PyObject *&o) { return new PyObjectListIterator(o); }
virtual DictIterator<PyObject *> *dict(PyObject *&o) { return new PyObjectDictIterator(o);; }
virtual ConverterDate date(PyObject *&o) {
@ -222,6 +223,7 @@ class PyObjectConverter : public Converter<PyObject *> {
virtual PyObject * fromFloating(double v) { return PyFloat_FromDouble(v); }
virtual PyObject * fromBoolean(bool v) { return PyBool_FromLong((long)v); }
virtual PyObject * fromString(const char *v) { return PyUnicode_FromString(v); }
virtual PyObject * fromBytes(const QByteArray &v) { return PyBytes_FromStringAndSize(v.constData(), v.size()); }
virtual PyObject * fromDate(ConverterDate v) { return PyDate_FromDate(v.y, v.m, v.d); }
virtual PyObject * fromTime(ConverterTime v) { return PyTime_FromTime(v.h, v.m, v.s, 1000 * v.ms); }
virtual PyObject * fromDateTime(ConverterDateTime v) {

View File

@ -134,6 +134,8 @@ class QVariantConverter : public Converter<QVariant> {
return FLOATING;
case QMetaType::QString:
return STRING;
case QMetaType::QByteArray:
return BYTES;
case QMetaType::QDate:
return DATE;
case QMetaType::QTime:
@ -213,6 +215,10 @@ class QVariantConverter : public Converter<QVariant> {
return stringstorage.constData();
}
virtual QByteArray bytes(QVariant &v) {
return stringstorage = v.toByteArray();
}
virtual PyObjectRef pyObject(QVariant &v) {
return v.value<PyObjectRef>();
}
@ -233,6 +239,7 @@ class QVariantConverter : public Converter<QVariant> {
virtual QVariant fromFloating(double v) { return QVariant(v); }
virtual QVariant fromBoolean(bool v) { return QVariant(v); }
virtual QVariant fromString(const char *v) { return QVariant(QString::fromUtf8(v)); }
virtual QVariant fromBytes(const QByteArray &v) { return QVariant(v); }
virtual QVariant fromDate(ConverterDate v) { return QVariant(QDate(v.y, v.m, v.d)); }
virtual QVariant fromTime(ConverterTime v) { return QVariant(QTime(v.h, v.m, v.s, v.ms)); }
virtual QVariant fromDateTime(ConverterDateTime v) {

View File

@ -62,6 +62,14 @@ test_converter_for(Converter<V> *conv)
QVERIFY(conv->type(v) == Converter<V>::STRING);
QVERIFY(strcmp(conv->string(v), "Hello World") == 0);
/* Convert from/to Bytes */
static const char BUF[] = { 'a', 'b', '\0', 'c', 'd' };
v = conv->fromBytes(QByteArray(BUF, sizeof(BUF)));
QVERIFY(conv->type(v) == Converter<V>::BYTES);
QByteArray res = conv->bytes(v);
QVERIFY(res.size() == sizeof(BUF));
QVERIFY(memcmp(BUF, res.constData(), res.size()) == 0);
/* Convert from/to List */
ListBuilder<V> *builder = conv->newList();
v = conv->fromInteger(444);