Skip to content

Commit dd3bedc

Browse files
committed
Move builtin input behind a boundary
1 parent 89e7cce commit dd3bedc

File tree

5 files changed

+54
-37
lines changed

5 files changed

+54
-37
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,78 +2618,75 @@ abstract static class InputNode extends PythonUnaryBuiltinNode {
26182618
static Object input(VirtualFrame frame, Object prompt,
26192619
@Bind Node inliningTarget,
26202620
@Bind PythonContext context,
2621-
@Cached SysModuleBuiltins.AuditNode auditNode,
2622-
@Cached PyObjectCallMethodObjArgs callMethod,
2623-
@Cached PyObjectStrAsObjectNode strNode,
2624-
@Cached PyObjectLookupAttr lookupAttr,
2625-
@Cached PyBytesCheckNode bytesCheck,
2626-
@Cached PyUnicodeCheckNode unicodeCheck,
2627-
@Cached CastToTruffleStringNode castToTruffleStringNode,
2628-
@Cached TruffleString.CodePointLengthNode codePointLengthNode,
2629-
@Cached TruffleString.CodePointAtIndexNode codePointAtIndexNode,
2630-
@Cached TruffleString.SubstringNode substringNode,
2631-
@Cached BytesNodes.ToBytesNode toBytesNode,
2632-
@Cached PRaiseNode raiseLostNode,
2633-
@Cached PRaiseNode raiseEOFNode,
2634-
@Cached PRaiseNode raiseWrongType) {
2621+
@Cached("createFor($node)") IndirectCallData indirectCallData) {
2622+
Object savedState = IndirectCallContext.enter(frame, inliningTarget, indirectCallData);
2623+
try {
2624+
return doInput(prompt, inliningTarget, context);
2625+
} finally {
2626+
IndirectCallContext.exit(frame, inliningTarget, indirectCallData, savedState);
2627+
}
2628+
}
2629+
2630+
@TruffleBoundary
2631+
private static Object doInput(Object prompt, Node inliningTarget, PythonContext context) {
26352632
PythonModule sysModule = context.getSysModule();
2636-
Object stdin = lookupAttr.execute(frame, inliningTarget, sysModule, T_STDIN);
2637-
Object stdout = lookupAttr.execute(frame, inliningTarget, sysModule, T_STDOUT);
2638-
Object stderr = lookupAttr.execute(frame, inliningTarget, sysModule, T_STDERR);
2633+
Object stdin = PyObjectLookupAttr.executeUncached(sysModule, T_STDIN);
2634+
Object stdout = PyObjectLookupAttr.executeUncached(sysModule, T_STDOUT);
2635+
Object stderr = PyObjectLookupAttr.executeUncached(sysModule, T_STDERR);
26392636

26402637
if (stdin instanceof PNone) {
2641-
throw raiseLostNode.raise(inliningTarget, RuntimeError, ErrorMessages.INPUT_LOST_SYS_S, T_STDIN);
2638+
throw PRaiseNode.raiseStatic(inliningTarget, RuntimeError, ErrorMessages.INPUT_LOST_SYS_S, T_STDIN);
26422639
}
26432640
if (stdout instanceof PNone) {
2644-
throw raiseLostNode.raise(inliningTarget, RuntimeError, ErrorMessages.INPUT_LOST_SYS_S, T_STDOUT);
2641+
throw PRaiseNode.raiseStatic(inliningTarget, RuntimeError, ErrorMessages.INPUT_LOST_SYS_S, T_STDOUT);
26452642
}
26462643
if (stderr instanceof PNone) {
2647-
throw raiseLostNode.raise(inliningTarget, RuntimeError, ErrorMessages.INPUT_LOST_SYS_S, T_STDERR);
2644+
throw PRaiseNode.raiseStatic(inliningTarget, RuntimeError, ErrorMessages.INPUT_LOST_SYS_S, T_STDERR);
26482645
}
26492646

2650-
auditNode.audit(inliningTarget, "builtins.input", prompt != NO_VALUE ? prompt : NONE);
2647+
SysModuleBuiltins.AuditNode.auditUncached("builtins.input", prompt != NO_VALUE ? prompt : NONE);
26512648

26522649
try {
2653-
callMethod.execute(frame, inliningTarget, stderr, T_FLUSH);
2650+
PyObjectCallMethodObjArgs.executeUncached(stderr, T_FLUSH);
26542651
} catch (AbstractTruffleException e) {
26552652
// Ignore
26562653
}
26572654

26582655
if (!(prompt instanceof PNone)) {
2659-
Object promptStr = strNode.execute(frame, inliningTarget, prompt);
2660-
callMethod.execute(frame, inliningTarget, stdout, T_WRITE, promptStr);
2656+
Object promptStr = PyObjectStrAsObjectNode.executeUncached(prompt);
2657+
PyObjectCallMethodObjArgs.executeUncached(stdout, T_WRITE, promptStr);
26612658
try {
2662-
callMethod.execute(frame, inliningTarget, stdout, T_FLUSH);
2659+
PyObjectCallMethodObjArgs.executeUncached(stdout, T_FLUSH);
26632660
} catch (AbstractTruffleException e) {
26642661
// Ignore
26652662
}
26662663
}
26672664

2668-
Object line = callMethod.execute(frame, inliningTarget, stdin, T_READLINE);
2669-
if (unicodeCheck.execute(inliningTarget, line)) {
2670-
TruffleString strLine = castToTruffleStringNode.castKnownString(inliningTarget, line);
2671-
int len = codePointLengthNode.execute(strLine, TS_ENCODING);
2665+
Object line = PyObjectCallMethodObjArgs.executeUncached(stdin, T_READLINE);
2666+
if (PyUnicodeCheckNode.executeUncached(line)) {
2667+
TruffleString strLine = CastToTruffleStringNode.castKnownStringUncached(line);
2668+
int len = strLine.codePointLengthUncached(TS_ENCODING);
26722669
if (len == 0) {
2673-
throw raiseEOFNode.raise(inliningTarget, EOFError, ErrorMessages.EOF_WHEN_READING_A_LINE);
2670+
throw PRaiseNode.raiseStatic(inliningTarget, EOFError, ErrorMessages.EOF_WHEN_READING_A_LINE);
26742671
}
2675-
int lastChar = codePointAtIndexNode.execute(strLine, len - 1, TS_ENCODING);
2672+
int lastChar = strLine.codePointAtIndexUncached(len - 1, TS_ENCODING);
26762673
if (lastChar == '\n') {
2677-
strLine = substringNode.execute(strLine, 0, len - 1, TS_ENCODING, false);
2674+
strLine = strLine.substringUncached(0, len - 1, TS_ENCODING, false);
26782675
}
26792676
return strLine;
2680-
} else if (bytesCheck.execute(inliningTarget, line)) {
2681-
byte[] bytesLine = toBytesNode.execute(frame, line);
2677+
} else if (PyBytesCheckNode.executeUncached(line)) {
2678+
byte[] bytesLine = PythonBufferAccessLibrary.getUncached().getCopiedByteArray(BytesNodes.GetBytesStorage.executeUncached(line));
26822679
if (bytesLine.length == 0) {
2683-
throw raiseEOFNode.raise(inliningTarget, EOFError, ErrorMessages.EOF_WHEN_READING_A_LINE);
2680+
throw PRaiseNode.raiseStatic(inliningTarget, EOFError, ErrorMessages.EOF_WHEN_READING_A_LINE);
26842681
}
2685-
PythonLanguage language = context.getLanguage(inliningTarget);
2682+
PythonLanguage language = context.getLanguage();
26862683
if (bytesLine[bytesLine.length - 1] == '\n') {
26872684
return PFactory.createBytes(language, bytesLine, bytesLine.length - 1);
26882685
} else {
26892686
return PFactory.createBytes(language, bytesLine);
26902687
}
26912688
} else {
2692-
throw raiseWrongType.raise(inliningTarget, TypeError, ErrorMessages.OBJECT_READLINE_RETURNED_NON_STRING);
2689+
throw PRaiseNode.raiseStatic(inliningTarget, TypeError, ErrorMessages.OBJECT_READLINE_RETURNED_NON_STRING);
26932690
}
26942691
}
26952692
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,10 @@ public void audit(Node inliningTarget, String event, Object... arguments) {
10501050
executeInternal(inliningTarget, event, arguments);
10511051
}
10521052

1053+
public static void auditUncached(String event, Object... arguments) {
1054+
SysModuleBuiltinsFactory.AuditNodeGen.getUncached().executeInternal(null, event, arguments);
1055+
}
1056+
10531057
public void audit(Node inliningTarget, TruffleString event, Object... arguments) {
10541058
executeInternal(inliningTarget, event, arguments);
10551059
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesNodes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,10 @@ static byte[] nonAscii(TruffleString str,
951951
public abstract static class GetBytesStorage extends Node {
952952
public abstract SequenceStorage execute(Node inliningTarget, Object bytes);
953953

954+
public static SequenceStorage executeUncached(Object bytes) {
955+
return BytesNodesFactory.GetBytesStorageNodeGen.getUncached().execute(null, bytes);
956+
}
957+
954958
@Specialization
955959
SequenceStorage getManaged(PBytesLike bytes) {
956960
return bytes.getSequenceStorage();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectStrAsObjectNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ public final Object execute(Node inliningTarget, Object object) {
8989
return execute(null, inliningTarget, object);
9090
}
9191

92+
public static Object executeUncached(Object object) {
93+
return getUncached().execute(null, null, object);
94+
}
95+
9296
@Specialization
9397
static Object str(TruffleString obj) {
9498
return obj;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/CastToTruffleStringNode.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ public final TruffleString castKnownString(Node inliningTarget, Object x) {
106106
}
107107
}
108108

109+
public static TruffleString castKnownStringUncached(Object x) {
110+
try {
111+
return executeUncached(x);
112+
} catch (CannotCastException ex) {
113+
throw CompilerDirectives.shouldNotReachHere(ex);
114+
}
115+
}
116+
109117
@Specialization
110118
static TruffleString doTruffleString(TruffleString x) {
111119
return x;

0 commit comments

Comments
 (0)