From 52fdebcb0b3822180a374ee821f3503d746656c1 Mon Sep 17 00:00:00 2001 From: Renato Filho Date: Wed, 28 Sep 2011 15:48:27 -0300 Subject: Update string usage to new Shiboken::String API. --- tests/samplebinding/bytearray_conversions.h | 16 ++++-- tests/samplebinding/typesystem_sample.xml | 75 +++++++++++++++++------------ 2 files changed, 57 insertions(+), 34 deletions(-) (limited to 'tests') diff --git a/tests/samplebinding/bytearray_conversions.h b/tests/samplebinding/bytearray_conversions.h index c37b28a99..0fd9a2b7d 100644 --- a/tests/samplebinding/bytearray_conversions.h +++ b/tests/samplebinding/bytearray_conversions.h @@ -8,17 +8,25 @@ inline bool Converter::isConvertible(PyObject* pyObj) if (ValueTypeConverter::isConvertible(pyObj)) return true; SbkObjectType* shiboType = reinterpret_cast(SbkType()); - return Shiboken::Converter::checkType(pyObj) + + return Shiboken::Converter::isConvertible(pyObj) + || PyBytes_Check(pyObj) || (ObjectType::isExternalConvertible(shiboType, pyObj)); } inline ByteArray Converter::toCpp(PyObject* pyObj) { - if (pyObj == Py_None) + if (pyObj == Py_None) { return ByteArray(); - else if (PyObject_TypeCheck(pyObj, SbkType())) + } else if (PyObject_TypeCheck(pyObj, SbkType())) { return *Converter::toCpp(pyObj); - else if (PyBytes_Check(pyObj)) + } else if (PyBytes_Check(pyObj)) { return ByteArray(PyBytes_AS_STRING(pyObj), PyBytes_GET_SIZE(pyObj)); + } else if (PyUnicode_Check(pyObj)) { + Shiboken::AutoDecRef data(PyUnicode_AsASCIIString(pyObj)); + return ByteArray(PyBytes_AsString(data.object()), PyBytes_GET_SIZE(data.object())); + } else if (Shiboken::String::check(pyObj)) { + return ByteArray(Shiboken::String::toCString(pyObj)); + } return ValueTypeConverter::toCpp(pyObj); } inline PyObject* Converter::toPython(const ByteArray& cppObj) diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index a2b041a41..1c5664272 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -125,13 +125,13 @@ - unsigned char* %out = reinterpret_cast<unsigned char*>(PyBytes_AS_STRING(%PYARG_1)); + const unsigned char* %out = reinterpret_cast<const unsigned char*>(Shiboken::String::toCString(%PYARG_1)); - unsigned int %out = static_cast<unsigned int>(PyBytes_GET_SIZE(%PYARG_1)); + unsigned int %out = static_cast<unsigned int>(Shiboken::String::len(%PYARG_1)); @@ -198,7 +198,7 @@ - + %0 = new %FUNCTION_NAME(atoi(%CONVERTTOCPP[const char*](%PYARG_1))); @@ -1110,7 +1110,7 @@ Tested in InjectCodeTest.testTypeNativeBeginning_TypeTargetBeginning: --> - PyObject* InjectCode_tpstr(PyObject*) { return PyBytes_FromString("Hi! I'm the inject code dummy class."); } + PyObject* InjectCode_tpstr(PyObject*) { return Shiboken::String::fromCString("Hi! I'm the inject code dummy class."); } - PyObject* tmp = PyBytes_FromString("end"); - PyBytes_Concat(&%PYARG_0, tmp); + PyObject* tmp = Shiboken::String::fromCString("end"); + Shiboken::String::concat(&%PYARG_0, tmp); Py_DECREF(tmp); @@ -1192,13 +1192,13 @@ - return PyBytes_FromString("PyObject"); + return Shiboken::String::fromCString("PyObject"); - return PyBytes_FromString("PyObject"); + return Shiboken::String::fromCString("PyObject"); @@ -1219,7 +1219,7 @@ int x2 = ((int) (%CPPSELF.x() * 100)) - (x1 * 100); int y1 = (int) %CPPSELF.y(); int y2 = ((int) (%CPPSELF.y() * 100)) - (y1 * 100); - %PYARG_0 = SBK_PyString_FromFormat("Point(%d.%d, %d.%d)", x1, x2, y1, y2); + %PYARG_0 = Shiboken::String::fromFormat("Point(%d.%d, %d.%d)", x1, x2, y1, y2); @@ -1228,7 +1228,7 @@ int x2 = ((int) (%CPPSELF.x() * 10)) - (x1 * 10); int y1 = (int) %CPPSELF.y(); int y2 = ((int) (%CPPSELF.y() * 10)) - (y1 * 10); - %PYARG_0 = SBK_PyString_FromFormat("<Point object at %p: (%d.%d, %d.%d)>", %CPPSELF, x1, x2, y1, y2); + %PYARG_0 = Shiboken::String::fromFormat("<Point object at %p: (%d.%d, %d.%d)>", %CPPSELF, x1, x2, y1, y2); @@ -1282,7 +1282,7 @@ int x2 = ((int) (%CPPSELF.x() * 100)) - (x1 * 100); int y1 = (int) %CPPSELF.y(); int y2 = ((int) (%CPPSELF.y() * 100)) - (y1 * 100); - %PYARG_0 = PyBytes_FromFormat("PointF(%d.%d, %d.%d)", x1, x2, y1, y2); + %PYARG_0 = Shiboken::String::fromFormat("PointF(%d.%d, %d.%d)", x1, x2, y1, y2); @@ -1291,7 +1291,7 @@ int x2 = ((int) (%CPPSELF.x() * 10)) - (x1 * 10); int y1 = (int) %CPPSELF.y(); int y2 = ((int) (%CPPSELF.y() * 10)) - (y1 * 10); - %PYARG_0 = PyBytes_FromFormat("<PointF object at %p: (%d.%d, %d.%d)>", %CPPSELF, x1, x2, y1, y2); + %PYARG_0 = Shiboken::String::fromFormat("<PointF object at %p: (%d.%d, %d.%d)>", %CPPSELF, x1, x2, y1, y2); @@ -1423,7 +1423,7 @@ Shiboken::AutoDecRef strList(PySequence_Fast(%PYARG_1, "The argument must be a sequence.")); int lineCount = PySequence_Fast_GET_SIZE(strList.object()); for (int line = 0; line < lineCount; ++line) { - if (!PyBytes_Check(PySequence_Fast_GET_ITEM(strList.object(), line))) { + if (!Shiboken::String::check(PySequence_Fast_GET_ITEM(strList.object(), line))) { PyErr_SetString(PyExc_TypeError, "The argument must be a sequence of strings."); break; } @@ -1482,7 +1482,7 @@ - %PYARG_0 = SBK_PyString_FromCString(%CPPSELF.cstring()); + %PYARG_0 = Shiboken::String::fromCString(%CPPSELF.cstring()); @@ -1499,7 +1499,7 @@ char res[2]; res[0] = %CPPSELF.get_char(_i); res[1] = 0; - return PyBytes_FromString(res); + return Shiboken::String::fromCString(res); } @@ -1540,7 +1540,16 @@ - %0 = new %TYPE(PyBytes_AS_STRING(%PYARG_1), PyBytes_GET_SIZE(%PYARG_1)); + PyObject* data = 0; + if (PyUnicode_CheckExact(%PYARG_1)) { + data = PyUnicode_AsASCIIString(%PYARG_1); + } else { + data = %PYARG_1; + Py_INCREF(data); + } + + %0 = new %TYPE(PyBytes_AsString(data), PyBytes_GET_SIZE(data)); + Py_DECREF(data); @@ -1576,7 +1585,7 @@ Shiboken::AutoDecRef data(PyUnicode_AsASCIIString(%PYARG_1)); if (!data.isNull()) { ByteArray ba(*%CPPSELF); - ba.append(PyBytes_AS_STRING(data.object()), PyBytes_GET_SIZE(data.object())); + ba.append(PyBytes_AsString(data.object()), PyBytes_GET_SIZE(data.object())); %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); } @@ -1585,7 +1594,7 @@ Shiboken::AutoDecRef data(PyUnicode_AsASCIIString(%PYARG_1)); if (!data.isNull()) { - ByteArray ba(PyBytes_AS_STRING(data.object()), PyBytes_GET_SIZE(data.object())); + ByteArray ba(PyBytes_AsString(data.object()), PyBytes_GET_SIZE(data.object())); ba.append(*%CPPSELF); %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); } @@ -1593,12 +1602,18 @@ - ByteArray ba(PyBytes_AS_STRING(%PYARG_1), PyBytes_GET_SIZE(%PYARG_1)); - ba = ba + *%CPPSELF; - %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); + ByteArray ba(PyBytes_AsString(%PYARG_1), PyBytes_GET_SIZE(%PYARG_1)); + ba = ba + *%CPPSELF; + %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); + + + + + ByteArray ba(PyBytes_AsString(%PYARG_1), PyBytes_GET_SIZE(%PYARG_1)); + ba.append(*%CPPSELF); + %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); - ByteArray ba(((PyObject*)%PYSELF)->ob_type->tp_name); @@ -1610,9 +1625,9 @@ - + - %PYARG_0 = PyBytes_FromStringAndSize(%CPPSELF.data(), %CPPSELF.size()); + %PYARG_0 = Shiboken::String::fromStringAndSize(%CPPSELF.data(), %CPPSELF.size()); @@ -1703,7 +1718,7 @@ - %PYARG_0 = PyBytes_FromString(%1); + %PYARG_0 = Shiboken::String::fromCString(%1); @@ -1733,11 +1748,11 @@ - + - %PYARG_0 = PyBytes_FromString(""); + %PYARG_0 = Shiboken::String::fromCString(""); for (unsigned int i = 0; i < %2; ++i) - PyBytes_Concat(&%PYARG_0, %PYARG_1); + Shiboken::String::concat(&%PYARG_0, %PYARG_1); -- cgit v1.2.3