My application can be extended via IronPython scripts provided by the user.
- First, I create a new
ScriptScope. - Using that script scope, I run a short setup script that performs some convenience imports.
- Finally, I run all user scripts in the prepared scope.
The setup script looks like this:
import clr
import System
from System import Exception
from System import Math
clr.ImportExtensions(System.Linq.Enumerable)
The idea is that user scripts can use common types like Math or Exception without having to import them first.
I would also like LINQ to work out of the box, so I added the last line to the setup script. However, while the regular imports work fine, the extension import does not. From what I could figure out, regular imports become part of the script scope and thus are available to any subsequent script running in the same scope. Extension imports, however, seem to work differently.
So my question is: how can a script I execute use LINQ without having to explicitly import the extension methods?
Note: There are a lot of scripts, many of which contain only a single expression. So I would rather avoid the performance penalty of automatically prefixing each script with the extension import.