Conversation
…nary operator 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In C the unary
!operator binds tighter than every binary operator, so!n + 1means(!n) + 1.gettext.c2py's parser (_parseinLib/gettext.py) emitted a barenotprefix and then appended the binary operators to the whole string, so!n + 1compiled tonot n + 1— i.e.not (n + 1)— because Python'snotbinds looser than arithmetic and comparison operators.This selects the wrong plural form for any
.mocatalog whosePlural-Formsrule applies!before a binary operator.The fix negates the operand as a self-contained parenthesised unit before the binary-operator loop, so
!n + 1compiles to(not n) + 1. Double negation still normalises to 0/1 as in C (!!n→(not (not n))).The existing
test_negationonly covered!!!nwith no trailing binary operator — the one form that happened to work — so the precedence bug wasn't pinned. Addedtest_negation_precedencecovering!before+/</*and double negation.!before a binary operator (wrong operator precedence) #157451./python -m test test_gettext→ SUCCESS (74 tests); the new test fails onmain(AssertionError: 0 != 2) and passes with the change.Misc/NEWS.dentry.AI-tools disclosure (per the devguide policy): I used an AI assistant to help locate the bug and draft the patch and test. I verified the behaviour and the fix against a locally built interpreter, understand the change, and stand by it.
!before a binary operator (wrong operator precedence) #157451