diff --git a/docs/source/Support/bskReleaseNotes.rst b/docs/source/Support/bskReleaseNotes.rst index b5285cb64b..751bc7df89 100644 --- a/docs/source/Support/bskReleaseNotes.rst +++ b/docs/source/Support/bskReleaseNotes.rst @@ -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) diff --git a/src/architecture/_GeneralModuleFiles/swig_conly_data.i b/src/architecture/_GeneralModuleFiles/swig_conly_data.i index 868c5adc95..8b1df7ec04 100644 --- a/src/architecture/_GeneralModuleFiles/swig_conly_data.i +++ b/src/architecture/_GeneralModuleFiles/swig_conly_data.i @@ -243,9 +243,9 @@ 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') @@ -253,20 +253,18 @@ 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 - -%} - +%} diff --git a/src/fswAlgorithms/stateEstimation/thrustCMEstimation/thrustCMEstimation.i b/src/fswAlgorithms/stateEstimation/thrustCMEstimation/thrustCMEstimation.i index 0d99f7e0f2..e6566eb997 100644 --- a/src/fswAlgorithms/stateEstimation/thrustCMEstimation/thrustCMEstimation.i +++ b/src/fswAlgorithms/stateEstimation/thrustCMEstimation/thrustCMEstimation.i @@ -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" diff --git a/src/simulation/dynamics/spinningBodies/spinningBodiesNDOF/spinningBodyNDOFStateEffector.i b/src/simulation/dynamics/spinningBodies/spinningBodiesNDOF/spinningBodyNDOFStateEffector.i index 32bee6640f..1eafb69b25 100644 --- a/src/simulation/dynamics/spinningBodies/spinningBodiesNDOF/spinningBodyNDOFStateEffector.i +++ b/src/simulation/dynamics/spinningBodies/spinningBodiesNDOF/spinningBodyNDOFStateEffector.i @@ -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" diff --git a/src/simulation/navigation/pinholeCamera/pinholeCamera.i b/src/simulation/navigation/pinholeCamera/pinholeCamera.i index d374ba19e3..f7eb3fb842 100755 --- a/src/simulation/navigation/pinholeCamera/pinholeCamera.i +++ b/src/simulation/navigation/pinholeCamera/pinholeCamera.i @@ -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" diff --git a/src/tests/test_protectAllClasses.py b/src/tests/test_protectAllClasses.py new file mode 100644 index 0000000000..17c92722d4 --- /dev/null +++ b/src/tests/test_protectAllClasses.py @@ -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()