summaryrefslogtreecommitdiff
path: root/Lib/python/pycontainer.swg
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/python/pycontainer.swg')
-rw-r--r--Lib/python/pycontainer.swg46
1 files changed, 13 insertions, 33 deletions
diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg
index a1affa86b..65223b615 100644
--- a/Lib/python/pycontainer.swg
+++ b/Lib/python/pycontainer.swg
@@ -1014,7 +1014,8 @@ namespace swig {
}
template <class Seq, class T = typename Seq::value_type >
- static int assign_iterator_protocol(PyObject *obj, Seq **seq) {
+ struct IteratorProtocol {
+ static int assign(PyObject *obj, Seq **seq) {
int ret = SWIG_ERROR;
PyObject *iter = PyObject_GetIter(obj);
if (iter) {
@@ -1046,12 +1047,21 @@ namespace swig {
return ret;
}
+ };
template <class Seq, class T = typename Seq::value_type >
struct traits_asptr_stdseq {
typedef Seq sequence;
typedef T value_type;
+ static bool is_iterable(PyObject *obj) {
+ PyObject *iter = PyObject_GetIter(obj);
+ bool is_iter = iter != 0;
+ Py_XDECREF(iter);
+ PyErr_Clear();
+ return is_iter;
+ }
+
static int asptr(PyObject *obj, sequence **seq) {
int ret = SWIG_ERROR;
if (obj == Py_None || SWIG_Python_GetSwigThis(obj)) {
@@ -1061,6 +1071,8 @@ namespace swig {
if (seq) *seq = p;
return SWIG_OLDOBJ;
}
+ } else if (is_iterable(obj)) {
+ ret = IteratorProtocol<Seq, T>::assign(obj, seq);
} else if (PySequence_Check(obj)) {
try {
SwigPySequence_Cont<value_type> swigpyseq(obj);
@@ -1080,38 +1092,6 @@ namespace swig {
}
return SWIG_ERROR;
}
- } else {
-#if 0
- PyObject *iter = PyObject_GetIter(obj);
- if (iter) {
- PyObject *item = PyIter_Next(iter);
- ret = SWIG_OK;
- if (seq)
- *seq = new sequence();
- while (item) {
- try {
- if (seq) {
- (*seq)->insert((*seq)->end(), swig::as<value_type>(item));
- } else {
- if (!swig::check<value_type>(item))
- ret = SWIG_ERROR;
- }
- } catch (std::exception& e) {
- if (seq) {
- if (!PyErr_Occurred()) {
- PyErr_SetString(PyExc_TypeError, e.what());
- }
- }
- ret = SWIG_ERROR;
- }
- Py_DECREF(item);
- item = (ret == SWIG_OK) ? PyIter_Next(iter) : 0;
- }
- Py_DECREF(iter);
- }
-#else
- ret = swig::assign_iterator_protocol<Seq, T>(obj, seq);
-#endif
}
return ret;
}