Skip to content

Prevent additions of new fields to Swigged payloads #305

@bbercoviciUspace

Description

@bbercoviciUspace

Basilisk prevents adding non-existing fields to swig-wrapped C and C++ modules, with C modules being C-structures exposed in Python. This is a sound behavior that is achieved by adding the following lines to the modules .i files.

%pythoncode %{
import sys
protectAllClasses(sys.modules[__name__])
%}

These lines will replace the default __setattr__ methods of the wrapped Python objects with a custom method checking first whether the to-be-set variable already exists in the object to be modified.

However, there isn't such a mechanism in place for message payloads. One can try to create an instance of a msgPayload and add new fields from python without hurdle or warning.

I naively tried to add the same code block as before to msgInterfacePy.i.in and was able to compile Basilisk. I could then check that adding new fields to the payload was indeed forbidden.

However, the unit tests are no longer passing due to improperly wrapped msg recorders. See the pytest trace below.

Freezing the msg payloads seems like a very desirable feature, but I guess there are other elements to consider in the trade off.

_____________________________________________________________ test_RecordingInputMessages ______________________________________________________________

    def test_RecordingInputMessages():
        """
        testing recording a C-wrapped input message with the recorder module
        """
    
        bskLogging.setDefaultLogLevel(bskLogging.BSK_WARNING)
        testFailCount = 0  # zero unit test result counter
        testMessages = []  # create empty array to store test log messages
    
        #  Create a sim module as an empty container
        scSim = SimulationBaseClass.SimBaseClass()
    
        #  create the simulation process
        dynProcess = scSim.CreateNewProcess("dynamicsProcess")
    
        # create the dynamics task and specify the integration update time
        dynProcess.addTask(scSim.CreateNewTask("dynamicsTask", macros.sec2nano(1.)))
    
        # create modules
        mod1 = cModuleTemplate.cModuleTemplateConfig()
        mod1Wrap = scSim.setModelDataWrap(mod1)
        mod1Wrap.ModelTag = "cModule1"
        scSim.AddModelToTask("dynamicsTask", mod1Wrap, mod1)
    
        # Write input data
        inputData = messaging.CModuleTemplateMsgPayload()
        inputData.dataVector = [1, 2, 3]
        inputDataMsg = messaging.CModuleTemplateMsg().write(inputData)
    
        # Subscribe input message to stand alone message
        mod1.dataInMsg.subscribeTo(inputDataMsg)
    
        # Create recorders tied to IO messages
>       dataInRec = mod1.dataInMsg.recorder()

src/architecture/messaging/_UnitTest/test_RecordInputMessages.py:65: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
dist3/Basilisk/architecture/messaging/CModuleTemplateMsgPayload.py:1458: in recorder
    return CModuleTemplateMsgRecorder(self, timeDiff)
dist3/Basilisk/architecture/messaging/CModuleTemplateMsgPayload.py:830: in __init__
    _CModuleTemplateMsgPayload.CModuleTemplateMsgRecorder_swiginit(self, _CModuleTemplateMsgPayload.new_CModuleTemplateMsgRecorder(*args))
dist3/Basilisk/architecture/messaging/CModuleTemplateMsgPayload.py:190: in protectSetAttr
    if(hasattr(self, name) or name == 'this' or name.find('swig') >= 0):
dist3/Basilisk/architecture/messaging/CModuleTemplateMsgPayload.py:960: in __getattr__
    data = self.__record_vector()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <Basilisk.architecture.messaging.CModuleTemplateMsgPayload.CModuleTemplateMsgRecorder;  >

    def __record_vector(self):
>       return _CModuleTemplateMsgPayload.CModuleTemplateMsgRecorder___record_vector(self)
E       TypeError: in method 'CModuleTemplateMsgRecorder___record_vector', argument 1 of type 'Recorder< CModuleTemplateMsgPayload > *'

dist3/Basilisk/architecture/messaging/CModuleTemplateMsgPayload.py:852: TypeError

Originally posted by @bbercoviciUspace in #304

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions