Skip to content

Commit 45828e2

Browse files
committed
[GR-19264] Fix message-less AssertionError args count
PullRequest: graalpython/887
2 parents 21821b7 + c3c6c36 commit 45828e2

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TryTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,17 @@ public void tryGrammarError() {
192192
assertLastLineErrorContains("SyntaxError", "try: 1+1\n");
193193
assertLastLineErrorContains("SyntaxError", "try:\n 1+1\n");
194194
}
195+
196+
@Test
197+
public void tryZeroArgLen() {
198+
assertPrints("0\n", "try:\n" +
199+
" assert False\n" +
200+
"except AssertionError as e:\n" +
201+
" print(len(e.args))\n");
202+
203+
assertPrints("1\n", "try:\n" +
204+
" assert False, \"\"\n" +
205+
"except AssertionError as e:\n" +
206+
" print(len(e.args))\n");
207+
}
195208
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,16 @@ private String getFormattedMessage(String format, Object... args) {
109109

110110
@Specialization(guards = "isNoValue(none)")
111111
public Object args(PBaseException self, @SuppressWarnings("unused") PNone none,
112-
@Cached("createBinaryProfile()") ConditionProfile nullArgsProfile) {
112+
@Cached("createBinaryProfile()") ConditionProfile nullArgsProfile,
113+
@Cached("createBinaryProfile()") ConditionProfile hasMessageFormat) {
113114
PTuple args = self.getArgs();
114115
if (nullArgsProfile.profile(args == null)) {
115-
// lazily format the exception message:
116-
args = factory().createTuple(new Object[]{getFormattedMessage(self.getMessageFormat(), self.getMessageArgs())});
116+
if (hasMessageFormat.profile(!self.hasMessageFormat())) {
117+
args = factory().createTuple(new Object[0]);
118+
} else {
119+
// lazily format the exception message:
120+
args = factory().createTuple(new Object[]{getFormattedMessage(self.getMessageFormat(), self.getMessageArgs())});
121+
}
117122
self.setArgs(args);
118123
}
119124
return args;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public final class PBaseException extends PythonObject {
6767
private PTuple args; // can be null for lazily generated message
6868

6969
// in case of lazily generated messages, these will be used to construct the message:
70+
private final boolean hasMessageFormat;
7071
private final String messageFormat;
7172
private final Object[] messageArgs;
7273

@@ -79,13 +80,15 @@ public final class PBaseException extends PythonObject {
7980
public PBaseException(LazyPythonClass cls, PTuple args) {
8081
super(cls);
8182
this.args = args;
83+
this.hasMessageFormat = false;
8284
this.messageFormat = null;
8385
this.messageArgs = null;
8486
}
8587

8688
public PBaseException(LazyPythonClass cls, String format, Object[] args) {
8789
super(cls);
8890
this.args = null;
91+
this.hasMessageFormat = true;
8992
this.messageFormat = format;
9093
this.messageArgs = args;
9194
}
@@ -144,6 +147,10 @@ public String getMessageFormat() {
144147
return messageFormat;
145148
}
146149

150+
public boolean hasMessageFormat() {
151+
return hasMessageFormat;
152+
}
153+
147154
public Object[] getMessageArgs() {
148155
// clone message args to ensure that they stay unmodified
149156
return messageArgs.clone();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import java.util.Arrays;
4747

4848
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
49-
import com.oracle.graal.python.builtins.objects.PNone;
5049
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallCapiFunction;
5150
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToSulongNode;
5251
import com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols;
@@ -204,7 +203,7 @@ static String doConvert(Object self, String errMsgFormat, Object[] errMsgArgs,
204203
@Cached PRaiseNode raiseNode) {
205204
String result = castToJavaStringNode.execute(self);
206205
if (errorProfile.profile(result == null)) {
207-
throw raiseNode.execute(PythonBuiltinClassType.TypeError, PNone.NO_VALUE, errMsgFormat, errMsgArgs);
206+
throw raiseNode.raise(PythonBuiltinClassType.TypeError, errMsgFormat, errMsgArgs);
208207
}
209208
return result;
210209
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/AssertNode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void executeVoid(VirtualFrame frame) {
8989
}
9090

9191
private PException assertionFailed(VirtualFrame frame) {
92-
String assertionMessage = "";
92+
String assertionMessage = null;
9393
if (message != null) {
9494
try {
9595
Object messageObj = message.execute(frame);
@@ -113,6 +113,9 @@ private PException assertionFailed(VirtualFrame frame) {
113113
CompilerDirectives.transferToInterpreterAndInvalidate();
114114
raise = insert(PRaiseNode.create());
115115
}
116+
if (assertionMessage == null) {
117+
return raise.raise(AssertionError);
118+
}
116119
return raise.raise(AssertionError, assertionMessage);
117120
}
118121

0 commit comments

Comments
 (0)