Skip to content

Commit 9a77d90

Browse files
committed
Setup indirect call for buffer lib usage
1 parent 201fcff commit 9a77d90

File tree

7 files changed

+42
-30
lines changed

7 files changed

+42
-30
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode;
8888
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
8989
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
90+
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
9091
import com.oracle.graal.python.runtime.PythonContext;
9192
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
9293
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
@@ -132,12 +133,15 @@ protected static LookupAndCallBinaryNode createCallWriteNode() {
132133
@Specialization
133134
Object doit(VirtualFrame frame, Object value, Object file, int version,
134135
@Cached("createCallWriteNode()") LookupAndCallBinaryNode callNode) {
136+
Object savedState = IndirectCallContext.enter(frame, this);
135137
try {
136138
return callNode.executeObject(frame, file, factory().createBytes(Marshal.dump(value, version, getCore())));
137139
} catch (IOException e) {
138140
throw CompilerDirectives.shouldNotReachHere(e);
139141
} catch (Marshal.MarshalError me) {
140142
throw raise(me.type, me.message, me.arguments);
143+
} finally {
144+
IndirectCallContext.exit(frame, this, savedState);
141145
}
142146
}
143147
}
@@ -152,13 +156,16 @@ protected ArgumentClinicProvider getArgumentClinic() {
152156
}
153157

