Skip to content

Commit

Permalink
Add __enter__ and __exit__ methods to Solver class (#7025)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alperaltuntas authored Nov 30, 2023
1 parent 26440ed commit d540d88
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/api/python/z3/z3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6955,6 +6955,13 @@ def __del__(self):
if self.solver is not None and self.ctx.ref() is not None and Z3_solver_dec_ref is not None:
Z3_solver_dec_ref(self.ctx.ref(), self.solver)

def __enter__(self):
self.push()
return self

def __exit__(self, *exc_info):
self.pop()

def set(self, *args, **keys):
"""Set a configuration option.
The method `help()` return a string containing all available options.
Expand Down

0 comments on commit d540d88

Please sign in to comment.