aboutsummaryrefslogtreecommitdiffstats
path: root/tests/samplebinding
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-08-08 03:29:21 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:18:26 -0300
commitb8bd47404fd3860bdb282750a1e5919921ca80df (patch)
tree3e2c6623379578c3e4033bdab46d45ee2d3af028 /tests/samplebinding
parentdf0378d142c55fe63b89735e3dd8f14988893a3e (diff)
Implemented new type converters, but only for wrapper types.
Diffstat (limited to 'tests/samplebinding')
-rw-r--r--tests/samplebinding/injectcode_test.py13
-rw-r--r--tests/samplebinding/typesystem_sample.xml63
2 files changed, 70 insertions, 6 deletions
diff --git a/tests/samplebinding/injectcode_test.py b/tests/samplebinding/injectcode_test.py
index 2941b9233..a60ef914e 100644
--- a/tests/samplebinding/injectcode_test.py
+++ b/tests/samplebinding/injectcode_test.py
@@ -90,6 +90,19 @@ class InjectCodeTest(unittest.TestCase):
result = ic.callArrayMethod(values)
self.assertEqual(result, ic.multiplier * sum(values))
+ def testUsageOfTypeSystemCheckVariableOnPrimitiveType(self):
+ '''When the sequence item is convertible to an integer -1 is returned, or -2 if its not convertible.'''
+ ic = InjectCode()
+ values = (1, 2, 3, 4, '5', 6.7)
+ print values
+ result = ic.arrayMethod(values)
+ print result
+ fixedValues = [v for v in values if isinstance(v, int)]\
+ + [-1 for v in values if isinstance(v, float)]\
+ + [-2 for v in values if not isinstance(v, int) and not isinstance(v, float)]
+ print fixedValues
+ #self.assertEqual(result, sum(fixedValues))
+
class IntArrayTest(unittest.TestCase):
'''Test case for converting python sequence to int array'''
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index bced56f8d..122456184 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -203,7 +203,8 @@
<replace-type modified-type="str"/>
</modify-argument>
<inject-code class='target' position='beginning'>
- %0 = new %FUNCTION_NAME(atoi(%CONVERTTOCPP[const char*](%PYARG_1)));
+ const char* tmpArg = %CONVERTTOCPP[const char*](%PYARG_1);
+ %0 = new %FUNCTION_NAME(atoi(tmpArg));
</inject-code>
</modify-function>
</object-type>
@@ -246,7 +247,18 @@
<inject-code class="native" position="beginning">
static void reparent_layout_items(PyObject* parent, PyObject* layout)
{
- const ObjectTypeList&amp; objChildren = %CONVERTTOCPP[ObjectTypeLayout*](layout)->objects();
+ // CHECKTYPE and ISCONVERTIBLE are used here for test purposes, don't change them.
+ if (!%CHECKTYPE[ObjectTypeLayout*](layout) &amp;&amp; !%ISCONVERTIBLE[ObjectTypeLayout*](layout))
+ return;
+ // %CHECKTYPE[ObjectTypeLayout*](layout)
+ // %ISCONVERTIBLE[ObjectTypeLayout*](layout)
+ ObjectTypeLayout* var;
+ var = %CONVERTTOCPP[ObjectTypeLayout*](layout);
+ // TODO-CONVERTER: erase this
+ /*
+ ObjectTypeLayout* var2 = %CONVERTTOCPP[ObjectTypeLayout*](layout);
+ */
+ const ObjectTypeList&amp; objChildren = var->objects();
ObjectTypeList::const_iterator it = objChildren.begin();
for (; it != objChildren.end(); ++it) {
if ((*it)->isLayoutType()) {
@@ -1083,8 +1095,14 @@
<conversion-rule class="native">
int numItems = PySequence_Size(%PYARG_1);
Shiboken::AutoArrayPointer&lt;int&gt; %out(numItems);
- for (int i = 0; i &lt; numItems; ++i)
- %out[i] = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(%PYARG_1, i));
+ for (int 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)))
+ %out[i] = -1;
+ else
+ %out[i] = -2;
+ }
</conversion-rule>
<conversion-rule class="target">
PyObject* %out = PyList_New(count);
@@ -1539,7 +1557,17 @@
</value-type>
<value-type name="ByteArray" hash-function="ByteArray::hash">
- <conversion-rule file="bytearray_conversions.h"/>
+ <conversion-rule file="bytearray_conversions.h">
+ <target-to-native>
+ <add-conversion type='Py_None' check='%in == Py_None'>
+ %out = %OUTTYPE();
+ </add-conversion>
+ <add-conversion type='PyString' check='PyString_Check(%in)'>
+ %out = %OUTTYPE(PyString_AS_STRING(%in), PyString_GET_SIZE(%in));
+ </add-conversion>
+ </target-to-native>
+ </conversion-rule>
+
<modify-function signature="ByteArray(const char*,int)" remove="all" />
<modify-function signature="ByteArray(const char*)">
<!-- Keep \x00 bytes passed in Python strings. -->
@@ -1774,7 +1802,30 @@
<extra-includes>
<include file-name="datetime.h" location="global"/>
</extra-includes>
- <conversion-rule class="target" file="date_conversions.h"/>
+ <inject-code class="native" position="beginning">
+ static bool PyDate_ImportAndCheck(PyObject* pyIn) {
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
+ return PyDate_Check(pyIn);
+ }
+ </inject-code>
+ <conversion-rule class="target" file="date_conversions.h">
+ <target-to-native>
+ <add-conversion type='PyDate' check='PyDate_ImportAndCheck(%in)'>
+ int day = PyDateTime_GET_DAY(%in);
+ int month = PyDateTime_GET_MONTH(%in);
+ int year = PyDateTime_GET_YEAR(%in);
+ %out = %OUTTYPE(day, month, year);
+ </add-conversion>
+ </target-to-native>
+ </conversion-rule>
+ <add-function signature="toPython()" return-type="PyDate">
+ <inject-code class="target">
+ if (!PyDateTimeAPI)
+ PyDateTime_IMPORT;
+ %PYARG_0 = PyDate_FromDate(%CPPSELF.day(), %CPPSELF.month(), %CPPSELF.year());
+ </inject-code>
+ </add-function>
</value-type>
<object-type name="HandleHolder" />