Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ def test_memoryview_acquire_release(self):
)
obj = TestType()
assert obj.get_bufcount() == 0
assert bytes(obj) == bytes([123, 124, 125, 126])
assert obj.get_bufcount() == 0
assert struct.unpack("4B", obj) == (123, 124, 125, 126)
assert obj.get_bufcount() == 0
with self.assertRaises(struct.error):
struct.unpack("B", obj)
assert obj.get_bufcount() == 0
mv1 = memoryview(obj)
assert mv1[3] == 126
assert obj.get_bufcount() == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@
import com.oracle.graal.python.builtins.PythonBuiltins;
import com.oracle.graal.python.builtins.objects.PNone;
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAcquireLibrary;
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
import com.oracle.graal.python.builtins.objects.str.PString;
import com.oracle.graal.python.lib.PyBufferRelease;
import com.oracle.graal.python.lib.PyObjectCheckBuffer;
import com.oracle.graal.python.lib.PyObjectGetBuffer;
import com.oracle.graal.python.nodes.ErrorMessages;
import com.oracle.graal.python.nodes.PRaiseNode;
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
Expand All @@ -75,8 +77,6 @@
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode;
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData;
import com.oracle.graal.python.runtime.PythonContext;
import com.oracle.graal.python.runtime.object.PFactory;
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
Expand Down Expand Up @@ -107,13 +107,11 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
}

