Skip to content

Commit 8ec065a

Browse files
committed
Resolve more DSL warnings in the io module
1 parent c7aa987 commit 8ec065a

File tree

6 files changed

+86
-63
lines changed

6 files changed

+86
-63
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/BufferedIOMixinBuiltins.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
129129
@GenerateNodeFactory
130130
abstract static class CloseNode extends PythonUnaryWithInitErrorBuiltinNode {
131131

132-
private static Object close(VirtualFrame frame, PBuffered self,
132+
private static Object close(VirtualFrame frame, Node inliningTarget, PBuffered self,
133133
EnterBufferedNode lock,
134134
PyObjectCallMethodObjArgs callMethodClose) {
135135
try {
136-
lock.enter(self);
136+
lock.enter(inliningTarget, self);
137137
Object res = callMethodClose.execute(frame, self.getRaw(), T_CLOSE);
138138
if (self.getBuffer() != null) {
139139
self.setBuffer(null);
@@ -154,7 +154,7 @@ static Object doit(VirtualFrame frame, PBuffered self,
154154
@Cached EnterBufferedNode lock,
155155
@Cached InlinedConditionProfile profile) {
156156
try {
157-
lock.enter(self);
157+
lock.enter(inliningTarget, self);
158158
if (profile.profile(inliningTarget, isClosedNode.execute(frame, inliningTarget, self))) {
159159
return PNone.NONE;
160160
}
@@ -171,13 +171,13 @@ static Object doit(VirtualFrame frame, PBuffered self,
171171
callMethodFlush.execute(frame, self, T_FLUSH);
172172
} catch (PException e) {
173173
try {
174-
close(frame, self, lock, callMethodClose);
174+
close(frame, inliningTarget, self, lock, callMethodClose);
175175
} catch (PException ee) {
176176
throw ee.chainException(e);
177177
}
178178
throw e;
179179
}
180-
return close(frame, self, lock, callMethodClose);
180+
return close(frame, inliningTarget, self, lock, callMethodClose);
181181
}
182182
}
183183

@@ -260,13 +260,14 @@ protected static boolean isSupportedWhence(int whence) {
260260

261261
@Specialization(guards = {"self.isOK()", "isSupportedWhence(whence)"})
262262
static long doit(VirtualFrame frame, PBuffered self, Object off, int whence,
263+
@Bind("this") Node inliningTarget,
263264
@Cached("create(T_SEEK)") CheckIsClosedNode checkIsClosedNode,
264265
@Cached BufferedIONodes.CheckIsSeekabledNode checkIsSeekabledNode,
265266
@Cached BufferedIONodes.AsOffNumberNode asOffNumberNode,
266267
@Cached BufferedIONodes.SeekNode seekNode) {
267268
checkIsClosedNode.execute(frame, self);
268269
checkIsSeekabledNode.execute(frame, self);
269-
long pos = asOffNumberNode.execute(frame, off, TypeError);
270+
long pos = asOffNumberNode.execute(frame, inliningTarget, off, TypeError);
270271
return seekNode.execute(frame, self, pos, whence);
271272
}
272273

@@ -310,14 +311,15 @@ protected ArgumentClinicProvider getArgumentClinic() {
310311

311312
@Specialization(guards = {"self.isOK()", "self.isWritable()"})
312313
static Object doit(VirtualFrame frame, PBuffered self, Object pos,
314+
@Bind("this") Node inliningTarget,
313315
@Cached EnterBufferedNode lock,
314316
@Cached("create(T_TRUNCATE)") CheckIsClosedNode checkIsClosedNode,
315317
@Cached RawTellNode rawTellNode,
316318
@Cached FlushAndRewindUnlockedNode flushAndRewindUnlockedNode,
317319
@Cached PyObjectCallMethodObjArgs callMethodTruncate) {
318320
checkIsClosedNode.execute(frame, self);
319321
try {
320-
lock.enter(self);
322+
lock.enter(inliningTarget, self);
321323
flushAndRewindUnlockedNode.execute(frame, self);
322324
Object res = callMethodTruncate.execute(frame, self.getRaw(), T_TRUNCATE, pos);
323325
/* Reset cached position */

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/BufferedIONodes.java

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -166,29 +166,30 @@ boolean isSeekable(VirtualFrame frame, PBuffered self,
166166

167167
@ImportStatic(PGuards.class)
168168
@TypeSystemReference(PythonArithmeticTypes.class)
169+
@GenerateInline
170+
@GenerateCached(false)
169171
// PyNumber_AsOff_t
170172
abstract static class AsOffNumberNode extends PNodeWithContext {
171173

172-
public abstract long execute(VirtualFrame frame, Object number, PythonBuiltinClassType err);
174+
public abstract long execute(VirtualFrame frame, Node inliningTarget, Object number, PythonBuiltinClassType err);
173175

174176
@Specialization
175177
static long doInt(long number, @SuppressWarnings("unused") PythonBuiltinClassType err) {
176178
return number;
177179
}
178180

179181
@Specialization
180-
static long toLong(VirtualFrame frame, Object number, PythonBuiltinClassType err,
181-
@Bind("this") Node inliningTarget,
182-
@Cached PRaiseNode raiseNode,
183-
@Cached PyNumberIndexNode indexNode,
184-
@Cached CastToJavaLongExactNode cast,
182+
static long toLong(VirtualFrame frame, Node inliningTarget, Object number, PythonBuiltinClassType err,
183+
@Cached PRaiseNode.Lazy raiseNode,
184+
@Cached(inline = false) PyNumberIndexNode indexNode,
185+
@Cached(inline = false) CastToJavaLongExactNode cast,
185186
@Cached IsBuiltinObjectProfile errorProfile) {
186187
Object index = indexNode.execute(frame, number);
187188
try {
188189
return cast.execute(index);
189190
} catch (PException e) {
190191
e.expect(inliningTarget, OverflowError, errorProfile);
191-
throw raiseNode.raise(err, CANNOT_FIT_P_IN_OFFSET_SIZE, number);
192+
throw raiseNode.get(inliningTarget).raise(err, CANNOT_FIT_P_IN_OFFSET_SIZE, number);
192193
} catch (CannotCastException e) {
193194
throw CompilerDirectives.shouldNotReachHere();
194195
}
@@ -205,11 +206,11 @@ public RawTellNode(boolean ignore) {
205206

206207
public abstract long execute(VirtualFrame frame, PBuffered self);
207208

208-
private static long tell(VirtualFrame frame, Object raw,
209+
private static long tell(VirtualFrame frame, Node inliningTarget, Object raw,
209210
PyObjectCallMethodObjArgs callMethod,
210211
AsOffNumberNode asOffNumberNode) {
211212
Object res = callMethod.execute(frame, raw, T_TELL);
212-
return asOffNumberNode.execute(frame, res, ValueError);
213+
return asOffNumberNode.execute(frame, inliningTarget, res, ValueError);
213214
}
214215

215216
/**
@@ -220,7 +221,7 @@ long bufferedRawTell(VirtualFrame frame, PBuffered self,
220221
@Bind("this") Node inliningTarget,
221222
@Shared("callMethod") @Cached PyObjectCallMethodObjArgs callMethod,
222223
@Shared("asOffT") @Cached AsOffNumberNode asOffNumberNode) {
223-
long n = tell(frame, self.getRaw(), callMethod, asOffNumberNode);
224+
long n = tell(frame, inliningTarget, self.getRaw(), callMethod, asOffNumberNode);
224225
if (n < 0) {
225226
throw raise(OSError, IO_STREAM_INVALID_POS, n);
226227
}
@@ -230,11 +231,12 @@ long bufferedRawTell(VirtualFrame frame, PBuffered self,
230231

231232
@Specialization(guards = "ignore")
232233
static long bufferedRawTellIgnoreException(VirtualFrame frame, PBuffered self,
234+
@Bind("this") Node inliningTarget,
233235
@Shared("callMethod") @Cached PyObjectCallMethodObjArgs callMethod,
234236
@Shared("asOffT") @Cached AsOffNumberNode asOffNumberNode) {
235237
long n;
236238
try {
237-
n = tell(frame, self.getRaw(), callMethod, asOffNumberNode);
239+
n = tell(frame, inliningTarget, self.getRaw(), callMethod, asOffNumberNode);
238240
} catch (PException e) {
239241
n = -1;
240242
// ignore
@@ -254,19 +256,21 @@ public static RawTellNode create() {
254256
/**
255257
* implementation of cpython/Modules/_io/bufferedio.c:_buffered_raw_seek
256258
*/
259+
@GenerateInline
260+
@GenerateCached(false)
257261
abstract static class RawSeekNode extends PNodeWithContext {
258262

259-
public abstract long execute(VirtualFrame frame, PBuffered self, long target, int whence);
263+
public abstract long execute(VirtualFrame frame, Node inliningTarget, PBuffered self, long target, int whence);
260264

261265
@Specialization
262-
static long bufferedRawSeek(VirtualFrame frame, PBuffered self, long target, int whence,
263-
@Cached PRaiseNode raise,
264-
@Cached PyObjectCallMethodObjArgs callMethod,
266+
static long bufferedRawSeek(VirtualFrame frame, Node inliningTarget, PBuffered self, long target, int whence,
267+
@Cached PRaiseNode.Lazy raise,
268+
@Cached(inline = false) PyObjectCallMethodObjArgs callMethod,
265269
@Cached AsOffNumberNode asOffNumberNode) {
266270
Object res = callMethod.execute(frame, self.getRaw(), T_SEEK, target, whence);
267-
long n = asOffNumberNode.execute(frame, res, ValueError);
271+
long n = asOffNumberNode.execute(frame, inliningTarget, res, ValueError);
268272
if (n < 0) {
269-
raise.raise(OSError, IO_STREAM_INVALID_POS, n);
273+
raise.get(inliningTarget).raise(OSError, IO_STREAM_INVALID_POS, n);
270274
}
271275
self.setAbsPos(n);
272276
return n;
@@ -282,12 +286,13 @@ abstract static class FlushAndRewindUnlockedNode extends PNodeWithContext {
282286

283287
@Specialization(guards = {"self.isReadable()", "!self.isWritable()"})
284288
protected static void readOnly(VirtualFrame frame, PBuffered self,
289+
@Bind("this") Node inliningTarget,
285290
@Cached RawSeekNode rawSeekNode) {
286291
/*
287292
* Rewind the raw stream so that its position corresponds to the current logical
288293
* position.
289294
*/
290-
long n = rawSeekNode.execute(frame, self, -rawOffset(self), 1);
295+
long n = rawSeekNode.execute(frame, inliningTarget, self, -rawOffset(self), 1);
291296
self.resetRead(); // _bufferedreader_reset_buf
292297
assert n != -1;
293298
}
@@ -300,14 +305,15 @@ protected static void writeOnly(VirtualFrame frame, PBuffered self,
300305

301306
@Specialization(guards = {"self.isReadable()", "self.isWritable()"})
302307
protected static void readWrite(VirtualFrame frame, PBuffered self,
308+
@Bind("this") Node inliningTarget,
303309
@Cached BufferedWriterNodes.FlushUnlockedNode flushUnlockedNode,
304310
@Cached RawSeekNode rawSeekNode) {
305311
flushUnlockedNode.execute(frame, self);
306312
/*
307313
* Rewind the raw stream so that its position corresponds to the current logical
308314
* position.
309315
*/
310-
long n = rawSeekNode.execute(frame, self, -rawOffset(self), 1);
316+
long n = rawSeekNode.execute(frame, inliningTarget, self, -rawOffset(self), 1);
311317
self.resetRead(); // _bufferedreader_reset_buf
312318
assert n != -1;
313319
}
@@ -362,7 +368,7 @@ static long seek(VirtualFrame frame, PBuffered self, long off, int whence,
362368
}
363369
}
364370

365-
lock.enter(self);
371+
lock.enter(inliningTarget, self);
366372
try {
367373
/* Fallback: invoke raw seek() method and clear buffer */
368374
if (isWriteableProfile.profile(inliningTarget, self.isWritable())) {
@@ -372,7 +378,7 @@ static long seek(VirtualFrame frame, PBuffered self, long off, int whence,
372378
if (whenceSeekCur) {
373379
target -= rawOffset(self);
374380
}
375-
long n = rawSeekNode.execute(frame, self, target, whence);
381+
long n = rawSeekNode.execute(frame, inliningTarget, self, target, whence);
376382
self.setRawPos(-1);
377383
if (selfIsReadable) {
378384
self.resetRead(); // _bufferedreader_reset_buf
@@ -385,23 +391,24 @@ static long seek(VirtualFrame frame, PBuffered self, long off, int whence,
385391
}
386392

387393
// TODO: experiment with threads count to avoid locking.
394+
@GenerateInline
395+
@GenerateCached(false)
388396
abstract static class EnterBufferedNode extends Node {
389397

390-
public abstract void execute(PBuffered self);
398+
public abstract void execute(Node inliningTarget, PBuffered self);
391399

392400
@Specialization
393-
static void doEnter(PBuffered self,
394-
@Bind("this") Node inliningTarget,
401+
static void doEnter(Node inliningTarget, PBuffered self,
395402
@Cached EnterBufferedBusyNode enterBufferedBusyNode,
396403
@Cached InlinedConditionProfile isBusy) {
397404
if (isBusy.profile(inliningTarget, !self.getLock().acquireNonBlocking())) {
398-
enterBufferedBusyNode.execute(self);
405+
enterBufferedBusyNode.execute(inliningTarget, self);
399406
}
400407
self.setOwner(ThreadModuleBuiltins.GetCurrentThreadIdNode.getId());
401408
}
402409

403-
void enter(PBuffered self) {
404-
execute(self);
410+
void enter(Node inliningTarget, PBuffered self) {
411+
execute(inliningTarget, self);
405412
}
406413

407414
static void leave(PBuffered self) {
@@ -413,37 +420,41 @@ static void leave(PBuffered self) {
413420
/**
414421
* implementation of cpython/Modules/_io/bufferedio.c:_enter_buffered_busy
415422
*/
416-
abstract static class EnterBufferedBusyNode extends PNodeWithRaise {
423+
@GenerateInline
424+
@GenerateCached(false)
425+
abstract static class EnterBufferedBusyNode extends PNodeWithContext {
417426

418-
public abstract void execute(PBuffered self);
427+
public abstract void execute(Node inliningTarget, PBuffered self);
419428

420429
@Specialization(guards = {"!self.isOwn()", "!getContext().isFinalizing()"})
421-
void normal(PBuffered self,
422-
@Cached GilNode gil) {
430+
static void normal(Node inliningTarget, PBuffered self,
431+
@Cached(inline = false) GilNode gil) {
423432
gil.release(true);
424433
try {
425-
self.getLock().acquireBlocking(this);
434+
self.getLock().acquireBlocking(inliningTarget);
426435
} finally {
427436
gil.acquire();
428437
}
429438
}
430439

431440
@Specialization(guards = {"!self.isOwn()", "getContext().isFinalizing()"})
432-
void finalizing(PBuffered self) {
441+
static void finalizing(Node inliningTarget, PBuffered self,
442+
@Shared @Cached PRaiseNode.Lazy lazyRaise) {
433443
/*
434444
* When finalizing, we don't want a deadlock to happen with daemon threads abruptly shut
435445
* down while they owned the lock. Therefore, only wait for a grace period (1 s.). Note
436446
* that non-daemon threads have already exited here, so this shouldn't affect carefully
437447
* written threaded I/O code.
438448
*/
439-
if (!self.getLock().acquireTimeout(this, (long) 1e3)) {
440-
throw raise(SystemError, SHUTDOWN_POSSIBLY_DUE_TO_DAEMON_THREADS);
449+
if (!self.getLock().acquireTimeout(inliningTarget, (long) 1e3)) {
450+
throw lazyRaise.get(inliningTarget).raise(SystemError, SHUTDOWN_POSSIBLY_DUE_TO_DAEMON_THREADS);
441451
}
442452
}
443453

444454
@Specialization(guards = "self.isOwn()")
445-
void error(PBuffered self) {
446-
throw raise(RuntimeError, REENTRANT_CALL_INSIDE_P, self);
455+
static void error(Node inliningTarget, PBuffered self,
456+
@Shared @Cached PRaiseNode.Lazy lazyRaise) {
457+
throw lazyRaise.get(inliningTarget).raise(RuntimeError, REENTRANT_CALL_INSIDE_P, self);
447458
}
448459
}
449460
}

0 commit comments

Comments
 (0)