113113@ CoreFunctions (extendClasses = PythonBuiltinClassType .PMMap )
114114public class MMapBuiltins extends PythonBuiltins {
115115
116- protected interface ByteReadingNode {
116+ protected interface MMapBaseNode {
117+ @ TruffleBoundary
118+ default long position (SeekableByteChannel ch ) throws IOException {
119+ return ch .position ();
120+ }
121+
122+ @ TruffleBoundary
123+ default void position (SeekableByteChannel ch , long offset ) throws IOException {
124+ ch .position (offset );
125+ }
126+ }
127+
128+ protected interface ByteReadingNode extends MMapBaseNode {
117129
118130 default ReadByteFromChannelNode createValueError () {
119131 return ReadByteFromChannelNode .create (() -> new ChannelNodes .ReadByteErrorHandler () {
@@ -137,7 +149,7 @@ public int execute(Channel channel) {
137149 }
138150 }
139151
140- protected interface ByteWritingNode {
152+ protected interface ByteWritingNode extends MMapBaseNode {
141153
142154 default WriteByteToChannelNode createValueError () {
143155 return WriteByteToChannelNode .create (() -> new ChannelNodes .WriteByteErrorHandler () {
@@ -243,13 +255,13 @@ int doSingle(VirtualFrame frame, PMMap self, Object idxObj,
243255 long idx = i < 0 ? i + len : i ;
244256
245257 // save current position
246- long oldPos = channel . position ();
258+ long oldPos = position (channel );
247259
248- channel . position (idx );
260+ position (channel , idx );
249261 int res = readByteNode .execute (channel ) & 0xFF ;
250262
251263 // restore position
252- channel . position (oldPos );
264+ position (channel , oldPos );
253265
254266 return res ;
255267
@@ -268,13 +280,13 @@ Object doSlice(VirtualFrame frame, PMMap self, PSlice idx,
268280 SeekableByteChannel channel = self .getChannel ();
269281
270282 // save current position
271- long oldPos = channel . position ();
283+ long oldPos = position (channel );
272284
273- channel . position (info .start );
285+ position (channel , info .start );
274286 ByteSequenceStorage s = readNode .execute (channel , info .length );
275287
276288 // restore position
277- channel . position (oldPos );
289+ position (channel , oldPos );
278290
279291 return factory ().createBytes (s );
280292 } catch (IOException e ) {
@@ -307,13 +319,13 @@ PNone doSingle(VirtualFrame frame, PMMap self, Object idxObj, Object val,
307319 }
308320
309321 // save current position
310- long oldPos = channel . position ();
322+ long oldPos = position (channel );
311323
312- channel . position (idx );
324+ position (channel , idx );
313325 writeByteNode .execute (channel , castToByteNode .execute (val ));
314326
315327 // restore position
316- channel . position (oldPos );
328+ position (channel , oldPos );
317329
318330 return PNone .NONE ;
319331
@@ -339,13 +351,13 @@ PNone doSlice(VirtualFrame frame, PMMap self, PSlice idx, PIBytesLike val,
339351 }
340352
341353 // save current position
342- long oldPos = channel . position ();
354+ long oldPos = position (channel );
343355
344- channel . position (info .start );
356+ position (channel , info .start );
345357 writeNode .execute (channel , getStorageNode .execute (val ), info .length );
346358
347359 // restore position
348- channel . position (oldPos );
360+ position (channel , oldPos );
349361
350362 return PNone .NONE ;
351363
@@ -428,13 +440,13 @@ long size(VirtualFrame frame, PMMap self,
428440
429441 @ Builtin (name = "tell" , fixedNumOfPositionalArgs = 1 )
430442 @ GenerateNodeFactory
431- abstract static class TellNode extends PythonBuiltinNode {
443+ abstract static class TellNode extends PythonBuiltinNode implements ByteReadingNode {
432444 @ Specialization
433445 long readline (VirtualFrame frame , PMMap self ) {
434446
435447 try {
436448 SeekableByteChannel channel = self .getChannel ();
437- return channel . position () - self .getOffset ();
449+ return position (channel ) - self .getOffset ();
438450 } catch (IOException e ) {
439451 throw raiseOSError (frame , OSErrorEnum .EIO , e .getMessage ());
440452 }
@@ -484,7 +496,7 @@ PBytes read(PMMap self, Object n,
484496
485497 @ Builtin (name = "readline" , fixedNumOfPositionalArgs = 1 )
486498 @ GenerateNodeFactory
487- abstract static class ReadlineNode extends PythonBuiltinNode {
499+ abstract static class ReadlineNode extends PythonUnaryBuiltinNode implements ByteReadingNode {
488500
489501 @ Specialization
490502 Object readline (PMMap self ,
@@ -504,7 +516,7 @@ Object readline(PMMap self,
504516 appendNode .execute (res , b );
505517 } else {
506518 // recover correct position (i.e. number of remaining bytes in buffer)
507- channel . position (channel . position () - buf .remaining () - 1 );
519+ position (channel , position (channel ) - buf .remaining () - 1 );
508520 break outer ;
509521 }
510522 }
@@ -550,7 +562,7 @@ int writeMemoryview(PMMap self, PMemoryView memoryView,
550562 @ Builtin (name = "seek" , minNumOfPositionalArgs = 2 , maxNumOfPositionalArgs = 3 )
551563 @ GenerateNodeFactory
552564 @ TypeSystemReference (PythonArithmeticTypes .class )
553- abstract static class SeekNode extends PythonBuiltinNode {
565+ abstract static class SeekNode extends PythonBuiltinNode implements MMapBaseNode {
554566 @ Child private CastToIndexNode castToLongNode ;
555567
556568 private final BranchProfile errorProfile = BranchProfile .create ();
@@ -590,24 +602,14 @@ Object seek(VirtualFrame frame, PMMap self, long dist, Object how) {
590602 errorProfile .enter ();
591603 throw raise (PythonBuiltinClassType .ValueError , "seek out of range" );
592604 }
593- doSeek (channel , where );
605+ position (channel , where );
594606 return PNone .NONE ;
595607 } catch (IOException e ) {
596608 errorProfile .enter ();
597609 throw raiseOSError (frame , OSErrorEnum .EIO , e .getMessage ());
598610 }
599611 }
600612
601- @ TruffleBoundary (allowInlining = true )
602- private static long position (SeekableByteChannel channel ) throws IOException {
603- return channel .position ();
604- }
605-
606- @ TruffleBoundary (allowInlining = true )
607- private static void doSeek (SeekableByteChannel channel , long where ) throws IOException {
608- channel .position (where );
609- }
610-
611613 private int castToInt (Object val ) {
612614 if (castToLongNode == null ) {
613615 CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -653,7 +655,7 @@ long find(PMMap primary, PIBytesLike sub, Object starting, Object ending,
653655 // TODO implement a more efficient algorithm
654656 outer : for (long i = start ; i < end ; i ++) {
655657 // TODO(fa) don't seek but use circular buffer
656- channel . position (i );
658+ position (channel , i );
657659 for (int j = 0 ; j < len2 ; j ++) {
658660 int hb = readByteNode .execute (channel );
659661 int nb = getGetRightItemNode ().executeInt (needle , j );
@@ -682,7 +684,7 @@ long find(PMMap primary, int sub, Object starting, @SuppressWarnings("unused") O
682684 long start = s < 0 ? s + len1 : s ;
683685 long end = Math .max (e < 0 ? e + len1 : e , len1 );
684686
685- channel . position (start );
687+ position (channel , start );
686688
687689 for (long i = start ; i < end ; i ++) {
688690 int hb = readByteNode .execute (channel );
0 commit comments