Skip to content

Commit 69887ad

Browse files
committed
[GR-13467] GetItemNode should raise TypeError not return NotImplemented value
PullRequest: graalpython/370
2 parents e374961 + d68a6a9 commit 69887ad

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_builtin.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates.
1+
# Copyright (c) 2018, 2019, Oracle and/or its affiliates.
22
# Copyright (C) 1996-2017 Python Software Foundation
33
#
44
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -37,3 +37,12 @@ def test_divmod_complex(self):
3737
self.assertRaises(TypeError, divmod, c1, c2)
3838
self.assertRaises(TypeError, divmod, 10, c2)
3939
self.assertRaises(TypeError, divmod, c1, 10)
40+
41+
def test_getitem_typeerror(self):
42+
a = object()
43+
try:
44+
a[1]
45+
except TypeError :
46+
pass
47+
else:
48+
self.assertTrue(False)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/subscript/GetItemNode.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -32,14 +32,17 @@
3232
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3333
import com.oracle.graal.python.nodes.frame.ReadNode;
3434
import com.oracle.graal.python.nodes.statement.StatementNode;
35-
import com.oracle.truffle.api.dsl.Cached;
35+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
36+
import com.oracle.truffle.api.CompilerDirectives;
3637
import com.oracle.truffle.api.dsl.Specialization;
3738
import com.oracle.truffle.api.frame.VirtualFrame;
3839
import com.oracle.truffle.api.nodes.NodeInfo;
3940

4041
@NodeInfo(shortName = __GETITEM__)
4142
public abstract class GetItemNode extends BinaryOpNode implements ReadNode {
4243

44+
@Child private LookupAndCallBinaryNode callGetitemNode;
45+
4346
public ExpressionNode getPrimary() {
4447
return getLeftNode();
4548
}
@@ -53,8 +56,16 @@ public ExpressionNode getSlice() {
5356
public abstract Object execute(Object primary, Object slice);
5457

5558
@Specialization
56-
public Object doSpecialObject(Object primary, Object index,
57-
@Cached("create(__GETITEM__)") LookupAndCallBinaryNode callGetitemNode) {
59+
public Object doSpecialObject(Object primary, Object index) {
60+
if (callGetitemNode == null) {
61+
CompilerDirectives.transferToInterpreterAndInvalidate();
62+
callGetitemNode = insert(LookupAndCallBinaryNode.create(__GETITEM__, null, () -> new LookupAndCallBinaryNode.NotImplementedHandler() {
63+
@Override
64+
public Object execute(Object arg, @SuppressWarnings("unused") Object arg2) {
65+
throw raise(TypeError, "'%p' object is not subscriptable", arg);
66+
}
67+
}));
68+
}
5869
return callGetitemNode.executeObject(primary, index);
5970
}
6071

0 commit comments

Comments
 (0)