IronLanguages / ironpython2

Implementation of the Python programming language for .NET Framework; built on top of the Dynamic Language Runtime (DLR).
http://ironpython.net
Apache License 2.0
1.08k stars 229 forks source link

IronPython.Hosting.Python.CreateEngine in Blazor 5 - WebAssembly .Net5. #769

Open JimmyHeartbreaker opened 3 years ago

JimmyHeartbreaker commented 3 years ago

Description

I am using a 3rd party library targeting .netstandard2.0 in Blazor which is built on web assembly which uses mono. This library uses IronPython. In Blazor version 3.2.1 IronPython works but in 5.0.2 it does not.

Steps to Reproduce

Create a Blazor .net 5 app. Run IronPython.Hosting.Python.CreateEngine(IDictionary`2 options)

See error below - is it possible to disable console input?

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: failed to evaluate assumptions. CorrelationId=="5f178588-9796-4c4f-968a-4cdd8079e303", System.Reflection.TargetInvocationException: Failed to load language 'IronPython 2.7.11': The type initializer for 'Microsoft.Scripting.Utils.ConsoleInputStream' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Microsoft.Scripting.Utils.ConsoleInputStream' threw an exception. ---> System.PlatformNotSupportedException: Operation is not supported on this platform. at System.ConsolePal.OpenStandardInput() at System.Console.OpenStandardInput() at Microsoft.Scripting.Utils.ConsoleInputStream..ctor() at Microsoft.Scripting.Utils.ConsoleInputStream..cctor() --- End of inner exception stack trace --- at Microsoft.Scripting.Runtime.SharedIO.InitializeInput() at Microsoft.Scripting.Runtime.SharedIO.get_InputReader() at Microsoft.Scripting.Runtime.SharedIO.GetReader(Encoding& encoding) at IronPython.Runtime.PythonFile.CreateConsoleReader() at IronPython.Runtime.PythonFile.InitializeConsole(SharedIO io, ConsoleStreamType type, String name) at IronPython.Runtime.PythonFile.CreateConsole(PythonContext context, SharedIO io, ConsoleStreamType type, String name) at IronPython.Runtime.PythonContext.SetStandardIO() at IronPython.Runtime.PythonContext.InitializeSystemState() at IronPython.Runtime.PythonContext..ctor(ScriptDomainManager manager, IDictionary2 options) at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, Object[] parameters, Boolean wrapExceptions) --- End of inner exception stack trace --- at Microsoft.Scripting.Runtime.LanguageConfiguration.LoadLanguageContext(ScriptDomainManager domainManager, Boolean& alreadyLoaded) at Microsoft.Scripting.Runtime.DlrConfiguration.LoadLanguageContext(ScriptDomainManager manager, LanguageConfiguration config) at Microsoft.Scripting.Runtime.DlrConfiguration.TryLoadLanguage(ScriptDomainManager manager, AssemblyQualifiedTypeName& providerName, LanguageContext& language) at Microsoft.Scripting.Runtime.ScriptDomainManager.GetLanguageByTypeName(String providerAssemblyQualifiedTypeName) at Microsoft.Scripting.Hosting.ScriptRuntime.GetEngineByTypeName(String assemblyQualifiedTypeName) at IronPython.Hosting.Python.GetEngine(ScriptRuntime runtime) at IronPython.Hosting.Python.CreateEngine(IDictionary2 options)

slozier commented 3 years ago

Thanks for the report. I've always wanted to try IronPython in Blazor but never got around to it. I was able to reproduce the issue but I'm having a hard time attaching a debugger to the project. Would be nice if it worked out of the box, but for now, here's a potential workaround for the issue:

var runtime = Python.CreateRuntime();
runtime.IO.SetInput(new MemoryStream(), Encoding.Default);
var engine = Python.GetEngine(runtime);
JimmyHeartbreaker commented 3 years ago

for me, the debugger takes a few moments to attach (the first run through never debugs for me) and occasionally not at all. Its a bit flaky to say the least. I use vanilla chrome.

Thx, I will try the work around. [edit] Works!

syedqutub commented 3 years ago

I got path1 Value can not be null exception when trying the work around. Can anyone give me a small working sample of the work around

slozier commented 3 years ago

@syedqutub I'm guessing you're running into this issue: https://github.com/IronLanguages/ironpython2/issues/762

syedqutub commented 3 years ago

@syedqutub I'm guessing you're running into this issue: #762

Yuo. Any work around?

slozier commented 3 years ago

No workaround that I know of - will need a release to fix.

TwoUnderscorez commented 2 years ago

@slozier @JimmyHeartbreaker Fixed IronPython3, might have fixed 2 as well. https://github.com/IronLanguages/dlr/pull/256