46
46
import static com .oracle .graal .python .util .PythonUtils .tsLiteral ;
47
47
import static com .oracle .graal .python .util .TimeUtils .SEC_TO_US ;
48
48
49
+ import java .util .HashMap ;
49
50
import java .util .List ;
50
51
import java .util .Map ;
51
52
import java .util .concurrent .ConcurrentHashMap ;
85
86
import com .oracle .graal .python .nodes .util .CastToJavaIntExactNode ;
86
87
import com .oracle .graal .python .runtime .AsyncHandler ;
87
88
import com .oracle .graal .python .runtime .PosixSupportLibrary ;
89
+ import com .oracle .graal .python .runtime .PythonContext ;
88
90
import com .oracle .graal .python .runtime .PythonOptions ;
89
91
import com .oracle .graal .python .runtime .exception .PException ;
90
92
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
@@ -127,18 +129,20 @@ public void initialize(Python3Core core) {
127
129
addBuiltinConstant ("NSIG" , Signals .SIGMAX + 1 );
128
130
}
129
131
130
- public enum EmulatedSignal {
131
- SIGTERM (15 ),
132
- SIGKILL (9 ),
133
- SIGINT (2 ),
134
- SIGABRT (6 ),
135
- SIGQUIT (3 );
132
+ /*
133
+ * When using emulated posix mode (where we esentially emulate Linux), we rely on some signals
134
+ * being present even if they don't exist on the underlying platform, i.e. SIGKILL doesn't exist
135
+ * on Windows, but we still need to pass it to emulated kill.
136
+ */
137
+ private enum EmulatedSignal {
138
+ TERM (15 ),
139
+ KILL (9 );
136
140
137
141
public final TruffleString name ;
138
142
public final int number ;
139
143
140
144
EmulatedSignal (int number ) {
141
- this .name = tsLiteral (name ());
145
+ this .name = tsLiteral ("SIG" + name ());
142
146
this .number = number ;
143
147
}
144
148
}
@@ -151,15 +155,19 @@ public void postInitialize(Python3Core core) {
151
155
ModuleData moduleData = new ModuleData ();
152
156
signalModule .setModuleState (moduleData );
153
157
158
+ for (int i = 0 ; i < Signals .PYTHON_SIGNAL_NAMES .length ; i ++) {
159
+ TruffleString name = Signals .PYTHON_SIGNAL_NAMES [i ];
160
+ if (name != null ) {
161
+ moduleData .signals .put (Signals .SIGNAL_NAMES [i ], i );
162
+ signalModule .setAttribute (name , i );
163
+ }
164
+ }
165
+
154
166
if (PosixSupportLibrary .getUncached ().getBackend (core .getContext ().getPosixSupport ()).equalsUncached (T_JAVA , TS_ENCODING )) {
155
167
for (EmulatedSignal signal : EmulatedSignal .values ()) {
156
- signalModule .setAttribute (signal .name , signal .number );
157
- }
158
- } else {
159
- for (int i = 0 ; i < Signals .PYTHON_SIGNAL_NAMES .length ; i ++) {
160
- TruffleString name = Signals .PYTHON_SIGNAL_NAMES [i ];
161
- if (name != null ) {
162
- signalModule .setAttribute (name , i );
168
+ if (signalModule .getAttribute (signal .name ) == PNone .NO_VALUE ) {
169
+ moduleData .signals .put (signal .name (), signal .number );
170
+ signalModule .setAttribute (signal .name , signal .number );
163
171
}
164
172
}
165
173
}
@@ -186,6 +194,12 @@ public void postInitialize(Python3Core core) {
186
194
}
187
195
}
188
196
197
+ @ TruffleBoundary
198
+ public static int signalFromName (PythonContext context , String name ) {
199
+ PythonModule mod = context .lookupBuiltinModule (T__SIGNAL );
200
+ return mod .getModuleState (ModuleData .class ).signals .getOrDefault (name , -1 );
201
+ }
202
+
189
203
public static void resetSignalHandlers (PythonModule mod ) {
190
204
ModuleData data = mod .getModuleState (ModuleData .class );
191
205
if (data != null ) {
@@ -546,6 +560,7 @@ static long getOldDelay(ModuleData moduleData) {
546
560
}
547
561
548
562
private static final class ModuleData {
563
+ final Map <String , Integer > signals = new HashMap <>();
549
564
final ConcurrentHashMap <Integer , Object > signalHandlers = new ConcurrentHashMap <>();
550
565
final ConcurrentHashMap <Integer , SignalHandler > defaultSignalHandlers = new ConcurrentHashMap <>();
551
566
final ConcurrentLinkedDeque <SignalTriggerAction > signalQueue = new ConcurrentLinkedDeque <>();
0 commit comments