From nobody Wed Apr 16 03:29:39 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1504618278954148.9212680620234; Tue, 5 Sep 2017 06:31:18 -0700 (PDT) Received: from localhost ([::1]:58958 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dpDwj-000236-Py for importer@patchew.org; Tue, 05 Sep 2017 09:31:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51265) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dpDus-0000jQ-L7 for qemu-devel@nongnu.org; Tue, 05 Sep 2017 09:29:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dpDun-0003rA-SE for qemu-devel@nongnu.org; Tue, 05 Sep 2017 09:29:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55656) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dpDun-0003qB-MG for qemu-devel@nongnu.org; Tue, 05 Sep 2017 09:29:17 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 84101267C5; Tue, 5 Sep 2017 13:29:16 +0000 (UTC) Received: from localhost (ovpn-117-180.ams2.redhat.com [10.36.117.180]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6BF726A0A5; Tue, 5 Sep 2017 13:29:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 84101267C5 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=stefanha@redhat.com From: Stefan Hajnoczi To: Date: Tue, 5 Sep 2017 14:29:05 +0100 Message-Id: <20170905132908.30931-2-stefanha@redhat.com> In-Reply-To: <20170905132908.30931-1-stefanha@redhat.com> References: <20170905132908.30931-1-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 05 Sep 2017 13:29:16 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 1/4] qemu.py: make VM() a context manager X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Eduardo Habkost , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" There are a number of ways to ensure that the QEMU process is shut down when the test ends, including atexit.register(), try: finally:, or unittest.teardown() methods. All of these require extra code and the programmer must remember to add vm.shutdown(). A nice solution is context managers: with VM(binary) as vm: ... # vm is guaranteed to be shut down here Cc: Eduardo Habkost Signed-off-by: Stefan Hajnoczi Reviewed-by: Eduardo Habkost Message-id: 20170824072202.26818-2-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi --- scripts/qemu.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/scripts/qemu.py b/scripts/qemu.py index 880e3e8219..4d8ee10943 100644 --- a/scripts/qemu.py +++ b/scripts/qemu.py @@ -21,7 +21,14 @@ import qmp.qmp =20 =20 class QEMUMachine(object): - '''A QEMU VM''' + '''A QEMU VM + + Use this object as a context manager to ensure the QEMU process termin= ates:: + + with VM(binary) as vm: + ... + # vm is guaranteed to be shut down here + ''' =20 def __init__(self, binary, args=3D[], wrapper=3D[], name=3DNone, test_= dir=3D"/var/tmp", monitor_address=3DNone, socket_scm_helper=3DNone, debug= =3DFalse): @@ -40,6 +47,13 @@ class QEMUMachine(object): self._socket_scm_helper =3D socket_scm_helper self._debug =3D debug =20 + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.shutdown() + return False + # This can be used to add an unused monitor instance. def add_monitor_telnet(self, ip, port): args =3D 'tcp:%s:%d,server,nowait,telnet' % (ip, port) --=20 2.13.5