Skip to content

Commit

Permalink
Merge pull request #713 from AVSLab/feature/bsk-712-fix-protectAllCla…
Browse files Browse the repository at this point in the history
…sses

Fix protectAllClasses
  • Loading branch information
juan-g-bonilla authored Jun 7, 2024
2 parents 49edf18 + a992871 commit fdc00bf
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
3 changes: 3 additions & 0 deletions docs/source/Support/bskReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Version |release|
a robotic arm that changes orientation through the use of the :ref:`prescribedRotation1DOF` profiler module.
:ref:`scenarioFlexiblePanel` simulates a flexible panel that has torsional and bending modes, which are approximated
though a lumped-mass approach and discretized to as many subpanels as needed.
- Fixed ``protectAllClasses`` method in ``Basilisk.architecture.swig_common_model`` so that it actually protects the classes
in the given module (prevents code from setting unknown attributes). This might impact user code that depended on adding
additional attributes to python classes generated by SWIG.


Version 2.3.0 (April 5, 2024)
Expand Down
20 changes: 9 additions & 11 deletions src/architecture/_GeneralModuleFiles/swig_conly_data.i
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,28 @@ def getStructSize(self):
return eval('sizeof_' + repr(self).split(';')[0].split('.')[-1])
except (NameError) as e:
typeString = 'sizeof_' + repr(self).split(';')[0].split('.')[-1]
raise NameError(e.message + '\nYou tried to get this size macro: ' + typeString +
'\n It appears to be undefined. \nYou need to run the SWIG GEN_SIZEOF' +
' SWIG macro against the class/struct in your SWIG file if you want to ' +
raise NameError(e.message + '\nYou tried to get this size macro: ' + typeString +
'\n It appears to be undefined. \nYou need to run the SWIG GEN_SIZEOF' +
' SWIG macro against the class/struct in your SWIG file if you want to ' +
' make this call.\n')


def protectSetAttr(self, name, value):
if(hasattr(self, name) or name == 'this' or name.find('swig') >= 0):
object.__setattr__(self, name, value)
else:
raise ValueError('You tried to add this variable: ' + name + '\n' +
raise ValueError('You tried to add this variable: ' + name + '\n' +
'To this class: ' + str(self))

def protectAllClasses(moduleType):
import inspect
import sys
clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)

clsmembers = inspect.getmembers(moduleType, inspect.isclass)
for member in clsmembers:
try:
exec(str(member[0]) + '.__setattr__ = protectSetAttr')
exec(str(member[0]) + '.getStructSize = getStructSize')
member[1].__setattr__ = protectSetAttr
member[1].getStructSize = getStructSize
except (AttributeError, TypeError) as e:
pass

%}


%}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ from Basilisk.architecture.swig_common_model import *

%include "stdint.i"
%include "std_string.i"
%include "sys_model.h"
%include "sys_model.i"
%include "swig_eigen.i"
%include "swig_conly_data.i"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ from Basilisk.architecture.swig_common_model import *
%include "swig_conly_data.i"
%include "swig_eigen.i"

%include "sys_model.h"
%include "sys_model.i"
%include "simulation/dynamics/_GeneralModuleFiles/stateData.h"
%include "simulation/dynamics/_GeneralModuleFiles/stateEffector.h"
%include "simulation/dynamics/_GeneralModuleFiles/dynParamManager.h"
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/navigation/pinholeCamera/pinholeCamera.i
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
%include "swig_conly_data.i"
%include "swig_eigen.i"

%include "sys_model.h"
%include "sys_model.i"
%include "pinholeCamera.h"
%include "std_vector.i"

Expand Down
20 changes: 20 additions & 0 deletions src/tests/test_protectAllClasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

from Basilisk.moduleTemplates import cModuleTemplate
from Basilisk.moduleTemplates import cppModuleTemplate

import numpy.testing as npt

def test_protectAllClassesRaises():
mod1 = cModuleTemplate.cModuleTemplate()
mod2 = cppModuleTemplate.CppModuleTemplate()

expected_ex = "You tried to add this variable(.*)"

with npt.assert_raises_regex(ValueError, expected_ex):
mod1.error = "error"

with npt.assert_raises_regex(ValueError, expected_ex):
mod2.error = "error"

if __name__ == "__main__":
test_protectAllClassesRaises()

0 comments on commit fdc00bf

Please sign in to comment.