Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After a great deal of trial and error, I arrived at a solution. I never managed to get the <code>PlatformAdaptationLayer</code> approach to work correctly. It never called back to the PAL when attempting to load the modules.</p> <p>So what I decided to do was replace the built-in import function by using the SetVariable method as shown below (Engine and Scope are protected members exposing the <code>ScriptEngine</code> and <code>ScriptScope</code> for the parent script):</p> <pre><code>delegate object ImportDelegate(CodeContext context, string moduleName, PythonDictionary globals, PythonDictionary locals, PythonTuple tuple); protected void OverrideImport() { ScriptScope scope = IronPython.Hosting.Python.GetBuiltinModule(Engine); scope.SetVariable("__import__", new ImportDelegate(DoDatabaseImport)); } protected object DoDatabaseImport(CodeContext context, string moduleName, PythonDictionary globals, PythonDictionary locals, PythonTuple tuple) { if (ScriptExistsInDb(moduleName)) { string rawScript = GetScriptFromDb(moduleName); ScriptSource source = Engine.CreateScriptSourceFromString(rawScript); ScriptScope scope = Engine.CreateScope(); Engine.Execute(rawScript, scope); Microsoft.Scripting.Runtime.Scope ret = Microsoft.Scripting.Hosting.Providers.HostingHelpers.GetScope(scope); Scope.SetVariable(moduleName, ret); return ret; } else { // fall back on the built-in method return IronPython.Modules.Builtin.__import__(context, moduleName); } } </code></pre> <p>Hope this helps someone!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload