aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/tests
diff options
context:
space:
mode:
authorCristián Maureira-Fredes <cristian.maureira-fredes@qt.io>2024-11-07 09:36:21 +0100
committerCristián Maureira-Fredes <Cristian.Maureira-Fredes@qt.io>2024-11-08 08:33:38 +0100
commitfb13a26a76eba415e743d119d5d2782c607fee2f (patch)
tree1547b255a7e4afa4e328549d0e613e5b25a45b84 /sources/shiboken6/tests
parent2b1dbe1b6610027dbf5f521ae9c7b56f3a343160 (diff)
limited api: replace PySequence_Fast_GET_ITEM by PySequence_GetItem
PySequence_Fast_GET_ITEM is defined as: (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i)) and when using the Limited API we re-define the _GET_ITEM macro to be the _GetItem function, and considering this is our standard use case, the macro could be replaced directly by the function. However, the function returns a new reference, so we need to manually drecrease a reference after the usage, to avoid reference counting issues. Change-Id: If361e80b9e40b033e009ad46b2b9430e5b4c8eaa Pick-to: 6.8 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/shiboken6/tests')
-rw-r--r--sources/shiboken6/tests/samplebinding/typesystem_sample.xml47
-rw-r--r--sources/shiboken6/tests/smartbinding/typesystem_smart.xml2
2 files changed, 30 insertions, 19 deletions
diff --git a/sources/shiboken6/tests/samplebinding/typesystem_sample.xml b/sources/shiboken6/tests/samplebinding/typesystem_sample.xml
index ca646a846..92a886b1e 100644
--- a/sources/shiboken6/tests/samplebinding/typesystem_sample.xml
+++ b/sources/shiboken6/tests/samplebinding/typesystem_sample.xml
@@ -226,8 +226,8 @@
<add-conversion type="PySequence">
%OUTTYPE&amp; list = %out;
Shiboken::AutoDecRef seq(PySequence_Fast(%in, 0));
- for (int i = 0; i &lt; PySequence_Fast_GET_SIZE(seq.object()); i++) {
- PyObject* pyItem = PySequence_Fast_GET_ITEM(seq.object(), i);
+ for (int i = 0; i &lt; PySequence_Size(seq.object()); i++) {
+ Shiboken::AutoDecRef pyItem(PySequence_GetItem(seq.object(), i));
PStr cppItem = %CONVERTTOCPP[PStr](pyItem);
list.push_back(cppItem);
}
@@ -1129,8 +1129,10 @@
<replace-type modified-type="PySequence" />
<conversion-rule class="native">
Shiboken::AutoArrayPointer&lt;Point&gt; %out(%1);
- for (Py_ssize_t i = 0; i &lt; %1; ++i)
- %out[i] = %CONVERTTOCPP[Point](PySequence_Fast_GET_ITEM(%PYARG_1, i));
+ for (Py_ssize_t i = 0; i &lt; %1; ++i) {
+ Shiboken::AutoDecRef _obj(PySequence_GetItem(%PYARG_1, i));
+ %out[i] = %CONVERTTOCPP[Point](_obj);
+ }
</conversion-rule>
</modify-argument>
</modify-function>
@@ -1385,18 +1387,22 @@
</template>
<template name="fix_native_return_int*,int*,int*,int*">
PyObject* _obj = %PYARG_0;
+ Shiboken::AutoDecRef _obj0(PySequence_GetItem(_obj, 0));
+ Shiboken::AutoDecRef _obj1(PySequence_GetItem(_obj, 1));
+ Shiboken::AutoDecRef _obj2(PySequence_GetItem(_obj, 2));
+ Shiboken::AutoDecRef _obj3(PySequence_GetItem(_obj, 3));
if (!PySequence_Check(_obj)
|| PySequence_Fast_GET_SIZE(_obj) != 4
- || !PyNumber_Check(PySequence_Fast_GET_ITEM(_obj, 0))
- || !PyNumber_Check(PySequence_Fast_GET_ITEM(_obj, 1))
- || !PyNumber_Check(PySequence_Fast_GET_ITEM(_obj, 2))
- || !PyNumber_Check(PySequence_Fast_GET_ITEM(_obj, 3))) {
+ || !PyNumber_Check(_obj0)
+ || !PyNumber_Check(_obj1)
+ || !PyNumber_Check(_obj2)
+ || !PyNumber_Check(_obj3)) {
PyErr_SetString(PyExc_TypeError, "Sequence of 4 numbers expected");
} else {
- *%1 = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(_obj, 0));
- *%2 = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(_obj, 1));
- *%3 = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(_obj, 2));
- *%4 = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(_obj, 3));
+ *%1 = %CONVERTTOCPP[int](_obj0);
+ *%2 = %CONVERTTOCPP[int](_obj1);
+ *%3 = %CONVERTTOCPP[int](_obj2);
+ *%4 = %CONVERTTOCPP[int](_obj3);
}
</template>
<modify-function signature="getMargins(int*,int*,int*,int*)const">
@@ -1586,9 +1592,10 @@
const auto numItems = PySequence_Size(%PYARG_1);
Shiboken::AutoArrayPointer&lt;int&gt; %out(numItems);
for (Py_ssize_t i = 0; i &lt; numItems; ++i) {
- if (%CHECKTYPE[int](PySequence_Fast_GET_ITEM(%PYARG_1, i)))
- %out[i] = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(%PYARG_1, i));
- else if (%ISCONVERTIBLE[int](PySequence_Fast_GET_ITEM(%PYARG_1, i)))
+ Shiboken::AutoDecRef _obj(PySequence_GetItem(%PYARG_1, i));
+ if (%CHECKTYPE[int](_obj))
+ %out[i] = %CONVERTTOCPP[int](_obj);
+ else if (%ISCONVERTIBLE[int](_obj))
%out[i] = -1;
else
%out[i] = -2;
@@ -1612,8 +1619,10 @@
<inject-code class="target" position="beginning">
int numItems = PySequence_Size(%PYARG_1);
int *cppItems = new int[numItems];
- for (int i = 0; i &lt; numItems; i++)
- cppItems[i] = %CONVERTTOCPP[int](PySequence_GetItem(%PYARG_1, i));
+ for (int i = 0; i &lt; numItems; i++) {
+ Shiboken::AutoDecRef _obj(PySequence_GetItem(%PYARG_1, i));
+ cppItems[i] = %CONVERTTOCPP[int](_obj);
+ }
%RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(numItems, cppItems);
%PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0);
delete[] cppItems;
@@ -1953,7 +1962,9 @@
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 (!Shiboken::String::check(PySequence_Fast_GET_ITEM(strList.object(), line))) {
+ Shiboken::AutoDecRef _obj(PySequence_GetItem(strList.object(), line));
+ bool isString = Shiboken::String::check(_obj);
+ if (!isString) {
PyErr_SetString(PyExc_TypeError, "The argument must be a sequence of strings.");
break;
}
diff --git a/sources/shiboken6/tests/smartbinding/typesystem_smart.xml b/sources/shiboken6/tests/smartbinding/typesystem_smart.xml
index e479e4ddf..fd44f4f6d 100644
--- a/sources/shiboken6/tests/smartbinding/typesystem_smart.xml
+++ b/sources/shiboken6/tests/smartbinding/typesystem_smart.xml
@@ -12,7 +12,7 @@
<template name="pyseq_to_cpplist_convertion">
Shiboken::AutoDecRef seq(PySequence_Fast(%in, 0));
for (int i = 0, size = PySequence_Fast_GET_SIZE(seq.object()); i &lt; size; ++i) {
- PyObject* pyItem = PySequence_Fast_GET_ITEM(seq.object(), i);
+ Shiboken::AutoDecRef pyItem(PySequence_GetItem(seq.object(), i));
%OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem);
%out.push_back(cppItem);
}