110
110
import com .oracle .graal .python .runtime .PosixSupportLibrary ;
111
111
import com .oracle .graal .python .runtime .PosixSupportLibrary .PosixException ;
112
112
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
113
+ import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
113
114
import com .oracle .graal .python .runtime .sequence .storage .ByteSequenceStorage ;
114
115
import com .oracle .graal .python .util .OverflowException ;
115
116
import com .oracle .graal .python .util .PythonUtils ;
117
+ import com .oracle .truffle .api .CompilerDirectives ;
116
118
import com .oracle .truffle .api .dsl .Cached ;
117
119
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
118
120
import com .oracle .truffle .api .dsl .NodeFactory ;
@@ -187,8 +189,17 @@ abstract static class StrNode extends PythonUnaryBuiltinNode {
187
189
abstract static class ReprNode extends StrNode {
188
190
}
189
191
192
+ private static PBytes createEmptyBytes (PythonObjectFactory factory ) {
193
+ if (CompilerDirectives .inInterpreter ()) {
194
+ return factory .createBytes (PythonUtils .EMPTY_BYTE_ARRAY );
195
+ } else {
196
+ return factory .createBytes (new byte [0 ]);
197
+ }
198
+ }
199
+
190
200
private static byte [] readBytes (PythonBuiltinBaseNode node , VirtualFrame frame , PMMap self , PosixSupportLibrary posixLib , long pos , int len ) {
191
201
try {
202
+ assert len > 0 ;
192
203
assert pos + len <= self .getLength ();
193
204
byte [] buffer = new byte [len ];
194
205
posixLib .mmapReadBytes (node .getPosixSupport (), self .getPosixSupportHandle (), pos , buffer , buffer .length );
@@ -222,12 +233,17 @@ int doSingle(VirtualFrame frame, PMMap self, Object idxObj,
222
233
@ Specialization
223
234
Object doSlice (VirtualFrame frame , PMMap self , PSlice idx ,
224
235
@ CachedLibrary ("getPosixSupport()" ) PosixSupportLibrary posixSupportLib ,
236
+ @ Cached ConditionProfile emptyProfile ,
225
237
@ Cached CoerceToIntSlice sliceCast ,
226
238
@ Cached ComputeIndices compute ,
227
239
@ Cached LenOfRangeNode sliceLenNode ) {
228
240
try {
229
241
SliceInfo info = compute .execute (sliceCast .execute (idx ), PInt .intValueExact (self .getLength ()));
230
- byte [] result = readBytes (this , frame , self , posixSupportLib , info .start , sliceLenNode .len (info ));
242
+ int len = sliceLenNode .len (info );
243
+ if (emptyProfile .profile (len == 0 )) {
244
+ return createEmptyBytes (factory ());
245
+ }
246
+ byte [] result = readBytes (this , frame , self , posixSupportLib , info .start , len );
231
247
return factory ().createBytes (result );
232
248
} catch (OverflowException e ) {
233
249
throw raise (PythonBuiltinClassType .OverflowError , e );
@@ -407,13 +423,15 @@ abstract static class ReadNode extends PythonBuiltinNode {
407
423
408
424
@ Specialization
409
425
PBytes readUnlimited (VirtualFrame frame , PMMap self , @ SuppressWarnings ("unused" ) PNone n ,
426
+ @ Cached ConditionProfile emptyProfile ,
410
427
@ CachedLibrary ("getPosixSupport()" ) PosixSupportLibrary posixLib ) {
411
428
// intentionally accept NO_VALUE and NONE; both mean that we read unlimited # of bytes
412
- return readBytes (frame , self , posixLib , self .getRemaining ());
429
+ return readBytes (frame , self , posixLib , self .getRemaining (), emptyProfile );
413
430
}
414
431
415
432
@ Specialization (guards = "!isNoValue(n)" , limit = "getCallSiteInlineCacheMaxDepth()" )
416
433
PBytes read (VirtualFrame frame , PMMap self , Object n ,
434
+ @ Cached ConditionProfile emptyProfile ,
417
435
@ CachedLibrary ("getPosixSupport()" ) PosixSupportLibrary posixLib ,
418
436
@ CachedLibrary ("n" ) PythonObjectLibrary lib ,
419
437
@ Cached ("createBinaryProfile()" ) ConditionProfile negativeProfile ) {
@@ -424,15 +442,18 @@ PBytes read(VirtualFrame frame, PMMap self, Object n,
424
442
long nread = lib .asSizeWithState (n , PArguments .getThreadState (frame ));
425
443
426
444
if (negativeProfile .profile (nread < 0 )) {
427
- return readUnlimited (frame , self , PNone .NO_VALUE , posixLib );
445
+ return readUnlimited (frame , self , PNone .NO_VALUE , emptyProfile , posixLib );
428
446
}
429
447
if (nread > self .getRemaining ()) {
430
448
nread = self .getRemaining ();
431
449
}
432
- return readBytes (frame , self , posixLib , nread );
450
+ return readBytes (frame , self , posixLib , nread , emptyProfile );
433
451
}
434
452
435
- private PBytes readBytes (VirtualFrame frame , PMMap self , PosixSupportLibrary posixLib , long nread ) {
453
+ private PBytes readBytes (VirtualFrame frame , PMMap self , PosixSupportLibrary posixLib , long nread , ConditionProfile emptyProfile ) {
454
+ if (emptyProfile .profile (nread == 0 )) {
455
+ return createEmptyBytes (factory ());
456
+ }
436
457
try {
437
458
byte [] buffer = MMapBuiltins .readBytes (this , frame , self , posixLib , self .getPos (), PythonUtils .toIntExact (nread ));
438
459
self .setPos (self .getPos () + buffer .length );
0 commit comments