Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __enter__ and __exit__ methods to Solver class in Python API #7025

Merged
merged 1 commit into from
Nov 30, 2023

Conversation

alperaltuntas
Copy link
Contributor

@alperaltuntas alperaltuntas commented Nov 30, 2023

This pull request adds the __enter__() and __exit__() methods to the Solver class in Python API for implicit scope management. This allows the usage of the with statement as a more concise and safer alternative to explicitly calling the push() and pop() methods when appropriate.

Example usage:

from z3 import *

p, q = Bools('p q')

with Solver() as s:
    s.add(Implies(p, q))
    s.add(Not(q))
    with s:
        s.add(p)
        print(s.check()) # unsat
    print(s.check())     # sat

print(s.assertions())    # []

To enable the usage of the with statement for the Solver class
instances, this commit adds __enter__ and __exit__ methods.
The with statement should offer a more concise and safer
alternative to explicit usage of push() and pop() methods.
@NikolajBjorner NikolajBjorner merged commit d540d88 into Z3Prover:master Nov 30, 2023
15 checks passed
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.

None yet

2 participants