95
95
import com .oracle .graal .python .builtins .objects .bytes .BytesNodes ;
96
96
import com .oracle .graal .python .builtins .objects .bytes .PByteArray ;
97
97
import com .oracle .graal .python .builtins .objects .bytes .PBytes ;
98
- import com .oracle .graal .python .builtins .objects .bytes .PIBytesLike ;
99
98
import com .oracle .graal .python .builtins .objects .common .SequenceNodes ;
100
99
import com .oracle .graal .python .builtins .objects .common .SequenceNodes .LenNode ;
101
100
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes ;
135
134
import com .oracle .graal .python .runtime .exception .PythonExitException ;
136
135
import com .oracle .graal .python .runtime .sequence .PSequence ;
137
136
import com .oracle .graal .python .runtime .sequence .storage .ByteSequenceStorage ;
137
+ import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
138
138
import com .oracle .graal .python .util .FileDeleteShutdownHook ;
139
139
import com .oracle .truffle .api .CompilerDirectives ;
140
140
import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
@@ -427,7 +427,7 @@ long getPid() throws Exception {
427
427
428
428
@ Specialization
429
429
@ TruffleBoundary
430
- long getPidFallback () {
430
+ static long getPidFallback () {
431
431
String info = java .lang .management .ManagementFactory .getRuntimeMXBean ().getName ();
432
432
return Long .parseLong (info .split ("@" )[0 ]);
433
433
}
@@ -437,12 +437,12 @@ long getPidFallback() {
437
437
@ GenerateNodeFactory
438
438
public abstract static class GetUidNode extends PythonBuiltinNode {
439
439
@ Specialization
440
- int getPid () {
440
+ static int getPid () {
441
441
return getSystemUid ();
442
442
}
443
443
444
444
@ TruffleBoundary
445
- int getSystemUid () {
445
+ static int getSystemUid () {
446
446
String osName = System .getProperty ("os.name" );
447
447
if (osName .contains ("Linux" )) {
448
448
return (int ) new com .sun .security .auth .module .UnixSystem ().getUid ();
@@ -488,31 +488,36 @@ Object fstat(VirtualFrame frame, int fd,
488
488
return statNode .executeWith (frame , resources .getFilePath (fd ), PNone .NO_VALUE );
489
489
} else {
490
490
fstatForNonFile .enter ();
491
- Channel fileChannel = resources .getFileChannel (fd , channelClassProfile );
492
- int mode = 0 ;
493
- if (fileChannel instanceof ReadableByteChannel ) {
494
- mode |= 0444 ;
495
- }
496
- if (fileChannel instanceof WritableByteChannel ) {
497
- mode |= 0222 ;
498
- }
499
- return factory ().createTuple (new Object []{
500
- mode ,
501
- 0 , // ino
502
- 0 , // dev
503
- 0 , // nlink
504
- 0 ,
505
- 0 ,
506
- 0 ,
507
- 0 ,
508
- 0 ,
509
- 0 ,
510
- });
491
+ return fstatWithoutPath (resources , fd , channelClassProfile );
511
492
}
512
493
}
513
494
495
+ @ TruffleBoundary (allowInlining = true )
496
+ private PTuple fstatWithoutPath (PosixResources resources , int fd , ValueProfile channelClassProfile ) {
497
+ Channel fileChannel = resources .getFileChannel (fd , channelClassProfile );
498
+ int mode = 0 ;
499
+ if (fileChannel instanceof ReadableByteChannel ) {
500
+ mode |= 0444 ;
501
+ }
502
+ if (fileChannel instanceof WritableByteChannel ) {
503
+ mode |= 0222 ;
504
+ }
505
+ return factory ().createTuple (new Object []{
506
+ mode ,
507
+ 0 , // ino
508
+ 0 , // dev
509
+ 0 , // nlink
510
+ 0 ,
511
+ 0 ,
512
+ 0 ,
513
+ 0 ,
514
+ 0 ,
515
+ 0 ,
516
+ });
517
+ }
518
+
514
519
@ Specialization (limit = "getCallSiteInlineCacheMaxDepth()" )
515
- Object fstatPInt (VirtualFrame frame , Object fd ,
520
+ static Object fstatPInt (VirtualFrame frame , Object fd ,
516
521
@ CachedLibrary ("fd" ) PythonObjectLibrary lib ,
517
522
@ Cached ("create()" ) FstatNode recursive ) {
518
523
return recursive .executeWith (frame , lib .asSizeWithState (fd , PArguments .getThreadState (frame )));
@@ -527,7 +532,7 @@ protected static FstatNode create() {
527
532
@ GenerateNodeFactory
528
533
public abstract static class SetInheritableNode extends PythonFileNode {
529
534
@ Specialization (guards = {"fd >= 0" , "fd <= 2" })
530
- Object setInheritableStd (@ SuppressWarnings ("unused" ) int fd , @ SuppressWarnings ("unused" ) Object inheritable ) {
535
+ static Object setInheritableStd (@ SuppressWarnings ("unused" ) int fd , @ SuppressWarnings ("unused" ) Object inheritable ) {
531
536
// TODO: investigate if for the stdout/in/err this flag can be set
532
537
return PNone .NONE ;
533
538
}
@@ -571,7 +576,7 @@ Object doStatDefault(VirtualFrame frame, Object path, @SuppressWarnings("unused"
571
576
}
572
577
573
578
@ TruffleBoundary
574
- long fileTimeToSeconds (FileTime t ) {
579
+ static long fileTimeToSeconds (FileTime t ) {
575
580
return t .to (TimeUnit .SECONDS );
576
581
}
577
582
@@ -1142,26 +1147,34 @@ public abstract static class WriteNode extends PythonFileNode {
1142
1147
1143
1148
public abstract Object executeWith (VirtualFrame frame , Object fd , Object data );
1144
1149
1150
+ @ TruffleBoundary (allowInlining = true , transferToInterpreterOnException = false )
1151
+ private static Object writableOp (byte [] data , Object channel ) throws IOException {
1152
+ if (channel instanceof WritableByteChannel ) {
1153
+ return doWriteOp (data , channel );
1154
+ }
1155
+ return null ;
1156
+ }
1157
+
1145
1158
@ Specialization
1146
1159
Object write (VirtualFrame frame , int fd , byte [] data ,
1147
1160
@ Cached ("createClassProfile()" ) ValueProfile channelClassProfile ) {
1148
1161
Channel channel = getResources ().getFileChannel (fd , channelClassProfile );
1149
- if (channel instanceof WritableByteChannel ) {
1150
- try {
1151
- return doWriteOp (data , (WritableByteChannel ) channel );
1152
- } catch (Exception e ) {
1153
- gotException .enter ();
1154
- throw raiseOSError (frame , e );
1162
+ try {
1163
+ Object ret = writableOp (data , channel );
1164
+ if (ret != null ) {
1165
+ return ret ;
1155
1166
}
1156
- } else {
1157
- notWritable .enter ();
1158
- throw raiseOSError (frame , OSErrorEnum . EBADF );
1167
+ } catch ( Exception e ) {
1168
+ gotException .enter ();
1169
+ throw raiseOSError (frame , e );
1159
1170
}
1171
+ notWritable .enter ();
1172
+ throw raiseOSError (frame , OSErrorEnum .EBADF );
1160
1173
}
1161
1174
1162
1175
@ TruffleBoundary (allowInlining = true , transferToInterpreterOnException = false )
1163
- private static int doWriteOp (byte [] data , WritableByteChannel channel ) throws IOException {
1164
- return channel .write (ByteBuffer .wrap (data ));
1176
+ private static int doWriteOp (byte [] data , Object channel ) throws IOException {
1177
+ return (( WritableByteChannel ) channel ) .write (ByteBuffer .wrap (data ));
1165
1178
}
1166
1179
1167
1180
@ Specialization
@@ -1178,28 +1191,28 @@ private static byte[] stringToBytes(String data) {
1178
1191
@ Specialization
1179
1192
Object write (VirtualFrame frame , int fd , PBytes data ,
1180
1193
@ Cached ("createClassProfile()" ) ValueProfile channelClassProfile ) {
1181
- return write (frame , fd , getByteArray (data ), channelClassProfile );
1194
+ return write (frame , fd , getByteArray (data . getSequenceStorage () ), channelClassProfile );
1182
1195
}
1183
1196
1184
1197
@ Specialization
1185
1198
Object write (VirtualFrame frame , int fd , PByteArray data ,
1186
1199
@ Cached ("createClassProfile()" ) ValueProfile channelClassProfile ) {
1187
- return write (frame , fd , getByteArray (data ), channelClassProfile );
1200
+ return write (frame , fd , getByteArray (data . getSequenceStorage () ), channelClassProfile );
1188
1201
}
1189
1202
1190
1203
@ Specialization (limit = "getCallSiteInlineCacheMaxDepth()" )
1191
- Object writePInt (VirtualFrame frame , Object fd , Object data ,
1204
+ static Object writePInt (VirtualFrame frame , Object fd , Object data ,
1192
1205
@ CachedLibrary ("fd" ) PythonObjectLibrary lib ,
1193
1206
@ Cached ("create()" ) WriteNode recursive ) {
1194
1207
return recursive .executeWith (frame , lib .asSizeWithState (fd , PArguments .getThreadState (frame )), data );
1195
1208
}
1196
1209
1197
- private byte [] getByteArray (PIBytesLike pByteArray ) {
1210
+ private byte [] getByteArray (SequenceStorage storage ) {
1198
1211
if (toByteArrayNode == null ) {
1199
1212
CompilerDirectives .transferToInterpreterAndInvalidate ();
1200
1213
toByteArrayNode = insert (ToByteArrayNodeGen .create ());
1201
1214
}
1202
- return toByteArrayNode .execute (pByteArray . getSequenceStorage () );
1215
+ return toByteArrayNode .execute (storage );
1203
1216
}
1204
1217
1205
1218
public static WriteNode create () {
@@ -1262,7 +1275,7 @@ boolean isATTY(long fd) {
1262
1275
}
1263
1276
1264
1277
@ Fallback
1265
- boolean isATTY (@ SuppressWarnings ("unused" ) Object fd ) {
1278
+ static boolean isATTY (@ SuppressWarnings ("unused" ) Object fd ) {
1266
1279
return false ;
1267
1280
}
1268
1281
}
@@ -1773,7 +1786,7 @@ boolean access(String path, int mode, Object dirFd, boolean effectiveIds, boolea
1773
1786
@ GenerateNodeFactory
1774
1787
abstract static class CpuCountNode extends PythonBuiltinNode {
1775
1788
@ Specialization
1776
- int getCpuCount () {
1789
+ static int getCpuCount () {
1777
1790
return Runtime .getRuntime ().availableProcessors ();
1778
1791
}
1779
1792
}
@@ -1893,7 +1906,7 @@ Object getTerminalSize(VirtualFrame frame, Object fd) {
1893
1906
return recursiveNode .execute (frame , value );
1894
1907
}
1895
1908
1896
- protected GetTerminalSizeNode create () {
1909
+ protected static GetTerminalSizeNode create () {
1897
1910
return PosixModuleBuiltinsFactory .GetTerminalSizeNodeFactory .create ();
1898
1911
}
1899
1912
}
@@ -1927,7 +1940,7 @@ public abstract static class StrErrorNode extends PythonBuiltinNode {
1927
1940
1928
1941
@ Specialization
1929
1942
@ TruffleBoundary
1930
- String getStrError (int errno ) {
1943
+ static String getStrError (int errno ) {
1931
1944
if (STR_ERROR_MAP .isEmpty ()) {
1932
1945
for (OSErrorEnum error : OSErrorEnum .values ()) {
1933
1946
STR_ERROR_MAP .put (error .getNumber (), error .getMessage ());
@@ -1945,7 +1958,7 @@ String getStrError(int errno) {
1945
1958
@ GenerateNodeFactory
1946
1959
abstract static class CtermId extends PythonBuiltinNode {
1947
1960
@ Specialization
1948
- String ctermid () {
1961
+ static String ctermid () {
1949
1962
return "/dev/tty" ;
1950
1963
}
1951
1964
}
0 commit comments