Skip to content

Commit 8df849f

Browse files
committed
Support start and end arguments in the bytes.index method
1 parent 7d5fb53 commit 8df849f

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.graal.python.builtins.objects.PNotImplemented;
6363
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltinsFactory.BytesLikeNoGeneralizationNodeGen;
6464
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexNode;
65+
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
6566
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode;
6667
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetSequenceStorageNode;
6768
import com.oracle.graal.python.builtins.objects.common.SequenceNodesFactory.GetObjectArrayNodeGen;
@@ -853,23 +854,42 @@ protected boolean doIt(byte[] bytes, byte[] suffix, int start, int end) {
853854

854855
// bytes.index(x)
855856
// bytearray.index(x)
856-
@Builtin(name = "index", minNumOfPositionalArgs = 2)
857+
@Builtin(name = "index", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 4)
857858
@GenerateNodeFactory
858-
public abstract static class ByteArrayIndexNode extends PythonBuiltinNode {
859-
@Child private SequenceStorageNodes.LenNode lenNode;
860-
859+
public abstract static class ByteArrayIndexNode extends PythonQuaternaryBuiltinNode {
861860
@Specialization
862-
public int index(VirtualFrame frame, PIBytesLike byteArray, Object arg,
863-
@Cached("create()") BytesNodes.FindNode findNode) {
864-
return findNode.execute(frame, byteArray, arg, 0, getLength(byteArray.getSequenceStorage()));
861+
int index(VirtualFrame frame, PIBytesLike byteArray, Object arg, @SuppressWarnings("unused") PNone start, @SuppressWarnings("unused") PNone end,
862+
@Shared("len") @Cached SequenceStorageNodes.LenNode lenNode,
863+
@Shared("storage") @Cached SequenceNodes.GetSequenceStorageNode getStorageNode,
864+
@Shared("findNode") @Cached("create()") BytesNodes.FindNode findNode) {
865+
return checkResult(findNode.execute(frame, byteArray, arg, 0, lenNode.execute(getStorageNode.execute(byteArray))));
865866
}
866867

867-
private int getLength(SequenceStorage s) {
868-
if (lenNode == null) {
869-
CompilerDirectives.transferToInterpreterAndInvalidate();
870-
lenNode = insert(SequenceStorageNodes.LenNode.create());
868+
@Specialization(guards = {"!isPNone(start)"})
869+
int indexWithStart(VirtualFrame frame, PIBytesLike byteArray, Object arg, Object start, @SuppressWarnings("unused") PNone end,
870+
@Shared("storage") @Cached SequenceNodes.GetSequenceStorageNode getStorageNode,
871+
@Shared("len") @Cached SequenceStorageNodes.LenNode lenNode,
872+
@Shared("findNode") @Cached("create()") BytesNodes.FindNode findNode) {
873+
return checkResult(findNode.execute(frame, byteArray, arg, start, lenNode.execute(getStorageNode.execute(byteArray))));
874+
}
875+
876+
@Specialization(guards = {"!isPNone(end)"})
877+
int indexWithEnd(VirtualFrame frame, PIBytesLike byteArray, Object arg, @SuppressWarnings("unused") PNone start, Object end,
878+
@Shared("findNode") @Cached("create()") BytesNodes.FindNode findNode) {
879+
return checkResult(checkResult(findNode.execute(frame, byteArray, arg, 0, end)));
880+
}
881+
882+
@Specialization(guards = {"!isPNone(start)", "!isPNone(end)"})
883+
int indexWithStartEnd(VirtualFrame frame, PIBytesLike byteArray, Object arg, Object start, Object end,
884+
@Shared("findNode") @Cached("create()") BytesNodes.FindNode findNode) {
885+
return checkResult(findNode.execute(frame, byteArray, arg, start, end));
886+
}
887+
888+
private int checkResult(int result) {
889+
if (result == -1) {
890+
throw raise(PythonBuiltinClassType.ValueError, ErrorMessages.SUBSECTION_NOT_FOUND);
871891
}
872-
return lenNode.execute(s);
892+
return result;
873893
}
874894
}
875895

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ public abstract class ErrorMessages {
473473
public static final String STRING_ARG_WO_ENCODING = "string argument without an encoding";
474474
public static final String STRING_INDEX_OUT_OF_RANGE = "IndexError: string index out of range";
475475
public static final String SUBSTRING_NOT_FOUND = "substring not found";
476+
public static final String SUBSECTION_NOT_FOUND = "subsection not found";
476477
public static final String SUPER_OBJ_MUST_BE_INST_SUB_OR_TYPE = "super(type, obj): obj must be an instance or subtype of type";
477478
public static final String TAKES_D_OR_D_ARGS = "%s takes %d or %d arguments";
478479
public static final String TAKES_D_POS_ARG_S_BUT_D_POS_ARG_S = "%s() takes %d positional argument%s but %d positional argument%s (and %d keyword-only argument%s) were given%s";

0 commit comments

Comments
 (0)