abstract static class AsciiBufferConverter extends ArgumentCastNode {
@Specialization(guards = "acquireLib.hasBuffer(value)", limit = "getCallSiteInlineCacheMaxDepth()")
@Specialization(guards = "checkBufferNode.execute(value)", limit = "1")
static Object doObject(VirtualFrame frame, Object value,
@Bind Node inliningTarget,
@Bind PythonContext context,
@Cached("createFor($node)") InteropCallData callData,
@CachedLibrary("value") PythonBufferAcquireLibrary acquireLib) {
return acquireLib.acquireReadonly(value, frame, context, context.getLanguage(inliningTarget), callData);
@Cached PyObjectCheckBuffer checkBufferNode,
@Cached PyObjectGetBuffer getBuffer) {
return getBuffer.executeReadonly(frame, value);
}

@ExportLibrary(PythonBufferAccessLibrary.class)
Expand Down Expand Up @@ -193,14 +191,14 @@ public static AsciiBufferConverter create() {
abstract static class A2bBase64Node extends PythonBinaryClinicBuiltinNode {
@Specialization(limit = "3")
PBytes doConvert(VirtualFrame frame, Object buffer, boolean strictMode,
@Cached("createFor($node)") InteropCallData callData,
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib,
@Bind PythonLanguage language) {
@Bind PythonLanguage language,
@Cached PyBufferRelease releaseBuffer) {
try {
ByteSequenceStorage storage = b64decode(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer), strictMode);
return PFactory.createBytes(language, storage);
} finally {
bufferLib.release(buffer, frame, callData);
releaseBuffer.execute(frame, buffer);
}
}

Expand Down Expand Up @@ -278,14 +276,14 @@ protected ArgumentClinicProvider getArgumentClinic() {
abstract static class A2bHexNode extends PythonUnaryClinicBuiltinNode {
@Specialization(limit = "3")
PBytes a2b(VirtualFrame frame, Object buffer,
@Cached("createFor($node)") InteropCallData callData,
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib,
@Bind PythonLanguage language) {
@Bind PythonLanguage language,
@Cached PyBufferRelease releaseBuffer) {
try {
byte[] bytes = a2b(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer));
return PFactory.createBytes(language, bytes);
} finally {
bufferLib.release(buffer, frame, callData);
releaseBuffer.execute(frame, buffer);
}
}

Expand Down Expand Up @@ -342,13 +340,13 @@ private PBytes b2a(byte[] data, int lenght, boolean newline, PythonLanguage lang

@Specialization(limit = "3")
PBytes b2aBuffer(VirtualFrame frame, Object buffer, boolean newline,
@Cached("createFor($node)") InteropCallData callData,
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib,
@Bind PythonLanguage language) {
@Bind PythonLanguage language,
@Cached PyBufferRelease releaseBuffer) {
try {
return b2a(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer), newline, language);
} finally {
bufferLib.release(buffer, frame, callData);
releaseBuffer.execute(frame, buffer);
}
}

Expand All @@ -369,18 +367,18 @@ abstract static class B2aHexNode extends PythonTernaryClinicBuiltinNode {
@Specialization(limit = "3")
static PBytes b2a(VirtualFrame frame, Object buffer, Object sep, int bytesPerSep,
@Bind Node inliningTarget,
@Cached("createFor($node)") InteropCallData callData,
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib,
@Bind PythonLanguage language,
@Cached PRaiseNode raiseNode) {
@Cached PRaiseNode raiseNode,
@Cached PyBufferRelease releaseBuffer) {
if (sep != PNone.NO_VALUE || bytesPerSep != 1) {
// TODO implement sep and bytes_per_sep
throw raiseNode.raise(inliningTarget, NotImplementedError);
}
try {
return b2a(bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer), language);
} finally {
bufferLib.release(buffer, frame, callData);
releaseBuffer.execute(frame, buffer);
}
}

Expand Down Expand Up @@ -410,12 +408,12 @@ abstract static class Crc32Node extends PythonBinaryClinicBuiltinNode {
@Specialization(limit = "3")
static long b2a(VirtualFrame frame, Object buffer, long crc,
@Bind Node inliningTarget,
@Cached("createFor($node)") InteropCallData callData,
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib) {
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib,
@Cached PyBufferRelease releaseBuffer) {
try {
return crc32(inliningTarget, (int) crc, bufferLib.getInternalOrCopiedByteArray(buffer), 0, bufferLib.getBufferLength(buffer));
} finally {
bufferLib.release(buffer, frame, callData);
releaseBuffer.execute(frame, buffer);
}
}

Expand All @@ -434,12 +432,12 @@ abstract static class CrcHqxNode extends PythonBinaryClinicBuiltinNode {
@Specialization(limit = "3")
static long b2a(VirtualFrame frame, Object buffer, long crc,
@Bind Node inliningTarget,
@Cached("createFor($node)") InteropCallData callData,
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib) {
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib,
@Cached PyBufferRelease releaseBuffer) {
try {
return crcHqx(inliningTarget, (int) crc, bufferLib.getInternalOrCopiedByteArray(buffer), 0, bufferLib.getBufferLength(buffer));
} finally {
bufferLib.release(buffer, frame, callData);
releaseBuffer.execute(frame, buffer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
import com.oracle.graal.python.builtins.objects.PNone;
import com.oracle.graal.python.builtins.objects.PNotImplemented;
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAcquireLibrary;
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
Expand Down Expand Up @@ -171,6 +170,7 @@
import com.oracle.graal.python.compiler.ParserCallbacksImpl;
import com.oracle.graal.python.compiler.bytecode_dsl.BytecodeDSLCompiler;
import com.oracle.graal.python.lib.IteratorExhausted;
import com.oracle.graal.python.lib.PyBufferRelease;
import com.oracle.graal.python.lib.PyBytesCheckNode;
import com.oracle.graal.python.lib.PyCallableCheckNode;
import com.oracle.graal.python.lib.PyEvalGetGlobals;
Expand All @@ -192,6 +192,7 @@
import com.oracle.graal.python.lib.PyObjectFormat;
import com.oracle.graal.python.lib.PyObjectGetAIter;
import com.oracle.graal.python.lib.PyObjectGetAttrO;
import com.oracle.graal.python.lib.PyObjectGetBuffer;
import com.oracle.graal.python.lib.PyObjectGetIter;
import com.oracle.graal.python.lib.PyObjectGetMethod;
import com.oracle.graal.python.lib.PyObjectHashNode;
Expand Down Expand Up @@ -269,7 +270,6 @@
import com.oracle.graal.python.runtime.ExecutionContext.BoundaryCallContext;
import com.oracle.graal.python.runtime.GilNode;
import com.oracle.graal.python.runtime.IndirectCallData.BoundaryCallData;
import com.oracle.graal.python.runtime.IndirectCallData.InteropCallData;
import com.oracle.graal.python.runtime.PythonContext;
import com.oracle.graal.python.runtime.PythonOptions;
import com.oracle.graal.python.runtime.exception.PException;
Expand Down Expand Up @@ -1067,7 +1067,7 @@ Object compile(TruffleString expression, TruffleString filename, TruffleString m
@Specialization(limit = "3")
@SuppressWarnings("truffle-static-method")
Object generic(VirtualFrame frame, Object wSource, Object wFilename, TruffleString mode, int flags, boolean dontInherit, int optimize, int featureVersion,
@CachedLibrary(limit = "3") PythonBufferAcquireLibrary acquireLib,
@Cached PyObjectGetBuffer getBuffer,
@CachedLibrary(limit = "3") PythonBufferAccessLibrary bufferLib,
@Bind PythonContext context,
@Bind Node inliningTarget,
Expand Down Expand Up @@ -1103,7 +1103,7 @@ Object generic(VirtualFrame frame, Object wSource, Object wFilename, TruffleStri
RootCallTarget rootCallTarget = context.getLanguage(inliningTarget).compileModule(context, mod, source, false, optimize, null, null, flags);
return wrapRootCallTarget(rootCallTarget, filename);
}
TruffleString source = sourceAsString(frame, inliningTarget, wSource, filename, interopLib, acquireLib, bufferLib, switchEncodingNode, raiseNode);
TruffleString source = sourceAsString(frame, inliningTarget, wSource, filename, interopLib, getBuffer, bufferLib, switchEncodingNode, raiseNode);
checkSource(source);
return compile(source, filename, mode, flags, dontInherit, optimize, featureVersion);
} finally {
Expand Down Expand Up @@ -1167,7 +1167,7 @@ private InputType getParserInputType(TruffleString mode, int flags) {
}

// modeled after _Py_SourceAsString
TruffleString sourceAsString(VirtualFrame frame, Node inliningTarget, Object source, TruffleString filename, InteropLibrary interopLib, PythonBufferAcquireLibrary acquireLib,
TruffleString sourceAsString(VirtualFrame frame, Node inliningTarget, Object source, TruffleString filename, InteropLibrary interopLib, PyObjectGetBuffer getBuffer,
PythonBufferAccessLibrary bufferLib, TruffleString.SwitchEncodingNode switchEncodingNode, PRaiseNode raiseNode) {
if (interopLib.isString(source)) {
try {
Expand All @@ -1180,7 +1180,7 @@ TruffleString sourceAsString(VirtualFrame frame, Node inliningTarget, Object sou
// buffers, since that's fast for us anyway
Object buffer;
try {
buffer = acquireLib.acquireReadonly(source, frame, InteropCallData.getUncached());
buffer = getBuffer.executeReadonly(frame, source);
} catch (PException e) {
throw raiseNode.raise(inliningTarget, TypeError, ErrorMessages.ARG_D_MUST_BE_S, "compile()", 1, "string, bytes or AST object");
}
Expand All @@ -1189,7 +1189,7 @@ TruffleString sourceAsString(VirtualFrame frame, Node inliningTarget, Object sou
int bytesLen = bufferLib.getBufferLength(source);
return doDecodeSource(getContext(), source, filename, bytes, bytesLen);
} finally {
bufferLib.release(buffer, frame, InteropCallData.getUncached());
PyBufferRelease.getUncached().execute(frame, buffer);
}
}
}
Expand Down
Loading
Loading