Skip to content

Commit 5330dae

Browse files
committed
Improve implementation of 'PyTuple_GetItem'.
1 parent 920526f commit 5330dae

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/modules/TruffleCextBuiltins.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.IndexError;
44+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError;
4345
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
44-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
4546
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
4647

4748
import java.math.BigInteger;
@@ -143,6 +144,7 @@
143144
import com.oracle.graal.python.runtime.exception.PException;
144145
import com.oracle.graal.python.runtime.exception.PythonErrorType;
145146
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
147+
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
146148
import com.oracle.truffle.api.CallTarget;
147149
import com.oracle.truffle.api.CompilerDirectives;
148150
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -2059,4 +2061,27 @@ int doI(PythonClass a, PythonClass b) {
20592061
}
20602062
}
20612063

2064+
@Builtin(name = "PyTuple_GetItem", fixedNumOfPositionalArgs = 2)
2065+
@GenerateNodeFactory
2066+
abstract static class PyTuple_GetItem extends PythonBinaryBuiltinNode {
2067+
2068+
@Specialization
2069+
Object doPTuple(PTuple tuple, long key,
2070+
@Cached("create()") SequenceStorageNodes.LenNode lenNode,
2071+
@Cached("createNotNormalized()") SequenceStorageNodes.GetItemNode getItemNode) {
2072+
SequenceStorage sequenceStorage = tuple.getSequenceStorage();
2073+
// we must do a bounds-check but we must not normalize the index
2074+
if (key < 0 || key >= lenNode.execute(sequenceStorage)) {
2075+
throw raise(IndexError, NormalizeIndexNode.TUPLE_OUT_OF_BOUNDS);
2076+
}
2077+
return getItemNode.execute(sequenceStorage, key);
2078+
}
2079+
2080+
@Fallback
2081+
Object doPTuple(Object tuple, @SuppressWarnings("unused") Object key) {
2082+
// TODO(fa) To be absolutely correct, we need to do a 'isinstance' check on the object.
2083+
throw raise(SystemError, "bad argument to internal function, was '%s' (type '%p')", tuple, tuple);
2084+
}
2085+
}
2086+
20622087
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,6 @@ def PyTuple_New(size):
812812
return (None,) * size
813813

814814

815-
@may_raise
816-
def PyTuple_GetItem(t, n):
817-
if not isinstance(t, tuple):
818-
__bad_internal_call(None, None, t)
819-
return t[n]
820-
821-
822815
@may_raise(-1)
823816
def PyTuple_Size(t):
824817
if not isinstance(t, tuple):

0 commit comments

Comments
 (0)