Returning const arrays forces a copy

* This doesn't cover all cases but helps with 'pythonic' cases where a list
  returned from a function should be expected to be mutable. Modifying direct
  'members' can still modify through const.
This commit is contained in:
baldurk
2026-08-13 17:46:43 +01:00
parent 0bab6e4059
commit c95a69c8a1
5 changed files with 28 additions and 6 deletions
@@ -328,6 +328,10 @@ void ARRAY_INSTANTIATION_CHECK_NAME(typeName)(typeName<innerType> *);
%typemap(ret) typeName & { ARRAY_INSTANTIATION_CHECK_NAME(typeName)($1); }
%typemap(ret) typeName { ARRAY_INSTANTIATION_CHECK_NAME(typeName)(&$1); }
%typemap(out) typeName const & {
$result = ConvertToPy(indirect($1));
}
%enddef
%define NON_TEMPLATE_ARRAY_INSTANTIATE(typeName)
+18
View File
@@ -98,6 +98,13 @@ VA_IGNORE_REST_OF_FILE
PyDateTime_IMPORT;
%}
%typemap(out) const SWIGTYPE & {
static_assert(false, "Const ref types must be explicitly allowed, to ensure proper copy semantics. " \
"Consider returning by copy if reasonable, or explicitly set up copy typemap.");
//$ltype owned = new std::remove_pointer<$ltype>::type(indirect($1));
//$result = SWIG_NewPointerObj(SWIG_as_voidptr(owned), $descriptor, SWIG_POINTER_OWN | 0 );
}
%include "pyconversion.i"
// typemaps for windowing data
@@ -232,6 +239,17 @@ VA_IGNORE_REST_OF_FILE
$1.assign(*$input);
}
// this is fine to return directly as it offers no mutable members
%typemap(out) const PipeState & {
$result = SWIG_NewPointerObj(SWIG_as_voidptr($1), $descriptor, 0 );
}
// this is returned directly due to the size of the data contained in the type.
// it's mostly "pythonic" to have an object returned that is considered mutable
%typemap(out) const SDFile & {
$result = SWIG_NewPointerObj(SWIG_as_voidptr($1), $descriptor, 0 );
}
%typemap(ret) const ActionDescription * {
// for ActionDescription pointers don't apply parent tracking, since these are preserved
// in other ways and the linked-list nature of walking them can produce absurdly long