Skip to content

Commit c658620

Browse files
committed
Fix detection of custom format specifiers.
1 parent 56f0043 commit c658620

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/BaseExceptionBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ private GetLazyClassNode getGetClassNode() {
109109
@TruffleBoundary
110110
private String getFormattedMessage(String format, Object... args) {
111111
try {
112-
// pre-format for '%p' which retrieves the Python class of an argument
113-
if (format.contains("%p")) {
112+
// pre-format for custom error message formatter
113+
if (ErrorMessageFormatter.containsCustomSpecifier(format)) {
114114
return formatter.format(getGetClassNode(), format, args);
115115
}
116116
return String.format(format, args);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ private static String getClassName(GetLazyClassNode getClassNode, Object obj) {
126126
return GetNameNode.doSlowPath(GetLazyClassNode.getUncached().execute(obj));
127127
}
128128

129+
/**
130+
* Use this method to check if a given format string contains any of the custom format
131+
* specifiers handled by this formatter.
132+
*/
133+
public static boolean containsCustomSpecifier(String format) {
134+
int pidx = -1;
135+
while ((pidx = format.indexOf('%', pidx + 1)) != -1 && pidx + 1 < format.length()) {
136+
char c = format.charAt(pidx + 1);
137+
if (c == 'p' || c == 'P' || c == 'm') {
138+
return true;
139+
}
140+
}
141+
return false;
142+
}
143+
129144
private static Object[] compact(Object[] args, int removedCnt) {
130145
Object[] compacted = new Object[args.length - removedCnt];
131146
int j = 0;

0 commit comments

Comments
 (0)