Python: Keep reference to owning container during element access
This commit is contained in:
parent
34ee191769
commit
65dd1c49bc
1 changed files with 18 additions and 0 deletions
|
|
@ -36,6 +36,13 @@
|
||||||
|
|
||||||
%include <std_except.i>
|
%include <std_except.i>
|
||||||
|
|
||||||
|
%fragment("container_owner_attribute", "header") {
|
||||||
|
PyObject* container_owner_attribute() {
|
||||||
|
static PyObject* name = SWIG_Python_str_FromChar("__owner");
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
%fragment(SWIG_Traits_frag(swig::SwigPtr_PyObject),"header",fragment="StdTraits") {
|
%fragment(SWIG_Traits_frag(swig::SwigPtr_PyObject),"header",fragment="StdTraits") {
|
||||||
namespace swig {
|
namespace swig {
|
||||||
template <> struct traits<SwigPtr_PyObject > {
|
template <> struct traits<SwigPtr_PyObject > {
|
||||||
|
|
@ -766,6 +773,17 @@ namespace swig
|
||||||
size_type __len__() const {
|
size_type __len__() const {
|
||||||
return self->size();
|
return self->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
%typemap(ret, fragment="container_owner_attribute") value_type& {
|
||||||
|
PyObject* swigThis = PyObject_GetAttrString($result, "this");
|
||||||
|
PyErr_Clear();
|
||||||
|
if (swigThis && SwigPyObject_Check(swigThis) &&
|
||||||
|
(reinterpret_cast<SwigPyObject*>(swigThis)->own & SWIG_POINTER_OWN) != SWIG_POINTER_OWN) {
|
||||||
|
// try to store reference to parent
|
||||||
|
PyObject_GenericSetAttr($result, container_owner_attribute(), $self);
|
||||||
|
}
|
||||||
|
Py_XDECREF(swigThis);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue