["f'{}'",
"f'{ }'"
"f' {} '",
- "f'{!r}'",
- "f'{ !r}'",
"f'{10:{ }}'",
"f' { } '",
# The Python parser ignores also the following
# whitespace characters in additional to a space.
"f'''{\t\f\r\n}'''",
+ ])
+
+ # Different error messeges are raised when a specfier ('!', ':' or '=') is used after an empty expression
+ self.assertAllRaise(SyntaxError, "f-string: expression required before '!'",
+ ["f'{!r}'",
+ "f'{ !r}'",
+ "f'{!}'",
+ "f'''{\t\f\r\n!a}'''",
+
+ # Catch empty expression before the
+ # missing closing brace.
+ "f'{!'",
+ "f'{!s:'",
- # Catch the empty expression before the
+ # Catch empty expression before the
# invalid conversion.
"f'{!x}'",
"f'{ !xr}'",
"f'{!x:a}'",
"f'{ !xr:}'",
"f'{ !xr:a}'",
+ ])
- "f'{!}'",
- "f'{:}'",
-
- # We find the empty expression before the
- # missing closing brace.
- "f'{!'",
- "f'{!s:'",
+ self.assertAllRaise(SyntaxError, "f-string: expression required before ':'",
+ ["f'{:}'",
+ "f'{ :!}'",
+ "f'{:2}'",
+ "f'''{\t\f\r\n:a}'''",
"f'{:'",
- "f'{:x'",
+ ])
+
+ self.assertAllRaise(SyntaxError, "f-string: expression required before '='",
+ ["f'{=}'",
+ "f'{ =}'",
+ "f'{ =:}'",
+ "f'{ =!}'",
+ "f'''{\t\f\r\n=}'''",
+ "f'{='",
])
# Different error message is raised for other whitespace characters.
break;
}
}
+
if (s == expr_end) {
+ if (*expr_end == '!' || *expr_end == ':' || *expr_end == '=') {
+ RAISE_SYNTAX_ERROR("f-string: expression required before '%c'", *expr_end);
+ return NULL;
+ }
RAISE_SYNTAX_ERROR("f-string: empty expression not allowed");
return NULL;
}