Skip to content

Commit 907ab2f

Browse files
author
Franziska Geiger
committed
[GR-17836] Add blog post example
PullRequest: graalpython/626
2 parents a4c71a2 + ab1db45 commit 907ab2f

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,21 @@
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;
55+
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
5356
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
57+
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
5458
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5559
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
5660
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
5761
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
62+
import com.oracle.graal.python.runtime.sequence.PSequence;
5863
import com.oracle.truffle.api.CompilerDirectives;
5964
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
65+
import com.oracle.truffle.api.dsl.Cached;
6066
import com.oracle.truffle.api.dsl.Fallback;
6167
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6268
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -136,6 +142,80 @@ public boolean doObject(VirtualFrame frame, Object value) {
136142
}
137143
}
138144

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

0 commit comments

Comments
 (0)