@@ -25,6 +25,28 @@ namespace PythonConsoleControl
25
25
/// </summary>
26
26
public class PythonConsole : IConsole , IDisposable
27
27
{
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
+
28
50
bool allowFullAutocompletion = true ;
29
51
public bool AllowFullAutocompletion
30
52
{
@@ -380,15 +402,33 @@ void ExecuteStatements()
380
402
}
381
403
else
382
404
{
383
- try
405
+ Exception capturedEx = null ;
406
+ var dispatcher = GetCommandDispatcherSafe ( ) ;
407
+
408
+ ManualResetEventSlim done = new ManualResetEventSlim ( ) ;
409
+
410
+ dispatcher ( ( ) =>
384
411
{
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 )
389
429
{
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 ;
392
432
}
393
433
}
394
434
}
0 commit comments