aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2011-09-16 14:27:32 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:17:15 -0300
commitfd5dd85d0d0dcc4faaf3a1ca5bd6a5df857f7844 (patch)
treecbbb5e773dc62b14e5e94e9c6217075ae6a2c41d /tests
parent9785f258d93b4976872a17fbf61bd27a4c4123cd (diff)
Port libsample typesystem and glue codes to Python 3.
Diffstat (limited to 'tests')
-rw-r--r--tests/samplebinding/bytearray_conversions.h4
-rw-r--r--tests/samplebinding/handle_conversions.h13
-rw-r--r--tests/samplebinding/simplefile_glue.cpp2
-rw-r--r--tests/samplebinding/typesystem_sample.xml68
4 files changed, 49 insertions, 38 deletions
diff --git a/tests/samplebinding/bytearray_conversions.h b/tests/samplebinding/bytearray_conversions.h
index 79a070d7..c37b28a9 100644
--- a/tests/samplebinding/bytearray_conversions.h
+++ b/tests/samplebinding/bytearray_conversions.h
@@ -17,8 +17,8 @@ inline ByteArray Converter<ByteArray>::toCpp(PyObject* pyObj)
return ByteArray();
else if (PyObject_TypeCheck(pyObj, SbkType<ByteArray>()))
return *Converter<ByteArray*>::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<ByteArray>::toCpp(pyObj);
}
inline PyObject* Converter<ByteArray>::toPython(const ByteArray& cppObj)
diff --git a/tests/samplebinding/handle_conversions.h b/tests/samplebinding/handle_conversions.h
index 2c600807..a6e56135 100644
--- a/tests/samplebinding/handle_conversions.h
+++ b/tests/samplebinding/handle_conversions.h
@@ -13,7 +13,11 @@ struct Converter<HANDLE>
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<HANDLE>
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 f427a51b..e06129f3 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 2fef355f..1dae83e2 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -125,13 +125,13 @@
<modify-argument index="1">
<replace-type modified-type="const char*"/>
<conversion-rule class="native">
- unsigned char* %out = reinterpret_cast&lt;unsigned char*>(PyString_AS_STRING(%PYARG_1));
+ unsigned char* %out = reinterpret_cast&lt;unsigned char*>(PyBytes_AS_STRING(%PYARG_1));
</conversion-rule>
</modify-argument>
<modify-argument index="2">
<remove-argument/>
<conversion-rule class="native">
- unsigned int %out = static_cast&lt;unsigned int>(PyString_GET_SIZE(%PYARG_1));
+ unsigned int %out = static_cast&lt;unsigned int>(PyBytes_GET_SIZE(%PYARG_1));
</conversion-rule>
</modify-argument>
</modify-function>
@@ -198,7 +198,7 @@
<object-type name="ModifiedConstructor">
<modify-function signature="ModifiedConstructor(int)">
<modify-argument index="1">
- <replace-type modified-type="PyString"/>
+ <replace-type modified-type="PyBytes"/>
</modify-argument>
<inject-code class='target' position='beginning'>
%0 = new %FUNCTION_NAME(atoi(%CONVERTTOCPP[const char*](%PYARG_1)));
@@ -1110,7 +1110,7 @@
Tested in InjectCodeTest.testTypeNativeBeginning_TypeTargetBeginning:
-->
<inject-code class="native" position="beginning">
- 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."); }
</inject-code>
<!--
Register our tp_str class using another inject code
@@ -1126,8 +1126,8 @@
%1 += 1;
</inject-code>
<inject-code class="target" position="end">
- PyObject* tmp = PyString_FromString("end");
- PyString_Concat(&amp;%PYARG_0, tmp);
+ PyObject* tmp = PyBytes_FromString("end");
+ PyBytes_Concat(&amp;%PYARG_0, tmp);
Py_DECREF(tmp);
</inject-code>
</modify-function>
@@ -1135,8 +1135,8 @@
<!-- Tested in InjectCodeTest.testFunctionTargetBeginning -->
<modify-function signature="simpleMethod2()">
<inject-code class="target" position="end">
- PyObject* tmp = PyString_FromString("end");
- PyString_Concat(&amp;%PYARG_0, tmp);
+ PyObject* tmp = PyBytes_FromString("end");
+ PyBytes_Concat(&amp;%PYARG_0, tmp);
Py_DECREF(tmp);
</inject-code>
</modify-function>
@@ -1192,13 +1192,13 @@
<value-type name="SortedOverload">
<add-function signature="overload(PyObject *)" return-type="const char *">
<inject-code class="target" position="beginning">
- return PyString_FromString("PyObject");
+ return PyBytes_FromString("PyObject");
</inject-code>
</add-function>
<add-function signature="overloadDeep(int, PyObject *)" return-type="const char *">
<inject-code class="target" position="beginning">
- return PyString_FromString("PyObject");
+ return PyBytes_FromString("PyObject");
</inject-code>
</add-function>
<modify-function signature="pyObjOverload(unsigned char*, int)">
@@ -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);
</inject-code>
</add-function>
<add-function signature="__repr__" return-type="PyObject*">
@@ -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("&lt;Point object at %p: (%d.%d, %d.%d)&gt;", %CPPSELF, x1, x2, y1, y2);
+ %PYARG_0 = PyBytes_FromFormat("&lt;Point object at %p: (%d.%d, %d.%d)&gt;", %CPPSELF, x1, x2, y1, y2);
</inject-code>
</add-function>
@@ -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);
</inject-code>
</add-function>
<add-function signature="__repr__" return-type="PyObject*">
@@ -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("&lt;PointF object at %p: (%d.%d, %d.%d)&gt;", %CPPSELF, x1, x2, y1, y2);
+ %PYARG_0 = PyBytes_FromFormat("&lt;PointF object at %p: (%d.%d, %d.%d)&gt;", %CPPSELF, x1, x2, y1, y2);
</inject-code>
</add-function>
@@ -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 &lt; 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 @@
<value-type name="Str" hash-function="strHash">
<add-function signature="__str__" return-type="PyObject*">
<inject-code class="target" position="beginning">
- %PYARG_0 = PyString_FromString(%CPPSELF.cstring());
+ %PYARG_0 = PyBytes_FromString(%CPPSELF.cstring());
</inject-code>
</add-function>
<add-function signature="__len__" >
@@ -1499,7 +1499,7 @@
char res[2];
res[0] = %CPPSELF.get_char(_i);
res[1] = 0;
- return PyString_FromString(res);
+ return PyBytes_FromString(res);
}
</inject-code>
</add-function>
@@ -1537,10 +1537,10 @@
<modify-function signature="ByteArray(const char*)">
<!-- Keep \x00 bytes passed in Python strings. -->
<modify-argument index="1">
- <replace-type modified-type="PyString"/>
+ <replace-type modified-type="PyBytes"/>
</modify-argument>
<inject-code class="target" position="beginning">
- %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));
</inject-code>
</modify-function>
@@ -1555,7 +1555,7 @@
<modify-function signature="data() const">
<inject-code class="target" position="beginning">
- %PYARG_0 = PyString_FromStringAndSize(%CPPSELF.%FUNCTION_NAME(), %CPPSELF.size());
+ %PYARG_0 = PyBytes_FromStringAndSize(%CPPSELF.%FUNCTION_NAME(), %CPPSELF.size());
</inject-code>
</modify-function>
@@ -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);
}
</inject-code>
@@ -1585,15 +1585,15 @@
<inject-code>
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);
}
</inject-code>
</add-function>
- <add-function signature="operator+(PyString,ByteArray)">
+ <add-function signature="operator+(PyBytes,ByteArray)">
<inject-code>
- 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);
</inject-code>
@@ -1603,16 +1603,16 @@
<inject-code class="target" position="beginning">
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());
</inject-code>
</add-function>
- <add-function signature="__str__" return-type="PyString">
+ <add-function signature="__str__" return-type="PyBytes">
<inject-code class="target" position="beginning">
- %PYARG_0 = PyString_FromStringAndSize(%CPPSELF.data(), %CPPSELF.size());
+ %PYARG_0 = PyBytes_FromStringAndSize(%CPPSELF.data(), %CPPSELF.size());
</inject-code>
</add-function>
@@ -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);
}
</inject-code>
</add-function>
@@ -1703,7 +1703,7 @@
<value-type name="Echo">
<add-function signature="echo(const char *)" return-type="PyObject*">
<inject-code class="target" position="beginning">
- %PYARG_0 = PyString_FromString(%1);
+ %PYARG_0 = PyBytes_FromString(%1);
</inject-code>
</add-function>
<add-function signature="operator>(int)">
@@ -1733,11 +1733,11 @@
</modify-function>
</value-type>
- <add-function signature="multiplyString(PyString*, unsigned int)" return-type="const char*">
+ <add-function signature="multiplyString(PyBytes*, unsigned int)" return-type="const char*">
<inject-code class="target" position="beginning">
- %PYARG_0 = PyString_FromString("");
+ %PYARG_0 = PyBytes_FromString("");
for (unsigned int i = 0; i &lt; %2; ++i)
- PyString_Concat(&amp;%PYARG_0, %PYARG_1);
+ PyBytes_Concat(&amp;%PYARG_0, %PYARG_1);
</inject-code>
</add-function>