mirror of
https://github.com/thp/pyotherside.git
synced 2025-01-17 23:22:53 +08:00
Add support for QByteArray
- Add converting from QByteArray to bytes - Convert bytes to QByteArray instead of trying to parse it as UTF-8 string
This commit is contained in:
parent
7f0ff4687b
commit
b78f53b4e9
@ -100,6 +100,7 @@ class Converter {
|
|||||||
FLOATING,
|
FLOATING,
|
||||||
BOOLEAN,
|
BOOLEAN,
|
||||||
STRING,
|
STRING,
|
||||||
|
BYTES,
|
||||||
LIST,
|
LIST,
|
||||||
DICT,
|
DICT,
|
||||||
DATE,
|
DATE,
|
||||||
@ -114,6 +115,7 @@ class Converter {
|
|||||||
virtual double floating(V&) = 0;
|
virtual double floating(V&) = 0;
|
||||||
virtual bool boolean(V&) = 0;
|
virtual bool boolean(V&) = 0;
|
||||||
virtual const char *string(V&) = 0;
|
virtual const char *string(V&) = 0;
|
||||||
|
virtual QByteArray bytes(V&) = 0;
|
||||||
virtual ListIterator<V> *list(V&) = 0;
|
virtual ListIterator<V> *list(V&) = 0;
|
||||||
virtual DictIterator<V> *dict(V&) = 0;
|
virtual DictIterator<V> *dict(V&) = 0;
|
||||||
virtual ConverterDate date(V&) = 0;
|
virtual ConverterDate date(V&) = 0;
|
||||||
@ -126,6 +128,7 @@ class Converter {
|
|||||||
virtual V fromFloating(double v) = 0;
|
virtual V fromFloating(double v) = 0;
|
||||||
virtual V fromBoolean(bool v) = 0;
|
virtual V fromBoolean(bool v) = 0;
|
||||||
virtual V fromString(const char *v) = 0;
|
virtual V fromString(const char *v) = 0;
|
||||||
|
virtual V fromBytes(const QByteArray &v) = 0;
|
||||||
virtual V fromDate(ConverterDate date) = 0;
|
virtual V fromDate(ConverterDate date) = 0;
|
||||||
virtual V fromTime(ConverterTime time) = 0;
|
virtual V fromTime(ConverterTime time) = 0;
|
||||||
virtual V fromDateTime(ConverterDateTime dateTime) = 0;
|
virtual V fromDateTime(ConverterDateTime dateTime) = 0;
|
||||||
@ -154,6 +157,8 @@ convert(F from)
|
|||||||
return tconv.fromBoolean(fconv.boolean(from));
|
return tconv.fromBoolean(fconv.boolean(from));
|
||||||
case FC::STRING:
|
case FC::STRING:
|
||||||
return tconv.fromString(fconv.string(from));
|
return tconv.fromString(fconv.string(from));
|
||||||
|
case FC::BYTES:
|
||||||
|
return tconv.fromBytes(fconv.bytes(from));
|
||||||
case FC::LIST:
|
case FC::LIST:
|
||||||
{
|
{
|
||||||
ListBuilder<T> *listBuilder = tconv.newList();
|
ListBuilder<T> *listBuilder = tconv.newList();
|
||||||
|
@ -150,8 +150,10 @@ class PyObjectConverter : public Converter<PyObject *> {
|
|||||||
return INTEGER;
|
return INTEGER;
|
||||||
} else if (PyFloat_Check(o)) {
|
} else if (PyFloat_Check(o)) {
|
||||||
return FLOATING;
|
return FLOATING;
|
||||||
} else if (PyUnicode_Check(o) || PyBytes_Check(o)) {
|
} else if (PyUnicode_Check(o)) {
|
||||||
return STRING;
|
return STRING;
|
||||||
|
} else if (PyBytes_Check(o)) {
|
||||||
|
return BYTES;
|
||||||
} else if (PyDateTime_Check(o)) {
|
} else if (PyDateTime_Check(o)) {
|
||||||
// Need to check PyDateTime before PyDate, because
|
// Need to check PyDateTime before PyDate, because
|
||||||
// it is a subclass of PyDate.
|
// 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 double floating(PyObject *&o) { return PyFloat_AsDouble(o); }
|
||||||
virtual bool boolean(PyObject *&o) { return (o == Py_True); }
|
virtual bool boolean(PyObject *&o) { return (o == Py_True); }
|
||||||
virtual const char *string(PyObject *&o) {
|
virtual const char *string(PyObject *&o) {
|
||||||
if (PyBytes_Check(o)) {
|
|
||||||
return PyBytes_AsString(o);
|
|
||||||
}
|
|
||||||
|
|
||||||
// XXX: In Python 3.3, we can use PyUnicode_UTF8()
|
// XXX: In Python 3.3, we can use PyUnicode_UTF8()
|
||||||
if (stringcontainer != NULL) {
|
if (stringcontainer != NULL) {
|
||||||
Py_DECREF(stringcontainer);
|
Py_DECREF(stringcontainer);
|
||||||
@ -186,6 +184,9 @@ class PyObjectConverter : public Converter<PyObject *> {
|
|||||||
stringcontainer = PyUnicode_AsUTF8String(o);
|
stringcontainer = PyUnicode_AsUTF8String(o);
|
||||||
return PyBytes_AsString(stringcontainer);
|
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 ListIterator<PyObject *> *list(PyObject *&o) { return new PyObjectListIterator(o); }
|
||||||
virtual DictIterator<PyObject *> *dict(PyObject *&o) { return new PyObjectDictIterator(o);; }
|
virtual DictIterator<PyObject *> *dict(PyObject *&o) { return new PyObjectDictIterator(o);; }
|
||||||
virtual ConverterDate date(PyObject *&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 * fromFloating(double v) { return PyFloat_FromDouble(v); }
|
||||||
virtual PyObject * fromBoolean(bool v) { return PyBool_FromLong((long)v); }
|
virtual PyObject * fromBoolean(bool v) { return PyBool_FromLong((long)v); }
|
||||||
virtual PyObject * fromString(const char *v) { return PyUnicode_FromString(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 * 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 * fromTime(ConverterTime v) { return PyTime_FromTime(v.h, v.m, v.s, 1000 * v.ms); }
|
||||||
virtual PyObject * fromDateTime(ConverterDateTime v) {
|
virtual PyObject * fromDateTime(ConverterDateTime v) {
|
||||||
|
@ -134,6 +134,8 @@ class QVariantConverter : public Converter<QVariant> {
|
|||||||
return FLOATING;
|
return FLOATING;
|
||||||
case QMetaType::QString:
|
case QMetaType::QString:
|
||||||
return STRING;
|
return STRING;
|
||||||
|
case QMetaType::QByteArray:
|
||||||
|
return BYTES;
|
||||||
case QMetaType::QDate:
|
case QMetaType::QDate:
|
||||||
return DATE;
|
return DATE;
|
||||||
case QMetaType::QTime:
|
case QMetaType::QTime:
|
||||||
@ -213,6 +215,10 @@ class QVariantConverter : public Converter<QVariant> {
|
|||||||
return stringstorage.constData();
|
return stringstorage.constData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual QByteArray bytes(QVariant &v) {
|
||||||
|
return stringstorage = v.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
virtual PyObjectRef pyObject(QVariant &v) {
|
virtual PyObjectRef pyObject(QVariant &v) {
|
||||||
return v.value<PyObjectRef>();
|
return v.value<PyObjectRef>();
|
||||||
}
|
}
|
||||||
@ -233,6 +239,7 @@ class QVariantConverter : public Converter<QVariant> {
|
|||||||
virtual QVariant fromFloating(double v) { return QVariant(v); }
|
virtual QVariant fromFloating(double v) { return QVariant(v); }
|
||||||
virtual QVariant fromBoolean(bool 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 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 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 fromTime(ConverterTime v) { return QVariant(QTime(v.h, v.m, v.s, v.ms)); }
|
||||||
virtual QVariant fromDateTime(ConverterDateTime v) {
|
virtual QVariant fromDateTime(ConverterDateTime v) {
|
||||||
|
@ -62,6 +62,14 @@ test_converter_for(Converter<V> *conv)
|
|||||||
QVERIFY(conv->type(v) == Converter<V>::STRING);
|
QVERIFY(conv->type(v) == Converter<V>::STRING);
|
||||||
QVERIFY(strcmp(conv->string(v), "Hello World") == 0);
|
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 */
|
/* Convert from/to List */
|
||||||
ListBuilder<V> *builder = conv->newList();
|
ListBuilder<V> *builder = conv->newList();
|
||||||
v = conv->fromInteger(444);
|
v = conv->fromInteger(444);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user