Skip to content

Commit ee327e4

Browse files
committed
throw python error on non-string arg in ord
1 parent 6b6c186 commit ee327e4

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,7 @@ protected static NextNode create() {
15721572
// ord(c)
15731573
@Builtin(name = ORD, minNumOfPositionalArgs = 1)
15741574
@GenerateNodeFactory
1575+
@ImportStatic(PGuards.class)
15751576
public abstract static class OrdNode extends PythonBuiltinNode {
15761577

15771578
@Specialization
@@ -1606,6 +1607,11 @@ public long ord(VirtualFrame frame, PBytesLike chr,
16061607
}
16071608
return castNode.execute(getItemNode.execute(frame, chr.getSequenceStorage(), 0));
16081609
}
1610+
1611+
@Specialization(guards = {"!isString(obj)", "!isBytes(obj)"})
1612+
public Object ord(@SuppressWarnings("unused") Object obj) {
1613+
throw raise(TypeError, ErrorMessages.S_EXPECTED_STRING_OF_LEN_BUT_P, "ord()", "1", "obj");
1614+
}
16091615
}
16101616

16111617
// print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ public abstract class ErrorMessages {
235235
public static final String EXPECTED_S_NOT_P = "expected %s, not %p";
236236
public static final String EXPECTED_S_P_FOUND = "expected %s, %p found";
237237
public static final String EXPECTED_STR_BYTE_OSPATHLIKE_OBJ = "expected str, bytes or os.PathLike object, not %p";
238+
public static final String S_EXPECTED_STRING_OF_LEN_BUT_P = "%s expected string of length %s, but %p found";
238239
public static final String EXPECTED_UNICODE_CHAR_NOT_P = "expected a unicode character, not %p";
239240
public static final String EXPONENT_TOO_LARGE = "exponent too large";
240241
public static final String FACTORIAL_NOT_DEFINED_FOR_NEGATIVE = "factorial() not defined for negative values";

0 commit comments

Comments
 (0)