Skip to content

Commit 3b14977

Browse files
committed
[GR-27031] Extract cases of TruffleBoundary-ied String.format() to a PythonUtils method.
PullRequest: graalpython/1356
2 parents 690551d + 6a7abe8 commit 3b14977

File tree

11 files changed

+33
-57
lines changed

11 files changed

+33
-57
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,19 +1702,14 @@ Object repr(VirtualFrame frame, Object obj,
17021702
} catch (PException e) {
17031703
// as in PyObject_Repr
17041704
Object qualName = readQualNameNode.executeObject(frame, klass);
1705-
return strFormat("<%s object at 0x%x>", qualName, PythonAbstractObject.systemHashCode(obj));
1705+
return PythonUtils.format("<%s object at 0x%x>", qualName, PythonAbstractObject.systemHashCode(obj));
17061706
}
17071707
result = callRepr.executeObject(frame, callable, obj);
17081708
if (isString.profile(result instanceof String) || isPString.profile(result instanceof PString)) {
17091709
return result;
17101710
}
17111711
throw raise(TypeError, ErrorMessages.RETURNED_NON_STRING, "__repr__", obj);
17121712
}
1713-
1714-
@TruffleBoundary
1715-
private static String strFormat(String fmt, Object... objects) {
1716-
return String.format(fmt, objects);
1717-
}
17181713
}
17191714

17201715
// format(object, [format_spec])

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/array/ArrayBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage;
7171
import com.oracle.graal.python.runtime.sequence.storage.LongSequenceStorage;
7272
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
73+
import com.oracle.graal.python.util.PythonUtils;
7374
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7475
import com.oracle.truffle.api.dsl.Cached;
7576
import com.oracle.truffle.api.dsl.Fallback;
@@ -201,12 +202,11 @@ private static String dtoString(double[] values) {
201202
return Arrays.toString(values);
202203
}
203204

204-
@TruffleBoundary
205205
private static String format(String typeName, String typeCode, String array, int storagelen) {
206206
if (storagelen == 0) {
207-
return String.format("%s('%s')", typeName, typeCode);
207+
return PythonUtils.format("%s('%s')", typeName, typeCode);
208208
} else {
209-
return String.format("%s('%s', %s)", typeName, typeCode, array);
209+
return PythonUtils.format("%s('%s', %s)", typeName, typeCode, array);
210210
}
211211
}
212212

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -799,25 +799,20 @@ public void releaseHPyHandleForObject(long handle) {
799799
if (LOGGER.isLoggable(Level.WARNING)) {
800800
LOGGER.warning(() -> String.format("tried to release invalid handle %d", handle));
801801
}
802-
assert false : format("tried to release invalid handle %d", handle);
802+
assert false : PythonUtils.format("tried to release invalid handle %d", handle);
803803
}
804804
}
805805

806806
public void releaseHPyHandleForObject(int handle) {
807807
assert handle != 0 : "NULL handle cannot be released";
808-
assert hpyHandleTable[handle] != null : format("releasing handle that has already been released: %d", handle);
808+
assert hpyHandleTable[handle] != null : PythonUtils.format("releasing handle that has already been released: %d", handle);
809809
if (LOGGER.isLoggable(Level.FINER)) {
810810
LOGGER.finer(() -> "releasing HPy handle " + handle);
811811
}
812812
hpyHandleTable[handle] = null;
813813
freeStack.push(handle);
814814
}
815815

