1414using ICSharpCode . AvalonEdit . Editing ;
1515using ICSharpCode . AvalonEdit . Document ;
1616using Style = Microsoft . Scripting . Hosting . Shell . Style ;
17- using System . Runtime . Remoting ;
1817
1918namespace PythonConsoleControl
2019{
@@ -26,6 +25,28 @@ namespace PythonConsoleControl
2625 /// </summary>
2726 public class PythonConsole : IConsole , IDisposable
2827 {
28+
29+ Action < Action > commandDispatcher ;
30+ public Action < Action > GetCommandDispatcherSafe ( )
31+ {
32+ if ( commandDispatcher == null )
33+ {
34+ try
35+ {
36+ var languageContext = Microsoft . Scripting . Hosting . Providers . HostingHelpers . GetLanguageContext ( commandLine . ScriptScope . Engine ) ;
37+ var pythonContext = ( IronPython . Runtime . PythonContext ) languageContext ;
38+ commandDispatcher = pythonContext . GetSetCommandDispatcher ( null ) ;
39+ pythonContext . GetSetCommandDispatcher ( commandDispatcher ) ;
40+ }
41+ catch ( Exception ex )
42+ {
43+ Debug . WriteLine ( "Failed to get dispatcher: " + ex . Message ) ;
44+ commandDispatcher = action => action ( ) ; // fallback dispatcher
45+ }
46+ }
47+ return commandDispatcher ;
48+ }
49+
2950 bool allowFullAutocompletion = true ;
3051 public bool AllowFullAutocompletion
3152 {
@@ -381,13 +402,35 @@ void ExecuteStatements()
381402 }
382403 else
383404 {
384- ObjectHandle wrapexception = null ;
385- GetCommandDispatcher ( ) ( ( ) => scriptSource . ExecuteAndWrap ( commandLine . ScriptScope , out wrapexception ) ) ;
386- if ( wrapexception != null )
405+ Exception capturedEx = null ;
406+ var dispatcher = GetCommandDispatcherSafe ( ) ;
407+
408+ ManualResetEventSlim done = new ManualResetEventSlim ( ) ;
409+
410+ dispatcher ( ( ) =>
411+ {
412+ try
413+ {
414+ scriptSource . Execute ( commandLine . ScriptScope ) ;
415+ }
416+ catch ( Exception ex )
417+ {
418+ capturedEx = ex ;
419+ }
420+ finally
421+ {
422+ done . Set ( ) ;
423+ }
424+ } ) ;
425+
426+ done . Wait ( ) ;
427+
428+ if ( capturedEx != null )
387429 {
388- error = "Exception : " + wrapexception . Unwrap ( ) . ToString ( ) + "\n " ;
430+ var eo = commandLine . ScriptScope . Engine . GetService < ExceptionOperations > ( ) ;
431+ error = eo . FormatException ( capturedEx ) + Environment . NewLine ;
389432 }
390- }
433+ }
391434 }
392435 catch ( ThreadAbortException tae )
393436 {
0 commit comments