From c9ce32ddffbe7e5bcbba55be805da0fc41963743 Mon Sep 17 00:00:00 2001 From: Oskar Haarklou Veileborg Date: Fri, 13 Dec 2024 11:09:08 +0100 Subject: [PATCH] Add __enter__ and __exit__ methods on Optimize class This enables the use of the with statement for the Optimize class to concisely call push() and pop(). This works similarly to the Solver class. --- src/api/python/z3/z3.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/api/python/z3/z3.py b/src/api/python/z3/z3.py index 8231c2036..34e046520 100644 --- a/src/api/python/z3/z3.py +++ b/src/api/python/z3/z3.py @@ -7991,6 +7991,13 @@ class Optimize(Z3PPObject): if self._on_models_id is not None: del _on_models[self._on_models_id] + 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.