Skip to content

Commit 7427dba

Browse files
committed
Move set-slice implementation to a single location in a node.
1 parent 1d04ab1 commit 7427dba

19 files changed

+510
-176
lines changed

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

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -675,17 +675,11 @@ protected static GetItemNode createGetItem() {
675675
@GenerateNodeFactory
676676
@ImportStatic(SpecialMethodNames.class)
677677
abstract static class SetItemNode extends PythonTernaryBuiltinNode {
678-
@Child private SequenceStorageNodes.NormalizeIndexNode normalize;
679-
680-
@Specialization
681-
PNone doInt(PByteArray self, int idx, Object value) {
682-
self.setItemNormalized(ensureNormalize().execute(idx, self.len()), value);
683-
return PNone.NONE;
684-
}
678+
@Child private SequenceStorageNodes.SetItemNode setItemNode;
685679

686-
@Specialization
687-
PNone doSliceSequence(PByteArray self, PSlice slice, PSequence value) {
688-
self.setSlice(slice, value);
680+
@Specialization(guards = "!isMemoryView(value)")
681+
PNone doBasic(PByteArray self, Object idx, Object value) {
682+
getSetItemNode().execute(self.getSequenceStorage(), idx, value);
689683
return PNone.NONE;
690684
}
691685

@@ -695,62 +689,35 @@ PNone doSliceMemoryview(PByteArray self, PSlice slice, PMemoryView value,
695689
@Cached("createBinaryProfile()") ConditionProfile isBytesProfile) {
696690
Object bytesObj = callToBytesNode.executeObject(value);
697691
if (isBytesProfile.profile(bytesObj instanceof PBytes)) {
698-
doSliceSequence(self, slice, (PBytes) bytesObj);
692+
doBasic(self, slice, bytesObj);
699693
return PNone.NONE;
700694
}
701695
throw raise(SystemError, "could not get bytes of memoryview");
702696
}
703697

704-
@Specialization(guards = "isScalar(value)")
705-
@SuppressWarnings("unused")
706-
PNone doSliceScalar(PByteArray self, PSlice slice, Object value) {
707-
throw raise(TypeError, "can assign only bytes, buffers, or iterables of ints in range(0, 256)");
708-
}
709-
710-
@Specialization(rewriteOn = ArithmeticException.class)
711-
public Object doLong(PByteArray primary, long idx, Object value) {
712-
return doInt(primary, PInt.intValueExact(idx), value);
713-
}
714-
715-
@Specialization(replaces = "doLong")
716-
public Object doLongOvf(PByteArray primary, long idx, Object value) {
717-
try {
718-
return doInt(primary, PInt.intValueExact(idx), value);
719-
} catch (ArithmeticException e) {
720-
throw raiseIndexError();
721-
}
722-
}
723-
724-
@Specialization(rewriteOn = ArithmeticException.class)
725-
public Object doPInt(PByteArray primary, PInt idx, Object value) {
726-
return doInt(primary, idx.intValueExact(), value);
727-
}
728-
729-
@Specialization(replaces = "doPInt")
730-
public Object doPIntOvf(PByteArray primary, PInt idx, Object value) {
731-
try {
732-
return doInt(primary, idx.intValueExact(), value);
733-
} catch (ArithmeticException e) {
734-
throw raiseIndexError();
735-
}
736-
}
698+
// TODO error message
699+
// @Specialization(guards = "isScalar(value)")
700+
// @SuppressWarnings("unused")
701+
// PNone doSliceScalar(PByteArray self, PSlice slice, Object value) {
702+
// throw raise(TypeError, "can assign only bytes, buffers, or iterables of ints in range(0, 256)");
703+
// }
737704

738705
@Fallback
739706
@SuppressWarnings("unused")
740707
Object doGeneric(Object self, Object idx, Object value) {
741708
return PNotImplemented.NOT_IMPLEMENTED;
742709
}
743710

744-
private SequenceStorageNodes.NormalizeIndexNode ensureNormalize() {
745-
if (normalize == null) {
711+
private SequenceStorageNodes.SetItemNode getSetItemNode() {
712+
if (setItemNode == null) {
746713
CompilerDirectives.transferToInterpreterAndInvalidate();
747-
normalize = insert(SequenceStorageNodes.NormalizeIndexNode.forArrayAssign());
714+
setItemNode = insert(SequenceStorageNodes.SetItemNode.create(NormalizeIndexNode.forBytearray()));
748715
}
749-
return normalize;
716+
return setItemNode;
750717
}
751718

752-
protected boolean isScalar(Object value) {
753-
return !(value instanceof PSequence || value instanceof PMemoryView);
719+
protected static boolean isMemoryView(Object value) {
720+
return value instanceof PMemoryView;
754721
}
755722
}
756723

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
3939
import com.oracle.graal.python.runtime.sequence.storage.EmptySequenceStorage;
4040
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage;
41-
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage.ElementType;
4241
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
4342
import com.oracle.graal.python.runtime.sequence.storage.SequenceStoreException;
43+
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage.ListStorageType;
4444
import com.oracle.truffle.api.CompilerAsserts;
4545

4646
public final class PByteArray extends PSequence implements PIBytesLike {
@@ -101,7 +101,7 @@ public SequenceStorage getSequenceStorage() {
101101

102102
@Override
103103
public final void setSequenceStorage(SequenceStorage store) {
104-
assert store instanceof ByteSequenceStorage || store instanceof NativeSequenceStorage && ((NativeSequenceStorage) store).getElementType() == ElementType.BYTE;
104+
assert store instanceof ByteSequenceStorage || store instanceof NativeSequenceStorage && ((NativeSequenceStorage) store).getElementType() == ListStorageType.Byte;
105105
this.store = store;
106106
}
107107

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
import com.oracle.graal.python.runtime.sequence.PSequence;
3434
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
3535
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage;
36-
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage.ElementType;
3736
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
37+
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage.ListStorageType;
3838
import com.oracle.truffle.api.CompilerAsserts;
3939

4040
public final class PBytes extends PImmutableSequence implements PIBytesLike {
@@ -58,7 +58,7 @@ public SequenceStorage getSequenceStorage() {
5858

5959
@Override
6060
public void setSequenceStorage(SequenceStorage store) {
61-
assert store instanceof ByteSequenceStorage || store instanceof NativeSequenceStorage && ((NativeSequenceStorage) store).getElementType() == ElementType.BYTE;
61+
assert store instanceof ByteSequenceStorage || store instanceof NativeSequenceStorage && ((NativeSequenceStorage) store).getElementType() == ListStorageType.Byte;
6262
this.store = store;
6363
}
6464

0 commit comments

Comments
 (0)