From a628bf9b6f5bf79cc28cfa09dbb76279b4d97f77 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Mon, 18 Feb 2019 18:50:22 +0000 Subject: [PATCH] Python std::vector back-reference changes Give reference in container_owner a more unique name (back_reference). Change back-reference 'ret' typemap to have a function name so that they are less unwittingly be used elsewhere where not intended. Note that they can be overridden by users if needed using: %extend std::vector { %typemap(ret) value_type const& __getitem__, value_type const& front, value_type const& back { ... } } These override the SWIG supplied versions because the SWIG supplied typemaps use non-const value_type&, but the methods use const, so the above have a higher precedence in the typemap search algorithm. --- Lib/python/pycontainer.swg | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index a2ee9522c..5c2a981ee 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -52,7 +52,7 @@ namespace swig { struct container_owner { // By default, do not add the back-reference (for value types) // Specialization below will check the reference for pointer types. - static bool reference(PyObject* child, PyObject* owner) { + static bool back_reference(PyObject* child, PyObject* owner) { return false; } }; @@ -66,7 +66,7 @@ namespace swig { * * returns whether the reference was set or not */ - static bool reference(PyObject* child, PyObject* owner) { + static bool back_reference(PyObject* child, PyObject* owner) { SwigPyObject* swigThis = SWIG_Python_GetSwigThis(child); if (swigThis && (swigThis->own & SWIG_POINTER_OWN) != SWIG_POINTER_OWN) { PyObject_SetAttr(child, container_owner_attribute(), owner); @@ -809,8 +809,10 @@ namespace swig return self->size(); } - %typemap(ret, fragment="reference_container_owner", noblock=1) value_type& { - (void)swig::container_owner::category>::reference($result, $self); + // Although __getitem__, front, back actually use a const value_type& return type, the typemaps below + // use non-const so that they can be easily overridden by users if necessary. + %typemap(ret, fragment="reference_container_owner", noblock=1) value_type& __getitem__, value_type& front, value_type& back { + (void)swig::container_owner::category>::back_reference($result, $self); } } %enddef