Skip to content

Commit b5e6544

Browse files
committed
intrinsified PySlice_XXX
1 parent f37bf79 commit b5e6544

File tree

5 files changed

+123
-4
lines changed

5 files changed

+123
-4
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_slice.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def compile_module(self, name):
4949
super(TestPySlice, self).compile_module(name)
5050

5151

52+
5253
def reference_get_indices(slize, length):
5354
r = slize.indices(length)
5455
slicelength = 0
@@ -170,3 +171,25 @@ def reference_adjust(length, start, stop, step):
170171
arguments=["int length", "int start", "int stop", "int step"],
171172
cmpfunc=unhandled_error_compare
172173
)
174+
175+
def reference_new_slice(args):
176+
return slice(args[0], args[1], args[2])
177+
178+
test_PySlice_New = CPyExtFunction(
179+
reference_new_slice,
180+
lambda: (
181+
(1, 2, 3,),
182+
(1, 2, "a",),
183+
(1, 2, None,),
184+
(1, None, None,),
185+
(-1, -1, -1,),
186+
("a", "b", "c"),
187+
("a", "b", None),
188+
("a", None, None),
189+
(None, None, None),
190+
),
191+
resultspec="O",
192+
argspec='OOO',
193+
arguments=["PyObject* start", "PyObject* stop", "PyObject* step"],
194+
cmpfunc=unhandled_error_compare
195+
)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
import com.oracle.graal.python.builtins.modules.cext.PythonCextAbstractBuiltins;
123123
import com.oracle.graal.python.builtins.modules.cext.PythonCextSetBuiltins;
124124
import com.oracle.graal.python.builtins.modules.cext.PythonCextUnicodeBuiltins;
125+
import com.oracle.graal.python.builtins.modules.cext.PythonCextSliceBuiltins;
125126
import com.oracle.graal.python.builtins.modules.cext.PythonCextSysBuiltins;
126127
import com.oracle.graal.python.builtins.modules.csv.CSVDialectBuiltins;
127128
import com.oracle.graal.python.builtins.modules.csv.CSVModuleBuiltins;
@@ -493,6 +494,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
493494
new PythonCextLongBuiltins(),
494495
new PythonCextMemoryViewBuiltins(),
495496
new PythonCextSetBuiltins(),
497+
new PythonCextSliceBuiltins(),
496498
new PythonCextSysBuiltins(),
497499
new PythonCextUnicodeBuiltins(),
498500
new WeakRefModuleBuiltins(),

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextBuiltins.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import static com.oracle.graal.python.builtins.modules.cext.PythonCextLongBuiltins.PYTHON_CEXT_LONG;
5555
import static com.oracle.graal.python.builtins.modules.cext.PythonCextMemoryViewBuiltins.PYTHON_CEXT_MEMORYVIEW;
5656
import static com.oracle.graal.python.builtins.modules.cext.PythonCextSetBuiltins.PYTHON_CEXT_SET;
57+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextSliceBuiltins.PYTHON_CEXT_SLICE;
5758
import static com.oracle.graal.python.builtins.modules.cext.PythonCextSysBuiltins.PYTHON_CEXT_SYS;
5859
import static com.oracle.graal.python.builtins.modules.cext.PythonCextUnicodeBuiltins.PYTHON_CEXT_UNICODE;
5960
>>>>>>> intrinsified PySys_XXX
@@ -384,6 +385,7 @@ public void postInitialize(Python3Core core) {
384385
addModuleDict(cext, PYTHON_CEXT_LIST, core);
385386
addModuleDict(cext, PYTHON_CEXT_MEMORYVIEW, core);
386387
addModuleDict(cext, PYTHON_CEXT_SET, core);
388+
addModuleDict(cext, PYTHON_CEXT_SLICE, core);
387389
addModuleDict(cext, PYTHON_CEXT_SYS, core);
388390
addModuleDict(cext, PYTHON_CEXT_UNICODE, core);
389391
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.modules.cext;
42+
43+
import com.oracle.graal.python.builtins.Builtin;
44+
import java.util.List;
45+
import com.oracle.graal.python.builtins.CoreFunctions;
46+
import com.oracle.graal.python.builtins.Python3Core;
47+
import com.oracle.graal.python.builtins.PythonBuiltins;
48+
import com.oracle.graal.python.builtins.objects.ints.PInt;
49+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
50+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
51+
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
52+
import com.oracle.graal.python.nodes.truffle.PythonTypes;
53+
import com.oracle.truffle.api.dsl.Cached;
54+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
55+
import com.oracle.truffle.api.dsl.NodeFactory;
56+
import com.oracle.truffle.api.dsl.Specialization;
57+
import com.oracle.truffle.api.dsl.TypeSystemReference;
58+
import com.oracle.truffle.api.frame.VirtualFrame;
59+
import com.oracle.truffle.api.profiles.BranchProfile;
60+
61+
@CoreFunctions(extendsModule = PythonCextBuiltins.PYTHON_CEXT)
62+
@GenerateNodeFactory
63+
public class PythonCextSliceBuiltins extends PythonBuiltins {
64+
65+
@Override
66+
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
67+
return PythonCextSliceBuiltinsFactory.getFactories();
68+
}
69+
70+
@Override
71+
public void initialize(Python3Core core) {
72+
super.initialize(core);
73+
}
74+
75+
@Builtin(name = "PySlice_New", minNumOfPositionalArgs = 1)
76+
@TypeSystemReference(PythonTypes.class)
77+
@GenerateNodeFactory
78+
public abstract static class PySliceNewNode extends PythonTernaryBuiltinNode {
79+
@Specialization
80+
public Object slice(VirtualFrame frame, long start, long stop, Object step,
81+
@Cached SliceLiteralNode sliceNode,
82+
@Cached BranchProfile isIntRangeProfile) {
83+
if (PInt.isIntRange(start) && PInt.isIntRange(stop)) {
84+
isIntRangeProfile.enter();
85+
return sliceNode.execute(frame, (int) start, (int) stop, step);
86+
}
87+
return sliceNode.execute(frame, start, stop, step);
88+
}
89+
90+
@Specialization(guards = {"!isInteger(start) || !isInteger(stop)"})
91+
public Object slice(VirtualFrame frame, Object start, Object stop, Object step,
92+
@Cached SliceLiteralNode sliceNode) {
93+
return sliceNode.execute(frame, start, stop, step);
94+
}
95+
}
96+
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,6 @@ def PyImport_GetModuleDict():
486486
def PyRun_String(source, typ, globals, locals):
487487
return exec(compile(source, typ, typ), globals, locals)
488488

489-
@may_raise
490-
def PySlice_New(start, stop, step):
491-
return slice(start, stop, step)
492-
493489
@may_raise
494490
def PyEval_GetBuiltins():
495491
global __builtins_module_dict

0 commit comments

Comments
 (0)