110110import com .oracle .graal .python .runtime .PosixSupportLibrary ;
111111import com .oracle .graal .python .runtime .PosixSupportLibrary .PosixException ;
112112import com .oracle .graal .python .runtime .exception .PythonErrorType ;
113+ import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
113114import com .oracle .graal .python .runtime .sequence .storage .ByteSequenceStorage ;
114115import com .oracle .graal .python .util .OverflowException ;
115116import com .oracle .graal .python .util .PythonUtils ;
117+ import com .oracle .truffle .api .CompilerDirectives ;
116118import com .oracle .truffle .api .dsl .Cached ;
117119import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
118120import com .oracle .truffle .api .dsl .NodeFactory ;
@@ -187,8 +189,17 @@ abstract static class StrNode extends PythonUnaryBuiltinNode {
187189 abstract static class ReprNode extends StrNode {
188190 }
189191
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+
190200 private static byte [] readBytes (PythonBuiltinBaseNode node , VirtualFrame frame , PMMap self , PosixSupportLibrary posixLib , long pos , int len ) {
191201 try {
202+ assert len > 0 ;
192203 assert pos + len <= self .getLength ();
193204 byte [] buffer = new byte [len ];
194205 posixLib .mmapReadBytes (node .getPosixSupport (), self .getPosixSupportHandle (), pos , buffer , buffer .length );
@@ -222,12 +233,17 @@ int doSingle(VirtualFrame frame, PMMap self, Object idxObj,
222233 @ Specialization
223234 Object doSlice (VirtualFrame frame , PMMap self , PSlice idx ,
224235 @ CachedLibrary ("getPosixSupport()" ) PosixSupportLibrary posixSupportLib ,
236+ @ Cached ConditionProfile emptyProfile ,
225237 @ Cached CoerceToIntSlice sliceCast ,
226238 @ Cached ComputeIndices compute ,
227239 @ Cached LenOfRangeNode sliceLenNode ) {
228240 try {
229241 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 );
231247 return factory ().createBytes (result );
232248 } catch (OverflowException e ) {
233249 throw raise (PythonBuiltinClassType .OverflowError , e );
@@ -407,13 +423,15 @@ abstract static class ReadNode extends PythonBuiltinNode {
407423
408424 @ Specialization
409425 PBytes readUnlimited (VirtualFrame frame , PMMap self , @ SuppressWarnings ("unused" ) PNone n ,
426+ @ Cached ConditionProfile emptyProfile ,
410427 @ CachedLibrary ("getPosixSupport()" ) PosixSupportLibrary posixLib ) {
411428 // 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 );
413430 }
414431
415432 @ Specialization (guards = "!isNoValue(n)" , limit = "getCallSiteInlineCacheMaxDepth()" )
416433 PBytes read (VirtualFrame frame , PMMap self , Object n ,
434+ @ Cached ConditionProfile emptyProfile ,
417435 @ CachedLibrary ("getPosixSupport()" ) PosixSupportLibrary posixLib ,
418436 @ CachedLibrary ("n" ) PythonObjectLibrary lib ,
419437 @ Cached ("createBinaryProfile()" ) ConditionProfile negativeProfile ) {
@@ -424,15 +442,18 @@ PBytes read(VirtualFrame frame, PMMap self, Object n,
424442 long nread = lib .asSizeWithState (n , PArguments .getThreadState (frame ));
425443
426444 if (negativeProfile .profile (nread < 0 )) {
427- return readUnlimited (frame , self , PNone .NO_VALUE , posixLib );
445+ return readUnlimited (frame , self , PNone .NO_VALUE , emptyProfile , posixLib );
428446 }
429447 if (nread > self .getRemaining ()) {
430448 nread = self .getRemaining ();
431449 }
432- return readBytes (frame , self , posixLib , nread );
450+ return readBytes (frame , self , posixLib , nread , emptyProfile );
433451 }
434452
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+ }
436457 try {
437458 byte [] buffer = MMapBuiltins .readBytes (this , frame , self , posixLib , self .getPos (), PythonUtils .toIntExact (nread ));
438459 self .setPos (self .getPos () + buffer .length );
0 commit comments