Since we know it is a Tuple or List we can use PySequence_Fast_GET_ITEM

This commit is contained in:
Robin Dunn
2017-04-12 14:32:50 -07:00
parent b01729f4ef
commit 708e2487dc
3 changed files with 33 additions and 57 deletions

View File

@@ -202,9 +202,8 @@ def run():
return 0;
// ensure all the items in the sequence are numbers
for (int idx=0; idx<len; idx+=1) {
PyObject* o = PySequence_ITEM(sipPy, idx);
PyObject* o = PySequence_Fast_GET_ITEM(sipPy, idx);
bool isNum = PyNumber_Check(o);
Py_DECREF(o);
if (!isNum)
return 0;
}
@@ -255,23 +254,19 @@ def run():
}
}
// Is it a 3 or 4 element sequence?
else if (PySequence_Check(sipPy)) {
size_t len = PyObject_Length(sipPy);
else if (PyTuple_Check(sipPy) || PyList_Check(sipPy)) {
size_t len = PySequence_Size(sipPy);
PyObject* o1 = PySequence_GetItem(sipPy, 0);
PyObject* o2 = PySequence_GetItem(sipPy, 1);
PyObject* o3 = PySequence_GetItem(sipPy, 2);
PyObject* o1 = PySequence_Fast_GET_ITEM(sipPy, 0);
PyObject* o2 = PySequence_Fast_GET_ITEM(sipPy, 1);
PyObject* o3 = PySequence_Fast_GET_ITEM(sipPy, 2);
if (len == 3)
*sipCppPtr = new wxColour(wxPyInt_AsLong(o1), wxPyInt_AsLong(o2), wxPyInt_AsLong(o3));
else {
PyObject* o4 = PySequence_GetItem(sipPy, 3);
PyObject* o4 = PySequence_Fast_GET_ITEM(sipPy, 3);
*sipCppPtr = new wxColour(wxPyInt_AsLong(o1), wxPyInt_AsLong(o2), wxPyInt_AsLong(o3),
wxPyInt_AsLong(o4));
Py_DECREF(o4);
}
Py_DECREF(o1);
Py_DECREF(o2);
Py_DECREF(o3);
return sipGetState(sipTransferObj);
}

View File

@@ -628,12 +628,10 @@ def convertTwoIntegersTemplate(CLASS):
if ((PyTuple_Check(sipPy) || PyList_Check(sipPy)) && PySequence_Size(sipPy) == 2) {{
int rval = 1;
PyObject* o1 = PySequence_ITEM(sipPy, 0);
PyObject* o2 = PySequence_ITEM(sipPy, 1);
PyObject* o1 = PySequence_Fast_GET_ITEM(sipPy, 0);
PyObject* o2 = PySequence_Fast_GET_ITEM(sipPy, 1);
if (!PyNumber_Check(o1) || !PyNumber_Check(o2))
rval = 0;
Py_DECREF(o1);
Py_DECREF(o2);
return rval;
}}
return 0;
@@ -648,11 +646,9 @@ def convertTwoIntegersTemplate(CLASS):
}}
// or create a new instance
PyObject* o1 = PySequence_ITEM(sipPy, 0);
PyObject* o2 = PySequence_ITEM(sipPy, 1);
PyObject* o1 = PySequence_Fast_GET_ITEM(sipPy, 0);
PyObject* o2 = PySequence_Fast_GET_ITEM(sipPy, 1);
*sipCppPtr = new {CLASS}(wxPyInt_AsLong(o1), wxPyInt_AsLong(o2));
Py_DECREF(o1);
Py_DECREF(o2);
return SIP_TEMPORARY;
""".format(**locals())
@@ -668,16 +664,12 @@ def convertFourIntegersTemplate(CLASS):
if ((PyTuple_Check(sipPy) || PyList_Check(sipPy)) && PySequence_Size(sipPy) == 4) {{
int rval = 1;
PyObject* o1 = PySequence_ITEM(sipPy, 0);
PyObject* o2 = PySequence_ITEM(sipPy, 1);
PyObject* o3 = PySequence_ITEM(sipPy, 2);
PyObject* o4 = PySequence_ITEM(sipPy, 3);
PyObject* o1 = PySequence_Fast_GET_ITEM(sipPy, 0);
PyObject* o2 = PySequence_Fast_GET_ITEM(sipPy, 1);
PyObject* o3 = PySequence_Fast_GET_ITEM(sipPy, 2);
PyObject* o4 = PySequence_Fast_GET_ITEM(sipPy, 3);
if (!PyNumber_Check(o1) || !PyNumber_Check(o2) || !PyNumber_Check(o3) || !PyNumber_Check(o4))
rval = 0;
Py_DECREF(o1);
Py_DECREF(o2);
Py_DECREF(o3);
Py_DECREF(o4);
return rval;
}}
return 0;
@@ -691,14 +683,12 @@ def convertFourIntegersTemplate(CLASS):
return 0; // not a new instance
}}
// or create a new instance
PyObject* o1 = PySequence_ITEM(sipPy, 0);
PyObject* o2 = PySequence_ITEM(sipPy, 1);
PyObject* o3 = PySequence_ITEM(sipPy, 2);
PyObject* o4 = PySequence_ITEM(sipPy, 3);
PyObject* o1 = PySequence_Fast_GET_ITEM(sipPy, 0);
PyObject* o2 = PySequence_Fast_GET_ITEM(sipPy, 1);
PyObject* o3 = PySequence_Fast_GET_ITEM(sipPy, 2);
PyObject* o4 = PySequence_Fast_GET_ITEM(sipPy, 3);
*sipCppPtr = new {CLASS}(wxPyInt_AsLong(o1), wxPyInt_AsLong(o2),
wxPyInt_AsLong(o3), wxPyInt_AsLong(o4));
Py_DECREF(o1);
Py_DECREF(o2);
return SIP_TEMPORARY;
""".format(**locals())
@@ -715,12 +705,10 @@ def convertTwoDoublesTemplate(CLASS):
if ((PyTuple_Check(sipPy) || PyList_Check(sipPy)) && PySequence_Size(sipPy) == 2) {{
int rval = 1;
PyObject* o1 = PySequence_ITEM(sipPy, 0);
PyObject* o2 = PySequence_ITEM(sipPy, 1);
PyObject* o1 = PySequence_Fast_GET_ITEM(sipPy, 0);
PyObject* o2 = PySequence_Fast_GET_ITEM(sipPy, 1);
if (!PyNumber_Check(o1) || !PyNumber_Check(o2))
rval = 0;
Py_DECREF(o1);
Py_DECREF(o2);
return rval;
}}
return 0;
@@ -735,11 +723,9 @@ def convertTwoDoublesTemplate(CLASS):
}}
// or create a new instance
PyObject* o1 = PySequence_ITEM(sipPy, 0);
PyObject* o2 = PySequence_ITEM(sipPy, 1);
PyObject* o1 = PySequence_Fast_GET_ITEM(sipPy, 0);
PyObject* o2 = PySequence_Fast_GET_ITEM(sipPy, 1);
*sipCppPtr = new {CLASS}(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2));
Py_DECREF(o1);
Py_DECREF(o2);
return SIP_TEMPORARY;
""".format(**locals())
@@ -755,16 +741,12 @@ def convertFourDoublesTemplate(CLASS):
if ((PyTuple_Check(sipPy) || PyList_Check(sipPy)) && PySequence_Size(sipPy) == 4) {{
int rval = 1;
PyObject* o1 = PySequence_ITEM(sipPy, 0);
PyObject* o2 = PySequence_ITEM(sipPy, 1);
PyObject* o3 = PySequence_ITEM(sipPy, 2);
PyObject* o4 = PySequence_ITEM(sipPy, 3);
PyObject* o1 = PySequence_Fast_GET_ITEM(sipPy, 0);
PyObject* o2 = PySequence_Fast_GET_ITEM(sipPy, 1);
PyObject* o3 = PySequence_Fast_GET_ITEM(sipPy, 2);
PyObject* o4 = PySequence_Fast_GET_ITEM(sipPy, 3);
if (!PyNumber_Check(o1) || !PyNumber_Check(o2) || !PyNumber_Check(o3) || !PyNumber_Check(o4))
rval = 0;
Py_DECREF(o1);
Py_DECREF(o2);
Py_DECREF(o3);
Py_DECREF(o4);
return rval;
}}
return 0;
@@ -779,14 +761,12 @@ def convertFourDoublesTemplate(CLASS):
}}
// or create a new instance
PyObject* o1 = PySequence_ITEM(sipPy, 0);
PyObject* o2 = PySequence_ITEM(sipPy, 1);
PyObject* o3 = PySequence_ITEM(sipPy, 2);
PyObject* o4 = PySequence_ITEM(sipPy, 3);
PyObject* o1 = PySequence_Fast_GET_ITEM(sipPy, 0);
PyObject* o2 = PySequence_Fast_GET_ITEM(sipPy, 1);
PyObject* o3 = PySequence_Fast_GET_ITEM(sipPy, 2);
PyObject* o4 = PySequence_Fast_GET_ITEM(sipPy, 3);
*sipCppPtr = new {CLASS}(PyFloat_AsDouble(o1), PyFloat_AsDouble(o2),
PyFloat_AsDouble(o3), PyFloat_AsDouble(o4));
Py_DECREF(o1);
Py_DECREF(o2);
PyFloat_AsDouble(o3), PyFloat_AsDouble(o4));
return SIP_TEMPORARY;
""".format(**locals())

View File

@@ -174,6 +174,7 @@ static void i_wxPyEndBlockThreads(wxPyBlock_t blocked)
//--------------------------------------------------------------------------
// Commonly used helpers for converting small sequences of numbers
// TODO: Are these still needed?
// A helper for converting a 2 element sequence to a pair of integers
static bool i_wxPy2int_seq_helper(PyObject* source, int* i1, int* i2)