@@ -25,6 +25,28 @@ namespace PythonConsoleControl
2525 /// </summary>
2626 public class PythonConsole : IConsole , IDisposable
2727 {
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+
2850 bool allowFullAutocompletion = true ;
2951 public bool AllowFullAutocompletion
3052 {
@@ -380,15 +402,33 @@ void ExecuteStatements()
380402 }
381403 else
382404 {
383- try
405+ Exception capturedEx = null ;
406+ var dispatcher = GetCommandDispatcherSafe ( ) ;
407+
408+ ManualResetEventSlim done = new ManualResetEventSlim ( ) ;
409+
410+ dispatcher ( ( ) =>
384411 {
385- executing = true ;
386- GetCommandDispatcher ( ) ( ( ) => scriptSource . Execute ( commandLine . ScriptScope ) ) ;
387- }
388- catch ( Exception ex )
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 )
389429 {
390- ExceptionOperations eo = commandLine . ScriptScope . Engine . GetService < ExceptionOperations > ( ) ;
391- error = eo . FormatException ( ex ) + Environment . NewLine ;
430+ var eo = commandLine . ScriptScope . Engine . GetService < ExceptionOperations > ( ) ;
431+ error = eo . FormatException ( capturedEx ) + Environment . NewLine ;
392432 }
393433 }
394434 }
0 commit comments