trick/trick_source/trick_swig/swig_int.i
Keith Vetter f2e93ac490
Fix for assigning to non-existent variables (#1708) (#1718)
* Fix for assigning to non-existent variables (#1708)

This fix modifies Trick's convert_swig script.  It
adds a directive to not accept dynamic attributes.
The directive will ensure that modelers do not
assign to non-existent parameter in their input files.

The prior fix (issues #1288 and/or #1603) did not
cover C structures, so this commit is really an
addendum to that fix.  With this commit, the convert_swig
script will generate a non-dynamic directive foreach
class and structure.

This fix also stops generating swig interface code
for typedef structure definitions since swig only
produces an interface to the actual typedef name.
For example, with this typedef:

typedef struct StructureName {
   double  main_engine_thrust;  /* N Thrust of main engine */
   struct StructureName* next;  /* ** Next pointer */
} TypeDefName;

swig creates an interface for "TypeDefName", and not
"StructureName", so there is no need for Trick to
produce anything in regards "StructureName".

* Fix for issue with classes defined in ifndef SWIG block

convert_swig takes in the raw header and does no preprocessing,
so blocks of c/c++ header code which are meant to be skipped
by Swig are processed by convert_swig.  This causes an issue
with the generated python code that try to access classes
that are ifndef SWIGed out.

To fix this, check for class existence in the generated python
code e.g:

if 'MomMom' in globals():
    MomMom.__setattr__ = _swig_setattr_nondynamic_instance_variable(object.__setattr__)

* Fix for assignment to swig_double and swig_int primitives

The previous commit(s) on this branch fixed bad assignments
like (misspell position an attribute of BSTATE_IN):
ball.state.input.positiaaaan = 4.0

This commit fixes bad assignments to leaf/primitive attributes
like (try to add attribute to primitive/leaf double type):

ball.state.input.position.fred = 4.0

* Moved _swig_setattr_nondynamic_instance_variable to right after each class in process_class and removed duplicate _swig_setattr_nondynamic_instance_variable blocks for classes that are not in a namespace.

* Add the call for __setattr__ for class templates.

---------

Co-authored-by: Hong Chen <hong.chen-1@nasa.gov>
2024-07-16 10:34:02 -05:00

108 lines
3.5 KiB
OpenEdge ABL

%module swig_int
%include "std_string.i"
%{
#include "trick/swig/swig_int.hh"
#include "trick_swig/swig_int.cpp"
%}
// expose the __str__ function to swig so that it can do pretty prints of parameters with units
class swig_int {
public:
std::string units;
char * __str__() ;
char * __repr__() ;
PyObject * __add__(PyObject * obj1) ;
PyObject * __sub__(PyObject * obj1) ;
PyObject * __mul__(PyObject * obj1) ;
PyObject * __div__(PyObject * obj1) ;
PyObject * __truediv__(PyObject * obj1) ;
PyObject * __floordiv__(PyObject * obj1) ;
PyObject * __mod__(PyObject * obj1) ;
//PyObject * __divmod__(PyObject * obj1) ;
PyObject * __pow__(PyObject * obj1) ;
PyObject * __lshift__(PyObject * obj1) ;
PyObject * __rshift__(PyObject * obj1) ;
PyObject * __and__(PyObject * obj1) ;
PyObject * __xor__(PyObject * obj1) ;
PyObject * __or__(PyObject * obj1) ;
PyObject * __radd__(PyObject * obj1) ;
PyObject * __rsub__(PyObject * obj1) ;
PyObject * __rmul__(PyObject * obj1) ;
PyObject * __rdiv__(PyObject * obj1) ;
PyObject * __rtruediv__(PyObject * obj1) ;
PyObject * __rfloordiv__(PyObject * obj1) ;
PyObject * __rmod__(PyObject * obj1) ;
//PyObject * __rdivmod__(PyObject * obj1) ;
PyObject * __rpow__(PyObject * obj1) ;
PyObject * __rlshift__(PyObject * obj1) ;
PyObject * __rrshift__(PyObject * obj1) ;
PyObject * __rand__(PyObject * obj1) ;
PyObject * __rxor__(PyObject * obj1) ;
PyObject * __ror__(PyObject * obj1) ;
PyObject * __iadd__(PyObject * obj1) ;
PyObject * __isub__(PyObject * obj1) ;
PyObject * __imul__(PyObject * obj1) ;
PyObject * __idiv__(PyObject * obj1) ;
PyObject * __itruediv__(PyObject * obj1) ;
PyObject * __ifloordiv__(PyObject * obj1) ;
PyObject * __imod__(PyObject * obj1) ;
PyObject * __ipow__(PyObject * obj1) ;
PyObject * __ilshift__(PyObject * obj1) ;
PyObject * __irshift__(PyObject * obj1) ;
PyObject * __iand__(PyObject * obj1) ;
PyObject * __ixor__(PyObject * obj1) ;
PyObject * __ior__(PyObject * obj1) ;
PyObject * __lt__(PyObject * obj1) ;
PyObject * __le__(PyObject * obj1) ;
PyObject * __eq__(PyObject * obj1) ;
PyObject * __ne__(PyObject * obj1) ;
PyObject * __gt__(PyObject * obj1) ;
PyObject * __ge__(PyObject * obj1) ;
PyObject * __neg__() ;
PyObject * __pos__() ;
PyObject * __abs__() ;
PyObject * __invert__() ;
PyObject * __nonzero__() ;
//PyObject * __complex__(PyObject * obj1) ;
PyObject * __int__() ;
PyObject * __long__() ;
PyObject * __float__() ;
PyObject * __oct__() ;
PyObject * __hex__() ;
PyObject * __len__() ;
int __index__() ;
PyObject * __bool__() ;
} ;
%pythoncode %{
#if SWIG_VERSION > 0x040000
def _trick_setattr_nondynamic_instance_variable(set):
def set_instance_attr(self, name, value):
if name == "thisown":
self.this.own(value)
elif name == "this":
set(self, name, value)
else:
msg = f'You cannot add instance attribute \'{name}\' to Trick swig_int'
raise AttributeError(msg)
return set_instance_attr
swig_int.__setattr__ = _trick_setattr_nondynamic_instance_variable(object.__setattr__)
#endif
%}