Skip to content

Commit 9b13a27

Browse files
committed
add tp_as_sequence and nb_multiply
1 parent 9c3d0af commit 9b13a27

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/NativeMemberNames.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public final class NativeMemberNames {
6060
public static final String TP_RICHCOMPARE = "tp_richcompare";
6161
public static final String TP_SUBCLASSES = "tp_subclasses";
6262
public static final String TP_AS_BUFFER = "tp_as_buffer";
63+
public static final String TP_AS_SEQUENCE = "tp_as_sequence";
6364
public static final String TP_GETATTR = "tp_getattr";
6465
public static final String TP_SETATTR = "tp_setattr";
6566
public static final String TP_GETATTRO = "tp_getattro";
@@ -85,12 +86,14 @@ public final class NativeMemberNames {
8586
public static final String NB_INDEX = "nb_index";
8687
public static final String NB_POW = "nb_power";
8788
public static final String NB_TRUE_DIVIDE = "nb_true_divide";
89+
public static final String NB_MULTIPLY = "nb_multiply";
8890
public static final String OB_FVAL = "ob_fval";
8991
public static final String START = "start";
9092
public static final String STOP = "stop";
9193
public static final String STEP = "step";
9294
public static final String IM_FUNC = "im_func";
9395
public static final String IM_SELF = "im_self";
96+
public static final String SQ_REPEAT = "sq_repeat";
9497

9598
@CompilationFinal(dimensions = 1) public static final String[] values;
9699
static {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PyNumberMethodsWrapperMR.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_ADD;
4242
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_INDEX;
43+
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_MULTIPLY;
4344
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_POW;
4445
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_TRUE_DIVIDE;
4546

@@ -78,6 +79,8 @@ private static String toAttributeName(String numberMethodsMember) {
7879
return SpecialMethodNames.__POW__;
7980
case NB_TRUE_DIVIDE:
8081
return SpecialMethodNames.__TRUEDIV__;
82+
case NB_MULTIPLY:
83+
return SpecialMethodNames.__MUL__;
8184
default:
8285
// TODO extend list
8386
throw UnknownIdentifierException.raise(numberMethodsMember);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates.
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 data
8+
* (collectively the "Software"), free of charge and under any and all copyright
9+
* rights in the Software, and any and all patent rights owned or freely
10+
* licensable by each licensor hereunder covering either (i) the unmodified
11+
* Software as contributed to or provided by such licensor, or (ii) the Larger
12+
* Works (as defined below), to deal in both
13+
*
14+
* (a) the Software, and
15+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
* one is included with the Software (each a "Larger Work" to which the
17+
* Software is contributed by such licensors),
18+
*
19+
* without restriction, including without limitation the rights to copy, create
20+
* derivative works of, display, perform, and distribute the Software and make,
21+
* use, sell, offer for sale, import, export, have made, and have sold the
22+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
* either these or other terms.
24+
*
25+
* This license is subject to the following condition:
26+
*
27+
* The above copyright notice and either this complete permission notice or at a
28+
* minimum a reference to the UPL must be included in all copies or substantial
29+
* portions of the Software.
30+
*
31+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
* SOFTWARE.
38+
*/
39+
package com.oracle.graal.python.builtins.objects.cext;
40+
41+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
42+
import com.oracle.truffle.api.interop.ForeignAccess;
43+
import com.oracle.truffle.api.interop.TruffleObject;
44+
45+
/**
46+
* Wraps a PythonObject to provide a native view with a shape like {@code PySequenceMethods}.
47+
*/
48+
public class PySequenceMethodsWrapper extends PythonNativeWrapper {
49+
50+
private final Object delegate;
51+
52+
public PySequenceMethodsWrapper(Object delegate) {
53+
this.delegate = delegate;
54+
}
55+
56+
@Override
57+
public Object getDelegate() {
58+
return delegate;
59+
}
60+
61+
static boolean isInstance(TruffleObject o) {
62+
return o instanceof PySequenceMethodsWrapper;
63+
}
64+
65+
@Override
66+
public ForeignAccess getForeignAccess() {
67+
return PySequenceMethodsWrapperMRForeign.ACCESS;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates.
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 data
8+
* (collectively the "Software"), free of charge and under any and all copyright
9+
* rights in the Software, and any and all patent rights owned or freely
10+
* licensable by each licensor hereunder covering either (i) the unmodified
11+
* Software as contributed to or provided by such licensor, or (ii) the Larger
12+
* Works (as defined below), to deal in both
13+
*
14+
* (a) the Software, and
15+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
* one is included with the Software (each a "Larger Work" to which the
17+
* Software is contributed by such licensors),
18+
*
19+
* without restriction, including without limitation the rights to copy, create
20+
* derivative works of, display, perform, and distribute the Software and make,
21+
* use, sell, offer for sale, import, export, have made, and have sold the
22+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
* either these or other terms.
24+
*
25+
* This license is subject to the following condition:
26+
*
27+
* The above copyright notice and either this complete permission notice or at a
28+
* minimum a reference to the UPL must be included in all copies or substantial
29+
* portions of the Software.
30+
*
31+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
* SOFTWARE.
38+
*/
39+
package com.oracle.graal.python.builtins.objects.cext;
40+
41+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToSulongNode;
42+
import com.oracle.graal.python.nodes.SpecialMethodNames;
43+
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
44+
import com.oracle.truffle.api.CompilerDirectives;
45+
import com.oracle.truffle.api.interop.MessageResolution;
46+
import com.oracle.truffle.api.interop.Resolve;
47+
import com.oracle.truffle.api.interop.UnknownIdentifierException;
48+
import com.oracle.truffle.api.nodes.Node;
49+
50+
@MessageResolution(receiverType = PySequenceMethodsWrapper.class)
51+
public class PySequenceMethodsWrapperMR {
52+
53+
@Resolve(message = "READ")
54+
abstract static class ReadNode extends Node {
55+
@Child private LookupAttributeInMRONode getAttributeNode;
56+
@Child private ToSulongNode toSulongNode;
57+
58+
public Object access(PySequenceMethodsWrapper object, String key) {
59+
// translate key to attribute name
60+
String attributeName = toAttributeName(key);
61+
62+
Object execute = getReadArrayItemNode().execute(object.getDelegate(), attributeName);
63+
return getToSulongNode().execute(execute);
64+
}
65+
66+
private static String toAttributeName(String numberMethodsMember) {
67+
switch (numberMethodsMember) {
68+
case NativeMemberNames.SQ_REPEAT:
69+
return SpecialMethodNames.__MUL__;
70+
default:
71+
// TODO extend list
72+
throw UnknownIdentifierException.raise(numberMethodsMember);
73+
}
74+
}
75+
76+
private LookupAttributeInMRONode getReadArrayItemNode() {
77+
if (getAttributeNode == null) {
78+
CompilerDirectives.transferToInterpreterAndInvalidate();
79+
getAttributeNode = insert(LookupAttributeInMRONode.create());
80+
}
81+
return getAttributeNode;
82+
}
83+
84+
private ToSulongNode getToSulongNode() {
85+
if (toSulongNode == null) {
86+
CompilerDirectives.transferToInterpreterAndInvalidate();
87+
toSulongNode = insert(ToSulongNode.create());
88+
}
89+
return toSulongNode;
90+
}
91+
}
92+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PythonObjectNativeWrapperMR.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ Object doTpAsBuffer(PythonClass object, @SuppressWarnings("unused") String key)
266266
return getToSulongNode().execute(PNone.NO_VALUE);
267267
}
268268

269+
@Specialization(guards = "eq(TP_AS_SEQUENCE, key)")
270+
Object doTpAsSequence(PythonClass object, @SuppressWarnings("unused") String key,
271+
@Cached("create()") LookupAttributeInMRONode getAttrNode) {
272+
if (getAttrNode.execute(object, SpecialMethodNames.__LEN__) != PNone.NO_VALUE) {
273+
return new PySequenceMethodsWrapper(object);
274+
} else {
275+
return getToSulongNode().execute(PNone.NO_VALUE);
276+
}
277+
}
278+
269279
@Specialization(guards = "eq(TP_NEW, key)")
270280
Object doTpNew(PythonClass object, @SuppressWarnings("unused") String key,
271281
@Cached("create()") LookupAttributeInMRONode getAttrNode) {

0 commit comments

Comments
 (0)