40
40
*/
41
41
package com .oracle .graal .python .builtins .modules ;
42
42
43
- import static java .lang .StrictMath .toIntExact ;
44
-
45
43
import java .util .List ;
46
44
import java .util .concurrent .ConcurrentHashMap ;
47
45
import java .util .concurrent .ConcurrentLinkedDeque ;
48
46
import java .util .concurrent .Semaphore ;
49
47
50
48
import com .oracle .graal .python .builtins .Builtin ;
51
49
import com .oracle .graal .python .builtins .CoreFunctions ;
52
- import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
53
50
import com .oracle .graal .python .builtins .PythonBuiltins ;
54
51
import com .oracle .graal .python .builtins .objects .PNone ;
55
52
import com .oracle .graal .python .builtins .objects .ints .PInt ;
56
53
import com .oracle .graal .python .builtins .objects .module .PythonModule ;
54
+ import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
57
55
import com .oracle .graal .python .nodes .attributes .ReadAttributeFromObjectNode ;
58
56
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
59
57
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
69
67
import com .oracle .truffle .api .dsl .NodeFactory ;
70
68
import com .oracle .truffle .api .dsl .Specialization ;
71
69
import com .oracle .truffle .api .dsl .TypeSystemReference ;
70
+ import com .oracle .truffle .api .library .CachedLibrary ;
72
71
import com .oracle .truffle .api .object .HiddenKey ;
73
72
74
73
@ CoreFunctions (defineModule = "_signal" )
@@ -205,23 +204,25 @@ Object defaultIntHandler(@SuppressWarnings("unused") Object[] args) {
205
204
@ Builtin (name = "signal" , minNumOfPositionalArgs = 3 , declaresExplicitSelf = true )
206
205
@ GenerateNodeFactory
207
206
abstract static class SignalNode extends PythonTernaryBuiltinNode {
208
- private int getSignum (long signum ) {
209
- try {
210
- return toIntExact (signum );
211
- } catch (ArithmeticException ae ) {
212
- throw raise (PythonBuiltinClassType .OverflowError , "Python int too large to convert to C int" );
213
- }
207
+
208
+ @ Specialization (guards = "!idNumLib.isCallable(idNum)" , limit = "1" )
209
+ Object signalId (@ SuppressWarnings ("unused" ) PythonModule self , Object signal , Object idNum ,
210
+ @ SuppressWarnings ("unused" ) @ CachedLibrary ("idNum" ) PythonObjectLibrary idNumLib ,
211
+ @ CachedLibrary ("signal" ) PythonObjectLibrary signalLib ) {
212
+ // Note: CPython checks if id is the same reference as SIG_IGN/SIG_DFL constants, which
213
+ // are instances of Handlers enum
214
+ // The -1 fallback will be correctly reported as an error later on
215
+ int id = idNum instanceof Integer ? (int ) idNum : -1 ;
216
+ return signal (signalLib .asSize (signal ), id );
214
217
}
215
218
216
- @ Specialization
217
219
@ TruffleBoundary
218
- Object signal (@ SuppressWarnings ("unused" ) PythonModule self , long signalNumber , int id ) {
219
- int signum = getSignum (signalNumber );
220
+ private Object signal (int signum , int id ) {
220
221
Object retval ;
221
222
try {
222
223
retval = Signals .setSignalHandler (signum , id );
223
224
} catch (IllegalArgumentException e ) {
224
- throw raise (PythonErrorType .ValueError , e );
225
+ throw raise (PythonErrorType .TypeError , "TypeError: signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object" );
225
226
}
226
227
if ((int ) retval == Signals .SIG_UNKNOWN ) {
227
228
if (signalHandlers .containsKey (signum )) {
@@ -234,12 +235,17 @@ Object signal(@SuppressWarnings("unused") PythonModule self, long signalNumber,
234
235
return retval ;
235
236
}
236
237
237
- @ Specialization
238
- @ TruffleBoundary
239
- Object signal (PythonModule self , long signalNumber , Object handler ,
238
+ @ Specialization (guards = "handlerLib.isCallable(handler)" , limit = "1" )
239
+ Object signalHandler (PythonModule self , Object signal , Object handler ,
240
+ @ SuppressWarnings ("unused" ) @ CachedLibrary ("handler" ) PythonObjectLibrary handlerLib ,
241
+ @ CachedLibrary ("signal" ) PythonObjectLibrary signalLib ,
240
242
@ Cached ("create()" ) ReadAttributeFromObjectNode readQueueNode ,
241
243
@ Cached ("create()" ) ReadAttributeFromObjectNode readSemaNode ) {
242
- int signum = getSignum (signalNumber );
244
+ return signal (self , signalLib .asSize (signal ), handler , readQueueNode , readSemaNode );
245
+ }
246
+
247
+ @ TruffleBoundary
248
+ private Object signal (PythonModule self , int signum , Object handler , ReadAttributeFromObjectNode readQueueNode , ReadAttributeFromObjectNode readSemaNode ) {
243
249
ConcurrentLinkedDeque <SignalTriggerAction > queue = getQueue (self , readQueueNode );
244
250
Semaphore semaphore = getSemaphore (self , readSemaNode );
245
251
Object retval ;
0 commit comments