47
47
import com .oracle .graal .python .builtins .CoreFunctions ;
48
48
import com .oracle .graal .python .builtins .PythonBuiltins ;
49
49
import com .oracle .graal .python .builtins .objects .PNone ;
50
- import com .oracle .graal .python .builtins .objects .function .PArguments ;
51
50
import com .oracle .graal .python .builtins .objects .function .PBuiltinFunction ;
51
+ import com .oracle .graal .python .builtins .objects .function .PFunction ;
52
+ import com .oracle .graal .python .builtins .objects .function .PythonCallable ;
52
53
import com .oracle .graal .python .builtins .objects .ints .PInt ;
53
54
import com .oracle .graal .python .builtins .objects .method .PBuiltinMethod ;
55
+ import com .oracle .graal .python .builtins .objects .method .PMethod ;
56
+ import com .oracle .graal .python .nodes .argument .CreateArgumentsNode ;
54
57
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
55
58
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
56
59
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
@@ -135,6 +138,8 @@ Object getsignal(int signum) {
135
138
@ Builtin (name = "signal" , fixedNumOfArguments = 2 )
136
139
@ GenerateNodeFactory
137
140
abstract static class SignalNode extends PythonBinaryBuiltinNode {
141
+ @ Child CreateArgumentsNode createArgs = CreateArgumentsNode .create ();
142
+
138
143
@ Specialization
139
144
@ TruffleBoundary
140
145
Object signal (int signum , int id ) {
@@ -155,25 +160,7 @@ Object signal(int signum, int id) {
155
160
return retval ;
156
161
}
157
162
158
- @ Specialization
159
- @ TruffleBoundary
160
- Object signal (int signum , PBuiltinMethod handler ) {
161
- // TODO: (tfel) definitely wrong
162
- return signal (signum , handler .getFunction ());
163
- }
164
-
165
- // TODO: This needs to be fixed, any object with a "__call__" should work
166
- @ Specialization
167
- @ TruffleBoundary
168
- Object signal (int signum , PBuiltinFunction handler ) {
169
- RootCallTarget callTarget = handler .getCallTarget ();
170
- Object [] arguments = PArguments .create (2 );
171
- PArguments .setArgument (arguments , 0 , signum );
172
- // TODO: the second argument should be the interrupted, currently executing frame
173
- // we'll get that when we switch to executing these handlers (just like finalizers)
174
- // on the main thread
175
- PArguments .setArgument (arguments , 1 , PNone .NONE );
176
-
163
+ private Object installSignalHandler (int signum , PythonCallable handler , RootCallTarget callTarget , Object [] arguments ) {
177
164
Object retval ;
178
165
try {
179
166
retval = Signals .setSignalHandler (signum , () -> {
@@ -192,6 +179,34 @@ Object signal(int signum, PBuiltinFunction handler) {
192
179
signalHandlers .put (signum , handler );
193
180
return retval ;
194
181
}
182
+ // TODO: This needs to be fixed, any object with a "__call__" should work
183
+
184
+ // TODO: the second argument should be the interrupted, currently executing frame
185
+ // we'll get that when we switch to executing these handlers (just like finalizers)
186
+ // on the main thread
187
+ @ Specialization
188
+ @ TruffleBoundary
189
+ Object signal (int signum , PBuiltinMethod handler ) {
190
+ return installSignalHandler (signum , handler , handler .getCallTarget (), createArgs .executeWithSelf (handler .getSelf (), new Object []{signum , PNone .NONE }));
191
+ }
192
+
193
+ @ Specialization
194
+ @ TruffleBoundary
195
+ Object signal (int signum , PBuiltinFunction handler ) {
196
+ return installSignalHandler (signum , handler , handler .getCallTarget (), createArgs .execute (new Object []{signum , PNone .NONE }));
197
+ }
198
+
199
+ @ Specialization
200
+ @ TruffleBoundary
201
+ Object signal (int signum , PMethod handler ) {
202
+ return installSignalHandler (signum , handler , handler .getCallTarget (), createArgs .executeWithSelf (handler .getSelf (), new Object []{signum , PNone .NONE }));
203
+ }
204
+
205
+ @ Specialization
206
+ @ TruffleBoundary
207
+ Object signal (int signum , PFunction handler ) {
208
+ return installSignalHandler (signum , handler , handler .getCallTarget (), createArgs .execute (new Object []{signum , PNone .NONE }));
209
+ }
195
210
}
196
211
}
197
212
0 commit comments