diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2023-04-06 12:53:47 +0100 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2023-04-26 18:18:15 +0100 |
commit | d5291a77e18dbc5342803f1d80bc55c6333f1271 (patch) | |
tree | 5e64656e605f2f752cf319df796673124d3f7d7b /Lib/python | |
parent | b2fd91bc41050ec4dd8fbb13b71abb796bc6c8b7 (diff) | |
download | swig-d5291a77e18dbc5342803f1d80bc55c6333f1271.tar.gz |
Remove now redundant use of Python Sequence protocol in STL wrappers
Removes SwigPySequence_Cont and support classes:
SwigPySequence_ArrowProxy, SwigPySequence_Cont, SwigPySequence_Ref
The recently added Iterator Protocol support should cover the Sequence
Protocol usage for converting from Python containers to STL containers.
Diffstat (limited to 'Lib/python')
-rw-r--r-- | Lib/python/pycontainer.swg | 248 | ||||
-rw-r--r-- | Lib/python/pyname_compat.i | 5 |
2 files changed, 1 insertions, 252 deletions
diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index 7012b0b1d..b77a37876 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -460,234 +460,7 @@ namespace swig { fragment="StdTraits", fragment="SwigPySequence_Base", fragment="SwigPyIterator_T") -{ -namespace swig -{ - template <class T> - struct SwigPySequence_Ref - { - SwigPySequence_Ref(PyObject* seq, Py_ssize_t index) - : _seq(seq), _index(index) - { - } - - operator T () const - { - swig::SwigVar_PyObject item = PySequence_GetItem(_seq, _index); - try { - return swig::as<T>(item); - } catch (const std::invalid_argument& e) { - char msg[1024]; - PyOS_snprintf(msg, sizeof(msg), "in sequence element %d ", (int)_index); - if (!PyErr_Occurred()) { - ::%type_error(swig::type_name<T>()); - } - SWIG_Python_AddErrorMsg(msg); - SWIG_Python_AddErrorMsg(e.what()); - throw; - } - } - - SwigPySequence_Ref& operator=(const T& v) - { - PySequence_SetItem(_seq, _index, swig::from<T>(v)); - return *this; - } - - private: - PyObject* _seq; - Py_ssize_t _index; - }; - - template <class T> - struct SwigPySequence_ArrowProxy - { - SwigPySequence_ArrowProxy(const T& x): m_value(x) {} - const T* operator->() const { return &m_value; } - operator const T*() const { return &m_value; } - T m_value; - }; - - template <class T, class Reference > - struct SwigPySequence_InputIterator - { - typedef SwigPySequence_InputIterator<T, Reference > self; - - typedef std::random_access_iterator_tag iterator_category; - typedef Reference reference; - typedef T value_type; - typedef T* pointer; - typedef Py_ssize_t difference_type; - - SwigPySequence_InputIterator() - { - } - - SwigPySequence_InputIterator(PyObject* seq, Py_ssize_t index) - : _seq(seq), _index(index) - { - } - - reference operator*() const - { - return reference(_seq, _index); - } - - SwigPySequence_ArrowProxy<T> - operator->() const { - return SwigPySequence_ArrowProxy<T>(operator*()); - } - - bool operator==(const self& ri) const - { - return (_index == ri._index) && (_seq == ri._seq); - } - - bool operator!=(const self& ri) const - { - return !(operator==(ri)); - } - - self& operator ++ () - { - ++_index; - return *this; - } - - self& operator -- () - { - --_index; - return *this; - } - - self& operator += (difference_type n) - { - _index += n; - return *this; - } - - self operator +(difference_type n) const - { - return self(_seq, _index + n); - } - - self& operator -= (difference_type n) - { - _index -= n; - return *this; - } - - self operator -(difference_type n) const - { - return self(_seq, _index - n); - } - - difference_type operator - (const self& ri) const - { - return _index - ri._index; - } - - bool operator < (const self& ri) const - { - return _index < ri._index; - } - - reference - operator[](difference_type n) const - { - return reference(_seq, _index + n); - } - - private: - PyObject* _seq; - difference_type _index; - }; - - // STL container wrapper around a Python sequence - template <class T> - struct SwigPySequence_Cont - { - typedef SwigPySequence_Ref<T> reference; - typedef const SwigPySequence_Ref<T> const_reference; - typedef T value_type; - typedef T* pointer; - typedef Py_ssize_t difference_type; - typedef size_t size_type; - typedef const pointer const_pointer; - typedef SwigPySequence_InputIterator<T, reference> iterator; - typedef SwigPySequence_InputIterator<T, const_reference> const_iterator; - - SwigPySequence_Cont(PyObject* seq) : _seq(0) - { - if (!PySequence_Check(seq)) { - throw std::invalid_argument("a sequence is expected"); - } - _seq = seq; - Py_INCREF(_seq); - } - - ~SwigPySequence_Cont() - { - Py_XDECREF(_seq); - } - - size_type size() const - { - return static_cast<size_type>(PySequence_Size(_seq)); - } - - bool empty() const - { - return size() == 0; - } - - iterator begin() - { - return iterator(_seq, 0); - } - - const_iterator begin() const - { - return const_iterator(_seq, 0); - } - - iterator end() - { - return iterator(_seq, size()); - } - - const_iterator end() const - { - return const_iterator(_seq, size()); - } - - reference operator[](difference_type n) - { - return reference(_seq, n); - } - - const_reference operator[](difference_type n) const - { - return const_reference(_seq, n); - } - - bool check() const - { - Py_ssize_t s = size(); - for (Py_ssize_t i = 0; i < s; ++i) { - swig::SwigVar_PyObject item = PySequence_GetItem(_seq, i); - if (!swig::check<value_type>(item)) - return false; - } - return true; - } - - private: - PyObject* _seq; - }; - -} -} +{} %define %swig_sequence_iterator(Sequence...) %swig_sequence_iterator_with_making_function(swig::make_output_iterator,Sequence...) @@ -1078,25 +851,6 @@ namespace swig { if (seq) delete *seq; return SWIG_ERROR; - } else if (PySequence_Check(obj)) { - try { - SwigPySequence_Cont<value_type> swigpyseq(obj); - if (seq) { - sequence *pseq = new sequence(); - assign(swigpyseq, pseq); - *seq = pseq; - return SWIG_NEWOBJ; - } else { - return swigpyseq.check() ? SWIG_OK : SWIG_ERROR; - } - } catch (std::exception& e) { - if (seq) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, e.what()); - } - } - return SWIG_ERROR; - } } return ret; } diff --git a/Lib/python/pyname_compat.i b/Lib/python/pyname_compat.i index a9630dbe7..ca063153b 100644 --- a/Lib/python/pyname_compat.i +++ b/Lib/python/pyname_compat.i @@ -25,7 +25,6 @@ */ %fragment("PySequence_Base", "header", fragment="SwigPySequence_Base") {} -%fragment("PySequence_Cont", "header", fragment="SwigPySequence_Cont") {} %fragment("PySwigIterator_T", "header", fragment="SwigPyIterator_T") {} %fragment("PyPairBoolOutputIterator", "header", fragment="SwigPyPairBoolOutputIterator") {} %fragment("PySwigIterator", "header", fragment="SwigPyIterator") {} @@ -39,10 +38,6 @@ #define PyObject_var SwigVar_PyObject #define PyOper SwigPyOper #define PySeq SwigPySeq -#define PySequence_ArrowProxy SwigPySequence_ArrowProxy -#define PySequence_Cont SwigPySequence_Cont -#define PySequence_InputIterator SwigPySequence_InputIterator -#define PySequence_Ref SwigPySequence_Ref #define PySwigClientData SwigPyClientData #define PySwigClientData_Del SwigPyClientData_Del #define PySwigClientData_New SwigPyClientData_New |