From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204204; cv=none; d=zohomail.com; s=zohoarc; b=Wnh7D4xQF+yYmkSCf2bDf2ppcA3VYGW9nkr2IFdub14C4FW160G2iwpqsOIL0wD3nP5bQUbx3BNk6fdR9LSrWF49ciyjWP/GuOCcePKKcYyZv9O6wIOBriC+p302BIH/wGUXt06h68yGvjaDLpiDEHh3HXWJYU3XO1dcnMSryt8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204204; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=sStIApWiVtCBKiexrQL7OTQwVFLoTBgkZqfp2zgj6T8=; b=eX3ApU08ncvAIOwq+01NDZDQEUmVEAu8hKWpZuek0NrRsOEaMaaiwyvnqCKOQXBMdgtPze1bvvIRz0UtSLB043RQ+WxASC2/RtUUYEAwzr3RZoErWg/dX3JZGGsCWNNz+N+MOoSzxqH6HkY3MX25vzGVsIi4kdASpqhhPDGNHHI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204204209815.2992486110663; Fri, 26 Jun 2020 13:43:24 -0700 (PDT) Received: from localhost ([::1]:60336 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovBy-0000Az-Uw for importer@patchew.org; Fri, 26 Jun 2020 16:43:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33722) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAR-0006ek-5i for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:47 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:42796 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAK-0005nf-Ll for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:46 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-151-tiQYQdTEMMCL4YgZb1qRhw-1; Fri, 26 Jun 2020 16:41:37 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CEAD7BFC1; Fri, 26 Jun 2020 20:41:36 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id B9B6F19C58; Fri, 26 Jun 2020 20:41:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204099; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sStIApWiVtCBKiexrQL7OTQwVFLoTBgkZqfp2zgj6T8=; b=ZEe/F850x2tROtX9E8/A1cYTW1NA8RWY8nmU288GHpW0NldTy0XUGSZh1ycE4JiKypGa9w rrrqnTKTu0MfxTMuVhnkE+pSCj296Wg8NebEc95mujaPaf7l+BP7q8tfrqVxOF3dCX2mje xT8EqKH/+1QmdhrPEly3MRXAon8HjoM= X-MC-Unique: tiQYQdTEMMCL4YgZb1qRhw-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 01/16] python/qmp.py: Define common types Date: Fri, 26 Jun 2020 16:41:18 -0400 Message-Id: <20200626204133.14500-2-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 03:23:21 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Define some common types that we'll need to annotate a lot of other functions going forward. Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- python/qemu/qmp.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index e64b6b5faa..8388c7b603 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -12,13 +12,31 @@ import socket import logging from typing import ( + Any, + Dict, Optional, TextIO, Type, + Tuple, + Union, ) from types import TracebackType =20 =20 +# QMPMessage is a QMP Message of any kind. +# e.g. {'yee': 'haw'} +# +# QMPReturnValue is the inner value of return values only. +# {'return': {}} is the QMPMessage, +# {} is the QMPReturnValue. +QMPMessage =3D Dict[str, Any] +QMPReturnValue =3D Dict[str, Any] + +InternetAddrT =3D Tuple[str, str] +UnixAddrT =3D str +SocketAddrT =3D Union[InternetAddrT, UnixAddrT] + + class QMPError(Exception): """ QMP base exception --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204202; cv=none; d=zohomail.com; s=zohoarc; b=QcTv1TVFbMHGCFfB0W1cJ64RcDcETKDCg1yaT1S0oIFJ7V+CsRNTspT6ESWtEySCCEyylv7FenrQSyZTbK6+lLOO4jjYxtywQ7Unyq+cSGh+Lsy1BAsnkdq6QOX9Com0r8bqTEl5Ewf3XTPR2J6SNGCOgA+Ady7V40le/7aNATA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204202; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=24hiEmEd4SvyGTS9rVJLtikp6NjBMGoDox6DBs1NjrA=; b=FRpPngtsw++Nbg1D/xOTrvX9kdRtxAzBf3FBJHMH8lIdf/lC8JeZz4IDF3ZZk7Xe68qW4JODuSnsYNo36Te4KnACax51zim49IN4gYUtT5Jycm5vxOOWO50bJOp+yTrCPqfiLVfJvbNHu+VH+JtUKvzqw4Cwf0dl6rttlcVV+Q8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204202608697.5664892933922; Fri, 26 Jun 2020 13:43:22 -0700 (PDT) Received: from localhost ([::1]:60242 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovBx-00006l-9f for importer@patchew.org; Fri, 26 Jun 2020 16:43:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33718) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAQ-0006dz-S9 for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:46 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:33949 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAN-0005oc-Rp for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:46 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-78-l5fUZFhFMySHakQNS8ka1g-1; Fri, 26 Jun 2020 16:41:38 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CE189464; Fri, 26 Jun 2020 20:41:37 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id F01FC19C58; Fri, 26 Jun 2020 20:41:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204103; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=24hiEmEd4SvyGTS9rVJLtikp6NjBMGoDox6DBs1NjrA=; b=fyOdzYYK9bDtkXSUMa6INFn7S2jllp2yRfqtP9LmlqXtYenA/eJufVO1kABSfLFWvAYoUd 9ulqTPqOtxmEtvn0xRhvfvA8eUnUIk6Uw/ygZEhlauGypkkGRt93sVFoq/5w/wA9DRMtUS xx1crXQnj84S2FU8O/nWRSLUev9U1uU= X-MC-Unique: l5fUZFhFMySHakQNS8ka1g-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 02/16] iotests.py: use qemu.qmp type aliases Date: Fri, 26 Jun 2020 16:41:19 -0400 Message-Id: <20200626204133.14500-3-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.120; envelope-from=jsnow@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 01:55:55 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" iotests.py should use the type definitions from qmp.py instead of its own. Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- tests/qemu-iotests/iotests.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py index 5ea4c4df8b..a8576f811b 100644 --- a/tests/qemu-iotests/iotests.py +++ b/tests/qemu-iotests/iotests.py @@ -35,13 +35,10 @@ # pylint: disable=3Dimport-error, wrong-import-position sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'pytho= n')) from qemu import qtest +from qemu.qmp import QMPMessage =20 assert sys.version_info >=3D (3, 6) =20 -# Type Aliases -QMPResponse =3D Dict[str, Any] - - # Use this logger for logging messages directly from the iotests module logger =3D logging.getLogger('qemu.iotests') logger.addHandler(logging.NullHandler()) @@ -554,7 +551,7 @@ def add_incoming(self, addr): self._args.append(addr) return self =20 - def hmp(self, command_line: str, use_log: bool =3D False) -> QMPRespon= se: + def hmp(self, command_line: str, use_log: bool =3D False) -> QMPMessag= e: cmd =3D 'human-monitor-command' kwargs =3D {'command-line': command_line} if use_log: @@ -575,7 +572,7 @@ def resume_drive(self, drive: str) -> None: self.hmp(f'qemu-io {drive} "remove_break bp_{drive}"') =20 def hmp_qemu_io(self, drive: str, cmd: str, - use_log: bool =3D False) -> QMPResponse: + use_log: bool =3D False) -> QMPMessage: """Write to a given drive using an HMP command""" return self.hmp(f'qemu-io {drive} "{cmd}"', use_log=3Duse_log) =20 --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204316; cv=none; d=zohomail.com; s=zohoarc; b=gNre/zpPJKWbZJNanu4dTXNWkd7/wM6W9IbRsvHLAhiKEwsNPlJ5Wq0z+7yCjVOOKH7UKoEE4d3tA6pmuWIDAu5ULSLq2H1pAzODJ8FfUCld+LpvUvH+WqnIh93M6Ty/WIVcmmuCcOGbGrIICzWR66aq/LlfrppAOsNDTtaDdJ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204316; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Ebpl/s2od5p2nkJdsUmlCGv8AXKiXwIj/vwcNQbJWCY=; b=YsvoX9+QokX5kk7Q1NeGDHc5BJkM5MlOhohFBgZjKpy415rwt6A7VRM9tWWZeAOQD/RcsIYM2W2mss9P5s3YQHo+0XH+dfmoOE9DzmpVBUO9NtEPEL1eG/scWiNL/3XR+5JiKXUexEDNWPgDWzDDW5760mgJrPB2UFWh8sLjUW4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204316892295.51965751686475; Fri, 26 Jun 2020 13:45:16 -0700 (PDT) Received: from localhost ([::1]:40230 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovDn-0003V5-8P for importer@patchew.org; Fri, 26 Jun 2020 16:45:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33712) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAQ-0006dX-J8 for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:46 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:53094 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAO-0005oj-38 for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:46 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-119-pcwcTblwMNuiAQVqm6OCcQ-1; Fri, 26 Jun 2020 16:41:39 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C932C107ACCA; Fri, 26 Jun 2020 20:41:38 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id EF4E219C58; Fri, 26 Jun 2020 20:41:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204103; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ebpl/s2od5p2nkJdsUmlCGv8AXKiXwIj/vwcNQbJWCY=; b=HiNNA90BS1gnjfAaE1bj2Odarh42XofxZAUwSOfP961HCN5MXXVTRYOneLt2+DnCrImgW4 O0Vzdt+KFmXMUBrYhpQ8LpyHw/13pAX+MlVfqtGJHaVsjuqdnWSJ/FOqpYivNYwzsUKBOj ojdbsETRq0DRe8NiLMdiNAlVV6fluU4= X-MC-Unique: pcwcTblwMNuiAQVqm6OCcQ-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 03/16] python/qmp.py: re-absorb MonitorResponseError Date: Fri, 26 Jun 2020 16:41:20 -0400 Message-Id: <20200626204133.14500-4-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 03:23:21 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) When I initially split this out, I considered this more of a machine error than a QMP protocol error, but I think that's misguided. Move this back to qmp.py and name it QMPResponseError. Convert qmp.command() to use this exception type. Signed-off-by: John Snow Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Kevin Wolf --- python/qemu/machine.py | 15 +-------------- python/qemu/qmp.py | 17 +++++++++++++++-- scripts/render_block_graph.py | 7 +++++-- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 8c9050af5d..a5a579095b 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -49,19 +49,6 @@ class QEMUMachineAddDeviceError(QEMUMachineError): """ =20 =20 -class MonitorResponseError(qmp.QMPError): - """ - Represents erroneous QMP monitor reply - """ - def __init__(self, reply): - try: - desc =3D reply["error"]["desc"] - except KeyError: - desc =3D reply - super().__init__(desc) - self.reply =3D reply - - class QEMUMachine: """ A QEMU VM @@ -477,7 +464,7 @@ def command(self, cmd, conv_keys=3DTrue, **args): if reply is None: raise qmp.QMPError("Monitor is closed") if "error" in reply: - raise MonitorResponseError(reply) + raise qmp.QMPResponseError(reply) return reply["return"] =20 def get_qmp_event(self, wait=3DFalse): diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index 8388c7b603..aa8a666b8a 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -61,6 +61,19 @@ class QMPTimeoutError(QMPError): """ =20 =20 +class QMPResponseError(QMPError): + """ + Represents erroneous QMP monitor reply + """ + def __init__(self, reply: QMPMessage): + try: + desc =3D reply['error']['desc'] + except KeyError: + desc =3D reply + super().__init__(desc) + self.reply =3D reply + + class QEMUMonitorProtocol: """ Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and = then @@ -251,8 +264,8 @@ def command(self, cmd, **kwds): Build and send a QMP command to the monitor, report errors if any """ ret =3D self.cmd(cmd, kwds) - if "error" in ret: - raise Exception(ret['error']['desc']) + if 'error' in ret: + raise QMPResponseError(ret) return ret['return'] =20 def pull_event(self, wait=3DFalse): diff --git a/scripts/render_block_graph.py b/scripts/render_block_graph.py index 409b4321f2..da6acf050d 100755 --- a/scripts/render_block_graph.py +++ b/scripts/render_block_graph.py @@ -25,7 +25,10 @@ from graphviz import Digraph =20 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'python')) -from qemu.machine import MonitorResponseError +from qemu.qmp import ( + QEMUMonitorProtocol, + QMPResponseError, +) =20 =20 def perm(arr): @@ -102,7 +105,7 @@ def command(self, cmd): reply =3D json.loads(subprocess.check_output(ar)) =20 if 'error' in reply: - raise MonitorResponseError(reply) + raise QMPResponseError(reply) =20 return reply['return'] =20 --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204187; cv=none; d=zohomail.com; s=zohoarc; b=MbtQmnhhvhGOPxKE8PfG8Q3YLEMy7tsxARFiyrvVez3j6YCvuNtrQYpAk2OVWIXxvhkbHkPwCyUKHS1uqk9ri86Venq5ku8iJ8AXedm588LN2RRaoGMu7jIoymDrEilsVLb6N2NFwxWyjzl75tLDORyGxHxpXyhq4xX0rNAqFkg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204187; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=oNhPhaxByI3BTfZupYYGTKC0C0PPGE+d9nTXAs0uWH4=; b=S6Now94qFVvof8Um1dUlHCyAgVkJgfQm04r0tSjoAhv+yrK8G25wvzG4XDtUpzdmmlVvlfg2PM0k+hcswv5Pez+vR4pPXis+6LvurEbG/UgaxOztPQ7CkcH/XIndIbPVTh22lu0r3P3aCIWMgkw71Kt/bLS5f6vHGtyEg8BT0/A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204187410477.09702971855154; Fri, 26 Jun 2020 13:43:07 -0700 (PDT) Received: from localhost ([::1]:59940 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovBi-0008NL-1x for importer@patchew.org; Fri, 26 Jun 2020 16:43:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33698) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAQ-0006cm-6N for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:46 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:54378 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAN-0005oB-9S for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:45 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-159-P_zF_z9WOnyt823INcb2DA-1; Fri, 26 Jun 2020 16:41:40 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C0149BFC1; Fri, 26 Jun 2020 20:41:39 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id EC26F19C58; Fri, 26 Jun 2020 20:41:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204102; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oNhPhaxByI3BTfZupYYGTKC0C0PPGE+d9nTXAs0uWH4=; b=U1eM6dGMYAjSKl7l0fJzVV69NTWl1XZZKAxNVKhCu1hkVotTomRbal4qJJBL1b0otXCOfM 5MleoDFTzk7PJJNDcvDUt+nqg7FtI/gPsPQ81UUGGDRohsAane/dGn774e0/1GP6m0dtWg UxQU2ff/TNh04TNv4I0TgDp7vHtFC2I= X-MC-Unique: P_zF_z9WOnyt823INcb2DA-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 04/16] python/qmp.py: Do not return None from cmd_obj Date: Fri, 26 Jun 2020 16:41:21 -0400 Message-Id: <20200626204133.14500-5-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=jsnow@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 15:42:27 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This makes typing the qmp library difficult, as it necessitates wrapping Optional[] around the type for every return type up the stack. At some point, it becomes difficult to discern or remember why it's None instead of the expected object. Use the python exception system to tell us exactly why we didn't get an object. Remove this special-cased return. Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- python/qemu/qmp.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index aa8a666b8a..ef3c919b76 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -225,22 +225,18 @@ def accept(self, timeout=3D15.0): self.__sockfile =3D self.__sock.makefile(mode=3D'r') return self.__negotiate_capabilities() =20 - def cmd_obj(self, qmp_cmd): + def cmd_obj(self, qmp_cmd: QMPMessage) -> QMPMessage: """ Send a QMP command to the QMP Monitor. =20 @param qmp_cmd: QMP command to be sent as a Python dict - @return QMP response as a Python dict or None if the connection has - been closed + @return QMP response as a Python dict """ self.logger.debug(">>> %s", qmp_cmd) - try: - self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8')) - except OSError as err: - if err.errno =3D=3D errno.EPIPE: - return None - raise err + self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8')) resp =3D self.__json_read() + if resp is None: + raise QMPConnectError("Unexpected empty reply from server") self.logger.debug("<<< %s", resp) return resp =20 --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204486; cv=none; d=zohomail.com; s=zohoarc; b=W7xV0cSV3q7Niu5mnmdV4kY0+hP+TyfD9QRJIsn5Uso6lla845QSObIFwq3qmHpXY5bF59ymILdOugKVdK380w9/2Abb1bdDgg0u76VUQyIeV0f46kXnjl/KOblYkLbyIaQqfV3TY9chASwPiIQAULhYtV8qmewvMefHCY7EHic= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204486; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=gqk7ovbcZenmmjkGeGLVpvHvzhizoBYcL+O5A5dVKHI=; b=QtnC/4+LHCpqBloQDajHPGvZmfACP3NEWT7zYwVS5NpZ2G49PA2BnvWL6RmZlRzydVUt4aB2T23a44sOImY21s1q1LX2lGJtaPmuogOZ0tgGT19vGToJwGSL7UiAcfa2+tCq95C/bcZmP7UVcu/foAqz7Qti2ViwRL35F1vDVpc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204486561478.83580223713795; Fri, 26 Jun 2020 13:48:06 -0700 (PDT) Received: from localhost ([::1]:48434 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovGX-00072B-9H for importer@patchew.org; Fri, 26 Jun 2020 16:48:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33764) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAX-0006sz-EF for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:53 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:49342 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAV-0005po-Jo for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:53 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-311-tUvaUqc-OSm0OFb4iS3uag-1; Fri, 26 Jun 2020 16:41:49 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4BA6918585BA; Fri, 26 Jun 2020 20:41:48 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id E1C8319C58; Fri, 26 Jun 2020 20:41:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204111; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gqk7ovbcZenmmjkGeGLVpvHvzhizoBYcL+O5A5dVKHI=; b=jG5uf58XoX8arOzwk/gVEOWfNOrrT62LWQk8tVVhsJFWiqcgn4caoHX5lLCd5MTkknPIXd YOSGnNBxyedEUQGkGgRCcRB9vmFaEn3wYgwipCV/FF7bCs/tw+9LdZtg2IIMnutRGz2v3Y +NbNAuoVgSFpFEveo3D8TzV2DFa3Oqg= X-MC-Unique: tUvaUqc-OSm0OFb4iS3uag-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 05/16] python/qmp.py: add casts to JSON deserialization Date: Fri, 26 Jun 2020 16:41:22 -0400 Message-Id: <20200626204133.14500-6-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 03:23:21 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" mypy and python type hints are not powerful enough to properly describe JSON messages in Python 3.6. The best we can do, generally, is describe them as Dict[str, Any]. Add casts to coerce this type for static analysis; but do NOT enforce this type at runtime in any way. Note: Python 3.8 adds a TypedDict construct which allows for the description of more arbitrary Dictionary shapes. There is a third-party module, "Pydantic", which is compatible with 3.6 that can be used instead of the JSON library that parses JSON messages to fully-typed Python objects, and may be preferable in some cases. (That is well beyond the scope of this commit or series.) Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- python/qemu/qmp.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index ef3c919b76..1ae36050a4 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -13,6 +13,7 @@ import logging from typing import ( Any, + cast, Dict, Optional, TextIO, @@ -130,7 +131,10 @@ def __json_read(self, only_event=3DFalse): data =3D self.__sockfile.readline() if not data: return None - resp =3D json.loads(data) + # By definition, any JSON received from QMP is a QMPMessage, + # and we are asserting only at static analysis time that it + # has a particular shape. + resp: QMPMessage =3D json.loads(data) if 'event' in resp: self.logger.debug("<<< %s", resp) self.__events.append(resp) @@ -262,7 +266,7 @@ def command(self, cmd, **kwds): ret =3D self.cmd(cmd, kwds) if 'error' in ret: raise QMPResponseError(ret) - return ret['return'] + return cast(QMPReturnValue, ret['return']) =20 def pull_event(self, wait=3DFalse): """ --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204365; cv=none; d=zohomail.com; s=zohoarc; b=KSce8fjnnMwhfBJxWueic6gKLvsjnEpMfTkCpqOtExxMODZx13be22htQcbxxnnD3sQvsB9dDKFjDthxp04Jpo7DgupxIpNJC1I+cXwnwdW5Kgb56C+HrV+RPD8vvqaYnArzlgS+GWEbxLJ0rnAb/uvzmIp/TbFLIWJmkVSKqbk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204365; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=JbxQguf5/Qcyyylen1TvxegiZi7WDnWU8Sf2ZEcWEVg=; b=aldutQbZG06h5B1cLK13qekN2yJIP03M5sUTgvLtkRJS0fLe38JpHUnisnC+JO176h8VT75Gd1ONBCsN3g+feSXY20B/5O4uAqK86NhMmooFll2Dkr49HyJLDWWha9z6738GLHfXQH6NsOXgaqrwJjV8eGZUQkgpRNZCNqi2S+I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204365968412.8438310024037; Fri, 26 Jun 2020 13:46:05 -0700 (PDT) Received: from localhost ([::1]:41718 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovEa-00045y-Kd for importer@patchew.org; Fri, 26 Jun 2020 16:46:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33844) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAa-0006yT-5M for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:56 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:33029 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAW-0005px-N7 for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:55 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-164-sBFdQPrkNwKCjvCYkLNDJQ-1; Fri, 26 Jun 2020 16:41:50 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4BFA31005512; Fri, 26 Jun 2020 20:41:49 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 709AB19C58; Fri, 26 Jun 2020 20:41:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204111; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JbxQguf5/Qcyyylen1TvxegiZi7WDnWU8Sf2ZEcWEVg=; b=Q0s6CJF2g30yUAtYgzIEdlCfhAcNK9ZSIz73TdzkQvJ/zJO6pnw2uV5mXnOCCAOMx5uqA6 qyAWTcDppa/wzVGClA3ojJ5YtxQ7bG1detnbx2ySrNrZbOvdzewX8st4X54uG7yOD0YAlR JvSmXP7JepZmWDZPqBqfn7g8U4vqd6U= X-MC-Unique: sBFdQPrkNwKCjvCYkLNDJQ-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 06/16] python/qmp.py: add QMPProtocolError Date: Fri, 26 Jun 2020 16:41:23 -0400 Message-Id: <20200626204133.14500-7-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.120; envelope-from=jsnow@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 01:55:55 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) In the case that we receive a reply but are unable to understand it, use this exception name to indicate that case. Signed-off-by: John Snow Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Kevin Wolf --- python/qemu/qmp.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index 1ae36050a4..7935dababb 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -62,6 +62,12 @@ class QMPTimeoutError(QMPError): """ =20 =20 +class QMPProtocolError(QMPError): + """ + QMP protocol error; unexpected response + """ + + class QMPResponseError(QMPError): """ Represents erroneous QMP monitor reply @@ -266,6 +272,10 @@ def command(self, cmd, **kwds): ret =3D self.cmd(cmd, kwds) if 'error' in ret: raise QMPResponseError(ret) + if 'return' not in ret: + raise QMPProtocolError( + "'return' key not found in QMP response '{}'".format(str(r= et)) + ) return cast(QMPReturnValue, ret['return']) =20 def pull_event(self, wait=3DFalse): --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204220; cv=none; d=zohomail.com; s=zohoarc; b=b4vYY12I8MBF2Vp3Vf3NVc4S5NQcX9eDwJsxWO2xjFxs5g0UFMSSr9n9M5Ugfqvruys2HFA4tQ2rvDazd0zjCL+WPSQWWQf2iBNN5EHCHwEaxyDQB2KWlpCR7assiHuWz1AaiICuvM/KFKpe1sai8fAL+rm+X8kVoXgnsfVINhU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204220; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=E2oICUjh28BTkC7WVWvyElOa/ZeTkgk5t3HbNOETuqY=; b=Vrv9HbWTWRZOCWchS4xg7rEbUbsglWp81cHef3/V6Slk+gWqKHSx/GM9ieLX21Fg6CB+7xRXkBBnjPVA7F6tRcAmJ5c0Xj0eiwbwjMiZ6LTHjXebH9fMY/GlWyWCJrWzMms1Bxi9k3KhCwYNZI2KfCtRa1V6c/KD1pGE+BW1rws= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204220337404.1412677788386; Fri, 26 Jun 2020 13:43:40 -0700 (PDT) Received: from localhost ([::1]:32962 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovCF-0000XM-32 for importer@patchew.org; Fri, 26 Jun 2020 16:43:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33852) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAa-0006zg-K5 for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:56 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:54430 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAY-0005qN-5Z for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:56 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-47-CiMx6lsAPaO62CKiIpNGtw-1; Fri, 26 Jun 2020 16:41:51 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4575E107ACCA; Fri, 26 Jun 2020 20:41:50 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6F0C019C58; Fri, 26 Jun 2020 20:41:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204113; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E2oICUjh28BTkC7WVWvyElOa/ZeTkgk5t3HbNOETuqY=; b=ZV1L0bK5BINx9him6v26Ou+5ccqw7D+pcsExIPNwqVwaqyjkWkg4g7+rZNITGsvQfJde0e pPtNEBeD2a4OralHVfrMtTm1NQUQxsLvFIsDHjG4p8v409wbd/KuIx4S4yU7/UHxC5oKLd 9amLMpkQWEpxZvE/lxfKgsU2l87Z7VQ= X-MC-Unique: CiMx6lsAPaO62CKiIpNGtw-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 07/16] python/machine.py: Fix monitor address typing Date: Fri, 26 Jun 2020 16:41:24 -0400 Message-Id: <20200626204133.14500-8-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=jsnow@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 15:42:27 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Prior to this, it's difficult for mypy to intuit what the concrete type of the monitor address is; it has difficulty inferring the type across two variables. Create _monitor_address as a property that always returns a valid address to simplify static type analysis. To preserve our ability to clean up, use a simple boolean to indicate whether or not we should try to clean up the sock file after execution. Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- python/qemu/machine.py | 45 +++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index a5a579095b..6d38147cd5 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -25,10 +25,14 @@ import signal import socket import tempfile -from typing import Optional, Type +from typing import ( + Optional, + Type, +) from types import TracebackType =20 from . import qmp +from .qmp import SocketAddrT =20 LOG =3D logging.getLogger(__name__) =20 @@ -62,7 +66,8 @@ class QEMUMachine: """ =20 def __init__(self, binary, args=3DNone, wrapper=3DNone, name=3DNone, - test_dir=3D"/var/tmp", monitor_address=3DNone, + test_dir=3D"/var/tmp", + monitor_address: Optional[SocketAddrT] =3D None, socket_scm_helper=3DNone, sock_dir=3DNone): ''' Initialize a QEMUMachine @@ -85,8 +90,14 @@ def __init__(self, binary, args=3DNone, wrapper=3DNone, = name=3DNone, if sock_dir is None: sock_dir =3D test_dir self._name =3D name - self._monitor_address =3D monitor_address - self._vm_monitor =3D None + if monitor_address is not None: + self._monitor_address =3D monitor_address + self._remove_monitor_sockfile =3D False + else: + self._monitor_address =3D os.path.join( + sock_dir, f"{name}-monitor.sock" + ) + self._remove_monitor_sockfile =3D True self._qemu_log_path =3D None self._qemu_log_file =3D None self._popen =3D None @@ -225,15 +236,17 @@ def _load_io_log(self): =20 def _base_args(self): args =3D ['-display', 'none', '-vga', 'none'] + if self._qmp_set: if isinstance(self._monitor_address, tuple): - moncdev =3D "socket,id=3Dmon,host=3D%s,port=3D%s" % ( - self._monitor_address[0], - self._monitor_address[1]) + moncdev =3D "socket,id=3Dmon,host=3D{},port=3D{}".format( + *self._monitor_address + ) else: - moncdev =3D 'socket,id=3Dmon,path=3D%s' % self._vm_monitor + moncdev =3D f"socket,id=3Dmon,path=3D{self._monitor_addres= s}" args.extend(['-chardev', moncdev, '-mon', 'chardev=3Dmon,mode=3Dcontrol']) + if self._machine is not None: args.extend(['-machine', self._machine]) for _ in range(self._console_index): @@ -258,14 +271,14 @@ def _pre_launch(self): self._qemu_log_file =3D open(self._qemu_log_path, 'wb') =20 if self._qmp_set: - if self._monitor_address is not None: - self._vm_monitor =3D self._monitor_address - else: - self._vm_monitor =3D os.path.join(self._sock_dir, - self._name + "-monitor.soc= k") - self._remove_files.append(self._vm_monitor) - self._qmp =3D qmp.QEMUMonitorProtocol(self._vm_monitor, server= =3DTrue, - nickname=3Dself._name) + if self._remove_monitor_sockfile: + assert isinstance(self._monitor_address, str) + self._remove_files.append(self._monitor_address) + self._qmp =3D qmp.QEMUMonitorProtocol( + self._monitor_address, + server=3DTrue, + nickname=3Dself._name + ) =20 def _post_launch(self): if self._qmp: --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204360; cv=none; d=zohomail.com; s=zohoarc; b=LrEBSzx83mtcPV9hmuqNIyA1De+oL27HGjQA3AI4iAshV46A8JU3b/ukrei2cCA2PRb9neP6g6Sqek3OLkHGscMKnOKYpKaySFSMe6WLE5GnxtYpauD5wLWj/7k5KTQ0B1I2BiXzr/2sR+nRUJyzATeNLJgmXt7YvydV/DlIItg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204360; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=m5FtFcNMJsPCBRXp0TnZIAiThT8ctXMzCke7CUDFGYk=; b=fFg3D5dRaq9mifVEN5nPhcg31t02CchmOjfGFehUVd79skGizfaPCusr/vZ2h7MU+A4p//y1G1bQ3oNGRnq10YztLunSM97k4ohaeGqPiCNmAADhbBnRJkKAZYqSxBJLkJuzyrN8dGXFHU3AzeYkA/R0IH9kfYnYnlvjpshY52k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204360590216.70148866904685; Fri, 26 Jun 2020 13:46:00 -0700 (PDT) Received: from localhost ([::1]:41376 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovEV-0003xl-7I for importer@patchew.org; Fri, 26 Jun 2020 16:45:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33864) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAb-00070b-4N for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:57 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:37799 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAY-0005qa-Py for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:56 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-215-zrBls-LeM9KQnFwsO5Bvkg-1; Fri, 26 Jun 2020 16:41:52 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3C64F800FEC; Fri, 26 Jun 2020 20:41:51 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 691E419C58; Fri, 26 Jun 2020 20:41:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m5FtFcNMJsPCBRXp0TnZIAiThT8ctXMzCke7CUDFGYk=; b=STSFHzJA9pLBwfopfBhazgGv+nzAuoY15HmhDce+vOwURTZGdFDBrtamH1wqRuBsekuv2z ZB9N/G/0gydN3ZxIO7gaM2zR1Bl8om7cRfdt27Dp2BqX/Ju6N0HmUA2s569Vt7obvD86Pt Yq1H4uTcDISJZI/KWx1TctbjnB82SOw= X-MC-Unique: zrBls-LeM9KQnFwsO5Bvkg-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 08/16] python/machine.py: reorder __init__ Date: Fri, 26 Jun 2020 16:41:25 -0400 Message-Id: <20200626204133.14500-9-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=jsnow@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 15:42:27 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Put the init arg handling all at the top, and mostly in order (deviating when one is dependent on another), and put what is effectively runtime state declaration at the bottom. Signed-off-by: John Snow Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Kevin Wolf --- python/qemu/machine.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 6d38147cd5..36812bd2be 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -81,38 +81,43 @@ def __init__(self, binary, args=3DNone, wrapper=3DNone,= name=3DNone, @param socket_scm_helper: helper program, required for send_fd_scm= () @note: Qemu process is not started until launch() is used. ''' + # Direct user configuration + + self._binary =3D binary + if args is None: args =3D [] + # Copy mutable input: we will be modifying our copy + self._args =3D list(args) + if wrapper is None: wrapper =3D [] - if name is None: - name =3D "qemu-%d" % os.getpid() - if sock_dir is None: - sock_dir =3D test_dir - self._name =3D name + self._wrapper =3D wrapper + + self._name =3D name or "qemu-%d" % os.getpid() + self._test_dir =3D test_dir + self._sock_dir =3D sock_dir or self._test_dir + self._socket_scm_helper =3D socket_scm_helper + if monitor_address is not None: self._monitor_address =3D monitor_address self._remove_monitor_sockfile =3D False else: self._monitor_address =3D os.path.join( - sock_dir, f"{name}-monitor.sock" + self._sock_dir, f"{self._name}-monitor.sock" ) self._remove_monitor_sockfile =3D True + + # Runstate self._qemu_log_path =3D None self._qemu_log_file =3D None self._popen =3D None - self._binary =3D binary - self._args =3D list(args) # Force copy args in case we modify = them - self._wrapper =3D wrapper self._events =3D [] self._iolog =3D None - self._socket_scm_helper =3D socket_scm_helper self._qmp_set =3D True # Enable QMP monitor by default. self._qmp =3D None self._qemu_full_args =3D None - self._test_dir =3D test_dir self._temp_dir =3D None - self._sock_dir =3D sock_dir self._launched =3D False self._machine =3D None self._console_index =3D 0 --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204352; cv=none; d=zohomail.com; s=zohoarc; b=e9SZ6kDdw/eYIXcK1FGDhTiKNdMQqty1pgYO1x1C1qFFlD7nGFgdCJNbTGM31gD83c9bSrgZhAnQH86rvIFb1Xfj5DJ3ANSrP3K2at0QNzuUOH3m845AAjDC00If3jkkLArHDHu6YfQp32+k8RS2ieUHFZQObm9fqgpgbt2kcqo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204352; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=dJ089/OA581TqlfhtJjck/toUDx4MtPcxO/s6iUB1D4=; b=nQHvx6aAyogNDZN1HHK7bNkwnU22NjmwqYeEgbZx7o2LKhxN2BUeyAejxkQjRR3v8rhq+cBwnJbimaVEZ3WblD4qg7zjCSpxgoZn1TyL3GG+KYr5AnivXmtvI0RMiuClLhB9tCErJ/dBs33Aq5PkJrKIh2bdVgb2S9gHw3epSNw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204352077301.4730906082083; Fri, 26 Jun 2020 13:45:52 -0700 (PDT) Received: from localhost ([::1]:41300 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovEM-0003vr-Ss for importer@patchew.org; Fri, 26 Jun 2020 16:45:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33890) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAc-00074Y-Qk for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:58 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:56843 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAa-0005rH-PU for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:41:58 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-376-QEt9sqRaMHiyST2j_sKV1w-1; Fri, 26 Jun 2020 16:41:54 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7C664800FF6; Fri, 26 Jun 2020 20:41:53 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5E83619C58; Fri, 26 Jun 2020 20:41:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204116; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dJ089/OA581TqlfhtJjck/toUDx4MtPcxO/s6iUB1D4=; b=jUTvzLyvHrRw8LBY43L0yTQ6t1mhEIF7ZV5Z3UWpwGbFv4FqqIGAnK+M6IMsGGgqMtrxAy KPkALZ9A7mbJzdTNEuHseweM6WGjqNy6hSMt+kpDcBj4MNADkG4FD1D1CNfmgDibMwZ2kL 8XvM29bRwMWzAEHxvX+z+2aaIQvpPZ8= X-MC-Unique: QEt9sqRaMHiyST2j_sKV1w-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 09/16] python/machine.py: Don't modify state in _base_args() Date: Fri, 26 Jun 2020 16:41:26 -0400 Message-Id: <20200626204133.14500-10-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.120; envelope-from=jsnow@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 01:55:55 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Don't append to the _remove_files list during _base_args; instead do so during _launch. Rework _base_args as a @property to help facilitate this impression. This has the additional benefit of making the type of _console_address easier to analyze statically. Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- python/qemu/machine.py | 16 ++++++++++------ python/qemu/qtest.py | 11 ++++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 36812bd2be..8eb10a4bc1 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -26,6 +26,7 @@ import socket import tempfile from typing import ( + List, Optional, Type, ) @@ -123,7 +124,9 @@ def __init__(self, binary, args=3DNone, wrapper=3DNone,= name=3DNone, self._console_index =3D 0 self._console_set =3D False self._console_device_type =3D None - self._console_address =3D None + self._console_address =3D os.path.join( + self._sock_dir, f"{self._name}-console.sock" + ) self._console_socket =3D None self._remove_files =3D [] self._killed =3D False @@ -239,7 +242,8 @@ def _load_io_log(self): with open(self._qemu_log_path, "r") as iolog: self._iolog =3D iolog.read() =20 - def _base_args(self): + @property + def _base_args(self) -> List[str]: args =3D ['-display', 'none', '-vga', 'none'] =20 if self._qmp_set: @@ -257,9 +261,6 @@ def _base_args(self): for _ in range(self._console_index): args.extend(['-serial', 'null']) if self._console_set: - self._console_address =3D os.path.join(self._sock_dir, - self._name + "-console.so= ck") - self._remove_files.append(self._console_address) chardev =3D ('socket,id=3Dconsole,path=3D%s,server,nowait' % self._console_address) args.extend(['-chardev', chardev]) @@ -275,6 +276,9 @@ def _pre_launch(self): self._qemu_log_path =3D os.path.join(self._temp_dir, self._name + = ".log") self._qemu_log_file =3D open(self._qemu_log_path, 'wb') =20 + if self._console_set: + self._remove_files.append(self._console_address) + if self._qmp_set: if self._remove_monitor_sockfile: assert isinstance(self._monitor_address, str) @@ -349,7 +353,7 @@ def _launch(self): devnull =3D open(os.path.devnull, 'rb') self._pre_launch() self._qemu_full_args =3D (self._wrapper + [self._binary] + - self._base_args() + self._args) + self._base_args + self._args) LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args)) self._popen =3D subprocess.Popen(self._qemu_full_args, stdin=3Ddevnull, diff --git a/python/qemu/qtest.py b/python/qemu/qtest.py index 888c8bd2f6..05c63a1d58 100644 --- a/python/qemu/qtest.py +++ b/python/qemu/qtest.py @@ -19,7 +19,11 @@ =20 import socket import os -from typing import Optional, TextIO +from typing import ( + List, + Optional, + TextIO, +) =20 from .machine import QEMUMachine =20 @@ -111,8 +115,9 @@ def __init__(self, binary, args=3DNone, name=3DNone, te= st_dir=3D"/var/tmp", self._qtest =3D None self._qtest_path =3D os.path.join(sock_dir, name + "-qtest.sock") =20 - def _base_args(self): - args =3D super()._base_args() + @property + def _base_args(self) -> List[str]: + args =3D super()._base_args args.extend(['-qtest', 'unix:path=3D' + self._qtest_path, '-accel', 'qtest']) return args --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204546; cv=none; d=zohomail.com; s=zohoarc; b=S3T2wLYVtoIcdS6p9MwtU61018s0MExKHDFTNAOaqhEesaYHCFTvN9dDsoyNFpar7vW6BV7NTu2dEHTu4Oog9OVChvs76GKORjiRE8gH+KDzy9Lk1vrE71+foI4+3rqR7ZFGZU5fMT8YWgOw1i/9idGKG4XCMPJWwtvFRATqB/k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204546; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=VGhfiTXbiXd+kEnKFvL/DKq9TUWbI6mf0WxW/E8MJ8E=; b=ck1JeWfW6IW/e5iMwC/6fPzVcL7Zjsfqrpu6bCoweCy7MZ291tREXwvSHyPAQuD2spe/gbeE6v7cUCSBttIIRokON9MsMS6mOBgGDVmMjru3f9EomFXl5IRHUH1uSGLiBS0jAQovCTHbUYECDTmk9WgS5S3ZirJzJIwDDAg5kWk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204546109864.4226757366132; Fri, 26 Jun 2020 13:49:06 -0700 (PDT) Received: from localhost ([::1]:52320 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovHU-00009J-Ok for importer@patchew.org; Fri, 26 Jun 2020 16:49:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33928) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAf-00078G-3M for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:01 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:22033 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAc-0005rm-0T for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:00 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-223-UiOS-Z6LNuSRkgzxosSuCQ-1; Fri, 26 Jun 2020 16:41:55 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 91559107ACCA; Fri, 26 Jun 2020 20:41:54 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9C5F219C58; Fri, 26 Jun 2020 20:41:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204117; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VGhfiTXbiXd+kEnKFvL/DKq9TUWbI6mf0WxW/E8MJ8E=; b=Cv5qGzMkQvRNKXBRDYQGvLzQvnA2na2pCqFmPjoLjY/ofaVAycsdDivxuD2N6Z+nXz5rey FxVC4plzFTr4OmtmT1dGoYfZf4zkQjiPxCGzPqWeAAKjbn/JwpoPfBXUNTvuG7N01Mwro/ LTn543pVP7I8YVC1Sl25efaN5+ytk/w= X-MC-Unique: UiOS-Z6LNuSRkgzxosSuCQ-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 10/16] python/machine.py: Handle None events in events_wait Date: Fri, 26 Jun 2020 16:41:27 -0400 Message-Id: <20200626204133.14500-11-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 03:23:21 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" If the timeout is 0, we can get None back. Handle this explicitly. Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- python/qemu/machine.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 8eb10a4bc1..f65bd18c1a 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -33,7 +33,7 @@ from types import TracebackType =20 from . import qmp -from .qmp import SocketAddrT +from .qmp import SocketAddrT, QMPMessage =20 LOG =3D logging.getLogger(__name__) =20 @@ -552,13 +552,20 @@ def event_wait(self, name, timeout=3D60.0, match=3DNo= ne): =20 def events_wait(self, events, timeout=3D60.0): """ - events_wait waits for and returns a named event - from QMP with a timeout. + events_wait waits for and returns a single named event from QMP. + In the case of multiple qualifying events, this function returns t= he + first one. =20 - events: a sequence of (name, match_criteria) tuples. - The match criteria are optional and may be None. - See event_match for details. - timeout: QEMUMonitorProtocol.pull_event timeout parameter. + :param events: A sequence of (name, match_criteria) tuples. + The match criteria are optional and may be None. + See event_match for details. + :param timeout: Optional timeout, in seconds. + See QEMUMonitorProtocol.pull_event. + + :raise QMPTimeoutError: If timeout was non-zero and no matching ev= ents + were found. + :return: A QMP event matching the filter criteria. + If timeout was 0 and no event matched, None. """ def _match(event): for name, match in events: @@ -566,6 +573,8 @@ def _match(event): return True return False =20 + event: Optional[QMPMessage] + # Search cached events for event in self._events: if _match(event): @@ -575,6 +584,10 @@ def _match(event): # Poll for new events while True: event =3D self._qmp.pull_event(wait=3Dtimeout) + if event is None: + # NB: None is only returned when timeout is false-ish. + # Timeouts raise QMPTimeoutError instead! + break if _match(event): return event self._events.append(event) --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204731; cv=none; d=zohomail.com; s=zohoarc; b=UOJReqg6oagd90bsruF9J6kpLL0G0qTRZJk74cZGoQYEoySNlRVAe73XaDPvgd1JbF2ub/6Cog1mPEuwlHm2UIZTWUQY4jh+KIjguWpMvBYdSYXTIeSgZNSRm+8XTrJs9HkbxOzIPp3PX8Ozwj6xKReRpQ8qGHFvVLmYCTV630w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204731; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Z+wqnE/KuG6Jhv7usc8rSQ03bDjzTNR3cIV4s++VyzA=; b=Ym3dEpApQCtNtRqq+1sWDZ45oHSGx7B9/xucIDu2fFRCIPrTMGQNy/c9dp5zZDFDF0Z9PSoOGFunn/+uEhlhO0qBeC+7B/NqtNtVZ22IPFGz/9Mdku6PINbpxzO523RaDT7U71WINT/GdTWdpyeb/5DqdEGOerXYED3VaxztqIc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204731884802.4780225000505; Fri, 26 Jun 2020 13:52:11 -0700 (PDT) Received: from localhost ([::1]:35316 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovKU-0004y7-HK for importer@patchew.org; Fri, 26 Jun 2020 16:52:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34072) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAq-0007Fz-6c for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:12 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:56701 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAj-0005to-IE for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:11 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-115-C4n4mccIOtCz-n5KQLtKDw-1; Fri, 26 Jun 2020 16:41:56 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 859F318585AA; Fri, 26 Jun 2020 20:41:55 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id B3E1619C58; Fri, 26 Jun 2020 20:41:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204123; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z+wqnE/KuG6Jhv7usc8rSQ03bDjzTNR3cIV4s++VyzA=; b=Uc3q1w4/G1TtqSLnQOH7ho9sJUjAWAlFrdTikerYxo3xR3xEj9Mf/0O5cAsXwLRu6Z7yrd Y9qr5axyY5CqNd0Sz3skiUXQ07PLXIwzZNUgyaPKoWOCGBMp93lhsSziWkRachSx+dCka+ nNqEyMh8nL49QMju3HoycfZIYkXEXco= X-MC-Unique: C4n4mccIOtCz-n5KQLtKDw-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 11/16] python/machine.py: use qmp.command Date: Fri, 26 Jun 2020 16:41:28 -0400 Message-Id: <20200626204133.14500-12-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=jsnow@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 15:42:27 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" machine.py and qmp.py both do the same thing here; refactor machine.py to use qmp.py's functionality more directly. Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- python/qemu/machine.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index f65bd18c1a..9bb5d26407 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -26,6 +26,8 @@ import socket import tempfile from typing import ( + Any, + Dict, List, Optional, Type, @@ -463,17 +465,23 @@ def set_qmp_monitor(self, enabled=3DTrue): self._qmp_set =3D False self._qmp =3D None =20 - def qmp(self, cmd, conv_keys=3DTrue, **args): - """ - Invoke a QMP command and return the response dict - """ + @classmethod + def _qmp_args(cls, _conv_keys: bool =3D True, **args: Any) -> Dict[str= , Any]: qmp_args =3D dict() for key, value in args.items(): - if conv_keys: + if _conv_keys: qmp_args[key.replace('_', '-')] =3D value else: qmp_args[key] =3D value + return qmp_args =20 + def qmp(self, cmd: str, + conv_keys: bool =3D True, + **args: Any) -> QMPMessage: + """ + Invoke a QMP command and return the response dict + """ + qmp_args =3D self._qmp_args(conv_keys, **args) return self._qmp.cmd(cmd, args=3Dqmp_args) =20 def command(self, cmd, conv_keys=3DTrue, **args): @@ -482,12 +490,8 @@ def command(self, cmd, conv_keys=3DTrue, **args): On success return the response dict. On failure raise an exception. """ - reply =3D self.qmp(cmd, conv_keys, **args) - if reply is None: - raise qmp.QMPError("Monitor is closed") - if "error" in reply: - raise qmp.QMPResponseError(reply) - return reply["return"] + qmp_args =3D self._qmp_args(conv_keys, **args) + return self._qmp.command(cmd, **qmp_args) =20 def get_qmp_event(self, wait=3DFalse): """ --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204711; cv=none; d=zohomail.com; s=zohoarc; b=gQ++X6lxCp3x6iDb3+GZC7hgZBZae5fp/9kKYhSUdWRof2IKvireg2inJ2EqZa38/vtw5vVH9BVHREuV7YIpMR3mvXlP8vWwxaBnpb4148ltmJp+Ugux3JSXpXYiya7Memfl6hQSRqVs5hDa07iu2BcMLRFgjaMSBCf+fFLGs+w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204711; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HqqNxG5KpI0efHrqCewjCVxiWjh+EBq81VttRszc3tE=; b=ArkC7tZg/52tYpOIskjusFlG/zpqRQt0R3KqmM8lsVZ6wVlVcszk/ufwhcsILy8Vizu1zlHvsOvRJ9xvrIlBpLW1AkOjBzj7KjIYTDB4JreA0QPRSBaiR6qgG+2LdirrjizXb7ALVvFUvVOhCq9AFLeTytCEd5iD/83wtX7WURE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204711015677.6773630163763; Fri, 26 Jun 2020 13:51:51 -0700 (PDT) Received: from localhost ([::1]:34042 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovK9-0004Qz-JS for importer@patchew.org; Fri, 26 Jun 2020 16:51:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34084) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAq-0007GA-KT for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:13 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:52852 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAf-0005t0-MI for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:12 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-26-02R0kKfAMTyZ1Ma_FDn7ug-1; Fri, 26 Jun 2020 16:41:57 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8080B107ACCD; Fri, 26 Jun 2020 20:41:56 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id A690B19C58; Fri, 26 Jun 2020 20:41:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204120; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HqqNxG5KpI0efHrqCewjCVxiWjh+EBq81VttRszc3tE=; b=U+Gs9oGAjIoKhqbO0a3/iI5HJJNmrjq1YZIq9n+ZqXqOi+9NAgw2vbY7ZBzm4yUZnSMBk0 1+3ZdruNOIpzLCyALQco0R2qDvQe5SSKjEtI2TipQ66MBz073Ie6uFyCrfqIsHROIZkWFK CFgcvh2nQzN6k5H7EjWCF+mdIMk02DI= X-MC-Unique: 02R0kKfAMTyZ1Ma_FDn7ug-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 12/16] python/machine.py: Add _qmp access shim Date: Fri, 26 Jun 2020 16:41:29 -0400 Message-Id: <20200626204133.14500-13-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.120; envelope-from=jsnow@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 01:55:55 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Like many other Optional[] types, it's not always a given that this object will be set. Wrap it in a type-shim that raises a meaningful error and will always return a concrete type. Signed-off-by: John Snow --- python/qemu/machine.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 9bb5d26407..cb3db1f66a 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -118,7 +118,7 @@ def __init__(self, binary, args=3DNone, wrapper=3DNone,= name=3DNone, self._events =3D [] self._iolog =3D None self._qmp_set =3D True # Enable QMP monitor by default. - self._qmp =3D None + self._qmp_connection: Optional[qmp.QEMUMonitorProtocol] =3D None self._qemu_full_args =3D None self._temp_dir =3D None self._launched =3D False @@ -285,14 +285,14 @@ def _pre_launch(self): if self._remove_monitor_sockfile: assert isinstance(self._monitor_address, str) self._remove_files.append(self._monitor_address) - self._qmp =3D qmp.QEMUMonitorProtocol( + self._qmp_connection =3D qmp.QEMUMonitorProtocol( self._monitor_address, server=3DTrue, nickname=3Dself._name ) =20 def _post_launch(self): - if self._qmp: + if self._qmp_connection: self._qmp.accept() =20 def _post_shutdown(self) -> None: @@ -370,7 +370,7 @@ def wait(self): Wait for the VM to power off """ self._popen.wait() - if self._qmp: + if self._qmp_connection: self._qmp.close() self._post_shutdown() =20 @@ -459,11 +459,13 @@ def set_qmp_monitor(self, enabled=3DTrue): line. Default is True. @note: call this function before launch(). """ - if enabled: - self._qmp_set =3D True - else: - self._qmp_set =3D False - self._qmp =3D None + self._qmp_set =3D enabled + + @property + def _qmp(self) -> qmp.QEMUMonitorProtocol: + if self._qmp_connection is None: + raise QEMUMachineError("Attempt to access QMP with no connecti= on") + return self._qmp_connection =20 @classmethod def _qmp_args(cls, _conv_keys: bool =3D True, **args: Any) -> Dict[str= , Any]: --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204504; cv=none; d=zohomail.com; s=zohoarc; b=b8vCVHK69tMgRKMnPwxxMrkw9j+mzV0yPHyfBenTQmIBm/yKcg6b+900R/Pa5I6didnVb9pLms6ATyxVBi9x1alKL5sqaTh13nkyVjBl873xlFfMPqFJugRgM9Za2S+pz41o6Vx/hzHeMT5PHGQZ+6cEQ3e7EoHWAtSn2Mkpxjw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204504; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=BgPXhXn50eUAAS9+s9eH+67pArlk591EX0K8bG86+G0=; b=TDfdkoLhkpmw/wTI/ZXWXaqX/tKa0aNNIg0dzWvR4qs8V5C5C+Ru5hjDg5t2tWjbBBdQkKn3mRH8Zccw/zINPCVaEqYJPywtgm904JNdh3jpquOhT2KxoNVIt6x245rb5o29jn7IACKtmSbIT24KgN0J7gI5Uw+Xqm1tkMC507w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204504128692.2983118637886; Fri, 26 Jun 2020 13:48:24 -0700 (PDT) Received: from localhost ([::1]:49826 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovGo-0007b5-R3 for importer@patchew.org; Fri, 26 Jun 2020 16:48:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33990) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAj-0007Ar-Oy for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:06 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:40627 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAe-0005sl-OT for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:03 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-241-9IMUI3OnNkaFtm6faG1Kvg-1; Fri, 26 Jun 2020 16:41:58 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 787F7107ACCA; Fri, 26 Jun 2020 20:41:57 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id A338B19C58; Fri, 26 Jun 2020 20:41:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204120; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BgPXhXn50eUAAS9+s9eH+67pArlk591EX0K8bG86+G0=; b=TRew1HBvn0svzxCkd6hqjZv0FaNt242N8Q4okhYPCe/cvGKE9WcPs+XMERdoSTFFWir17e TFmqwt0hHFxNWhbMHaBnU7ngC1aaRastleCvB2ctvhKI1hDvbWI8ZJRPq+BdKnSTfKOV82 bywFp28Er8gHs98jpiWkdnuFkGvGPyk= X-MC-Unique: 9IMUI3OnNkaFtm6faG1Kvg-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 13/16] python/machine.py: fix _popen access Date: Fri, 26 Jun 2020 16:41:30 -0400 Message-Id: <20200626204133.14500-14-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 03:23:21 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" As always, Optional[T] causes problems with unchecked access. Add a helper that asserts the pipe is present before we attempt to talk with it. Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- python/qemu/machine.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index cb3db1f66a..c189fa4e28 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -114,7 +114,7 @@ def __init__(self, binary, args=3DNone, wrapper=3DNone,= name=3DNone, # Runstate self._qemu_log_path =3D None self._qemu_log_file =3D None - self._popen =3D None + self._popen: Optional['subprocess.Popen[bytes]'] =3D None self._events =3D [] self._iolog =3D None self._qmp_set =3D True # Enable QMP monitor by default. @@ -227,6 +227,12 @@ def is_running(self): """Returns true if the VM is running.""" return self._popen is not None and self._popen.poll() is None =20 + @property + def _subp(self) -> 'subprocess.Popen[bytes]': + if self._popen is None: + raise QEMUMachineError('Subprocess pipe not present') + return self._popen + def exitcode(self): """Returns the exit code if possible, or None.""" if self._popen is None: @@ -237,7 +243,7 @@ def get_pid(self): """Returns the PID of the running process, or None.""" if not self.is_running(): return None - return self._popen.pid + return self._subp.pid =20 def _load_io_log(self): if self._qemu_log_path is not None: @@ -369,7 +375,7 @@ def wait(self): """ Wait for the VM to power off """ - self._popen.wait() + self._subp.wait() if self._qmp_connection: self._qmp.close() self._post_shutdown() @@ -381,8 +387,8 @@ def _hard_shutdown(self) -> None: if not self.is_running(): return =20 - self._popen.kill() - self._popen.wait(timeout=3D60) + self._subp.kill() + self._subp.wait(timeout=3D60) =20 def _soft_shutdown(self, has_quit: bool =3D False, timeout: int =3D 3)= -> None: """ @@ -407,7 +413,7 @@ def _soft_shutdown(self, has_quit: bool =3D False, time= out: int =3D 3) -> None: raise self._qmp.close() =20 - self._popen.wait(timeout=3Dtimeout) + self._subp.wait(timeout=3Dtimeout) =20 def _do_shutdown(self, has_quit: bool =3D False, timeout: int =3D 3) -= > None: """ --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204653; cv=none; d=zohomail.com; s=zohoarc; b=iP1IOgsLSfHUs+x02YjnBDa2SzfabpQktp3mdJIKYogY7zRpBig73/gUxABgF5QbRmr5qrFgAz1fh2tKOapETuLMhq/Zo7KwZIxoGdJlRwxOxK1bnhA5sSAm5NIg/81ntvpgtRIQLfvLsxBHFT9rx9Jr5AGukZVqLwAg+Hub5mk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204653; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=eVtsA0DHrcqkkn68fiQz+xgrExE5tHtuRy1db8hnKnQ=; b=SR/EiiSWt9Pl5b12DdXSWpefkqES9U9XpUnWUBYsWG39lrK+QmU9zpQp27DsSxSjhN2B0fyR4WXAAmHLksO7S3POUoSM9/Q2u8Xj36AGnlQzm9VvZeatx24Ylvt17vDEmkDGS8Mr+YYaQkTPBi2+Hl8H7pCZHC1OIbO18XVODOo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 159320465315294.05782407572315; Fri, 26 Jun 2020 13:50:53 -0700 (PDT) Received: from localhost ([::1]:58718 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovJD-0002sw-V3 for importer@patchew.org; Fri, 26 Jun 2020 16:50:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34006) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAl-0007Bl-0P for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:07 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:27441 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAf-0005t3-Mv for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:05 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-198-LL7J-eVNP4mhOPpLwlI1OA-1; Fri, 26 Jun 2020 16:41:59 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 74C8C107ACCD; Fri, 26 Jun 2020 20:41:58 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 998A019724; Fri, 26 Jun 2020 20:41:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204121; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eVtsA0DHrcqkkn68fiQz+xgrExE5tHtuRy1db8hnKnQ=; b=B2HfqQqJD4ohNjJSXThF3AR3XkW5MUovcdYrSq0NjqRrNaJ48JnGTBG6YW1n1U7t43CPX5 d+fhnAkJd7BzM1zU1QVteUpOE66uskEQih6bvBF2YpsAfd/QRwa2K4WnooSAETvXDKyWSy aJA3rQHsYaVJhLXj0pQDEFSH9szx+Ro= X-MC-Unique: LL7J-eVNP4mhOPpLwlI1OA-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 14/16] python/qemu: make 'args' style arguments immutable Date: Fri, 26 Jun 2020 16:41:31 -0400 Message-Id: <20200626204133.14500-15-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.61; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 16:23:57 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" These arguments don't need to be mutable and aren't really used as such. Clarify their types as immutable and adjust code to match where necessary. In general, It's probably best not to accept a user-defined mutable object and store it as internal object state unless there's a strong justification for doing so. Instead, try to use generic types as input with empty tuples as the default, and coerce to list where necessary. Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- python/qemu/machine.py | 30 +++++++++++++++++------------- python/qemu/qtest.py | 16 ++++++++++++---- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index c189fa4e28..15e854e04f 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -18,6 +18,7 @@ # =20 import errno +from itertools import chain import logging import os import subprocess @@ -30,6 +31,8 @@ Dict, List, Optional, + Sequence, + Tuple, Type, ) from types import TracebackType @@ -68,8 +71,12 @@ class QEMUMachine: # vm is guaranteed to be shut down here """ =20 - def __init__(self, binary, args=3DNone, wrapper=3DNone, name=3DNone, - test_dir=3D"/var/tmp", + def __init__(self, + binary: str, + args: Sequence[str] =3D (), + wrapper: Sequence[str] =3D (), + name: Optional[str] =3D None, + test_dir: str =3D "/var/tmp", monitor_address: Optional[SocketAddrT] =3D None, socket_scm_helper=3DNone, sock_dir=3DNone): ''' @@ -87,14 +94,7 @@ def __init__(self, binary, args=3DNone, wrapper=3DNone, = name=3DNone, # Direct user configuration =20 self._binary =3D binary - - if args is None: - args =3D [] - # Copy mutable input: we will be modifying our copy self._args =3D list(args) - - if wrapper is None: - wrapper =3D [] self._wrapper =3D wrapper =20 self._name =3D name or "qemu-%d" % os.getpid() @@ -119,7 +119,7 @@ def __init__(self, binary, args=3DNone, wrapper=3DNone,= name=3DNone, self._iolog =3D None self._qmp_set =3D True # Enable QMP monitor by default. self._qmp_connection: Optional[qmp.QEMUMonitorProtocol] =3D None - self._qemu_full_args =3D None + self._qemu_full_args: Tuple[str, ...] =3D () self._temp_dir =3D None self._launched =3D False self._machine =3D None @@ -340,7 +340,7 @@ def launch(self): raise QEMUMachineError('VM already launched') =20 self._iolog =3D None - self._qemu_full_args =3D None + self._qemu_full_args =3D () try: self._launch() self._launched =3D True @@ -360,8 +360,12 @@ def _launch(self): """ devnull =3D open(os.path.devnull, 'rb') self._pre_launch() - self._qemu_full_args =3D (self._wrapper + [self._binary] + - self._base_args + self._args) + self._qemu_full_args =3D tuple( + chain(self._wrapper, + [self._binary], + self._base_args, + self._args) + ) LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args)) self._popen =3D subprocess.Popen(self._qemu_full_args, stdin=3Ddevnull, diff --git a/python/qemu/qtest.py b/python/qemu/qtest.py index 05c63a1d58..ae4661d4d3 100644 --- a/python/qemu/qtest.py +++ b/python/qemu/qtest.py @@ -22,6 +22,7 @@ from typing import ( List, Optional, + Sequence, TextIO, ) =20 @@ -103,8 +104,13 @@ class QEMUQtestMachine(QEMUMachine): A QEMU VM, with a qtest socket available. """ =20 - def __init__(self, binary, args=3DNone, name=3DNone, test_dir=3D"/var/= tmp", - socket_scm_helper=3DNone, sock_dir=3DNone): + def __init__(self, + binary: str, + args: Sequence[str] =3D (), + name: Optional[str] =3D None, + test_dir: str =3D "/var/tmp", + socket_scm_helper: Optional[str] =3D None, + sock_dir: Optional[str] =3D None): if name is None: name =3D "qemu-%d" % os.getpid() if sock_dir is None: @@ -118,8 +124,10 @@ def __init__(self, binary, args=3DNone, name=3DNone, t= est_dir=3D"/var/tmp", @property def _base_args(self) -> List[str]: args =3D super()._base_args - args.extend(['-qtest', 'unix:path=3D' + self._qtest_path, - '-accel', 'qtest']) + args.extend([ + '-qtest', f"unix:path=3D{self._qtest_path}", + '-accel', 'qtest' + ]) return args =20 def _pre_launch(self): --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204626; cv=none; d=zohomail.com; s=zohoarc; b=ONvlMU7Ob8eP9bWvXTOGkIdkhVU9yoxBiulp77CsgbYDKRqN5IN1i1MkSAIyiVjh2rEhUY6PkzAEnmmxLcoT01zVYXw4RBsIDtEP3YiGYbyAyfOl9iUts6tNGMMYnTyd9mqf+hvedJ3tYpjShpLmoTlM3pFx2NzN4SCVuXJ+fMs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204626; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3XuqgNBoHiB1EqTrAtUfEk1aG8bC1rg5A/meSP+YByQ=; b=Xm1oE17RuakhGsCDlvazQfZ9CVpoxdHKEdHjX+m+CgiTzlkhQn93Q5N1RA/KeP7lLO1a877s8nr9A5ISUFKv7bIh+PidJubTPBXTAnKbFUkerl0ciOOZNkJnv07riyk9tFPQUAdgyfPFhTVprsPy8DAVp3y/u27CjKeZk/tE7M8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204626640359.79177407516886; Fri, 26 Jun 2020 13:50:26 -0700 (PDT) Received: from localhost ([::1]:57112 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovIn-00029x-BI for importer@patchew.org; Fri, 26 Jun 2020 16:50:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34078) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAq-0007G0-Gx for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:13 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:36934 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAl-0005u4-1s for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:12 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-373-kD8QHXdPMLyOIYQnyNNsHw-1; Fri, 26 Jun 2020 16:42:00 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6D02D800D5C; Fri, 26 Jun 2020 20:41:59 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 966D219C58; Fri, 26 Jun 2020 20:41:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204124; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3XuqgNBoHiB1EqTrAtUfEk1aG8bC1rg5A/meSP+YByQ=; b=a/IPFDoFwuAiLeDpoUmWuMlgMEemln165Q5PekNeaatOtlH+PwlmwBIK7m0f0elrMU6Q0/ FxEYirUflujB9/BBRXvX0dBr1WfSM/6cYNdwhBgrH92wSvfrxgvp20OSNyQNKcTg2ViNSw sX3jyagH49JVqgyJ8kCLX5owGvzmKoA= X-MC-Unique: kD8QHXdPMLyOIYQnyNNsHw-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 15/16] iotests.py: Adjust HMP kwargs typing Date: Fri, 26 Jun 2020 16:41:32 -0400 Message-Id: <20200626204133.14500-16-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=jsnow@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 15:42:27 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" mypy wants to ensure there's consistency between the kwargs arguments types and any unspecified keyword arguments. In this case, conv_keys is a bool, but the remaining keys are Any type. Mypy (correctly) infers the **kwargs type to be **Dict[str, str], which is not compatible with conv_keys: bool. Because QMP typing is a little fraught right now anyway, re-type kwargs to Dict[str, Any] which has the benefit of silencing this check right now. A future re-design might type these more aggressively, but this will give us a baseline to work from with minimal disruption. (Thanks Kevin Wolf for the debugging assist here) Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- tests/qemu-iotests/iotests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py index a8576f811b..9040a671f9 100644 --- a/tests/qemu-iotests/iotests.py +++ b/tests/qemu-iotests/iotests.py @@ -553,7 +553,7 @@ def add_incoming(self, addr): =20 def hmp(self, command_line: str, use_log: bool =3D False) -> QMPMessag= e: cmd =3D 'human-monitor-command' - kwargs =3D {'command-line': command_line} + kwargs: Dict[str, Any] =3D {'command-line': command_line} if use_log: return self.qmp_log(cmd, **kwargs) else: --=20 2.21.3 From nobody Sat Apr 27 06:31:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593204803; cv=none; d=zohomail.com; s=zohoarc; b=hxainkA6sinp4Fyuyq2w8/eJAXr+fZSkyIkZ2qO4+L7U/2J33/mh2/MvbYX5nDI32vSdZev1e2Jr/32KmC0FsdlP+6APnEBcY/lqJ0YD9LRzU6lgjZCoEdBW45FgyiwsJRNvBk+9Kho98rAfICyDBDY1/6iW8Yl47QFEd9eE6mo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593204803; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=f1wpgVAPE4U4/ZFi+obNRppVSKp5dvn5ZK9lvilHuwM=; b=cSQ5cqsWOCOltoxg6YKRBYVk715i1ELqNBJ4DYMAL6LwI1Q2dTQvy8E+FfIRk9pP26Q8L/2zNfHfj09Cz3MkVV6tcTYML86++WrG2iCAg2LbnRcknxeUdA1jLfd2CGXoDFYKYu5e1lhagyri0aXnqMZ1wW1fGV7Dhr0Vl5TA1ag= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593204803732771.2766342439212; Fri, 26 Jun 2020 13:53:23 -0700 (PDT) Received: from localhost ([::1]:37922 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jovLe-00065R-H3 for importer@patchew.org; Fri, 26 Jun 2020 16:53:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34082) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jovAq-0007G6-JD for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:13 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:36886 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jovAj-0005tu-Iv for qemu-devel@nongnu.org; Fri, 26 Jun 2020 16:42:12 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-433-uOTpvyaiMMWPRnAsiwcJYQ-1; Fri, 26 Jun 2020 16:42:01 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 045B5800597; Fri, 26 Jun 2020 20:42:01 +0000 (UTC) Received: from probe.redhat.com (ovpn-119-184.rdu2.redhat.com [10.10.119.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id BE15919C58; Fri, 26 Jun 2020 20:41:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593204123; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f1wpgVAPE4U4/ZFi+obNRppVSKp5dvn5ZK9lvilHuwM=; b=hg7rckK9YUSPsGqfedNa7ocTt6q4JcMtLvEatUCL2JfTmBK5nMmK75uZjsw+e45HaWBR0A +6Kw/eFvkiNTrkEmsRdSS/NB3m/28zmv6ZyxHTbernI4Iapg7M3D3F4oilvIo6/IE6zMJf pgjaLpEomUEMKTcWWiouCBafvkrFPMY= X-MC-Unique: uOTpvyaiMMWPRnAsiwcJYQ-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v4 16/16] python/qemu: Add mypy type annotations Date: Fri, 26 Jun 2020 16:41:33 -0400 Message-Id: <20200626204133.14500-17-jsnow@redhat.com> In-Reply-To: <20200626204133.14500-1-jsnow@redhat.com> References: <20200626204133.14500-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/26 03:23:21 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Eduardo Habkost , qemu-block@nongnu.org, John Snow , Max Reitz , Cleber Rosa , philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" These should all be purely annotations with no changes in behavior at all. You need to be in the python folder, but you should be able to confirm that these annotations are correct (or at least self-consistent) by running `mypy --strict qemu`. Signed-off-by: John Snow Reviewed-by: Kevin Wolf --- python/qemu/accel.py | 8 ++-- python/qemu/machine.py | 94 ++++++++++++++++++++++++------------------ python/qemu/qmp.py | 44 +++++++++++--------- python/qemu/qtest.py | 26 +++++++----- 4 files changed, 98 insertions(+), 74 deletions(-) diff --git a/python/qemu/accel.py b/python/qemu/accel.py index 7fabe62920..4325114e51 100644 --- a/python/qemu/accel.py +++ b/python/qemu/accel.py @@ -17,6 +17,7 @@ import logging import os import subprocess +from typing import List, Optional =20 LOG =3D logging.getLogger(__name__) =20 @@ -29,7 +30,7 @@ } =20 =20 -def list_accel(qemu_bin): +def list_accel(qemu_bin: str) -> List[str]: """ List accelerators enabled in the QEMU binary. =20 @@ -49,7 +50,8 @@ def list_accel(qemu_bin): return [acc.strip() for acc in out.splitlines()[1:]] =20 =20 -def kvm_available(target_arch=3DNone, qemu_bin=3DNone): +def kvm_available(target_arch: Optional[str] =3D None, + qemu_bin: Optional[str] =3D None) -> bool: """ Check if KVM is available using the following heuristic: - Kernel module is present in the host; @@ -72,7 +74,7 @@ def kvm_available(target_arch=3DNone, qemu_bin=3DNone): return True =20 =20 -def tcg_available(qemu_bin): +def tcg_available(qemu_bin: str) -> bool: """ Check if TCG is available. =20 diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 15e854e04f..9963f76741 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -28,6 +28,7 @@ import tempfile from typing import ( Any, + BinaryIO, Dict, List, Optional, @@ -38,7 +39,7 @@ from types import TracebackType =20 from . import qmp -from .qmp import SocketAddrT, QMPMessage +from .qmp import QMPMessage, QMPReturnValue, SocketAddrT =20 LOG =3D logging.getLogger(__name__) =20 @@ -61,7 +62,7 @@ class QEMUMachineAddDeviceError(QEMUMachineError): =20 class QEMUMachine: """ - A QEMU VM + A QEMU VM. =20 Use this object as a context manager to ensure the QEMU process terminates:: @@ -78,7 +79,8 @@ def __init__(self, name: Optional[str] =3D None, test_dir: str =3D "/var/tmp", monitor_address: Optional[SocketAddrT] =3D None, - socket_scm_helper=3DNone, sock_dir=3DNone): + socket_scm_helper: Optional[str] =3D None, + sock_dir: Optional[str] =3D None): ''' Initialize a QEMUMachine =20 @@ -112,28 +114,28 @@ def __init__(self, self._remove_monitor_sockfile =3D True =20 # Runstate - self._qemu_log_path =3D None - self._qemu_log_file =3D None + self._qemu_log_path: Optional[str] =3D None + self._qemu_log_file: Optional[BinaryIO] =3D None self._popen: Optional['subprocess.Popen[bytes]'] =3D None - self._events =3D [] - self._iolog =3D None + self._events: List[QMPMessage] =3D [] + self._iolog: Optional[str] =3D None self._qmp_set =3D True # Enable QMP monitor by default. self._qmp_connection: Optional[qmp.QEMUMonitorProtocol] =3D None self._qemu_full_args: Tuple[str, ...] =3D () - self._temp_dir =3D None + self._temp_dir: Optional[str] =3D None self._launched =3D False - self._machine =3D None + self._machine: Optional[str] =3D None self._console_index =3D 0 self._console_set =3D False - self._console_device_type =3D None + self._console_device_type: Optional[str] =3D None self._console_address =3D os.path.join( self._sock_dir, f"{self._name}-console.sock" ) - self._console_socket =3D None - self._remove_files =3D [] + self._console_socket: Optional[socket.socket] =3D None + self._remove_files: List[str] =3D [] self._killed =3D False =20 - def __enter__(self): + def __enter__(self) -> 'QEMUMachine': return self =20 def __exit__(self, @@ -142,14 +144,15 @@ def __exit__(self, exc_tb: Optional[TracebackType]) -> None: self.shutdown() =20 - def add_monitor_null(self): + def add_monitor_null(self) -> None: """ This can be used to add an unused monitor instance. """ self._args.append('-monitor') self._args.append('null') =20 - def add_fd(self, fd, fdset, opaque, opts=3D''): + def add_fd(self, fd: int, fdset: int, + opaque: str, opts: str =3D '') -> 'QEMUMachine': """ Pass a file descriptor to the VM """ @@ -168,7 +171,8 @@ def add_fd(self, fd, fdset, opaque, opts=3D''): self._args.append(','.join(options)) return self =20 - def send_fd_scm(self, fd=3DNone, file_path=3DNone): + def send_fd_scm(self, fd: Optional[int] =3D None, + file_path: Optional[str] =3D None) -> int: """ Send an fd or file_path to socket_scm_helper. =20 @@ -212,7 +216,7 @@ def send_fd_scm(self, fd=3DNone, file_path=3DNone): return proc.returncode =20 @staticmethod - def _remove_if_exists(path): + def _remove_if_exists(path: str) -> None: """ Remove file object at path if it exists """ @@ -223,7 +227,7 @@ def _remove_if_exists(path): return raise =20 - def is_running(self): + def is_running(self) -> bool: """Returns true if the VM is running.""" return self._popen is not None and self._popen.poll() is None =20 @@ -233,19 +237,19 @@ def _subp(self) -> 'subprocess.Popen[bytes]': raise QEMUMachineError('Subprocess pipe not present') return self._popen =20 - def exitcode(self): + def exitcode(self) -> Optional[int]: """Returns the exit code if possible, or None.""" if self._popen is None: return None return self._popen.poll() =20 - def get_pid(self): + def get_pid(self) -> Optional[int]: """Returns the PID of the running process, or None.""" if not self.is_running(): return None return self._subp.pid =20 - def _load_io_log(self): + def _load_io_log(self) -> None: if self._qemu_log_path is not None: with open(self._qemu_log_path, "r") as iolog: self._iolog =3D iolog.read() @@ -279,7 +283,7 @@ def _base_args(self) -> List[str]: args.extend(['-device', device]) return args =20 - def _pre_launch(self): + def _pre_launch(self) -> None: self._temp_dir =3D tempfile.mkdtemp(dir=3Dself._test_dir) self._qemu_log_path =3D os.path.join(self._temp_dir, self._name + = ".log") self._qemu_log_file =3D open(self._qemu_log_path, 'wb') @@ -297,7 +301,7 @@ def _pre_launch(self): nickname=3Dself._name ) =20 - def _post_launch(self): + def _post_launch(self) -> None: if self._qmp_connection: self._qmp.accept() =20 @@ -330,7 +334,7 @@ def _post_shutdown(self) -> None: self._killed =3D False self._launched =3D False =20 - def launch(self): + def launch(self) -> None: """ Launch the VM and make sure we cleanup and expose the command line/output in case of exception @@ -354,7 +358,7 @@ def launch(self): LOG.debug('Output: %r', self._iolog) raise =20 - def _launch(self): + def _launch(self) -> None: """ Launch the VM and establish a QMP connection """ @@ -375,7 +379,7 @@ def _launch(self): close_fds=3DFalse) self._post_launch() =20 - def wait(self): + def wait(self) -> None: """ Wait for the VM to power off """ @@ -454,13 +458,13 @@ def shutdown(self, has_quit: bool =3D False, hard: bo= ol =3D False) -> None: finally: self._post_shutdown() =20 - def kill(self): + def kill(self) -> None: """ Terminate the VM forcefully and perform cleanup. """ self.shutdown(hard=3DTrue) =20 - def set_qmp_monitor(self, enabled=3DTrue): + def set_qmp_monitor(self, enabled: bool =3D True) -> None: """ Set the QMP monitor. =20 @@ -496,7 +500,9 @@ def qmp(self, cmd: str, qmp_args =3D self._qmp_args(conv_keys, **args) return self._qmp.cmd(cmd, args=3Dqmp_args) =20 - def command(self, cmd, conv_keys=3DTrue, **args): + def command(self, cmd: str, + conv_keys: bool =3D True, + **args: Any) -> QMPReturnValue: """ Invoke a QMP command. On success return the response dict. @@ -505,7 +511,7 @@ def command(self, cmd, conv_keys=3DTrue, **args): qmp_args =3D self._qmp_args(conv_keys, **args) return self._qmp.command(cmd, **qmp_args) =20 - def get_qmp_event(self, wait=3DFalse): + def get_qmp_event(self, wait: bool =3D False) -> Optional[QMPMessage]: """ Poll for one queued QMP events and return it """ @@ -513,7 +519,7 @@ def get_qmp_event(self, wait=3DFalse): return self._events.pop(0) return self._qmp.pull_event(wait=3Dwait) =20 - def get_qmp_events(self, wait=3DFalse): + def get_qmp_events(self, wait: bool =3D False) -> List[QMPMessage]: """ Poll for queued QMP events and return a list of dicts """ @@ -524,7 +530,7 @@ def get_qmp_events(self, wait=3DFalse): return events =20 @staticmethod - def event_match(event, match=3DNone): + def event_match(event: Any, match: Optional[Any]) -> bool: """ Check if an event matches optional match criteria. =20 @@ -554,9 +560,11 @@ def event_match(event, match=3DNone): return True except TypeError: # either match or event wasn't iterable (not a dict) - return match =3D=3D event + return bool(match =3D=3D event) =20 - def event_wait(self, name, timeout=3D60.0, match=3DNone): + def event_wait(self, name: str, + timeout: float =3D 60.0, + match: Optional[QMPMessage] =3D None) -> Optional[QMPMe= ssage]: """ event_wait waits for and returns a named event from QMP with a tim= eout. =20 @@ -566,7 +574,9 @@ def event_wait(self, name, timeout=3D60.0, match=3DNone= ): """ return self.events_wait([(name, match)], timeout) =20 - def events_wait(self, events, timeout=3D60.0): + def events_wait(self, + events: Sequence[Tuple[str, Any]], + timeout: float =3D 60.0) -> Optional[QMPMessage]: """ events_wait waits for and returns a single named event from QMP. In the case of multiple qualifying events, this function returns t= he @@ -583,7 +593,7 @@ def events_wait(self, events, timeout=3D60.0): :return: A QMP event matching the filter criteria. If timeout was 0 and no event matched, None. """ - def _match(event): + def _match(event: QMPMessage) -> bool: for name, match in events: if event['event'] =3D=3D name and self.event_match(event, = match): return True @@ -610,20 +620,20 @@ def _match(event): =20 return None =20 - def get_log(self): + def get_log(self) -> Optional[str]: """ After self.shutdown or failed qemu execution, this returns the out= put of the qemu process. """ return self._iolog =20 - def add_args(self, *args): + def add_args(self, *args: str) -> None: """ Adds to the list of extra arguments to be given to the QEMU binary """ self._args.extend(args) =20 - def set_machine(self, machine_type): + def set_machine(self, machine_type: str) -> None: """ Sets the machine type =20 @@ -632,7 +642,9 @@ def set_machine(self, machine_type): """ self._machine =3D machine_type =20 - def set_console(self, device_type=3DNone, console_index=3D0): + def set_console(self, + device_type: Optional[str] =3D None, + console_index: int =3D 0) -> None: """ Sets the device type for a console device =20 @@ -663,7 +675,7 @@ def set_console(self, device_type=3DNone, console_index= =3D0): self._console_index =3D console_index =20 @property - def console_socket(self): + def console_socket(self) -> socket.socket: """ Returns a socket connected to the console """ diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index 7935dababb..303e82ee6b 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -15,6 +15,7 @@ Any, cast, Dict, + List, Optional, TextIO, Type, @@ -90,7 +91,9 @@ class QEMUMonitorProtocol: #: Logger object for debugging messages logger =3D logging.getLogger('QMP') =20 - def __init__(self, address, server=3DFalse, nickname=3DNone): + def __init__(self, address: SocketAddrT, + server: bool =3D False, + nickname: Optional[str] =3D None): """ Create a QEMUMonitorProtocol class. =20 @@ -102,7 +105,7 @@ def __init__(self, address, server=3DFalse, nickname=3D= None): @note No connection is established, this is done by the connect() = or accept() methods """ - self.__events =3D [] + self.__events: List[QMPMessage] =3D [] self.__address =3D address self.__sock =3D self.__get_sock() self.__sockfile: Optional[TextIO] =3D None @@ -114,14 +117,14 @@ def __init__(self, address, server=3DFalse, nickname= =3DNone): self.__sock.bind(self.__address) self.__sock.listen(1) =20 - def __get_sock(self): + def __get_sock(self) -> socket.socket: if isinstance(self.__address, tuple): family =3D socket.AF_INET else: family =3D socket.AF_UNIX return socket.socket(family, socket.SOCK_STREAM) =20 - def __negotiate_capabilities(self): + def __negotiate_capabilities(self) -> QMPMessage: greeting =3D self.__json_read() if greeting is None or "QMP" not in greeting: raise QMPConnectError @@ -131,7 +134,7 @@ def __negotiate_capabilities(self): return greeting raise QMPCapabilitiesError =20 - def __json_read(self, only_event=3DFalse): + def __json_read(self, only_event: bool =3D False) -> Optional[QMPMessa= ge]: assert self.__sockfile is not None while True: data =3D self.__sockfile.readline() @@ -148,7 +151,7 @@ def __json_read(self, only_event=3DFalse): continue return resp =20 - def __get_events(self, wait=3DFalse): + def __get_events(self, wait: Union[bool, float] =3D False) -> None: """ Check for new events in the stream and cache them in __events. =20 @@ -186,7 +189,7 @@ def __get_events(self, wait=3DFalse): raise QMPConnectError("Error while reading from socket") self.__sock.settimeout(None) =20 - def __enter__(self): + def __enter__(self) -> 'QEMUMonitorProtocol': # Implement context manager enter function. return self =20 @@ -199,7 +202,7 @@ def __exit__(self, # Implement context manager exit function. self.close() =20 - def connect(self, negotiate=3DTrue): + def connect(self, negotiate: bool =3D True) -> Optional[QMPMessage]: """ Connect to the QMP Monitor and perform capabilities negotiation. =20 @@ -214,7 +217,7 @@ def connect(self, negotiate=3DTrue): return self.__negotiate_capabilities() return None =20 - def accept(self, timeout=3D15.0): + def accept(self, timeout: float =3D 15.0) -> QMPMessage: """ Await connection from QMP Monitor and perform capabilities negotia= tion. =20 @@ -250,7 +253,9 @@ def cmd_obj(self, qmp_cmd: QMPMessage) -> QMPMessage: self.logger.debug("<<< %s", resp) return resp =20 - def cmd(self, name, args=3DNone, cmd_id=3DNone): + def cmd(self, name: str, + args: Optional[Dict[str, Any]] =3D None, + cmd_id: Optional[Any] =3D None) -> QMPMessage: """ Build a QMP command and send it to the QMP Monitor. =20 @@ -258,14 +263,14 @@ def cmd(self, name, args=3DNone, cmd_id=3DNone): @param args: command arguments (dict) @param cmd_id: command id (dict, list, string or int) """ - qmp_cmd =3D {'execute': name} + qmp_cmd: QMPMessage =3D {'execute': name} if args: qmp_cmd['arguments'] =3D args if cmd_id: qmp_cmd['id'] =3D cmd_id return self.cmd_obj(qmp_cmd) =20 - def command(self, cmd, **kwds): + def command(self, cmd: str, **kwds: Any) -> QMPReturnValue: """ Build and send a QMP command to the monitor, report errors if any """ @@ -278,7 +283,8 @@ def command(self, cmd, **kwds): ) return cast(QMPReturnValue, ret['return']) =20 - def pull_event(self, wait=3DFalse): + def pull_event(self, + wait: Union[bool, float] =3D False) -> Optional[QMPMess= age]: """ Pulls a single event. =20 @@ -298,7 +304,7 @@ def pull_event(self, wait=3DFalse): return self.__events.pop(0) return None =20 - def get_events(self, wait=3DFalse): + def get_events(self, wait: bool =3D False) -> List[QMPMessage]: """ Get a list of available QMP events. =20 @@ -315,13 +321,13 @@ def get_events(self, wait=3DFalse): self.__get_events(wait) return self.__events =20 - def clear_events(self): + def clear_events(self) -> None: """ Clear current list of pending events. """ self.__events =3D [] =20 - def close(self): + def close(self) -> None: """ Close the socket and socket file. """ @@ -330,7 +336,7 @@ def close(self): if self.__sockfile: self.__sockfile.close() =20 - def settimeout(self, timeout): + def settimeout(self, timeout: float) -> None: """ Set the socket timeout. =20 @@ -339,7 +345,7 @@ def settimeout(self, timeout): """ self.__sock.settimeout(timeout) =20 - def get_sock_fd(self): + def get_sock_fd(self) -> int: """ Get the socket file descriptor. =20 @@ -347,7 +353,7 @@ def get_sock_fd(self): """ return self.__sock.fileno() =20 - def is_scm_available(self): + def is_scm_available(self) -> bool: """ Check if the socket allows for SCM_RIGHTS. =20 diff --git a/python/qemu/qtest.py b/python/qemu/qtest.py index ae4661d4d3..49c04bc98d 100644 --- a/python/qemu/qtest.py +++ b/python/qemu/qtest.py @@ -27,6 +27,7 @@ ) =20 from .machine import QEMUMachine +from .qmp import SocketAddrT =20 =20 class QEMUQtestProtocol: @@ -43,7 +44,8 @@ class QEMUQtestProtocol: No conection is estabalished by __init__(), this is done by the connect() or accept() methods. """ - def __init__(self, address, server=3DFalse): + def __init__(self, address: SocketAddrT, + server: bool =3D False): self._address =3D address self._sock =3D self._get_sock() self._sockfile: Optional[TextIO] =3D None @@ -51,14 +53,14 @@ def __init__(self, address, server=3DFalse): self._sock.bind(self._address) self._sock.listen(1) =20 - def _get_sock(self): + def _get_sock(self) -> socket.socket: if isinstance(self._address, tuple): family =3D socket.AF_INET else: family =3D socket.AF_UNIX return socket.socket(family, socket.SOCK_STREAM) =20 - def connect(self): + def connect(self) -> None: """ Connect to the qtest socket. =20 @@ -67,7 +69,7 @@ def connect(self): self._sock.connect(self._address) self._sockfile =3D self._sock.makefile(mode=3D'r') =20 - def accept(self): + def accept(self) -> None: """ Await connection from QEMU. =20 @@ -76,7 +78,7 @@ def accept(self): self._sock, _ =3D self._sock.accept() self._sockfile =3D self._sock.makefile(mode=3D'r') =20 - def cmd(self, qtest_cmd): + def cmd(self, qtest_cmd: str) -> str: """ Send a qtest command on the wire. =20 @@ -87,14 +89,16 @@ def cmd(self, qtest_cmd): resp =3D self._sockfile.readline() return resp =20 - def close(self): - """Close this socket.""" + def close(self) -> None: + """ + Close this socket. + """ self._sock.close() if self._sockfile: self._sockfile.close() self._sockfile =3D None =20 - def settimeout(self, timeout): + def settimeout(self, timeout: Optional[float]) -> None: """Set a timeout, in seconds.""" self._sock.settimeout(timeout) =20 @@ -118,7 +122,7 @@ def __init__(self, super().__init__(binary, args, name=3Dname, test_dir=3Dtest_dir, socket_scm_helper=3Dsocket_scm_helper, sock_dir=3Dsock_dir) - self._qtest =3D None + self._qtest: Optional[QEMUQtestProtocol] =3D None self._qtest_path =3D os.path.join(sock_dir, name + "-qtest.sock") =20 @property @@ -130,7 +134,7 @@ def _base_args(self) -> List[str]: ]) return args =20 - def _pre_launch(self): + def _pre_launch(self) -> None: super()._pre_launch() self._qtest =3D QEMUQtestProtocol(self._qtest_path, server=3DTrue) =20 @@ -139,7 +143,7 @@ def _post_launch(self) -> None: super()._post_launch() self._qtest.accept() =20 - def _post_shutdown(self): + def _post_shutdown(self) -> None: super()._post_shutdown() self._remove_if_exists(self._qtest_path) =20 --=20 2.21.3