Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/core/IronPython.Modules/signal.SimpleSignalState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@
namespace IronPython.Modules {
public static partial class PythonSignal {
private class SimpleSignalState : PythonSignalState {
private readonly ConsoleCancelEventHandler? _consoleHandler;

public SimpleSignalState(PythonContext pc) : base(pc) {
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
if (pc.Console is Microsoft.Scripting.Hosting.Shell.BasicConsole console) {
// in console hosting scenarios, we need to override the console handler of Ctrl+C
_consoleHandler = console.ConsoleCancelEventHandler;
console.ConsoleCancelEventHandler = null;
}
}


Expand All @@ -42,8 +48,8 @@ private void Console_CancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
throw new InvalidOperationException("unreachable");
}
} else if (ReferenceEquals(handler, default_int_handler) && pySignal == SIGINT) {
// Let the real interrupt handler throw a KeyboardInterrupt for SIGINT.
// It handles this far more gracefully than we can
// Forward the signal to the console handler, if any
_consoleHandler?.Invoke(sender, e);
return;
} else {
CallPythonHandler(pySignal, handler);
Expand Down
1 change: 1 addition & 0 deletions src/core/IronPython/Hosting/PythonCommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ protected override void Initialize() {

Console.Output = new OutputWriter(PythonContext, false);
Console.ErrorOutput = new OutputWriter(PythonContext, true);
Language.Console = Console;

// TODO: must precede path initialization! (??? - test test_importpkg.py)
int pathIndex = PythonContext.PythonOptions.SearchPaths.Count;
Expand Down
10 changes: 10 additions & 0 deletions src/core/IronPython/Runtime/PythonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Microsoft.Scripting.Actions;
using Microsoft.Scripting.Debugging.CompilerServices;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Hosting.Shell;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

Expand Down Expand Up @@ -684,6 +685,15 @@ internal static Version GetPythonVersion()

internal FloatFormat DoubleFormat { get; set; }

#nullable enable

/// <summary>
/// Not null if the Python context is running in a console host.
/// </summary>
internal IConsole? Console { get; set; }

#nullable restore

/// <summary>
/// Initializes the sys module on startup. Called both to load and reload sys
/// </summary>
Expand Down
Loading