Skip to content

Commit 5f2dd33

Browse files
committed
Ensure that 'toString' does not need a context.
1 parent 2df65c1 commit 5f2dd33

File tree

9 files changed

+95
-19
lines changed

9 files changed

+95
-19
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public enum PythonBuiltinClassType implements LazyPythonClass {
203203
this(name, null);
204204
}
205205

206+
@Override
206207
public String getName() {
207208
return name;
208209
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public interface PythonNativeClass extends PythonAbstractClass {
5353

5454
TruffleObject getPtr();
5555

56+
@Override
57+
default String getName() {
58+
return String.format("PythonNativeClass(%s)", getPtr());
59+
}
60+
5661
static boolean isInstance(Object object) {
5762
return object instanceof PythonAbstractNativeObject;
5863
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PBuiltinFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public String toString() {
118118
if (enclosingType == null) {
119119
return String.format("PBuiltinFunction %s at 0x%x", name, hashCode());
120120
} else {
121-
return String.format("PBuiltinFunction %s.%s at 0x%x", GetNameNode.doSlowPath(enclosingType), name, hashCode());
121+
return String.format("PBuiltinFunction %s.%s at 0x%x", enclosingType.getName(), name, hashCode());
122122
}
123123
}
124124

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/LazyString.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4949
import com.oracle.truffle.api.profiles.ConditionProfile;
5050

51-
public class LazyString implements CharSequence {
51+
public class LazyString implements PCharSequence {
5252

5353
protected static final int MinLazyStringLength;
5454
protected static final boolean UseLazyStrings;
@@ -157,22 +157,26 @@ public int length() {
157157

158158
@Override
159159
public String toString() {
160-
if (!isFlat()) {
161-
flatten();
160+
if (!isMaterialized()) {
161+
return materialize();
162162
}
163163
return (String) left;
164164
}
165165

166-
private boolean isFlat() {
166+
@Override
167+
public boolean isMaterialized() {
167168
return right == null;
168169
}
169170

171+
@Override
170172
@TruffleBoundary
171-
private void flatten() {
173+
public String materialize() {
172174
char[] dst = new char[len];
173-
flatten(this, 0, len, dst, 0);
174-
left = new String(dst);
175+
LazyString.flatten(this, 0, len, dst, 0);
176+
String flattened = new String(dst);
177+
left = flattened;
175178
right = null;
179+
return flattened;
176180
}
177181

178182
private static void flatten(CharSequence src, int srcBegin, int srcEnd, char[] dst, int dstBegin) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/NativeCharSequence.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,13 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.str;
4242

43+
import java.util.Objects;
44+
4345
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallCapiFunction;
4446
import com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols;
45-
import org.graalvm.nativeimage.ImageInfo;
46-
47-
import com.oracle.graal.python.nodes.PGuards;
48-
import com.oracle.graal.python.runtime.PythonOptions;
4947
import com.oracle.truffle.api.CompilerAsserts;
50-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
51-
import com.oracle.truffle.api.profiles.ConditionProfile;
5248

53-
public class NativeCharSequence implements CharSequence {
49+
public class NativeCharSequence implements PCharSequence {
5450

5551
private final Object ptr;
5652
private String materialized;
@@ -74,15 +70,25 @@ public CharSequence subSequence(int start, int end) {
7470
return materialize().subSequence(start, end);
7571
}
7672

77-
private String materialize() {
78-
if(materialized == null) {
73+
@Override
74+
public boolean isMaterialized() {
75+
return materialized != null;
76+
}
77+
78+
@Override
79+
public String materialize() {
80+
if(!isMaterialized()) {
7981
materialized = (String) PCallCapiFunction.getUncached().call(NativeCAPISymbols.FUN_PY_TRUFFLE_CSTR_TO_STRING, ptr);
8082
}
8183
return materialized;
8284
}
8385

8486
@Override
8587
public String toString() {
86-
return materialize();
88+
CompilerAsserts.neverPartOfCompilation();
89+
if(isMaterialized()) {
90+
return materialized;
91+
}
92+
return Objects.toString(ptr);
8793
}
8894
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2019, 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.objects.str;
42+
43+
public interface PCharSequence extends CharSequence {
44+
45+
boolean isMaterialized();
46+
String materialize();
47+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/PString.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public PString(LazyPythonClass clazz, CharSequence value) {
4444
}
4545

4646
public String getValue() {
47+
if(value instanceof PCharSequence) {
48+
PCharSequence s = (PCharSequence)value;
49+
if(!s.isMaterialized()) {
50+
return s.materialize();
51+
}
52+
return s.toString();
53+
}
4754
return value.toString();
4855
}
4956

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/LazyPythonClass.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@
4343
import com.oracle.truffle.api.interop.TruffleObject;
4444

4545
public interface LazyPythonClass extends TruffleObject {
46+
47+
/**
48+
* Returns the name of the class for debugging purposes. This method must assume that no context is available.
49+
*/
50+
String getName();
4651
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonManagedClass.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public MroSequenceStorage getMethodResolutionOrder() {
130130
return methodResolutionOrder;
131131
}
132132

133-
String getName() {
133+
@Override
134+
public String getName() {
134135
return className;
135136
}
136137

0 commit comments

Comments
 (0)