forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignal.SimpleSignalState.cs
More file actions
76 lines (64 loc) · 3.09 KB
/
Copy pathsignal.SimpleSignalState.cs
File metadata and controls
76 lines (64 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
#nullable enable
#if FEATURE_PROCESS
using System;
using System.Runtime.InteropServices;
using Microsoft.Scripting.Hosting.Shell;
using IronPython.Runtime;
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 BasicConsole console) {
// in console hosting scenarios, we need to override the console handler of Ctrl+C
_consoleHandler = console.ConsoleCancelEventHandler;
console.ConsoleCancelEventHandler = null;
}
}
protected override void Dispose(bool disposing) {
if (disposing) {
Console.CancelKeyPress -= new ConsoleCancelEventHandler(Console_CancelKeyPress);
if (_consoleHandler != null && DefaultContext.DefaultPythonContext.Console is BasicConsole console) {
// restore the original console handler
console.ConsoleCancelEventHandler = _consoleHandler;
}
}
base.Dispose(disposing);
}
private void Console_CancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
int pySignal = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? SIGINT
: e.SpecialKey switch {
ConsoleSpecialKey.ControlC => SIGINT,
ConsoleSpecialKey.ControlBreak => SIGBREAK,
_ => throw new InvalidOperationException("unreachable"),
};
object? handler = PySignalToPyHandler[pySignal];
if (handler is int tempId) {
if (tempId == SIG_DFL) {
// SIG_DFL - do whatever it normally would
return;
} else if (tempId == SIG_IGN) {
// SIG_IGN - we do nothing, but tell the OS we handled the signal
e.Cancel = true;
return;
} else {
throw new InvalidOperationException("unreachable");
}
} else if (ReferenceEquals(handler, default_int_handler) && pySignal == SIGINT) {
// Forward the signal to the console handler, if any
_consoleHandler?.Invoke(sender, e);
return;
} else {
CallPythonHandler(pySignal, handler);
e.Cancel = true;
return;
}
}
}
}
}
#endif