816-
@TruffleBoundary
817-
private static String format(String fmt, Object... args) {
818-
return String.format(fmt, args);
819-
}
820-
821816
// nb. keep in sync with 'meth.h'
822817
private static final int HPy_METH = 0x100000;
823818

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
4646
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
4747
import com.oracle.graal.python.runtime.exception.PythonErrorType;
48+
import com.oracle.graal.python.util.PythonUtils;
4849
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4950
import com.oracle.truffle.api.dsl.Cached;
5051
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -66,16 +67,14 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
6667
@GenerateNodeFactory
6768
public abstract static class ReprNode extends PythonUnaryBuiltinNode {
6869
@Specialization(guards = "self.getEnclosingType() == null")
69-
@TruffleBoundary
7070
Object reprModuleFunction(PBuiltinFunction self) {
7171
// (tfel): these really shouldn't be accessible, I think
72-
return String.format("<built-in function %s>", self.getName());
72+
return PythonUtils.format("<built-in function %s>", self.getName());
7373
}
7474

7575
@Specialization(guards = "self.getEnclosingType() != null")
76-
@TruffleBoundary
7776
Object reprClassFunction(PBuiltinFunction self) {
78-
return String.format("<method '%s' of '%s' objects>", self.getName(), GetNameNode.doSlowPath(self.getEnclosingType()));
77+
return PythonUtils.format("<method '%s' of '%s' objects>", self.getName(), GetNameNode.doSlowPath(self.getEnclosingType()));
7978
}
8079
}
8180

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/getsetdescriptor/GetSetDescriptorTypeBuiltins.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
7474
import static com.oracle.graal.python.runtime.exception.PythonErrorType.AttributeError;
7575
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
76+
import com.oracle.graal.python.util.PythonUtils;
7677
import com.oracle.truffle.api.CompilerDirectives;
77-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7878
import com.oracle.truffle.api.dsl.Cached;
7979
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
8080
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -96,15 +96,13 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9696
@GenerateNodeFactory
9797
abstract static class GetSetReprNode extends PythonUnaryBuiltinNode {
9898
@Specialization
99-
@TruffleBoundary
10099
Object repr(GetSetDescriptor descr) {
101-
return String.format("<attribute '%s' of '%s' objects>", descr.getName(), GetNameNode.doSlowPath(descr.getType()));
100+
return PythonUtils.format("<attribute '%s' of '%s' objects>", descr.getName(), GetNameNode.doSlowPath(descr.getType()));
102101
}
103102

104103
@Specialization
105-
@TruffleBoundary
106104
Object repr(HiddenKeyDescriptor descr) {
107-
return String.format("<attribute '%s' of '%s' objects>", descr.getKey(), GetNameNode.doSlowPath(descr.getType()));
105+
return PythonUtils.format("<attribute '%s' of '%s' objects>", descr.getKey(), GetNameNode.doSlowPath(descr.getType()));
108106
}
109107
}
110108

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/BuiltinMethodBuiltins.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
6060
import com.oracle.graal.python.runtime.exception.PException;
6161
import com.oracle.graal.python.runtime.exception.PythonErrorType;
62+
import com.oracle.graal.python.util.PythonUtils;
6263
import com.oracle.truffle.api.CompilerDirectives;
6364
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6465
import com.oracle.truffle.api.dsl.Cached;
@@ -94,13 +95,13 @@ boolean isBuiltinFunction(PMethod self) {
9495
Object reprBuiltinFunction(VirtualFrame frame, PMethod self,
9596
@Cached("createGetAttributeNode()") GetAttributeNode getNameNode) {
9697
// (tfel): this only happens for builtin modules ... I think
97-
return strFormat("<built-in function %s>", getNameNode.executeObject(frame, self.getFunction()));
98+
return PythonUtils.format("<built-in function %s>", getNameNode.executeObject(frame, self.getFunction()));
9899
}
99100

100101
@Specialization(guards = "isBuiltinFunction(self)")
101102
String reprBuiltinFunction(VirtualFrame frame, PBuiltinMethod self,
102103
@Cached("createGetAttributeNode()") GetAttributeNode getNameNode) {
103-
return strFormat("<built-in function %s>", getNameNode.executeObject(frame, self.getFunction()));
104+
return PythonUtils.format("<built-in function %s>", getNameNode.executeObject(frame, self.getFunction()));
104105
}
105106

106107
@Specialization(guards = "!isBuiltinFunction(self)", limit = "3")
@@ -109,7 +110,7 @@ Object reprBuiltinMethod(VirtualFrame frame, PBuiltinMethod self,
109110
@Cached("createGetAttributeNode()") GetAttributeNode getNameNode,
110111
@Cached("create()") GetNameNode getTypeNameNode) {
111112
String typeName = getTypeNameNode.execute(lib.getLazyPythonClass(self.getSelf()));
112-
return strFormat("<built-in method %s of %s object at 0x%x>", getNameNode.executeObject(frame, self.getFunction()), typeName, PythonAbstractObject.systemHashCode(self.getSelf()));
113+
return PythonUtils.format("<built-in method %s of %s object at 0x%x>", getNameNode.executeObject(frame, self.getFunction()), typeName, PythonAbstractObject.systemHashCode(self.getSelf()));
113114
}
114115

115116
@Specialization(guards = "!isBuiltinFunction(self)", limit = "3")
@@ -118,12 +119,7 @@ Object reprBuiltinMethod(VirtualFrame frame, PMethod self,
118119
@Cached("createGetAttributeNode()") GetAttributeNode getNameNode,
119120
@Cached("create()") GetNameNode getTypeNameNode) {
120121
String typeName = getTypeNameNode.execute(lib.getLazyPythonClass(self.getSelf()));
121-
return strFormat("<built-in method %s of %s object at 0x%x>", getNameNode.executeObject(frame, self.getFunction()), typeName, PythonAbstractObject.systemHashCode(self.getSelf()));
122-
}
123-
124-
@TruffleBoundary
125-
private static String strFormat(String fmt, Object... objects) {
126-
return String.format(fmt, objects);
122+
return PythonUtils.format("<built-in method %s of %s object at 0x%x>", getNameNode.executeObject(frame, self.getFunction()), typeName, PythonAbstractObject.systemHashCode(self.getSelf()));
127123
}
128124

129125
protected static GetAttributeNode createGetAttributeNode() {
@@ -277,12 +273,7 @@ private Object makeQualname(VirtualFrame frame, Object method, Object self, GetA
277273
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.IS_NOT_A, __QUALNAME__, "unicode object");
278274
}
279275

280-
return getQualNameGeneric(typeQualName, methodName);
281-
}
282-
283-
@TruffleBoundary
284-
private static Object getQualNameGeneric(String typeQualName, String name) {
285-
return String.format("%s.%s", typeQualName, name);
276+
return PythonUtils.format("%s.%s", typeQualName, methodName);
286277
}
287278
}
288279
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/MethodBuiltins.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
import com.oracle.graal.python.nodes.util.CannotCastException;
6666
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
6767
import com.oracle.graal.python.runtime.exception.PException;
68+
import com.oracle.graal.python.util.PythonUtils;
6869
import com.oracle.truffle.api.CompilerDirectives;
69-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7070
import com.oracle.truffle.api.dsl.Cached;
7171
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7272
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -173,16 +173,11 @@ Object reprMethod(VirtualFrame frame, PMethod method,
173173
}
174174

