Skip to content

Commit ea37718

Browse files
committed
[GR-36519] fix GetItemNode specialization on list subclassing
PullRequest: graalpython/2118
2 parents cbe911a + fd6a3b4 commit ea37718

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -78,3 +78,20 @@ def __init__(self, a, b=None):
7878
assert l == [20]
7979
l = MyList(10, b=30)
8080
assert l == [30]
81+
82+
83+
def test_list_custom_getitem():
84+
class MyList(list):
85+
def __init__(self, it, mapping):
86+
list.__init__(self, it)
87+
self.mapping = mapping
88+
89+
def __getitem__(self, key):
90+
idx = key
91+
if key in self.mapping:
92+
idx = self.mapping[idx]
93+
return list.__getitem__(self, idx)
94+
95+
l = MyList([1, 2, 3, 4], {"a": 1, "b": 2})
96+
assert l[1] == 2
97+
assert l["a"] == 2

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -85,6 +85,10 @@ public static GetItemNode create(ExpressionNode primary, ExpressionNode slice) {
8585
return GetItemNodeGen.create(primary, slice);
8686
}
8787

88+
protected LookupAndCallBinaryNode createLookupAndCallGetItemNode() {
89+
return LookupAndCallBinaryNode.create(SpecialMethodSlot.GetItem);
90+
}
91+
8892
@Override
8993
public StatementNode makeWriteNode(ExpressionNode rhs) {
9094
return SetItemNode.create(getPrimary(), getSlice(), rhs);
@@ -101,9 +105,10 @@ Object doBuiltinList(VirtualFrame frame, PList primary, Object index,
101105
@SuppressWarnings("unused")
102106
@Specialization(guards = {"!indexCheckNode.execute(index)", "!isPSlice(index)"}, limit = "1")
103107
public Object doListError(VirtualFrame frame, PList primary, Object index,
108+
@Cached("createLookupAndCallGetItemNode()") LookupAndCallBinaryNode lookupAndCallGetItem,
104109
@SuppressWarnings("unused") @Cached PyIndexCheckNode indexCheckNode,
105110
@Cached PRaiseNode raiseNode) {
106-
throw raiseNode.raise(TypeError, ErrorMessages.OBJ_INDEX_MUST_BE_INT_OR_SLICES, "list", index);
111+
return lookupAndCallGetItem.executeObject(frame, primary, index);
107112
}
108113

109114
@Specialization(guards = {"indexCheckNode.execute(index) || isPSlice(index)", "isBuiltinTuple.profileIsAnyBuiltinObject(primary)"}, limit = "1")

0 commit comments

Comments
 (0)