I'm trying to implement support for IronPython in a section of our application that uses scripting and currently supports a number of existing languages such as Lua.
The scenario is that we need to both support return values from scripts (using the language's return or equivalent syntax) and also read on exit, and write before execution variables to be used throughout the script.
The problem I'm having with IronPython is that it seems to not allow use of return in a script outside of a function. I can therefore wrap all scripts in a function, though this is messy as I then have to indent all the code, whilst that's not a massive problem in itself it appears that any variables referenced in the script are treated as function scope local variables, and that the GetVariable/SetVariable methods do not allow me to specify these as globals thus the now method wrapped script fails as it cannot find the variables set by GetVariable and SetVariable.
So for example taking the script:
integerVar = integerVar + 10
stringVar = stringVar + " modified"
return true
Will not work directly, because the return statement fails due to not being in a method. Wrapping in a function:
def run():
integerVar = integerVar + 10
stringVar = stringVar + " modified"
return true
Fails when the method is called with "Local variable 'integerVar' referenced before assignment", despite being set previously with SetVariable on the scope.
Is there any way I can execute IronPython scripts in a way that allows me to both obtain any value returned from a return statement, and to be able to both set values to be manipulated by the script prior to execution, and obtain their values post execution?