Commit dc6fd6c
committed
gh-157451: Fix gettext.c2py precedence of unary
In C the unary `!` operator binds tighter than every binary operator, so
`!n + 1` means `(!n) + 1`. `c2py`'s parser emitted a bare `not ` prefix and
then appended the binary operators to the whole string, so `!n + 1` compiled
to `not n + 1` — i.e. `not (n + 1)` — because Python's `not` binds looser than
arithmetic and comparison operators.
Negate the operand as a self-contained parenthesised unit before the
binary-operator loop, so `!n + 1` compiles to `(not n) + 1`. Double negation
still normalises to 0/1 as in C (`!!n` -> `(not (not n))`).
Add a regression test; the existing test only covered `!!!n` with no trailing
binary operator, which is the one form that happened to work.! before a binary operator1 parent fb46c67 commit dc6fd6c
3 files changed
Lines changed: 24 additions & 1 deletion
File tree
- Lib
- test
- Misc/NEWS.d/next/Library
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| 122 | + | |
122 | 123 | | |
123 | | - | |
| 124 | + | |
124 | 125 | | |
125 | 126 | | |
126 | 127 | | |
| |||
136 | 137 | | |
137 | 138 | | |
138 | 139 | | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
139 | 145 | | |
140 | 146 | | |
141 | 147 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
613 | 613 | | |
614 | 614 | | |
615 | 615 | | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
616 | 629 | | |
617 | 630 | | |
618 | 631 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
0 commit comments