8000 fix error message when conversion is done with wrong characters (#170) · isidentical/cpython@afe901e · GitHub
[go: up one dir, main page]

Skip to content

Commit afe901e

Browse files
authored
fix error message when conversion is done with wrong characters (python#170)
1 parent 435fead commit afe901e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Parser/pegen.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,12 +2828,16 @@ expr_ty _PyPegen_formatted_value(Parser *p, expr_ty expression, Token *debug, ex
28282828
int conversion_val = -1;
28292829
if (conversion != NULL) {
28302830
assert(conversion->kind == Name_kind);
2831-
if (PyUnicode_GetLength(conversion->v.Name.id) != 1) {
2832-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(conversion, "Invalid conversion character");
2831+
Py_UCS4 first = PyUnicode_READ_CHAR(conversion->v.Name.id, 0);
2832+
2833+
if (PyUnicode_GET_LENGTH(conversion->v.Name.id) > 1 ||
2834+
!(first == 's' || first == 'r' || first == 'a')) {
2835+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(conversion,
2836+
"f-string: invalid conversion character: expected 's', 'r', or 'a'");
28332837
return NULL;
28342838
}
2835-
const char* id = PyUnicode_AsUTF8(conversion->v.Name.id);
2836-
conversion_val = (int)(id[0]);
2839+
2840+
conversion_val = Py_SAFE_DOWNCAST(first, Py_UCS4, int);
28372841
}
28382842
else if (debug && !format) {
28392843
/* If no conversion is specified, use !r for debug expressions */

0 commit comments

Comments
 (0)
0