Skip to content

Commit 8a03a40

Browse files
committed
fix toNative of PySequenceArrayWrapper to generalize to Object before converting
1 parent 8a95851 commit 8a03a40

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PyMemoryViewBufferWrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol.FUN_GET_PY_BUFFER_TYPEID;
4444
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol.FUN_PY_TRUFFLE_LONG_ARRAY_TO_NATIVE;
4545

46+
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
4647
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
4748
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
4849
import com.oracle.graal.python.builtins.objects.object.PythonObject;
@@ -170,7 +171,7 @@ static Object getBufManaged(PMemoryView object, @SuppressWarnings("unused") Stri
170171
@Cached PySequenceArrayWrapper.ToNativeStorageNode toNativeStorageNode) {
171172
// TODO GR-21120: Add support for PArray
172173
PSequence owner = (PSequence) object.getOwner();
173-
NativeSequenceStorage nativeStorage = toNativeStorageNode.execute(getStorage.execute(owner));
174+
NativeSequenceStorage nativeStorage = toNativeStorageNode.execute(getStorage.execute(owner), owner instanceof PBytesLike);
174175
if (nativeStorage == null) {
175176
throw CompilerDirectives.shouldNotReachHere("cannot allocate native storage");
176177
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PySequenceArrayWrapper.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import com.oracle.graal.python.util.PythonUtils;
7676
import com.oracle.truffle.api.CompilerAsserts;
7777
import com.oracle.truffle.api.CompilerDirectives;
78+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7879
import com.oracle.truffle.api.dsl.Cached;
7980
import com.oracle.truffle.api.dsl.Cached.Exclusive;
8081
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -397,7 +398,7 @@ static Object doPSequence(PySequenceArrayWrapper object,
397398
@CachedLibrary(limit = "3") PythonNativeWrapperLibrary lib,
398399
@Exclusive @Cached ToNativeStorageNode toNativeStorageNode) {
399400
PSequence sequence = (PSequence) lib.getDelegate(object);
400-
NativeSequenceStorage nativeStorage = toNativeStorageNode.execute(getStorage.execute(sequence));
401+
NativeSequenceStorage nativeStorage = toNativeStorageNode.execute(getStorage.execute(sequence), sequence instanceof PBytesLike);
401402
if (nativeStorage == null) {
402403
CompilerDirectives.transferToInterpreter();
403404
throw new IllegalStateException("could not allocate native storage");
@@ -423,22 +424,38 @@ protected static boolean isPSequence(Object obj) {
423424
@GenerateUncached
424425
abstract static class ToNativeStorageNode extends Node {
425426

426-
public abstract NativeSequenceStorage execute(SequenceStorage object);
427+
public abstract NativeSequenceStorage execute(SequenceStorage object, boolean isBytesLike);
427428

428-
@Specialization(guards = "!isNative(s)")
429-
static NativeSequenceStorage doManaged(SequenceStorage s,
429+
public static boolean isEmptySequenceStorage(SequenceStorage s) {
430+
return s instanceof EmptySequenceStorage;
431+
}
432+
433+
@Specialization(guards = {"!isNative(s)", "!isEmptySequenceStorage(s)"})
434+
static NativeSequenceStorage doManaged(SequenceStorage s, @SuppressWarnings("unused") boolean isBytesLike,
435+
@Cached ConditionProfile isObjectArrayProfile,
430436
@Shared("storageToNativeNode") @Cached SequenceStorageNodes.StorageToNativeNode storageToNativeNode,
431437
@Cached SequenceStorageNodes.GetInternalArrayNode getInternalArrayNode) {
432-
return storageToNativeNode.execute(getInternalArrayNode.execute(s));
438+
Object array = getInternalArrayNode.execute(s);
439+
if (isBytesLike) {
440+
assert array instanceof byte[];
441+
} else if (!isObjectArrayProfile.profile(array instanceof Object[])) {
442+
array = generalize(s);
443+
}
444+
return storageToNativeNode.execute(array);
445+
}
446+
447+
@TruffleBoundary
448+
private static Object generalize(SequenceStorage s) {
449+
return s.getInternalArray();
433450
}
434451

435452
@Specialization
436-
static NativeSequenceStorage doNative(NativeSequenceStorage s) {
453+
static NativeSequenceStorage doNative(NativeSequenceStorage s, @SuppressWarnings("unused") boolean isBytesLike) {
437454
return s;
438455
}
439456

440457
@Specialization
441-
static NativeSequenceStorage doEmptyStorage(@SuppressWarnings("unused") EmptySequenceStorage s,
458+
static NativeSequenceStorage doEmptyStorage(@SuppressWarnings("unused") EmptySequenceStorage s, @SuppressWarnings("unused") boolean isBytesLike,
442459
@Shared("storageToNativeNode") @Cached SequenceStorageNodes.StorageToNativeNode storageToNativeNode) {
443460
// TODO(fa): not sure if that completely reflects semantics
444461
return storageToNativeNode.execute(PythonUtils.EMPTY_BYTE_ARRAY);

0 commit comments

Comments
 (0)