| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 15ba816 commit 7b44ade
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -628,33 +628,51 @@ def test_missing_expression(self): | |||
| 628 | 628 | ["f'{}'", | |
| 629 | 629 | "f'{ }'" | |
| 630 | 630 | "f' {} '", | |
| 631 | - "f'{!r}'", | ||
| 632 | - "f'{ !r}'", | ||
| 633 | 631 | "f'{10:{ }}'", | |
| 634 | 632 | "f' { } '", | |
| 635 | 633 | ||
| 636 | 634 | # The Python parser ignores also the following | |
| 637 | 635 | # whitespace characters in additional to a space. | |
| 638 | 636 | "f'''{\t\f\r\n}'''", | |
| 637 | + ]) | ||
| 638 | + | ||
| 639 | + # Different error messeges are raised when a specfier ('!', ':' or '=') is used after an empty expression | ||
| 640 | + self.assertAllRaise(SyntaxError, "f-string: expression required before '!'", | ||
| 641 | + ["f'{!r}'", | ||
| 642 | + "f'{ !r}'", | ||
| 643 | + "f'{!}'", | ||
| 644 | + "f'''{\t\f\r\n!a}'''", | ||
| 645 | + | ||
| 646 | + # Catch empty expression before the | ||
| 647 | + # missing closing brace. | ||
| 648 | + "f'{!'", | ||
| 649 | + "f'{!s:'", | ||
| 639 | 650 | ||
| 640 | - # Catch the empty expression before the | ||
| 651 | + # Catch empty expression before the | ||
| 641 | 652 | # invalid conversion. | |
| 642 | 653 | "f'{!x}'", | |
| 643 | 654 | "f'{ !xr}'", | |
| 644 | 655 | "f'{!x:}'", | |
| 645 | 656 | "f'{!x:a}'", | |
| 646 | 657 | "f'{ !xr:}'", | |
| 647 | 658 | "f'{ !xr:a}'", | |
| 659 | + ]) | ||
| 648 | 660 | ||
| 649 | - "f'{!}'", | ||
| 650 | - "f'{:}'", | ||
| 651 | - | ||
| 652 | - # We find the empty expression before the | ||
| 653 | - # missing closing brace. | ||
| 654 | - "f'{!'", | ||
| 655 | - "f'{!s:'", | ||
| 661 | + self.assertAllRaise(SyntaxError, "f-string: expression required before ':'", | ||
| 662 | + ["f'{:}'", | ||
| 663 | + "f'{ :!}'", | ||
| 664 | + "f'{:2}'", | ||
| 665 | + "f'''{\t\f\r\n:a}'''", | ||
| 656 | 666 | "f'{:'", | |
| 657 | - "f'{:x'", | ||
| 667 | + ]) | ||
| 668 | + | ||
| 669 | + self.assertAllRaise(SyntaxError, "f-string: expression required before '='", | ||
| 670 | + ["f'{=}'", | ||
| 671 | + "f'{ =}'", | ||
| 672 | + "f'{ =:}'", | ||
| 673 | + "f'{ =!}'", | ||
| 674 | + "f'''{\t\f\r\n=}'''", | ||
| 675 | + "f'{='", | ||
| 658 | 676 | ]) | |
| 659 | 677 | ||
| 660 | 678 | # Different error message is raised for other whitespace characters. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Improve error messages in f-string syntax errors concerning empty expressions. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -357,7 +357,12 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end, | |||
| 357 | 357 | break; | |
| 358 | 358 | } | |
| 359 | 359 | } | |
| 360 | + | ||
| 360 | 361 | if (s == expr_end) { | |
| 362 | + if (*expr_end == '!' || *expr_end == ':' || *expr_end == '=') { | ||
| 363 | + RAISE_SYNTAX_ERROR("f-string: expression required before '%c'", *expr_end); | ||
| 364 | + return NULL; | ||
| 365 | + } | ||
| 361 | 366 | RAISE_SYNTAX_ERROR("f-string: empty expression not allowed"); | |
| 362 | 367 | return NULL; | |
| 363 | 368 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments