Skip to content

Commit 221e012

Browse files
committed
[GR-12020] Work with PythonBuiltinClassType directly where possible.
PullRequest: graalpython/217
2 parents 68aa438 + 03b5f37 commit 221e012

File tree

162 files changed

+2499
-1846
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+2499
-1846
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ declare_type(PyByteArray_Type, bytearray, PyByteArrayObject);
131131
declare_type(PyCFunction_Type, builtin_function_or_method, PyCFunctionObject);
132132
declare_type(PyMethodDescr_Type, method_descriptor, PyMethodDescrObject);
133133
declare_type(PyGetSetDescr_Type, getset_descriptor, PyGetSetDescrObject);
134-
declare_type(PyWrapperDescr_Type, wrapper_descriptor, PyWrapperDescrObject);
135-
declare_type(PyMemberDescr_Type, member_descriptor, PyMemberDescrObject);
134+
declare_type(PyWrapperDescr_Type, method_descriptor, PyWrapperDescrObject); // LS: previously wrapper_descriptor
135+
declare_type(PyMemberDescr_Type, property, PyMemberDescrObject); // LS: previously member_descriptor
136136
declare_type(_PyExc_BaseException, BaseException, PyBaseExceptionObject);
137137
declare_type(PyBuffer_Type, buffer, PyBufferDecorator);
138138
declare_type(PyFunction_Type, function, PyFunctionObject);

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/datatype/PRangeTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
import com.oracle.graal.python.builtins.objects.range.PRange;
3636
import com.oracle.graal.python.nodes.control.GetIteratorNode;
3737
import com.oracle.graal.python.nodes.control.GetNextNode;
38+
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
3839
import com.oracle.graal.python.runtime.exception.PException;
3940
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4041
import com.oracle.graal.python.test.PythonTests;
4142
import com.oracle.truffle.api.frame.VirtualFrame;
4243
import com.oracle.truffle.api.nodes.Node;
4344
import com.oracle.truffle.api.nodes.RootNode;
4445
import com.oracle.truffle.api.nodes.UnexpectedResultException;
45-
import com.oracle.truffle.api.profiles.ConditionProfile;
4646

