diff options
Diffstat (limited to 'Lib/python/std_array.i')
-rw-r--r-- | Lib/python/std_array.i | 60 |
1 files changed, 25 insertions, 35 deletions
diff --git a/Lib/python/std_array.i b/Lib/python/std_array.i index 9cca563c0..d41d1f15e 100644 --- a/Lib/python/std_array.i +++ b/Lib/python/std_array.i @@ -31,48 +31,38 @@ template <class T, size_t N> struct IteratorProtocol<std::array<T, N>, T> { - static int assign(PyObject *obj, std::array<T, N> **seq) { - int ret = SWIG_ERROR; - PyObject *iter = PyObject_GetIter(obj); + + static void assign(PyObject *obj, std::array<T, N> *seq) { + SwigVar_PyObject iter = PyObject_GetIter(obj); if (iter) { - PyObject *item = PyIter_Next(iter); + SwigVar_PyObject item = PyIter_Next(iter); size_t count = 0; - typename std::array<T, N>::iterator array_iter = nullptr; - ret = SWIG_OK; - if (seq) { - *seq = new std::array<T, N>(); - array_iter = (*seq)->begin(); - } + typename std::array<T, N>::iterator array_iter = seq->begin(); while (item && (count < N)) { - try { - if (seq) { - *array_iter++ = swig::as<T>(item); - } else { - if (!swig::check<T>(item)) - ret = SWIG_ERROR; - } - } catch (std::exception& e) { - if (seq) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, e.what()); - } - } - ret = SWIG_ERROR; - } ++count; - Py_DECREF(item); - item = (ret == SWIG_OK) ? PyIter_Next(iter) : 0; - } - if ((ret == SWIG_OK) && (count != N || item)) { - PyErr_SetString(PyExc_TypeError, "std::array size does not match source container size"); - ret = SWIG_ERROR; + *array_iter++ = swig::as<T>(item); + item = PyIter_Next(iter); } - Py_XDECREF(item); - Py_DECREF(iter); - if (seq && (ret == SWIG_ERROR)) - delete *seq; + if (count != N || item) + throw std::invalid_argument("std::array size does not match source container size"); } + } + static bool check(PyObject *obj) { + int ret = false; + SwigVar_PyObject iter = PyObject_GetIter(obj); + if (iter) { + SwigVar_PyObject item = PyIter_Next(iter); + size_t count = 0; + ret = true; + while (item && (count < N)) { + ++count; + ret = swig::check<T>(item); + item = ret ? PyIter_Next(iter) : 0; + } + if (count != N || item) + ret = false; + } return ret; } }; |