From fd5dd85d0d0dcc4faaf3a1ca5bd6a5df857f7844 Mon Sep 17 00:00:00 2001 From: Hugo Parente Lima Date: Fri, 16 Sep 2011 14:27:32 -0300 Subject: Port libsample typesystem and glue codes to Python 3. --- tests/samplebinding/bytearray_conversions.h | 4 +- tests/samplebinding/handle_conversions.h | 13 +++++- tests/samplebinding/simplefile_glue.cpp | 2 +- tests/samplebinding/typesystem_sample.xml | 68 ++++++++++++++--------------- 4 files changed, 49 insertions(+), 38 deletions(-) (limited to 'tests') diff --git a/tests/samplebinding/bytearray_conversions.h b/tests/samplebinding/bytearray_conversions.h index 79a070d7b..c37b28a99 100644 --- a/tests/samplebinding/bytearray_conversions.h +++ b/tests/samplebinding/bytearray_conversions.h @@ -17,8 +17,8 @@ inline ByteArray Converter::toCpp(PyObject* pyObj) return ByteArray(); else if (PyObject_TypeCheck(pyObj, SbkType())) return *Converter::toCpp(pyObj); - else if (PyString_Check(pyObj)) - return ByteArray(PyString_AS_STRING(pyObj), PyString_GET_SIZE(pyObj)); + else if (PyBytes_Check(pyObj)) + return ByteArray(PyBytes_AS_STRING(pyObj), PyBytes_GET_SIZE(pyObj)); return ValueTypeConverter::toCpp(pyObj); } inline PyObject* Converter::toPython(const ByteArray& cppObj) diff --git a/tests/samplebinding/handle_conversions.h b/tests/samplebinding/handle_conversions.h index 2c6008077..a6e561359 100644 --- a/tests/samplebinding/handle_conversions.h +++ b/tests/samplebinding/handle_conversions.h @@ -13,7 +13,11 @@ struct Converter static inline bool isConvertible(PyObject* pyObj) { +#ifdef IS_PY3K + return PyCapsule_CheckExact(pyObj); +#else return PyCObject_Check(pyObj); +#endif } static inline PyObject* toPython(void* cppobj) @@ -24,13 +28,20 @@ struct Converter static inline PyObject* toPython(HANDLE cppobj) { +#ifdef IS_PY3K + return PyCapsule_New(cppobj, 0, 0); +#else return PyCObject_FromVoidPtr(cppobj, 0); +#endif } static inline HANDLE toCpp(PyObject* pyobj) { - +#ifdef IS_PY3K + return (HANDLE) PyCapsule_GetPointer(pyobj, 0); +#else return (HANDLE) PyCObject_AsVoidPtr(pyobj); +#endif } }; } diff --git a/tests/samplebinding/simplefile_glue.cpp b/tests/samplebinding/simplefile_glue.cpp index f427a51b3..e06129f3e 100644 --- a/tests/samplebinding/simplefile_glue.cpp +++ b/tests/samplebinding/simplefile_glue.cpp @@ -1,5 +1,5 @@ if (!%CPPSELF.%FUNCTION_NAME()) { - PyObject* error_msg = PyString_FromFormat( + PyObject* error_msg = PyBytes_FromFormat( "Could not open file: \"%s\"", %CPPSELF->filename()); PyErr_SetObject(PyExc_IOError, error_msg); return 0; diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 2fef355fc..1dae83e2d 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -125,13 +125,13 @@ - unsigned char* %out = reinterpret_cast<unsigned char*>(PyString_AS_STRING(%PYARG_1)); + unsigned char* %out = reinterpret_cast<unsigned char*>(PyBytes_AS_STRING(%PYARG_1)); - unsigned int %out = static_cast<unsigned int>(PyString_GET_SIZE(%PYARG_1)); + unsigned int %out = static_cast<unsigned int>(PyBytes_GET_SIZE(%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 PyString_FromString("Hi! I'm the inject code dummy class."); } + PyObject* InjectCode_tpstr(PyObject*) { return PyBytes_FromString("Hi! I'm the inject code dummy class."); } - PyObject* tmp = PyString_FromString("end"); - PyString_Concat(&%PYARG_0, tmp); + PyObject* tmp = PyBytes_FromString("end"); + PyBytes_Concat(&%PYARG_0, tmp); Py_DECREF(tmp); @@ -1192,13 +1192,13 @@ - return PyString_FromString("PyObject"); + return PyBytes_FromString("PyObject"); - return PyString_FromString("PyObject"); + return PyBytes_FromString("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 = PyString_FromFormat("Point(%d.%d, %d.%d)", x1, x2, y1, y2); + %PYARG_0 = PyBytes_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 = PyString_FromFormat("<Point object at %p: (%d.%d, %d.%d)>", %CPPSELF, x1, x2, y1, y2); + %PYARG_0 = PyBytes_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 = PyString_FromFormat("PointF(%d.%d, %d.%d)", x1, x2, y1, y2); + %PYARG_0 = PyBytes_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 = PyString_FromFormat("<PointF object at %p: (%d.%d, %d.%d)>", %CPPSELF, x1, x2, y1, y2); + %PYARG_0 = PyBytes_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 (!PyString_Check(PySequence_Fast_GET_ITEM(strList.object(), line))) { + if (!PyBytes_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 = PyString_FromString(%CPPSELF.cstring()); + %PYARG_0 = PyBytes_FromString(%CPPSELF.cstring()); @@ -1499,7 +1499,7 @@ char res[2]; res[0] = %CPPSELF.get_char(_i); res[1] = 0; - return PyString_FromString(res); + return PyBytes_FromString(res); } @@ -1537,10 +1537,10 @@ - + - %0 = new %TYPE(PyString_AS_STRING(%PYARG_1), PyString_GET_SIZE(%PYARG_1)); + %0 = new %TYPE(PyBytes_AS_STRING(%PYARG_1), PyBytes_GET_SIZE(%PYARG_1)); @@ -1555,7 +1555,7 @@ - %PYARG_0 = PyString_FromStringAndSize(%CPPSELF.%FUNCTION_NAME(), %CPPSELF.size()); + %PYARG_0 = PyBytes_FromStringAndSize(%CPPSELF.%FUNCTION_NAME(), %CPPSELF.size()); @@ -1576,7 +1576,7 @@ Shiboken::AutoDecRef data(PyUnicode_AsASCIIString(%PYARG_1)); if (!data.isNull()) { ByteArray ba(*%CPPSELF); - ba.append(PyString_AS_STRING(data.object()), PyString_GET_SIZE(data.object())); + ba.append(PyBytes_AS_STRING(data.object()), PyBytes_GET_SIZE(data.object())); %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); } @@ -1585,15 +1585,15 @@ Shiboken::AutoDecRef data(PyUnicode_AsASCIIString(%PYARG_1)); if (!data.isNull()) { - ByteArray ba(PyString_AS_STRING(data.object()), PyString_GET_SIZE(data.object())); + ByteArray ba(PyBytes_AS_STRING(data.object()), PyBytes_GET_SIZE(data.object())); ba.append(*%CPPSELF); %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); } - + - ByteArray ba(PyString_AS_STRING(%PYARG_1), PyString_GET_SIZE(%PYARG_1)); + ByteArray ba(PyBytes_AS_STRING(%PYARG_1), PyBytes_GET_SIZE(%PYARG_1)); ba = ba + *%CPPSELF; %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); @@ -1603,16 +1603,16 @@ ByteArray ba(((PyObject*)%PYSELF)->ob_type->tp_name); ba += '('; - Shiboken::AutoDecRef contents(PyObject_Repr(PyString_FromStringAndSize(%CPPSELF.data(), %CPPSELF.size()))); - ba += PyString_AS_STRING(contents.object()); + Shiboken::AutoDecRef contents(PyObject_Repr(PyBytes_FromStringAndSize(%CPPSELF.data(), %CPPSELF.size()))); + ba += PyBytes_AS_STRING(contents.object()); ba += ")"; - %PYARG_0 = PyString_FromStringAndSize(ba.data(), ba.size()); + %PYARG_0 = PyBytes_FromStringAndSize(ba.data(), ba.size()); - + - %PYARG_0 = PyString_FromStringAndSize(%CPPSELF.data(), %CPPSELF.size()); + %PYARG_0 = PyBytes_FromStringAndSize(%CPPSELF.data(), %CPPSELF.size()); @@ -1630,7 +1630,7 @@ char res[2]; res[0] = %CPPSELF.at(_i); res[1] = 0; - return PyString_FromStringAndSize(res, 1); + return PyBytes_FromStringAndSize(res, 1); } @@ -1703,7 +1703,7 @@ - %PYARG_0 = PyString_FromString(%1); + %PYARG_0 = PyBytes_FromString(%1); @@ -1733,11 +1733,11 @@ - + - %PYARG_0 = PyString_FromString(""); + %PYARG_0 = PyBytes_FromString(""); for (unsigned int i = 0; i < %2; ++i) - PyString_Concat(&%PYARG_0, %PYARG_1); + PyBytes_Concat(&%PYARG_0, %PYARG_1); -- cgit v1.2.3