summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2023-04-01 21:57:34 +0100
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2023-04-01 21:57:34 +0100
commitb72703acdf42ce0939737ed73ca4830c1e1b6b2d (patch)
tree22678b18a4d19fc9d6cb85e4333d967f93eb0b32 /Lib
parent33f6a2d0b2c3d90b928f56ddfa599afe87903f76 (diff)
downloadswig-b72703acdf42ce0939737ed73ca4830c1e1b6b2d.tar.gz
STL support for copying Python objects supporting Iterator protocol
std::array not working though
Diffstat (limited to 'Lib')
-rw-r--r--Lib/python/pycontainer.swg69
1 files changed, 68 insertions, 1 deletions
diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg
index 4910fecc0..a1affa86b 100644
--- a/Lib/python/pycontainer.swg
+++ b/Lib/python/pycontainer.swg
@@ -1014,11 +1014,46 @@ namespace swig {
}
template <class Seq, class T = typename Seq::value_type >
+ static int assign_iterator_protocol(PyObject *obj, Seq **seq) {
+ int ret = SWIG_ERROR;
+ PyObject *iter = PyObject_GetIter(obj);
+ if (iter) {
+ PyObject *item = PyIter_Next(iter);
+ ret = SWIG_OK;
+ if (seq)
+ *seq = new Seq();
+ while (item) {
+ try {
+ if (seq) {
+ (*seq)->insert((*seq)->end(), 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;
+ }
+ Py_DECREF(item);
+ item = (ret == SWIG_OK) ? PyIter_Next(iter) : 0;
+ }
+ Py_DECREF(iter);
+ }
+
+ return ret;
+ }
+
+ template <class Seq, class T = typename Seq::value_type >
struct traits_asptr_stdseq {
typedef Seq sequence;
typedef T value_type;
static int asptr(PyObject *obj, sequence **seq) {
+ int ret = SWIG_ERROR;
if (obj == Py_None || SWIG_Python_GetSwigThis(obj)) {
sequence *p;
swig_type_info *descriptor = swig::type_info<sequence>();
@@ -1045,8 +1080,40 @@ 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 SWIG_ERROR;
+ return ret;
}
};