113
113
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PMMap )
114
114
public class MMapBuiltins extends PythonBuiltins {
115
115
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 {
117
129
118
130
default ReadByteFromChannelNode createValueError () {
119
131
return ReadByteFromChannelNode .create (() -> new ChannelNodes .ReadByteErrorHandler () {
@@ -137,7 +149,7 @@ public int execute(Channel channel) {
137
149
}
138
150
}
139
151
140
- protected interface ByteWritingNode {
152
+ protected interface ByteWritingNode extends MMapBaseNode {
141
153
142
154
default WriteByteToChannelNode createValueError () {
143
155
return WriteByteToChannelNode .create (() -> new ChannelNodes .WriteByteErrorHandler () {
@@ -243,13 +255,13 @@ int doSingle(VirtualFrame frame, PMMap self, Object idxObj,
243
255
long idx = i < 0 ? i + len : i ;
244
256
245
257
// save current position
246
- long oldPos = channel . position ();
258
+ long oldPos = position (channel );
247
259
248
- channel . position (idx );
260
+ position (channel , idx );
249
261
int res = readByteNode .execute (channel ) & 0xFF ;
250
262
251
263
// restore position
252
- channel . position (oldPos );
264
+ position (channel , oldPos );
253
265
254
266
return res ;
255
267
@@ -268,13 +280,13 @@ Object doSlice(VirtualFrame frame, PMMap self, PSlice idx,
268
280
SeekableByteChannel channel = self .getChannel ();
269
281
270
282
// save current position
271
- long oldPos = channel . position ();
283
+ long oldPos = position (channel );
272
284
273
- channel . position (info .start );
285
+ position (channel , info .start );
274
286
ByteSequenceStorage s = readNode .execute (channel , info .length );
275
287
276
288
// restore position
277
- channel . position (oldPos );
289
+ position (channel , oldPos );
278
290
279
291
return factory ().createBytes (s );
280
292
} catch (IOException e ) {
@@ -307,13 +319,13 @@ PNone doSingle(VirtualFrame frame, PMMap self, Object idxObj, Object val,
307
319
}
308
320
309
321
// save current position
310
- long oldPos = channel . position ();
322
+ long oldPos = position (channel );
311
323
312
- channel . position (idx );
324
+ position (channel , idx );
313
325
writeByteNode .execute (channel , castToByteNode .execute (val ));
314
326
315
327
// restore position
316
- channel . position (oldPos );
328
+ position (channel , oldPos );
317
329
318
330
return PNone .NONE ;
319
331
@@ -339,13 +351,13 @@ PNone doSlice(VirtualFrame frame, PMMap self, PSlice idx, PIBytesLike val,
339
351
}
340
352
341
353
// save current position
342
- long oldPos = channel . position ();
354
+ long oldPos = position (channel );
343
355
344
- channel . position (info .start );
356
+ position (channel , info .start );
345
357
writeNode .execute (channel , getStorageNode .execute (val ), info .length );
346
358
347
359
// restore position
348
- channel . position (oldPos );
360
+ position (channel , oldPos );
349
361
350
362
return PNone .NONE ;
351
363
@@ -428,13 +440,13 @@ long size(VirtualFrame frame, PMMap self,
428
440
429
441
@ Builtin (name = "tell" , fixedNumOfPositionalArgs = 1 )
430
442
@ GenerateNodeFactory
431
- abstract static class TellNode extends PythonBuiltinNode {
443
+ abstract static class TellNode extends PythonBuiltinNode implements ByteReadingNode {
432
444
@ Specialization
433
445
long readline (VirtualFrame frame , PMMap self ) {
434
446
435
447
try {
436
448
SeekableByteChannel channel = self .getChannel ();
437
- return channel . position () - self .getOffset ();
449
+ return position (channel ) - self .getOffset ();
438
450
} catch (IOException e ) {
439
451
throw raiseOSError (frame , OSErrorEnum .EIO , e .getMessage ());
440
452
}
@@ -484,7 +496,7 @@ PBytes read(PMMap self, Object n,
484
496
485
497
@ Builtin (name = "readline" , fixedNumOfPositionalArgs = 1 )
486
498
@ GenerateNodeFactory
487
- abstract static class ReadlineNode extends PythonBuiltinNode {
499
+ abstract static class ReadlineNode extends PythonUnaryBuiltinNode implements ByteReadingNode {
488
500
489
501
@ Specialization
490
502
Object readline (PMMap self ,
@@ -504,7 +516,7 @@ Object readline(PMMap self,
504
516
appendNode .execute (res , b );
505
517
} else {
506
518
// 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 );
508
520
break outer ;
509
521
}
510
522
}
@@ -550,7 +562,7 @@ int writeMemoryview(PMMap self, PMemoryView memoryView,
550
562
@ Builtin (name = "seek" , minNumOfPositionalArgs = 2 , maxNumOfPositionalArgs = 3 )
551
563
@ GenerateNodeFactory
552
564
@ TypeSystemReference (PythonArithmeticTypes .class )
553
- abstract static class SeekNode extends PythonBuiltinNode {
565
+ abstract static class SeekNode extends PythonBuiltinNode implements MMapBaseNode {
554
566
@ Child private CastToIndexNode castToLongNode ;
555
567
556
568
private final BranchProfile errorProfile = BranchProfile .create ();
@@ -590,24 +602,14 @@ Object seek(VirtualFrame frame, PMMap self, long dist, Object how) {
590
602
errorProfile .enter ();
591
603
throw raise (PythonBuiltinClassType .ValueError , "seek out of range" );
592
604
}
593
- doSeek (channel , where );
605
+ position (channel , where );
594
606
return PNone .NONE ;
595
607
} catch (IOException e ) {
596
608
errorProfile .enter ();
597
609
throw raiseOSError (frame , OSErrorEnum .EIO , e .getMessage ());
598
610
}
599
611
}
600
612
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
-
611
613
private int castToInt (Object val ) {
612
614
if (castToLongNode == null ) {
613
615
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -653,7 +655,7 @@ long find(PMMap primary, PIBytesLike sub, Object starting, Object ending,
653
655
// TODO implement a more efficient algorithm
654
656
outer : for (long i = start ; i < end ; i ++) {
655
657
// TODO(fa) don't seek but use circular buffer
656
- channel . position (i );
658
+ position (channel , i );
657
659
for (int j = 0 ; j < len2 ; j ++) {
658
660
int hb = readByteNode .execute (channel );
659
661
int nb = getGetRightItemNode ().executeInt (needle , j );
@@ -682,7 +684,7 @@ long find(PMMap primary, int sub, Object starting, @SuppressWarnings("unused") O
682
684
long start = s < 0 ? s + len1 : s ;
683
685
long end = Math .max (e < 0 ? e + len1 : e , len1 );
684
686
685
- channel . position (start );
687
+ position (channel , start );
686
688
687
689
for (long i = start ; i < end ; i ++) {
688
690
int hb = readByteNode .execute (channel );
0 commit comments