Skip to content

Preserve Boolean call arguments and bounded OR chains - #25

Open
EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/boolean-call-argument-values
Open

EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/boolean-call-argument-values

Conversation

@EiNSTeiN-

Copy link
Copy Markdown

I am opening this PR to keep a Boolean expression as one call argument and preserve its operand evaluation order. The code below currently decompiles incorrectly in Python 3.14:

def select(call, before, first, second, last, after):
    return call(before(), (first() and second()) or last(), after())

The current output adds an argument and evaluates first() twice:

def select(call, before, first, second, last, after):
    return call(before(), first(), first() and second() or last(), after())

The value-chain recognizer leaves the initial COPY-retained operand on the stack after pushing the reconstructed expression. A later call then sees an extra argument, or consumes its callee/NULL prefix incorrectly. This change consumes the matching retained operand while preserving unrelated argument and callee entries, including an earlier argument with the same value.

The same reconstruction also handles multiple OR tails:

def alternatives(a, b, c, d, e):
    return bool((a and b) or c or d or e)

The current parser can emit None(a and (b or bool), c or d or e) instead. Each non-final OR operand must prove the same COPY/TO_BOOL/true-jump-to-one-merge protocol and matching discard. Arbitrary branches or different merge targets still fail recognition. The retained operand must be consumed together with this reconstruction so that extending the chain does not corrupt the enclosing call's stack.

Variations include a preceding OR chain, method calls within an operand, storing the expression in a local variable, and other arguments before or after the expression. Pure AND and OR chains continue through their existing paths.

The recognizer applies to Python 3.13+, matching the introduction of TO_BOOL and its COPY-based value-preservation protocol. The Boolean source expressions themselves predate this bytecode layout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant