Skip to content

Commit 72bbb21

Browse files
committed
Fixup getBufferBytes for memoryview
1 parent e30a2e0 commit 72bbb21

File tree

1 file changed

+10
-9
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview

1 file changed

+10
-9
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/PMemoryView.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.memoryview;
4242

43+
import static com.oracle.graal.python.nodes.SpecialMethodNames.TOBYTES;
44+
4345
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
4446
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
4547
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromDynamicObjectNode;
46-
import com.oracle.graal.python.nodes.util.CastToByteNode;
48+
import com.oracle.truffle.api.CompilerDirectives;
4749
import com.oracle.truffle.api.dsl.Cached;
4850
import com.oracle.truffle.api.dsl.Cached.Shared;
51+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
4952
import com.oracle.truffle.api.library.CachedLibrary;
5053
import com.oracle.truffle.api.library.ExportLibrary;
5154
import com.oracle.truffle.api.library.ExportMessage;
@@ -76,15 +79,13 @@ int getBufferLength(
7679
@ExportMessage
7780
byte[] getBufferBytes(
7881
@Shared("readNativeMemoryViewNode") @Cached ReadAttributeFromDynamicObjectNode readNativeMemoryViewNode,
79-
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
80-
@Cached PInteropSubscriptNode subscriptNode,
81-
@Cached CastToByteNode castToByteNode) {
82+
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
8283
Object nativeMemoryViewObject = readNativeMemoryViewNode.execute(getStorage(), C_MEMORYVIEW);
83-
int len = lib.length(nativeMemoryViewObject);
84-
byte[] data = new byte[len];
85-
for (int i = 0; i < data.length; i++) {
86-
data[i] = castToByteNode.execute(null, subscriptNode.execute(nativeMemoryViewObject, i));
84+
Object bytes = lib.lookupAndCallRegularMethod(nativeMemoryViewObject, null, TOBYTES);
85+
try {
86+
return lib.getBufferBytes(bytes);
87+
} catch (UnsupportedMessageException e) {
88+
throw CompilerDirectives.shouldNotReachHere("memoryview.tobytes didn't return bytes");
8789
}
88-
return data;
8990
}
9091
}

0 commit comments

Comments
 (0)