Skip to content

Commit 910065a

Browse files
msimacektimfel
authored andcommitted
Assert statement shouldn't convert message to string
1 parent 9b5b5fc commit 910065a

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_assertion.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,15 @@ def test_assert(self):
4747
except:
4848
pass
4949
else:
50-
raise Exception("Assertions doesn't work!")
50+
raise Exception("Assertions don't work!")
51+
52+
def test_assert_message(self):
53+
try:
54+
assert False, 1
55+
except AssertionError as e:
56+
if e.args != (1,):
57+
raise Exception(f"Expected AssertionError.args to be {(1,)}, was {e.args}")
58+
else:
59+
raise Exception("Assertions don't work!")
60+
5161

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import java.io.PrintStream;
3131

3232
import com.oracle.graal.python.nodes.PRaiseNode;
33-
import com.oracle.graal.python.nodes.SpecialMethodNames;
34-
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
3533
import com.oracle.graal.python.nodes.expression.CoerceToBooleanNode;
3634
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3735
import com.oracle.graal.python.runtime.PythonContext;
@@ -47,7 +45,6 @@ public class AssertNode extends StatementNode {
4745
@Child private PRaiseNode raise;
4846
@Child private CoerceToBooleanNode condition;
4947
@Child private ExpressionNode message;
50-
@Child private LookupAndCallUnaryNode callNode;
5148
@CompilationFinal private Boolean assertionsEnabled = null;
5249

5350
private final ConditionProfile profile = ConditionProfile.createBinaryProfile();
@@ -83,15 +80,10 @@ public void executeVoid(VirtualFrame frame) {
8380
}
8481

8582
private PException assertionFailed(VirtualFrame frame) {
86-
String assertionMessage = null;
83+
Object assertionMessage = null;
8784
if (message != null) {
8885
try {
89-
Object messageObj = message.execute(frame);
90-
if (callNode == null) {
91-
CompilerDirectives.transferToInterpreterAndInvalidate();
92-
callNode = insert(LookupAndCallUnaryNode.create(SpecialMethodNames.__STR__));
93-
}
94-
assertionMessage = (String) callNode.executeObject(frame, messageObj);
86+
assertionMessage = message.execute(frame);
9587
} catch (PException e) {
9688
// again, Python exceptions just fall through
9789
throw e;

graalpython/lib-graalpython/python_cext.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,29 +581,29 @@ def PySequence_Contains(haystack, needle):
581581
@may_raise
582582
def PySequence_Repeat(obj, n):
583583
if not PySequence_Check(obj):
584-
raise TypeError("'%p' object can't be repeated", obj)
584+
raise TypeError("'%s' object can't be repeated" % type(obj))
585585
return obj * n
586586

587587

588588
@may_raise
589589
def PySequence_InPlaceRepeat(obj, n):
590590
if not PySequence_Check(obj):
591-
raise TypeError("'%p' object can't be repeated", obj)
591+
raise TypeError("'%s' object can't be repeated" % type(obj))
592592
obj *= n
593593
return obj
594594

595595

596596
@may_raise
597597
def PySequence_Concat(s, o):
598598
if not (PySequence_Check(s) and PySequence_Check(o)):
599-
raise TypeError("'%p' object can't be repeated", s)
599+
raise TypeError("'%s' object can't be concatenated" % type(s))
600600
return s + o
601601

602602

603603
@may_raise
604604
def PySequence_InPlaceConcat(s, o):
605605
if not (PySequence_Check(s) and PySequence_Check(o)):
606-
raise TypeError("'%p' object can't be repeated", s)
606+
raise TypeError("'%s' object can't be concatenated" % type(s))
607607
s += o
608608
return s
609609

0 commit comments

Comments
 (0)