def __exit__(self, exception_type, exception_value, traceback): super().__exit__(exception_type, exception_value, traceback) if not QueuingContext.recording(): # remove the monkeypatching self._stack.__exit__(exception_type, exception_value, traceback) self._process_queue()
def __enter__(self): if not QueuingContext.recording(): # if the tape is the first active queuing context # monkeypatch the operations to support the new queuing context with contextlib.ExitStack() as stack: for mock in mock_operations(): stack.enter_context(mock) self._stack = stack.pop_all() QueuingContext.append(self) return super().__enter__()
def __exit__(self, exception_type, exception_value, traceback): try: super().__exit__(exception_type, exception_value, traceback) if not QueuingContext.recording(): # remove the monkeypatching self._stack.__exit__(exception_type, exception_value, traceback) self._process_queue() finally: QuantumTape._lock.release()
def __enter__(self): QuantumTape._lock.acquire() try: if not QueuingContext.recording(): # if the tape is the first active queuing context # monkeypatch the operations to support the new queuing context with contextlib.ExitStack() as stack: for mock in mock_operations(): stack.enter_context(mock) self._stack = stack.pop_all() QueuingContext.append(self) return super().__enter__() except Exception as _: QuantumTape._lock.release() raise