Skip to content

Commit 48c6b53

Browse files
author
Franziska Geiger
committed
Add operator.eq and operator.getitem method with test case. Change truffle.Loop.execute to Loop.executeLoop
1 parent 85d3293 commit 48c6b53

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
import operator
40+
import unittest
41+
42+
class OperatorTest(unittest.TestCase):
43+
44+
def test_eq(self):
45+
class C(object):
46+
def __eq__(self, other):
47+
raise SyntaxError
48+
self.assertRaises(TypeError, operator.eq)
49+
self.assertRaises(SyntaxError, operator.eq, C(), C())
50+
self.assertFalse(operator.eq(1, 0))
51+
self.assertFalse(operator.eq(1, 0.0))
52+
self.assertTrue(operator.eq(1, 1))
53+
self.assertTrue(operator.eq(1, 1.0))
54+
self.assertFalse(operator.eq(1, 2))
55+
self.assertFalse(operator.eq(1, 2.0))
56+
57+
58+
def test_getitem(self):
59+
a = range(10)
60+
self.assertRaises(TypeError, operator.getitem)
61+
self.assertRaises(TypeError, operator.getitem, a, None)
62+
self.assertEqual(operator.getitem(a, 2), 2)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/OperatorModuleBuiltins.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,20 @@
4848
import com.oracle.graal.python.builtins.CoreFunctions;
4949
import com.oracle.graal.python.builtins.PythonBuiltins;
5050
import com.oracle.graal.python.builtins.objects.PNone;
51+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
52+
import com.oracle.graal.python.builtins.objects.dict.PDict;
5153
import com.oracle.graal.python.builtins.objects.ints.PInt;
5254
import com.oracle.graal.python.nodes.SpecialMethodNames;
5355
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
56+
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
5457
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5558
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
5659
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
5760
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
61+
import com.oracle.graal.python.runtime.sequence.PSequence;
5862
import com.oracle.truffle.api.CompilerDirectives;
5963
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
64+
import com.oracle.truffle.api.dsl.Cached;
6065
import com.oracle.truffle.api.dsl.Fallback;
6166
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6267
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -136,6 +141,79 @@ public boolean doObject(VirtualFrame frame, Object value) {
136141
}
137142
}
138143

144+
@Builtin(name = "eq", minNumOfPositionalArgs = 2)
145+
@TypeSystemReference(PythonArithmeticTypes.class)
146+
@GenerateNodeFactory
147+
public abstract static class EqualsNode extends PythonBinaryBuiltinNode {
148+
149+
@Specialization
150+
public boolean doBoolean(boolean value1, boolean value2) {
151+
return value1 == value2;
152+
}
153+
154+
@Specialization
155+
public boolean doNone(@SuppressWarnings("unused") PNone value1, @SuppressWarnings("unused") PNone value2) {
156+
return true;
157+
}
158+
159+
@Specialization
160+
public boolean doInt(long value1, long value2) {
161+
return value1 == value2;
162+
}
163+
164+
@Specialization
165+
@TruffleBoundary
166+
public boolean doPInt(PInt value1, PInt value2) {
167+
return value1.getValue().equals(value2.getValue());
168+
}
169+
170+
@Specialization
171+
public boolean doDouble(double value1, double value2) {
172+
return value1 == value2;
173+
}
174+
175+
@Specialization
176+
public boolean doString(String value1, String value2) {
177+
return value1.equals(value2);
178+
}
179+
180+
private @Child BinaryComparisonNode equalsNode;
181+
182+
@Fallback
183+
public boolean doObject(VirtualFrame frame, Object value1, Object value2) {
184+
if (value1 == value2) {
185+
return true;
186+
}
187+
if (equalsNode == null) {
188+
CompilerDirectives.transferToInterpreterAndInvalidate();
189+
equalsNode = insert((BinaryComparisonNode.create(SpecialMethodNames.__EQ__, SpecialMethodNames.__EQ__, "==")));
190+
}
191+
return equalsNode.executeBool(frame, value1, value2);
192+
}
193+
}
194+
195+
@Builtin(name = "getitem", minNumOfPositionalArgs = 2)
196+
@TypeSystemReference(PythonArithmeticTypes.class)
197+
@GenerateNodeFactory
198+
public abstract static class GetItemNode extends PythonBinaryBuiltinNode {
199+
200+
@Specialization
201+
public Object doDict(PDict dict, Object item) {
202+
return dict.getItem(item);
203+
}
204+
205+
@Specialization
206+
public Object doSequence(PSequence value, Object index,
207+
@Cached("create()") SequenceStorageNodes.GetItemNode getItemNode) {
208+
return getItemNode.execute(value.getSequenceStorage(), index);
209+
}
210+
211+
@Fallback
212+
public Object doObject(@SuppressWarnings("unused") Object value, @SuppressWarnings("unused") Object index) {
213+
return PNone.NONE;
214+
}
215+
}
216+
139217
// _compare_digest
140218
@Builtin(name = "_compare_digest", minNumOfPositionalArgs = 2)
141219
@TypeSystemReference(PythonArithmeticTypes.class)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/ForNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public void executeVoid(VirtualFrame frame) {
185185
}
186186
frame.setObject(iteratorSlot, iterator.execute(frame));
187187
try {
188-
loopNode.execute(frame);
188+
loopNode.executeLoop(frame);
189189
} finally {
190190
frame.setObject(iteratorSlot, null);
191191
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/WhileNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ public CastToBooleanNode getCondition() {
8181

8282
@Override
8383
public void executeVoid(VirtualFrame frame) {
84-
loopNode.execute(frame);
84+
loopNode.executeLoop(frame);
8585
}
8686
}

0 commit comments

Comments
 (0)