SWIG bindings for member functions returning a reference

Currently, the lsst package provides the returnCopy SWIG macro to safely wrap member functions returning references.

But to really return an internal reference also in the python bindings, avoiding dangling references, the following macro could be adopted:

%define %returnReference(METHOD)
%feature("pythonappend") METHOD %{  val.__back_ref = self  %}
%enddef

Thanks for the suggestion! I didn’t realize Swig supported this, and I’d looked for it in the past.

Do you happen to know if the Swig version when this first appeared (I think we’re on the latest now, so it’s not critical in terms of us using it, but I’m curious as to whether my lack of knowledge was a long-standing documentation failure or just something recent I hadn’t noticed).

I’ve created DM-7215 to implement this.

The solution above is using the following features:

  • the python proxy class created by SWIG (a new style python class inheriting from object),
  • the proxy/shadow functions or methods provided by SWIG

Proxy classes and methods are available since SWIG 1.3 but I don’t know when SWIG started using new style python classes.

With a new style python class I can add a new attribute to the instance by assignment

class Val(object):
  pass

val = Val()
val.any_attribute = 0