60
60
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
61
61
import com .oracle .graal .python .builtins .PythonBuiltins ;
62
62
import com .oracle .graal .python .builtins .objects .PNone ;
63
+ import com .oracle .graal .python .builtins .objects .exception .OSErrorEnum ;
63
64
import com .oracle .graal .python .builtins .objects .ints .PInt ;
64
65
import com .oracle .graal .python .builtins .objects .list .PList ;
65
66
import com .oracle .graal .python .builtins .objects .socket .PSocket ;
73
74
import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
74
75
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
75
76
import com .oracle .truffle .api .dsl .Cached ;
77
+ import com .oracle .truffle .api .dsl .Fallback ;
76
78
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
77
79
import com .oracle .truffle .api .dsl .NodeFactory ;
78
80
import com .oracle .truffle .api .dsl .Specialization ;
81
+ import com .oracle .truffle .api .frame .VirtualFrame ;
79
82
80
83
import org .graalvm .nativeimage .ImageInfo ;
81
84
@@ -169,7 +172,8 @@ private static String searchServicesForPort(int port, String protocol) {
169
172
return null ;
170
173
}
171
174
172
- private static String [] cleanLine (String line ) {
175
+ private static String [] cleanLine (String input ) {
176
+ String line = input ;
173
177
if (line .startsWith ("#" )) {
174
178
return null ;
175
179
}
@@ -203,67 +207,34 @@ Object socket(LazyPythonClass cls, @SuppressWarnings("unused") PNone family, @Su
203
207
return createSocketInternal (cls , PSocket .AF_INET , PSocket .SOCK_STREAM , 0 );
204
208
}
205
209
206
- @ Specialization (guards = {"isNoValue(family)" , "isNoValue(type)" , "isNoValue(proto)" })
207
- Object socket (LazyPythonClass cls , @ SuppressWarnings ("unused" ) PNone family , @ SuppressWarnings ("unused" ) PNone type , @ SuppressWarnings ("unused" ) PNone proto , int fileno ) {
208
- return createSocketInternal (cls , -1 , -1 , -1 , fileno );
210
+ @ Specialization (guards = {"isNoValue(family)" , "isNoValue(type)" , "isNoValue(proto)" , "!isNoValue(fileno)" })
211
+ Object socket (VirtualFrame frame , LazyPythonClass cls , @ SuppressWarnings ("unused" ) PNone family , @ SuppressWarnings ("unused" ) PNone type , @ SuppressWarnings ("unused" ) PNone proto , Object fileno ,
212
+ @ Cached CastToIndexNode cast ) {
213
+ return createSocketInternal (frame , cls , -1 , -1 , -1 , cast .execute (fileno ));
209
214
}
210
215
211
- @ Specialization (guards = {"isNoValue(type)" , "isNoValue(proto)" , "isNoValue(fileno)" })
212
- Object socket (LazyPythonClass cls , PInt family , @ SuppressWarnings ("unused" ) PNone type , @ SuppressWarnings ("unused" ) PNone proto , @ SuppressWarnings ("unused" ) PNone fileno ) {
213
- return createSocketInternal (cls , family .intValue (), PSocket .SOCK_STREAM , 0 );
214
- }
215
-
216
- @ Specialization (guards = {"isNoValue(type)" , "isNoValue(proto)" , "isNoValue(fileno)" })
216
+ @ Specialization (guards = {"!isNoValue(family)" , "isNoValue(type)" , "isNoValue(proto)" , "isNoValue(fileno)" })
217
217
Object socket (LazyPythonClass cls , Object family , @ SuppressWarnings ("unused" ) PNone type , @ SuppressWarnings ("unused" ) PNone proto , @ SuppressWarnings ("unused" ) PNone fileno ,
218
218
@ Cached CastToIndexNode cast ) {
219
219
return createSocketInternal (cls , cast .execute (family ), PSocket .SOCK_STREAM , 0 );
220
220
}
221
221
222
- @ Specialization (guards = {"isNoValue(proto)" , "isNoValue(fileno)" })
222
+ @ Specialization (guards = {"!isNoValue(family)" , "!isNoValue(type)" , " isNoValue(proto)" , "isNoValue(fileno)" })
223
223
Object socket (LazyPythonClass cls , Object family , Object type , @ SuppressWarnings ("unused" ) PNone proto , @ SuppressWarnings ("unused" ) PNone fileno ,
224
224
@ Cached CastToIndexNode cast ) {
225
225
return createSocketInternal (cls , cast .execute (family ), cast .execute (type ), 0 );
226
226
}
227
227
228
- @ Specialization (guards = {"isNoValue(proto)" , "isNoValue(fileno)" })
229
- Object socket (LazyPythonClass cls , int family , long type , @ SuppressWarnings ("unused" ) PNone proto , @ SuppressWarnings ("unused" ) PNone fileno ) {
230
- return createSocketInternal (cls , family , ((int ) type ), 0 );
231
- }
232
-
233
- @ Specialization (guards = {"isNoValue(proto)" , "isNoValue(fileno)" })
234
- Object socket (LazyPythonClass cls , PInt family , long type , @ SuppressWarnings ("unused" ) PNone proto , @ SuppressWarnings ("unused" ) PNone fileno ) {
235
- return createSocketInternal (cls , family .asInt (), ((int ) type ), 0 );
236
- }
237
-
238
- @ Specialization (guards = {"isNoValue(fileno)" })
228
+ @ Specialization (guards = {"!isNoValue(family)" , "!isNoValue(type)" , "!isNoValue(proto)" , "isNoValue(fileno)" })
239
229
Object socket (LazyPythonClass cls , Object family , Object type , Object proto , @ SuppressWarnings ("unused" ) PNone fileno ,
240
230
@ Cached CastToIndexNode cast ) {
241
231
return createSocketInternal (cls , cast .execute (family ), cast .execute (type ), cast .execute (proto ));
242
232
}
243
233
244
- @ Specialization (guards = {"isNoValue(fileno)" })
245
- Object socket (LazyPythonClass cls , PInt family , PInt type , Integer proto , @ SuppressWarnings ("unused" ) PNone fileno ) {
246
- return createSocketInternal (cls , family .asInt (), type .asInt (), proto );
247
- }
248
-
249
- @ Specialization ()
250
- Object socket (LazyPythonClass cls , PInt family , PInt type , PInt proto , int fileno ) {
251
- return createSocketInternal (cls , family .intValue (), type .intValue (), proto .intValue (), fileno );
252
- }
253
-
254
- @ Specialization ()
255
- Object socket (LazyPythonClass cls , PInt family , PInt type , int proto , int fileno ) {
256
- return createSocketInternal (cls , family .intValue (), type .intValue (), proto , fileno );
257
- }
258
-
259
- @ Specialization ()
260
- Object socket (LazyPythonClass cls , int family , int type , int proto , int fileno ) {
261
- return createSocketInternal (cls , family , type , proto , fileno );
262
- }
263
-
264
- @ Specialization ()
265
- Object socket (LazyPythonClass cls , long family , long type , int proto , int fileno ) {
266
- return createSocketInternal (cls , ((int ) family ), ((int ) type ), proto , fileno );
234
+ @ Specialization (guards = {"!isNoValue(family)" , "!isNoValue(type)" , "!isNoValue(proto)" , "!isNoValue(fileno)" })
235
+ Object socket (VirtualFrame frame , LazyPythonClass cls , Object family , Object type , Object proto , Object fileno ,
236
+ @ Cached CastToIndexNode cast ) {
237
+ return createSocketInternal (frame , cls , cast .execute (family ), cast .execute (type ), cast .execute (proto ), cast .execute (fileno ));
267
238
}
268
239
269
240
private Object createSocketInternal (LazyPythonClass cls , int family , int type , int proto ) {
@@ -273,16 +244,15 @@ private Object createSocketInternal(LazyPythonClass cls, int family, int type, i
273
244
newSocket .setFileno (fd );
274
245
return newSocket ;
275
246
} else {
276
- throw raise (PythonErrorType .OSError , "creating sockets not allowed" );
247
+ throw raise (PythonErrorType .RuntimeError , "creating sockets not allowed" );
277
248
}
278
249
}
279
250
280
- @ TruffleBoundary
281
- private Object createSocketInternal (LazyPythonClass cls , int family , int type , int proto , int fileno ) {
251
+ private Object createSocketInternal (VirtualFrame frame , LazyPythonClass cls , int family , int type , int proto , int fileno ) {
282
252
if (getContext ().getEnv ().isNativeAccessAllowed ()) {
283
253
PSocket oldSocket = getContext ().getResources ().getSocket (fileno );
284
254
if (oldSocket == null ) {
285
- throw raise ( PythonErrorType . OSError , "bad filedescriptor" );
255
+ throw raiseOSError ( frame , OSErrorEnum . EBADF . getNumber () );
286
256
}
287
257
PSocket newSocket = factory ().createSocket (cls , family == -1 ? oldSocket .getFamily () : family , type == -1 ? oldSocket .getType () : type , proto == -1 ? oldSocket .getProto () : proto , fileno );
288
258
if (oldSocket .getSocket () != null ) {
@@ -293,7 +263,7 @@ private Object createSocketInternal(LazyPythonClass cls, int family, int type, i
293
263
getContext ().getResources ().reopenSocket (newSocket , fileno );
294
264
return newSocket ;
295
265
} else {
296
- throw raise (PythonErrorType .OSError , "creating sockets not allowed" );
266
+ throw raise (PythonErrorType .RuntimeError , "creating sockets not allowed" );
297
267
}
298
268
}
299
269
}
@@ -629,48 +599,46 @@ InetAddress[] resolveHost(String host) {
629
599
@ GenerateNodeFactory
630
600
public abstract static class moduleCloseNode extends PythonBuiltinNode {
631
601
@ Specialization
632
- @ TruffleBoundary
633
- Object close (@ SuppressWarnings ("unused" ) PNone fd ) {
634
- throw raise (PythonBuiltinClassType .TypeError );
635
- }
636
-
637
- @ Specialization
638
- @ TruffleBoundary
639
- Object close (int fd ) {
602
+ Object close (VirtualFrame frame , int fd ) {
640
603
if (fd < 0 ) {
641
604
throw raise (PythonBuiltinClassType .OSError , "Bad file descriptor" );
642
605
}
643
606
644
607
PSocket socket = getContext ().getResources ().getSocket (fd );
645
608
646
609
if (socket == null ) {
647
- throw raise ( PythonBuiltinClassType . OSError , "Bad file descriptor" );
610
+ throw raiseOSError ( frame , OSErrorEnum . EBADF . getNumber () );
648
611
}
649
612
650
613
if (socket .getSocket () != null ) {
651
614
if (!socket .getSocket ().isOpen ()) {
652
- throw raise ( PythonBuiltinClassType . OSError , "Bad file descriptor" );
615
+ throw raiseOSError ( frame , OSErrorEnum . EBADF . getNumber () );
653
616
}
654
617
655
618
try {
656
619
socket .getSocket ().close ();
657
620
} catch (IOException e ) {
658
- throw raise ( PythonBuiltinClassType . OSError , "Bad file descriptor" );
621
+ throw raiseOSError ( frame , OSErrorEnum . EBADF . getNumber () );
659
622
}
660
623
}
661
624
else if (socket .getServerSocket () != null ) {
662
625
if (!socket .getServerSocket ().isOpen ()) {
663
- throw raise ( PythonBuiltinClassType . OSError , "Bad file descriptor" );
626
+ throw raiseOSError ( frame , OSErrorEnum . EBADF . getNumber () );
664
627
}
665
628
666
629
try {
667
630
socket .getServerSocket ().close ();
668
631
} catch (IOException e ) {
669
- throw raise ( PythonBuiltinClassType . OSError , "Bad file descriptor" );
632
+ throw raiseOSError ( frame , OSErrorEnum . EBADF . getNumber () );
670
633
}
671
634
}
672
- getContext ().getResources ().closeSocket (socket .getFileno ());
635
+ getContext ().getResources ().close (socket .getFileno ());
673
636
return PNone .NONE ;
674
637
}
638
+
639
+ @ Fallback
640
+ Object close (@ SuppressWarnings ("unused" ) Object fd ) {
641
+ throw raise (PythonBuiltinClassType .TypeError );
642
+ }
675
643
}
676
644
}
0 commit comments