154158
@Specialization
155-
Object doit(Object value, int version) {
159+
Object doit(VirtualFrame frame, Object value, int version) {
160+
Object savedState = IndirectCallContext.enter(frame, this);
156161
try {
157162
return factory().createBytes(Marshal.dump(value, version, getCore()));
158163
} catch (IOException e) {
159164
throw CompilerDirectives.shouldNotReachHere(e);
160165
} catch (Marshal.MarshalError me) {
161166
throw raise(me.type, me.message, me.arguments);
167+
} finally {
168+
IndirectCallContext.exit(frame, this, savedState);
162169
}
163170
}
164171
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
6060
import com.oracle.graal.python.nodes.util.CannotCastException;
6161
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
62+
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
6263
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6364
import com.oracle.truffle.api.dsl.Cached;
6465
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -105,7 +106,7 @@ static Object doObject(VirtualFrame frame, Object value, Object index,
105106
abstract static class CompareDigestNode extends PythonBinaryBuiltinNode {
106107

107108
@Specialization
108-
public boolean compare(Object left, Object right,
109+
boolean compare(VirtualFrame frame, Object left, Object right,
109110
@Cached CastToJavaStringNode cast,
110111
@CachedLibrary(limit = "3") PythonBufferAcquireLibrary bufferAcquireLib,
111112
@CachedLibrary(limit = "3") PythonBufferAccessLibrary bufferLib) {
@@ -117,6 +118,7 @@ public boolean compare(Object left, Object right,
117118
if (!bufferAcquireLib.hasBuffer(left) || !bufferAcquireLib.hasBuffer(right)) {
118119
throw raise(TypeError, "unsupported operand types(s) or combination of types: '%p' and '%p'", left, right);
119120
}
121+
Object savedState = IndirectCallContext.enter(frame, this);
120122
Object leftBuffer = bufferAcquireLib.acquireReadonly(left);
121123
try {
122124
Object rightBuffer = bufferAcquireLib.acquireReadonly(right);
@@ -127,6 +129,7 @@ public boolean compare(Object left, Object right,
127129
}
128130
} finally {
129131
bufferLib.release(leftBuffer);
132+
IndirectCallContext.exit(frame, this, savedState);
130133
}
131134
}
132135
}

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
5555
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAcquireLibrary;
5656
import com.oracle.graal.python.builtins.objects.function.PFunction;
57-
import com.oracle.graal.python.nodes.PRaiseNode;
57+
import com.oracle.graal.python.nodes.PNodeWithRaiseAndIndirectCall;
5858
import com.oracle.graal.python.nodes.call.CallNode;
5959
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6060
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
@@ -78,7 +78,6 @@
7878
import com.oracle.truffle.api.interop.UnsupportedMessageException;
7979
import com.oracle.truffle.api.interop.UnsupportedTypeException;
8080
import com.oracle.truffle.api.library.CachedLibrary;
81-
import com.oracle.truffle.api.nodes.Node;
8281
import com.oracle.truffle.api.profiles.BranchProfile;
8382
import com.oracle.truffle.api.source.Source;
8483
import com.oracle.truffle.api.source.SourceSection;
@@ -96,9 +95,9 @@ public void initialize(Python3Core core) {
9695
super.initialize(core);
9796
}
9897

99-
abstract static class ToRegexSourceNode extends Node {
98+
abstract static class ToRegexSourceNode extends PNodeWithRaiseAndIndirectCall {
10099

101-
public abstract Source execute(Object pattern, String flags);
100+
public abstract Source execute(VirtualFrame frame, Object pattern, String flags);
102101

103102
@TruffleBoundary
104103
private static String decodeLatin1(byte[] bytes, int length) {
@@ -123,11 +122,10 @@ protected Source doString(String pattern, String flags) {
123122
}
124123

125124
@Specialization(limit = "3")
126-
protected Source doGeneric(Object pattern, String flags,
125+
protected Source doGeneric(VirtualFrame frame, Object pattern, String flags,
127126
@CachedLibrary("pattern") InteropLibrary interopLib,
128127
@CachedLibrary("pattern") PythonBufferAcquireLibrary bufferAcquireLib,
129-
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib,
130-
@Cached PRaiseNode raise) {
128+
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib) {
131129
if (interopLib.isString(pattern)) {
132130
try {
133131
return doString(interopLib.asString(pattern), flags);
@@ -137,9 +135,9 @@ protected Source doGeneric(Object pattern, String flags,
137135
}
138136
Object buffer;
139137
try {
140-
buffer = bufferAcquireLib.acquireReadonly(pattern);
138+
buffer = bufferAcquireLib.acquireReadonly(pattern, frame, this);
141139
} catch (PException e) {
142-
throw raise.raise(TypeError, "expected string or bytes-like object");
140+
throw raise(TypeError, "expected string or bytes-like object");
143141
}
144142
try {
145143
String options = "Flavor=PythonBytes,Encoding=BYTES";
@@ -170,12 +168,11 @@ Object call(VirtualFrame frame, Object pattern, Object flags, PFunction fallback
170168
@CachedLibrary(limit = "2") InteropLibrary compiledRegexLib) {
171169
try {
172170
String flagsStr = toStringNode.execute(flags);
173-
Source regexSource = toRegexSourceNode.execute(pattern, flagsStr);
174-
PythonContext context = PythonContext.get(this);
175-
Object compiledRegex = context.getEnv().parseInternal(regexSource).call();
171+
Source regexSource = toRegexSourceNode.execute(frame, pattern, flagsStr);
172+
Object compiledRegex = getContext().getEnv().parseInternal(regexSource).call();
176173
if (compiledRegexLib.isNull(compiledRegex)) {
177174
unsupportedRegexError.enter();
178-
if (context.getLanguage().getEngineOption(PythonOptions.TRegexUsesSREFallback)) {
175+
if (getLanguage().getEngineOption(PythonOptions.TRegexUsesSREFallback)) {
179176
return callFallbackCompilerNode.execute(frame, fallbackCompiler, pattern, flags);
180177
} else {
181178
throw raise(ValueError, "regular expression not supported, no fallback engine present");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/LazyPyCArrayTypeBuiltins.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
8787
import com.oracle.truffle.api.dsl.NodeFactory;
8888
import com.oracle.truffle.api.dsl.Specialization;
89+
import com.oracle.truffle.api.frame.VirtualFrame;
8990
import com.oracle.truffle.api.library.CachedLibrary;
9091

9192
public class LazyPyCArrayTypeBuiltins extends PythonBuiltins {
@@ -144,10 +145,10 @@ Object doGet(CDataObject self, @SuppressWarnings("unused") PNone value,
144145
}
145146

146147
@Specialization
147-
Object doSet(CDataObject self, Object value,
148+
Object doSet(VirtualFrame frame, CDataObject self, Object value,
148149
@CachedLibrary(limit = "1") PythonBufferAcquireLibrary qlib,
149150
@CachedLibrary(limit = "1") PythonBufferAccessLibrary alib) {
150-
Object buf = qlib.acquire(value, BufferFlags.PyBUF_SIMPLE);
151+
Object buf = qlib.acquire(value, BufferFlags.PyBUF_SIMPLE, frame, this);
151152
byte[] bytes = alib.getInternalOrCopiedByteArray(buf);
152153
if (bytes.length > self.b_size) {
153154
throw raise(ValueError, BYTE_STRING_TOO_LONG);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
import com.oracle.graal.python.nodes.util.CastToJavaByteNode;
119119
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
120120
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
121+
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
121122
import com.oracle.graal.python.runtime.PythonContext;
122123
import com.oracle.graal.python.runtime.exception.PException;
123124
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -586,16 +587,21 @@ static Object doGeneric(@SuppressWarnings("unused") Object self) {
586587
abstract static class ModNode extends PythonBinaryBuiltinNode {
587588

588589
@Specialization(limit = "2")
589-
Object mod(PBytesLike self, Object right,
590+
Object mod(VirtualFrame frame, PBytesLike self, Object right,
590591
@CachedLibrary("self") PythonBufferAccessLibrary bufferLib,
591592
@Cached BytesNodes.CreateBytesNode create,
592593
@Cached PyObjectGetItem getItemNode,
593594
@Cached TupleBuiltins.GetItemNode getTupleItemNode) {
594595
byte[] bytes = bufferLib.getInternalOrCopiedByteArray(self);
595596
int bytesLen = bufferLib.getBufferLength(self);
596597
BytesFormatProcessor formatter = new BytesFormatProcessor(PythonContext.get(this).getCore(), getRaiseNode(), getItemNode, getTupleItemNode, bytes, bytesLen);
597-
byte[] data = formatter.format(right);
598-
return create.execute(factory(), self, data);
598+
Object savedState = IndirectCallContext.enter(frame, this);
599+
try {
600+
byte[] data = formatter.format(right);
601+
return create.execute(factory(), self, data);
602+
} finally {
603+
IndirectCallContext.exit(frame, this, savedState);
604+
}
599605
}
600606

601607
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
import com.oracle.graal.python.nodes.PNodeWithContext;
8181
import com.oracle.graal.python.nodes.PNodeWithRaise;
8282
import com.oracle.graal.python.nodes.PNodeWithRaiseAndIndirectCall;
83-
import com.oracle.graal.python.nodes.PRaiseNode;
8483
import com.oracle.graal.python.nodes.SpecialMethodNames;
8584
import com.oracle.graal.python.nodes.control.GetNextNode;
8685
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode;
@@ -210,7 +209,7 @@ public abstract static class ToBytesNode extends PNodeWithRaiseAndIndirectCall {
210209
this.errorType = errorType;
211210
this.errorMessageFormat = errorMessageFormat;
212211
}
213-
212+
214213
public final byte[] execute(PBytesLike obj) {
215214
return execute(null, obj);
216215
}
@@ -586,22 +585,21 @@ public static ExpectStringNode create(@ArgumentIndex int argNum, String classNam
586585
* already bytes. We obviously cannot do that here, it must be done by the caller if the need
587586
* this behavior.
588587
*/
589-
public abstract static class BytesFromObject extends PNodeWithContext {
588+
public abstract static class BytesFromObject extends PNodeWithRaiseAndIndirectCall {
590589
public abstract byte[] execute(VirtualFrame frame, Object object);
591590

592591
// TODO make fast paths for builtin list/tuple - note that FromSequenceNode doesn't work
593592
// properly when the list is mutated by its __index__
594593

595594
@Specialization
596-
static byte[] doGeneric(VirtualFrame frame, Object object,
595+
byte[] doGeneric(VirtualFrame frame, Object object,
597596
@CachedLibrary(limit = "3") PythonBufferAcquireLibrary bufferAcquireLib,
598597
@CachedLibrary(limit = "3") PythonBufferAccessLibrary bufferLib,
599598
@Cached BytesNodes.IterableToByteNode iterableToByteNode,
600-
@Cached IsBuiltinClassProfile errorProfile,
601-
@Cached PRaiseNode raise) {
599+
@Cached IsBuiltinClassProfile errorProfile) {
602600
if (bufferAcquireLib.hasBuffer(object)) {
603601
// TODO PyBUF_FULL_RO
604-
Object buffer = bufferAcquireLib.acquire(object, BufferFlags.PyBUF_ND);
602+
Object buffer = bufferAcquireLib.acquire(object, BufferFlags.PyBUF_ND, frame, this);
605603
try {
606604
return bufferLib.getCopiedByteArray(buffer);
607605
} finally {
@@ -615,7 +613,7 @@ static byte[] doGeneric(VirtualFrame frame, Object object,
615613
e.expect(TypeError, errorProfile);
616614
}
617615
}
618-
throw raise.raise(TypeError, ErrorMessages.CANNOT_CONVERT_P_OBJ_TO_S, object);
616+
throw raise(TypeError, ErrorMessages.CANNOT_CONVERT_P_OBJ_TO_S, object);
619617
}
620618
}
621619

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/SocketBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode;
8787
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
8888
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
89-
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
9089
import com.oracle.graal.python.runtime.GilNode;
9190
import com.oracle.graal.python.runtime.PosixConstants;
9291
import com.oracle.graal.python.runtime.PosixSupportLibrary;
@@ -1030,13 +1029,14 @@ Object setInt(VirtualFrame frame, PSocket socket, int level, int option, Object
10301029
len = bytes.length;
10311030
PythonUtils.arrayAccessor.putInt(bytes, 0, flag);
10321031
} catch (PException e) {
1033-
Object buffer = bufferAcquireLib.acquireReadonly(value);
1032+
Object buffer = bufferAcquireLib.acquireReadonly(value, frame, this);
10341033
try {
10351034
len = bufferLib.getBufferLength(buffer);
10361035
bytes = bufferLib.getInternalOrCopiedByteArray(buffer);
10371036
} finally {
10381037
bufferLib.release(buffer);
10391038
}
1039+
10401040
}
10411041
try {
10421042
posixLib.setsockopt(getPosixSupport(), socket.getFd(), level, option, bytes, len);

0 commit comments

Comments
 (0)