4747
public class PRangeTests {
4848
@Before
@@ -75,14 +75,14 @@ public void loopWithOnlyStop() throws UnexpectedResultException {
7575
Object iter = getIter.executeWith(range);
7676
GetNextNode next = GetNextNode.create();
7777
testRoot.doInsert(next);
78-
ConditionProfile errorProfile = ConditionProfile.createBinaryProfile();
78+
IsBuiltinClassProfile errorProfile = IsBuiltinClassProfile.create();
7979

8080
while (true) {
8181
try {
8282
int item = next.executeInt(iter);
8383
assertEquals(index, item);
8484
} catch (PException e) {
85-
e.expectStopIteration(PythonLanguage.getCore(), errorProfile);
85+
e.expectStopIteration(errorProfile);
8686
break;
8787
}
8888
index++;
@@ -99,14 +99,14 @@ public void loopWithStep() throws UnexpectedResultException {
9999
Object iter = getIter.executeWith(range);
100100
GetNextNode next = GetNextNode.create();
101101
testRoot.doInsert(next);
102-
ConditionProfile errorProfile = ConditionProfile.createBinaryProfile();
102+
IsBuiltinClassProfile errorProfile = IsBuiltinClassProfile.create();
103103

104104
while (true) {
105105
try {
106106
int item = next.executeInt(iter);
107107
assertEquals(index, item);
108108
} catch (PException e) {
109-
e.expectStopIteration(PythonLanguage.getCore(), errorProfile);
109+
e.expectStopIteration(errorProfile);
110110
break;
111111
}
112112
index += 2;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright (c) 2018, 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+
40+
41+
def test_builtins():
42+
import array
43+
assert array.array.__module__ == "array" # builtin class
44+
assert int.__module__ == "builtins" # builtin class
45+
import sys
46+
assert sys.getrecursionlimit.__module__ == "sys" # builtin module method
47+
48+
def test_imported():
49+
import code
50+
assert code.InteractiveInterpreter.__module__ == "code" # class
51+
assert code.InteractiveInterpreter.runsource.__module__ == "code" # function
52+
assert code.InteractiveInterpreter().runsource.__module__ == "code" # method
53+
54+
class TestClass():
55+
def foo(self):
56+
pass
57+
58+
def test_user_class():
59+
assert TestClass.__module__ == __name__
60+
assert TestClass().__module__ == __name__
61+
assert TestClass.foo.__module__ == __name__
62+
assert TestClass().foo.__module__ == __name__
63+
# test redefine:
64+
TestClass.__module__ = "bar"
65+
assert TestClass.__module__ == "bar"
66+
t = TestClass()
67+
t.__module__ = "baz"
68+
assert t.__module__ == "baz"

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
import java.text.MessageFormat;
3131
import java.util.ArrayList;
3232
import java.util.concurrent.ConcurrentHashMap;
33+
import java.util.function.Supplier;
3334

3435
import org.graalvm.options.OptionDescriptors;
3536

3637
import com.oracle.graal.python.builtins.Python3Core;
3738
import com.oracle.graal.python.builtins.objects.PNone;
3839
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
40+
import com.oracle.graal.python.builtins.objects.code.PCode;
3941
import com.oracle.graal.python.builtins.objects.function.PArguments;
4042
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
4143
import com.oracle.graal.python.builtins.objects.function.PKeyword;
@@ -78,6 +80,9 @@
7880
import com.oracle.truffle.api.nodes.ExecutableNode;
7981
import com.oracle.truffle.api.nodes.Node;
8082
import com.oracle.truffle.api.nodes.RootNode;
83+
import com.oracle.truffle.api.object.Layout;
84+
import com.oracle.truffle.api.object.ObjectType;
85+
import com.oracle.truffle.api.object.Shape;
8186
import com.oracle.truffle.api.source.Source;
8287
import com.oracle.truffle.api.source.Source.SourceBuilder;
8388

@@ -101,6 +106,9 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
101106
private final NodeFactory nodeFactory;
102107
public final ConcurrentHashMap<Class<? extends PythonBuiltinBaseNode>, RootCallTarget> builtinCallTargetCache = new ConcurrentHashMap<>();
103108

109+
private static final Layout objectLayout = Layout.newLayout().build();
110+
private static final Shape freshShape = objectLayout.createShape(new ObjectType());
111+
104112
public PythonLanguage() {
105113
this.nodeFactory = NodeFactory.create(this);
106114
}
@@ -384,12 +392,34 @@ public static Source newSource(PythonContext ctxt, String src, String name) {
384392
}
385393
}
386394

387-
public static Source newSource(PythonContext ctxt, TruffleFile src, String name) throws IOException {
388-
return newSource(ctxt, Source.newBuilder(ID, src), name);
395+
private final ConcurrentHashMap<Object, Source> cachedSources = new ConcurrentHashMap<>();
396+
397+
public Source newSource(PythonContext ctxt, TruffleFile src, String name) throws IOException {
398+
try {
399+
return cachedSources.computeIfAbsent(src, t -> {
400+
try {
401+
return newSource(ctxt, Source.newBuilder(ID, src), name);
402+
} catch (IOException e) {
403+
throw new RuntimeException(e);
404+
}
405+
});
406+
} catch (RuntimeException e) {
407+
throw (IOException) e.getCause();
408+
}
389409
}
390410

391-
public static Source newSource(PythonContext ctxt, URL url, String name) throws IOException {
392-
return newSource(ctxt, Source.newBuilder(ID, url), name);
411+
public Source newSource(PythonContext ctxt, URL url, String name) throws IOException {
412+
try {
413+
return cachedSources.computeIfAbsent(url, t -> {
414+
try {
415+
return newSource(ctxt, Source.newBuilder(ID, url), name);
416+
} catch (IOException e) {
417+
throw new RuntimeException(e);
418+
}
419+
});
420+
} catch (RuntimeException e) {
421+
throw (IOException) e.getCause();
422+
}
393423
}
394424

395425
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder, String name) throws IOException {
@@ -411,4 +441,14 @@ protected void initializeMultipleContexts() {
411441
super.initializeMultipleContexts();
412442
singleContextAssumption.invalidate();
413443
}
444+
445+
private final ConcurrentHashMap<String, PCode> cachedCode = new ConcurrentHashMap<>();
446+
447+
public Object cacheCode(String filename, Supplier<PCode> createCode) {
448+
return cachedCode.computeIfAbsent(filename, f -> createCode.get());
449+
}
450+
451+
public static Shape freshShape() {
452+
return freshShape;
453+
}
414454
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@
4343
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4444

4545
public interface BoundBuiltinCallable<T> {
46-
T boundToObject(Object binding, PythonObjectFactory factory);
46+
T boundToObject(PythonBuiltinClassType binding, PythonObjectFactory factory);
4747
}

0 commit comments

Comments
 (0)