175175
try {
176-
return strFormat("<bound method %s of %s>", toJavaStringNode.execute(funcName), callReprNode.executeObject(frame, self));
176+
return PythonUtils.format("<bound method %s of %s>", toJavaStringNode.execute(funcName), callReprNode.executeObject(frame, self));
177177
} catch (CannotCastException e) {
178-
return strFormat("<bound method %s of %s>", defname, callReprNode.executeObject(frame, self));
178+
return PythonUtils.format("<bound method %s of %s>", defname, callReprNode.executeObject(frame, self));
179179
}
180180
}
181-
182-
@TruffleBoundary
183-
private static String strFormat(String fmt, Object... objects) {
184-
return String.format(fmt, objects);
185-
}
186181
}
187182

188183
@Builtin(name = __DEFAULTS__, minNumOfPositionalArgs = 1, isGetter = true)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
108108
import com.oracle.graal.python.nodes.util.SplitArgsNode;
109109
import com.oracle.graal.python.runtime.PythonContext;
110+
import com.oracle.graal.python.util.PythonUtils;
110111
import com.oracle.truffle.api.CompilerDirectives;
111112
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
112113
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -374,14 +375,9 @@ String repr(VirtualFrame frame, Object self,
374375
Object moduleName = readModuleNode.executeObject(frame, type);
375376
Object qualName = readQualNameNode.executeObject(frame, type);
376377
if (moduleName != PNone.NO_VALUE && !BuiltinNames.BUILTINS.equals(moduleName)) {
377-
return strFormat("<%s.%s object at 0x%x>", moduleName, qualName, PythonAbstractObject.systemHashCode(self));
378+
return PythonUtils.format("<%s.%s object at 0x%x>", moduleName, qualName, PythonAbstractObject.systemHashCode(self));
378379
}
379-
return strFormat("<%s object at 0x%x>", qualName, PythonAbstractObject.systemHashCode(self));
380-
}
381-
382-
@TruffleBoundary
383-
private static String strFormat(String fmt, Object... objects) {
384-
return String.format(fmt, objects);
380+
return PythonUtils.format("<%s object at 0x%x>", qualName, PythonAbstractObject.systemHashCode(self));
385381
}
386382
}
387383

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/charset/PythonRawUnicodeEscapeCharsetEncoder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.charset;
4242

43+
import com.oracle.graal.python.util.PythonUtils;
4344
import java.nio.ByteBuffer;
4445
import java.nio.CharBuffer;
4546
import java.nio.charset.Charset;
@@ -82,7 +83,7 @@ protected CoderResult encodeLoop(CharBuffer source, ByteBuffer target) {
8283
// ASCII
8384
target.put((byte) codePoint);
8485
} else {
85-
String hexString = String.format((codePoint <= 0xFFFF ? "\\u%04x" : "\\U%08x"), codePoint);
86+
String hexString = PythonUtils.format((codePoint <= 0xFFFF ? "\\u%04x" : "\\U%08x"), codePoint);
8687
for (int i = 0; i < hexString.length(); i++) {
8788
if (!target.hasRemaining()) {
8889
source.position(initialPosition);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/formatting/ErrorMessageFormatter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
4848
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
49+
import com.oracle.graal.python.util.PythonUtils;
4950
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5051

5152
/**
@@ -117,7 +118,7 @@ public String format(PythonObjectLibrary lib, String format, Object... args) {
117118
matchIdx++;
118119
}
119120
}
120-
return String.format(sb.toString(), compact(args, removedCnt));
121+
return PythonUtils.format(sb.toString(), compact(args, removedCnt));
121122
}
122123

123124
@TruffleBoundary

0 commit comments

Comments
 (0)