Skip to content

Commit ce434b5

Browse files
authored
Clean up and modernize code in PythonSignal (#1941)
* Enable nullable annotations in module PythonSignal * Clean up and modernize code in PythonSignal * Fix nit
1 parent 0a18220 commit ce434b5

File tree

4 files changed

+154
-112
lines changed

4 files changed

+154
-112
lines changed

src/core/IronPython.Modules/NtSignalState.cs

+21-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System;
68
using System.Runtime.InteropServices;
79
using System.Runtime.Versioning;
@@ -13,17 +15,19 @@
1315
namespace IronPython.Modules {
1416
public static partial class PythonSignal {
1517
[SupportedOSPlatform("windows")]
16-
internal class NtSignalState : PythonSignalState {
17-
//We use a single Windows event handler to process all signals. This handler simply
18-
//delegates the work out to PySignalToPyHandler.
19-
public NativeSignal.WinSignalsHandler WinAllSignalsHandlerDelegate;
18+
private class NtSignalState : PythonSignalState {
19+
// We use a single Windows event handler to process all signals. This handler simply
20+
// delegates the work out to PySignalToPyHandler.
21+
public NativeWindowsSignal.WinSignalsHandler WinAllSignalsHandlerDelegate;
22+
2023

2124
public NtSignalState(PythonContext pc) : base(pc) {
22-
WinAllSignalsHandlerDelegate = new NativeSignal.WinSignalsHandler(WindowsEventHandler);
23-
NativeSignal.SetConsoleCtrlHandler(this.WinAllSignalsHandlerDelegate, true);
25+
WinAllSignalsHandlerDelegate = new NativeWindowsSignal.WinSignalsHandler(WindowsEventHandler);
26+
NativeWindowsSignal.SetConsoleCtrlHandler(this.WinAllSignalsHandlerDelegate, true);
2427
}
2528

26-
//Our implementation of WinSignalsHandler
29+
30+
// Our implementation of WinSignalsHandler
2731
private bool WindowsEventHandler(uint winSignal) {
2832
bool retVal;
2933
int pySignal;
@@ -53,27 +57,27 @@ private bool WindowsEventHandler(uint winSignal) {
5357
int tempId = (int)PySignalToPyHandler[pySignal];
5458

5559
if (tempId == SIG_DFL) {
56-
//SIG_DFL - we let Windows do whatever it normally would
60+
// SIG_DFL - we let Windows do whatever it normally would
5761
retVal = false;
5862
} else if (tempId == SIG_IGN) {
59-
//SIG_IGN - we do nothing, but tell Windows we handled the signal
63+
// SIG_IGN - we do nothing, but tell Windows we handled the signal
6064
retVal = true;
6165
} else {
6266
throw new Exception("unreachable");
6367
}
6468
} else if (PySignalToPyHandler[pySignal] == default_int_handler) {
6569
if (pySignal != SIGINT) {
66-
//We're dealing with the default_int_handlerImpl which we
67-
//know doesn't care about the frame parameter
70+
// We're dealing with the default_int_handlerImpl which we
71+
// know doesn't care about the frame parameter
6872
retVal = true;
6973
default_int_handlerImpl(pySignal, null);
7074
} else {
71-
//Let the real interrupt handler throw a KeyboardInterrupt for SIGINT.
72-
//It handles this far more gracefully than we can
75+
// Let the real interrupt handler throw a KeyboardInterrupt for SIGINT.
76+
// It handles this far more gracefully than we can
7377
retVal = false;
7478
}
7579
} else {
76-
//We're dealing with a callable matching PySignalHandler's signature
80+
// We're dealing with a callable matching PySignalHandler's signature
7781
retVal = true;
7882
PySignalHandler temp = (PySignalHandler)Converter.ConvertToDelegate(PySignalToPyHandler[pySignal],
7983
typeof(PySignalHandler));
@@ -96,7 +100,9 @@ private bool WindowsEventHandler(uint winSignal) {
96100
}
97101
}
98102

99-
internal static class NativeSignal {
103+
104+
[SupportedOSPlatform("windows")]
105+
internal static class NativeWindowsSignal {
100106
// Windows API expects to be given a function pointer like this to handle signals
101107
internal delegate bool WinSignalsHandler(uint winSignal);
102108

src/core/IronPython.Modules/SimpleSignalState.cs

+15-22
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,49 @@
1212

1313
namespace IronPython.Modules {
1414
public static partial class PythonSignal {
15-
internal class SimpleSignalState : PythonSignalState {
15+
private class SimpleSignalState : PythonSignalState {
16+
1617
public SimpleSignalState(PythonContext pc)
1718
: base(pc) {
1819
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
1920
}
2021

21-
private void Console_CancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
22-
int pySignal;
23-
switch (e.SpecialKey) {
24-
case ConsoleSpecialKey.ControlC:
25-
pySignal = SIGINT;
26-
break;
27-
28-
case ConsoleSpecialKey.ControlBreak:
29-
pySignal = SIGBREAK;
30-
break;
3122

32-
default:
33-
throw new InvalidOperationException("unreachable");
34-
}
23+
private void Console_CancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
24+
int pySignal = e.SpecialKey switch {
25+
ConsoleSpecialKey.ControlC => SIGINT,
26+
ConsoleSpecialKey.ControlBreak => SIGBREAK,
27+
_ => throw new InvalidOperationException("unreachable"),
28+
};
3529

3630
lock (PySignalToPyHandler) {
3731
if (PySignalToPyHandler[pySignal].GetType() == typeof(int)) {
3832
int tempId = (int)PySignalToPyHandler[pySignal];
3933

4034
if (tempId == SIG_DFL) {
41-
//SIG_DFL - do whatever it normally would
35+
// SIG_DFL - do whatever it normally would
4236
return;
4337
} else if (tempId == SIG_IGN) {
44-
//SIG_IGN - we do nothing, but tell the OS we handled the signal
38+
// SIG_IGN - we do nothing, but tell the OS we handled the signal
4539
e.Cancel = false;
4640
return;
4741
} else {
4842
throw new Exception("unreachable");
4943
}
5044
} else if (PySignalToPyHandler[pySignal] == default_int_handler) {
5145
if (pySignal != SIGINT) {
52-
//We're dealing with the default_int_handlerImpl which we
53-
//know doesn't care about the frame parameter
46+
// We're dealing with the default_int_handlerImpl which we
47+
// know doesn't care about the frame parameter
5448
e.Cancel = true;
5549
default_int_handlerImpl(pySignal, null);
5650
return;
5751
} else {
58-
//Let the real interrupt handler throw a KeyboardInterrupt for SIGINT.
59-
//It handles this far more gracefully than we can
52+
// Let the real interrupt handler throw a KeyboardInterrupt for SIGINT.
53+
// It handles this far more gracefully than we can
6054
return;
6155
}
6256
} else {
63-
//We're dealing with a callable matching PySignalHandler's signature
57+
// We're dealing with a callable matching PySignalHandler's signature
6458
PySignalHandler temp = (PySignalHandler)Converter.ConvertToDelegate(PySignalToPyHandler[pySignal],
6559
typeof(PySignalHandler));
6660

@@ -86,4 +80,3 @@ private void Console_CancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
8680
}
8781

8882
#endif
89-

src/core/IronPython.Modules/nt.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1870,13 +1870,15 @@ public static void kill(CodeContext/*!*/ context, int pid, int sig) {
18701870
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
18711871
RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
18721872
killUnix(pid, sig);
1873-
} else {
1874-
if (PythonSignal.NativeSignal.GenerateConsoleCtrlEvent((uint)sig, (uint)pid)) return;
1873+
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
1874+
if (PythonSignal.NativeWindowsSignal.GenerateConsoleCtrlEvent((uint)sig, (uint)pid)) return;
18751875

18761876
// If the calls to GenerateConsoleCtrlEvent didn't work, simply
18771877
// forcefully kill the process.
18781878
Process toKill = Process.GetProcessById(pid);
18791879
toKill.Kill();
1880+
} else {
1881+
throw new PlatformNotSupportedException();
18801882
}
18811883
}
18821884

0 commit comments

Comments
 (0)