From nobody Thu May 16 23:37:53 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=1591134477; cv=none; d=zohomail.com; s=zohoarc; b=UjMY0IyfpisS8w4hlJowuQkUzhGQWpUAg3m0FVZuKXXMEusL39qKj9qCCgVfAEcjC6nTpgZbWdIrpmzm8Ish9b2fLFc77KnHLujdYpi5Jj9hhuQsUkSqqkE3fX8+dp0n6GTAdDnOhqfGTdyb3HXJmxKeIdXpOPhp/gdNDDQSB2o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134477; 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=AffFvAJ982E8rMg3USDtpzSTkHD0X9KrviRMhx0uy5A=; b=YSwNNupyoI4tlUs49BHo55gVS7pA3h0/bJYG4dkx6Q43LNQ8zjhZ8jvn0tNuNrwAqyq1sIUvlVFExfMBu7o6dyy8ZHM5ZDCFdEtbODKtAVafp6nyjFDkDu0/+dVwpdRwXuE8T9lTXOsa5MsY8TX5Ew202Gk6hX2mIrAqz54RNdE= 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 1591134477521438.56452376538334; Tue, 2 Jun 2020 14:47:57 -0700 (PDT) Received: from localhost ([::1]:56184 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgElI-00072g-8X for importer@patchew.org; Tue, 02 Jun 2020 17:47:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47300) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEj6-0004oN-1B for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:40 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:48580 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 1jgEj3-0008H2-Ta for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:39 -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-118-KNH124CNO8qj6vIC_KslmA-1; Tue, 02 Jun 2020 17:45:32 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C5FFCA0BD8; Tue, 2 Jun 2020 21:45:31 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9084860BE1; Tue, 2 Jun 2020 21:45:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134334; 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=AffFvAJ982E8rMg3USDtpzSTkHD0X9KrviRMhx0uy5A=; b=IN53GaNnq2qzzG/e7FgPAyeG02WyrxZ0Jzv8/HIugET2fyN2M6enlR/P4UjbJnj4QRYwDD 4fCzujTwDI1DLHdxUwGCIfXPNMOfLVI1sM22qYRXohjfgDkN8tFDoy+0sd+1go6p0QOGO7 CwERNjxeD45PTg6s2rsASk4xfzNqk5M= X-MC-Unique: KNH124CNO8qj6vIC_KslmA-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 01/16] python/qmp.py: Define common types Date: Tue, 2 Jun 2020 17:45:13 -0400 Message-Id: <20200602214528.12107-2-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 03:23:32 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H2=-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 e64b6b5faa7..8388c7b6030 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 Thu May 16 23:37:53 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=1591134467; cv=none; d=zohomail.com; s=zohoarc; b=YGMAkrbSM/1MaZhkK315H2uoI9CTlG5pABrCLi4E6lmxsWV/UbiUg8x6UaCz3unkjgx/YcIsB6S6F/nHNAMYrRv238b6hFAfMnWf2UoLGe/tjnwhyeBylpq01sVNS+XeL9rDmWpmKCnvanmWhPgeaiYLbDc2BK9Tsf+Wa78LmpE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134467; 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=1ZHtmt1m1ZkVFBw/uJkXhMkYPR+P+eLoSIG2usa9Dcg=; b=DDmnwjIIB9dKE+uUq0FaLOMG+5rOOaQSmXPia+YTgtaIk7OAiWZFgg3OT7uwGJF+1BIv52qxrRngPCbqKz95FXwAq75D+DmEI91wFeWyReGkwKa9y2t10B+3Wmtwp9hglNe6r5NzDSRIDc/BfjAHAHWbBbYSvMq8NwSyj2sa/oY= 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 1591134467632781.0848239257425; Tue, 2 Jun 2020 14:47:47 -0700 (PDT) Received: from localhost ([::1]:55548 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEl7-0006nE-Lp for importer@patchew.org; Tue, 02 Jun 2020 17:47:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47296) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEj5-0004oB-Gd for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:39 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:37886 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 1jgEj4-0008Io-2Z for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:39 -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-208-MMhQxAZDNJa26a-oDlz2qw-1; Tue, 02 Jun 2020 17:45:34 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 43D3A1800D42; Tue, 2 Jun 2020 21:45:33 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0B45A60BE1; Tue, 2 Jun 2020 21:45:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134337; 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=1ZHtmt1m1ZkVFBw/uJkXhMkYPR+P+eLoSIG2usa9Dcg=; b=UyihemoR6z9/t/af0m4yQoQHJdeC6JtHsMhMlxez7G/Kxdb5GEbALurWwQ2uLzv5Yijdp1 xHAvTiiasHzsdsHLyjdL2shLjdoqZCLFK1Wtpo82Nku/MMvpAWVNrzbqyIuAxB8OmvlpbJ YJAR6ZDNFZYcObJNkn4/LXPTooASvnE= X-MC-Unique: MMhQxAZDNJa26a-oDlz2qw-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 02/16] iotests.py: use qemu.qmp type aliases Date: Tue, 2 Jun 2020 17:45:14 -0400 Message-Id: <20200602214528.12107-3-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 03:23:32 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H2=-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 f20d90f9698..7c1773bba37 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 Thu May 16 23:37:53 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=1591134484; cv=none; d=zohomail.com; s=zohoarc; b=JeD7+f0bCkCJb0gcCyMyeXmgYMFfI7OU92Lj74x/ZcsKPj7qW7K4uYOn+WOub3z2AOGpdR3l9HSVLh72Gs8LvOJYlmDP9FYzubAdUDUMplvdKtzLf3rJyFS5+WVLQv+/CcjU0PPgpGzogrNMP34y9E3vb7QBoIfG1yUZdhkeWHs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134484; 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=GT6U2bxbUb9Mw2IcL8NNeCjQ97NYBIX3pDsA3BoritU=; b=KMqxTT+mwj33vo8SSlGevDkBK94jKMiKlYP4eZotZZZwS8Y0WxJ07EmRH9hUSAbau0Lqw/RL8IIZnBpVAljp3/LMG/GgR19H20rleBfmwkT3vKjZqL6qGeDsgsTn4myfD9TjFN3zMjYx4+KmGT7wR3aN8zqV/IQeOQVWbsYXEKE= 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 1591134484240701.7583725089438; Tue, 2 Jun 2020 14:48:04 -0700 (PDT) Received: from localhost ([::1]:56894 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgElO-0007KC-UQ for importer@patchew.org; Tue, 02 Jun 2020 17:48:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47306) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEj6-0004oa-Fg for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:40 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:30285 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 1jgEj4-0008Ir-39 for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:40 -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-238-OYdNBhd1M4SsMqjiOc0a7g-1; Tue, 02 Jun 2020 17:45:35 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B5BAE80B724; Tue, 2 Jun 2020 21:45:34 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7960660BE1; Tue, 2 Jun 2020 21:45:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134337; 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=GT6U2bxbUb9Mw2IcL8NNeCjQ97NYBIX3pDsA3BoritU=; b=WVBz8/CGTZQ94tLFDimvgK2eVPuArfI2lRNksbYN72DeQPMFx9FwNon9TQSY3gk23ajZbE 29Ah4L3nUktNy6OLMYz0/Y23Y/uaz2SVqeos661X2thj1y55+vtvgnUR4bPsO9ISweCPny 430dbcyNkMIA+y4ZRITAzDlmmNV3e6w= X-MC-Unique: OYdNBhd1M4SsMqjiOc0a7g-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 03/16] python/qmp.py: re-absorb MonitorResponseError Date: Tue, 2 Jun 2020 17:45:15 -0400 Message-Id: <20200602214528.12107-4-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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.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/02 03:23:32 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H2=-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 a2abd2c35e3..b2b7ae9fbf9 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -48,19 +48,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 @@ -464,7 +451,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 8388c7b6030..aa8a666b8ab 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 409b4321f2e..da6acf050d1 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 Thu May 16 23:37:53 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=1591134477; cv=none; d=zohomail.com; s=zohoarc; b=Xk3PQ961voyDP0MkMV/LYuEF0Ffj/2j0+MnJuDjiO7DukhCYfVHtLOAMGtikxUcDy/ZhznYKOa/9n1GVdiSLEONwxB3LdQQmp1ND0iHm0ydAhIfaW7I22wI/FrqeDHt95J8ruPUfVRjO1YBwlZXAMcket/3LLWYT4p6h0GdxKOM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134477; 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=V6vPM6AYq3oZLyd83BJvWKOnCz6g7DCIxzAnkyqnH8c=; b=gq692OY8Ye+sN2KaUxqxS25Xgxy/VhHUvgWEGWfIjlPU0p2yBdvympOCCKbtFTDVkmSsLbrwbfycSQSok2DeTCa/IM7Iea+OhTL0mInuk4oflNK/SfW/e74IY3hrh40YkyGPaCj9av6HG8tGkV6i2wb62PfWtTyQ+PG2vkt7TeI= 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 159113447786570.26879783560094; Tue, 2 Jun 2020 14:47:57 -0700 (PDT) Received: from localhost ([::1]:56236 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgElI-00073v-EH for importer@patchew.org; Tue, 02 Jun 2020 17:47:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47320) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEj9-0004sS-5m for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:43 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:44204 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 1jgEj8-0008L9-5h for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:42 -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-155-TzK20cZlMAu1V1JhkOIPwg-1; Tue, 02 Jun 2020 17:45:38 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EE2981856975; Tue, 2 Jun 2020 21:45:37 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id EB9C060BE1; Tue, 2 Jun 2020 21:45:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134341; 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=V6vPM6AYq3oZLyd83BJvWKOnCz6g7DCIxzAnkyqnH8c=; b=gFzauK2XZ5Lvya9XRcgxPXfbRhCzF9XjJNzQfcTv/uoR0FwUp43/Ri+nX90uFR3aGev58X eePmoUcNdSpv0q4+K1QG5+6gnQ0F+nFNu9+s3Y7KJLw6L2M4Lm3KZScDkwi2UQW+Z89mPy r9vVK/q1DkOhwWA/Xw7u9d0QPeGzZXI= X-MC-Unique: TzK20cZlMAu1V1JhkOIPwg-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 04/16] python/qmp.py: Do not return None from cmd_obj Date: Tue, 2 Jun 2020 17:45:16 -0400 Message-Id: <20200602214528.12107-5-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 15:54:57 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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 aa8a666b8ab..ef3c919b76c 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 Thu May 16 23:37:53 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=1591134639; cv=none; d=zohomail.com; s=zohoarc; b=JwCj/3bTb9oaEQ21BX247QcuqeggsgLvIuhHXhP8ain0E+CAEVJ7SmpBuAvriKFitd+FUSDN3XmH0Ta3tUw+lQ/Iz4H+/Qn5XolwyjiyVaZ1iGyqlBUaPLxzf8LqdO6zWIQj0JLgx34WIIBpLFZ4KSMD0wTlKhsnbOq1Pp1/wi8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134639; 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=5Qf8v0ok6NoZn3ONyeNpiiry8fEwaJOYjh5wGIbsVw4=; b=geUPqewz/HSAZ7I5R9Jvl6YbJyrhFGB2z6ShsDhPxPwdEtsw1I3j2qqLtdiNkgZlCV8KmK9tftGdNSEJDvBEEpZMzft4EtnCJwSUicHGa7DgCxMz3q5QuabTpKEp2adjN+IrVAinvOBo6Mh+TnGUYVPrOhqF1bAHWLukQkohZDQ= 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 1591134639049643.6269555670283; Tue, 2 Jun 2020 14:50:39 -0700 (PDT) Received: from localhost ([::1]:36788 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEnt-0002Jz-Qa for importer@patchew.org; Tue, 02 Jun 2020 17:50:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47338) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjC-0004yt-Ou for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:46 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:45675 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 1jgEjB-0008NF-8z for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45: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-20-fnYXv3SsPaKMIBdzcUX9hw-1; Tue, 02 Jun 2020 17:45:42 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B186F8018AB; Tue, 2 Jun 2020 21:45:41 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 943206116F; Tue, 2 Jun 2020 21:45:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134344; 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=5Qf8v0ok6NoZn3ONyeNpiiry8fEwaJOYjh5wGIbsVw4=; b=QuZ0DBvwZGt4T+oFHuOpWK7BqbbY85UxvjMB6zrbwmX9Zse56tuZ2XF2o4XDsM3YHk3vXA OMhMBQDQkMM5BhQ+hhOFL4UocknC4uE010HwzERG8HmvzU2PZZRX8lVA8q4kg2RCRtjbOR NE3WSvVAtKNqY2/LdlRlKDAqhw4fdps= X-MC-Unique: fnYXv3SsPaKMIBdzcUX9hw-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 05/16] python/qmp.py: add casts to JSON deserialization Date: Tue, 2 Jun 2020 17:45:17 -0400 Message-Id: <20200602214528.12107-6-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/02 03:23:32 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H2=-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 ef3c919b76c..5f3558e3066 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 =3D cast(QMPMessage, 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 Thu May 16 23:37:53 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=1591134632; cv=none; d=zohomail.com; s=zohoarc; b=aSp/rtK2elS6lztAaoxcuzrWkSY6AVmbhEoxlbYMyCWwDOcExt9brMpsJVy4MGCY7bK381uRnHdlKXyp9DCcQ9UnA2m8wmWACWSMQiPryyNs9OKv1Golz8+5AGHX0xD1gKMTTlTlim6TJUW+tsVo6z2JySk/ftB0b3U4baDUrv0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134632; 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=UuN7kDO+yFdxcmAFajdCQ051EB+5vcNmJCYoCn0GDFg=; b=DsPfnlRLE6oBGm+DhevdKjJBnEQCYKArpTJB4oZEZ0PKd8KCUg9PEcNzLuoiJg1lww4o4uoI/22LDaZX9ZERbYHmLdMagTdK6ZCcr80jN0tw4+viD3WedrmSyZgKWmTSKt9Z5Z9yQ93mNC35sa+aXgZ+hp1fXVbl38TLhqiGW4g= 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 1591134632484939.8027659615855; Tue, 2 Jun 2020 14:50:32 -0700 (PDT) Received: from localhost ([::1]:36646 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEnn-0002ED-7x for importer@patchew.org; Tue, 02 Jun 2020 17:50:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47346) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjE-00052Q-Qn for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:50 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:36194 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 1jgEjD-0008Oc-S3 for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:48 -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-295-AreIRHF_PaOpChfV0AQ7dg-1; Tue, 02 Jun 2020 17:45:45 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6D1EF80058E; Tue, 2 Jun 2020 21:45:44 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id E7A3461987; Tue, 2 Jun 2020 21:45:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134347; 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=UuN7kDO+yFdxcmAFajdCQ051EB+5vcNmJCYoCn0GDFg=; b=B7EOXjSmfEFB7oeoRAhweVYL/Hd1mXqG416cxzqlrMz7LedCYPY18g41xsj+DIAMfwbOeF 816dX7V1kLF0IQzrMe8QUBgv9J0sTIe8+HOAU5SiBDjiVQN58Q+KbLaNAJv4Tc4oJhUVlw Alx4Dhpzx9Ff7ZGGCAgPqRAtgOrkNQY= X-MC-Unique: AreIRHF_PaOpChfV0AQ7dg-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 06/16] python/qmp.py: add QMPProtocolError Date: Tue, 2 Jun 2020 17:45:18 -0400 Message-Id: <20200602214528.12107-7-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 15:54:57 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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 5f3558e3066..1aefc00c93b 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 Thu May 16 23:37:53 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=1591134763; cv=none; d=zohomail.com; s=zohoarc; b=e87UntuWxb6gjWTiEX2+lygG3cXgPpGee8Biu52z0bUpRXP7DAeO/skFA7DoNRbteYA8k5Kms2VzAMk1QKp6ycEzVCVqTJZw8xev+EDg4HwSaJjXzEXFS7OPD9saFezLpjH5at3kbJz7t3sFaeiRsygF9rSnZGxiLefVaPCeR98= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134763; 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=WmR5YvbGHreGpHGw2x++lSpFmO72b+HVV3Dr5yf4DHc=; b=EGSdCqiSxTY9pAB4mqh06Twjg9+Z6MLJ0nulScllRkTRv1GoQbVSQcY7CyYcW2/xmOxfGhIbHwtFHiIN16/Lbdge4WDRsamQ/0HCDmeiX3A9nnXKeQU64TBzNw0RD31gcFGGxeUMsfDfhFCMV8jDeRF2BYwcKOkuZRdjXw8sl7A= 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 1591134763748929.2989951665214; Tue, 2 Jun 2020 14:52:43 -0700 (PDT) Received: from localhost ([::1]:44048 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEpu-0005Rh-DH for importer@patchew.org; Tue, 02 Jun 2020 17:52:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47350) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjH-00053B-7a for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:51 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:40773 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 1jgEjF-0008Oq-SX for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:50 -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-147-dg4RKLb-OL6O6S5bXIvxsg-1; Tue, 02 Jun 2020 17:45:47 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7C1B61800D42; Tue, 2 Jun 2020 21:45:46 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id A2F5F61984; Tue, 2 Jun 2020 21:45:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134349; 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=WmR5YvbGHreGpHGw2x++lSpFmO72b+HVV3Dr5yf4DHc=; b=Zf91iqA3ExrSvIeiP03da22X31jHD+aRX1RBf10WdeFtG83yqydALML+yKirUeQHSVMgsi +1oY4pZZGYXFUj9rwPZqG8yNYcXMKXvRCTIk9lNkUZWqPO3VtZ88uw7RLJDpDM3+OVE5/K kHsKav7iFGw7oLX57khkuCcXVEfyLU4= X-MC-Unique: dg4RKLb-OL6O6S5bXIvxsg-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 07/16] python/machine.py: Fix monitor address typing Date: Tue, 2 Jun 2020 17:45:19 -0400 Message-Id: <20200602214528.12107-8-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 15:48:49 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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.001, RCVD_IN_MSPIKE_WL=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 simply 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 b2b7ae9fbf9..e8d7f9e4298 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -24,10 +24,14 @@ import shutil 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 @@ -61,7 +65,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 @@ -84,8 +89,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 @@ -223,15 +234,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): @@ -256,14 +269,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 Thu May 16 23:37:53 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=1591134896; cv=none; d=zohomail.com; s=zohoarc; b=HjL8JlHzrZRqEb9SfdOjrYHloCGXbtR2P5h80Fe260x9z4A9EJBJdggkcutgHXrY91jHhR4DwmEr4awvt/F7VoW2OOpxQG1/uiT+wUxhSTMiFKI2vnMDBphhbmfSdrFlixkgKt7jKmk24P2M1ktpNRSxnYGKF7pkSAmcxIezLbQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134896; 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=ajaH1edmkCnFO2PjT1+Of0xuEo1dVETXTLswW3pandQ=; b=A69mKb12IeS8+ppkjw/ea2rK9q4V3V8/dqxZLatCIRtXKHxxgXm/OsN3ah5i6k5UY234UolUUCiWaAYcd5dovcuLL89FAMl3M6KbtTyve91XbsFUUvUqfix24K7TkkNNCLmRzb/dX41krepZetpiGEildcOE1NrcTd6DOadhG1U= 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 1591134896401225.3150384614088; Tue, 2 Jun 2020 14:54:56 -0700 (PDT) Received: from localhost ([::1]:52498 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEs3-0000Xk-3J for importer@patchew.org; Tue, 02 Jun 2020 17:54:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47372) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjL-0005C6-K8 for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:55 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:25118 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 1jgEjK-0008PU-5i for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45: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-424-fJddRlVANk-mz_uhdrxtOQ-1; Tue, 02 Jun 2020 17:45:48 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EBD481083E80; Tue, 2 Jun 2020 21:45:47 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id B1AFF61984; Tue, 2 Jun 2020 21:45:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134353; 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=ajaH1edmkCnFO2PjT1+Of0xuEo1dVETXTLswW3pandQ=; b=FxUKY1uUx6HKnKfyshGSzk1AsxTfeOkWb0+rR0d2666jtio57g/j4sA2BAHom2j1WdAUH5 +bOVgA9EnPiIl4a/YCuijCe6Q9tpCBxz+olE3Ikk3tMu5rk72JIWuRbkgFbqYV5gqaMIgQ s672iT7aCtx7PakRl72P5fIH8H+t95s= X-MC-Unique: fJddRlVANk-mz_uhdrxtOQ-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 08/16] python/machine.py: reorder __init__ Date: Tue, 2 Jun 2020 17:45:20 -0400 Message-Id: <20200602214528.12107-9-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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.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/02 03:23:32 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H2=-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 e8d7f9e4298..6b7d188b951 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -80,38 +80,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 Thu May 16 23:37:53 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=1591134794; cv=none; d=zohomail.com; s=zohoarc; b=RyWsrk7S6p20xfQfaLmHZ6HCSZ+Ui8qbQCvNPDBAhG+eECo83D/8xg1rUVTvdJZaHAq0Y8sVst7N/rEXbOPHTzPqF9pOF4k8zP8z6ApNew3ZuHQvH+d4ccA3NzLct2Y02ei+ubfKUASmSuNczBWO5hcCRUO8wBrNB4fZnVDA7KY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134794; 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=9hBfPgbWRlmnr3gHoNTn2l2v14/i4J/v9qsZPyjpOJU=; b=mJ+WpCs4rqUn8HNt4+fLnoOOB3qsfBd/2+F/qowth/j7QdudsLh/xNu4pknocOlrTGfGu+TeQNo3rmSXGWlKQLr8vIsUclvy+SgAxOSPWxqb6zlI8h95OTnDXHLBSo1+++wZmmmWf30EMuaj4f/pG2adrjJ2AtsXKPmrDKDh5vU= 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 1591134794078240.6595915518908; Tue, 2 Jun 2020 14:53:14 -0700 (PDT) Received: from localhost ([::1]:46150 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEqO-0006I8-Kz for importer@patchew.org; Tue, 02 Jun 2020 17:53:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47370) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjL-0005B6-33 for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:55 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:27528 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 1jgEjI-0008P9-M4 for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:54 -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-368-D0ONXUufNeu_QBIiovIMeQ-1; Tue, 02 Jun 2020 17:45:50 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 658C9107ACF3; Tue, 2 Jun 2020 21:45:49 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F25261984; Tue, 2 Jun 2020 21:45:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134351; 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=9hBfPgbWRlmnr3gHoNTn2l2v14/i4J/v9qsZPyjpOJU=; b=SKaLUHeA1qO+hlgHKuLgZ/cYnbbM2mMLFtXSZFqwebw9OBu+qJvX/y/p7st1Ltmxb1vJxZ ES0EGr1uCRZIRi1AzVyNKOZSmXCprTlcDkwVUxgy03/m5GMXodiohbbVasyi8PDQJr2v9S fMKCtHbdLGB7yYM4YP276UStiaJHuGc= X-MC-Unique: D0ONXUufNeu_QBIiovIMeQ-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 09/16] python/machine.py: Don't modify state in _base_args() Date: Tue, 2 Jun 2020 17:45:21 -0400 Message-Id: <20200602214528.12107-10-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 17:45:34 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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 6b7d188b951..2f12cebde40 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -25,6 +25,7 @@ import socket import tempfile from typing import ( + List, Optional, Type, ) @@ -122,7 +123,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 [] =20 @@ -237,7 +240,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: @@ -255,9 +259,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]) @@ -273,6 +274,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) @@ -345,7 +349,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 888c8bd2f60..05c63a1d583 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 Thu May 16 23:37:53 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=1591134910; cv=none; d=zohomail.com; s=zohoarc; b=CWYb3ndeMTw6jU4R4g4IR3B1PYliWikqXBwDuU/hGMHPVJCxeWhGgJfZtl07UMFDPC36PH2UDHv0fAY8gPdA2vISIC6v03kojLM2lxyx4wBs+CnzcS+h1qYd/MUhnf+gHfTiwzDebAWUTtFGj9ujMoA8c+HqQg2lxbVO92Ot0hQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134910; 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=GhiauXHtsy5r3b11N89stTQeUZUHOjLasA26muAm4UM=; b=Fz3/hrGgI4DAPlYrAga8F11AuLmN1win1tn5jdrL8Pvl3rrAFrT6Ry58TVnPAyoITRyP4JPceSfmff5Ot/XrUoCFjcI47DTu+rzjxDTvCdAAFQm0Qm0OVnM836DUUj2XX8lg0vfso/qypmusVF5VwRFaHnl7KeImf7mU09p/e2A= 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 1591134910470639.9667997662125; Tue, 2 Jun 2020 14:55:10 -0700 (PDT) Received: from localhost ([::1]:53124 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEsH-0000nx-8b for importer@patchew.org; Tue, 02 Jun 2020 17:55:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47376) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjM-0005Eq-LZ for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:57 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:53055 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 1jgEjK-0008Pi-Dk for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45: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-152-z68Av6uVMwmmNyw8SaJ0Mg-1; Tue, 02 Jun 2020 17:45:51 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D6D0CA0BD7; Tue, 2 Jun 2020 21:45:50 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9D03A6116F; Tue, 2 Jun 2020 21:45:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134353; 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=GhiauXHtsy5r3b11N89stTQeUZUHOjLasA26muAm4UM=; b=UgYcwq//AQBTNjK7M17ZGlz7dGC3L6olloMbBNuvrv1u7VxdSpH3x2Xx9/6gYsdAeOCgCi HezzKDHAA4Cyt9AY58cWrONPozMjg6g4GUQe6V6TkCqN3mlJUjUQffUz1Du0RGOuaMf6sY WApSMxmSWKF+jjH7QRhpV33uerzI+hA= X-MC-Unique: z68Av6uVMwmmNyw8SaJ0Mg-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 10/16] python/machine.py: Handle None events in event_wait Date: Tue, 2 Jun 2020 17:45:22 -0400 Message-Id: <20200602214528.12107-11-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 03:23:32 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H2=-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 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 2f12cebde40..a835b7550da 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -32,7 +32,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 @@ -553,6 +553,8 @@ def _match(event): return True return False =20 + event: Optional[QMPMessage] + # Search cached events for event in self._events: if _match(event): @@ -562,6 +564,8 @@ def _match(event): # Poll for new events while True: event =3D self._qmp.pull_event(wait=3Dtimeout) + if event is None: + break if _match(event): return event self._events.append(event) --=20 2.21.3 From nobody Thu May 16 23:37:53 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=1591135104; cv=none; d=zohomail.com; s=zohoarc; b=GwnUYP6z0cdbMfxS0JwxoiQDb2+eLn83NbCP/1OwgBm1gW2uaw+1fYRQmxAN4PgkNordCeMUbaogLmEirgj8n+7RrkElxdq3APrLL5rFALC4v1qY6iSLDdB5G4vU9nzotLGjMTLwqblckC+jjd+4cN5JIHlKxY/JGQy/jFsKevY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591135104; 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=8rScsdZiWSfPPTJL0UGkB6AtAaEGgD4vNhN/UdNeA2U=; b=aYdORZi+DPFYeuhUIDEH75jH5fl47sW6ZMX5lCVUhtUxM9kcC0SqR0z+qX9+0XoHRRCsUO2XaeDLTE/djVcAP8r/vtFO0TzzuZRn7QmnjVfqPHCUDDtFjnE1H5NiTWkfdl9i3rXVrJpeXP+hXv6XwQMtFv8OawWz3LN4bLEXDnI= 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 15911351040868.489742800311205; Tue, 2 Jun 2020 14:58:24 -0700 (PDT) Received: from localhost ([::1]:34652 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEvO-0005BT-P6 for importer@patchew.org; Tue, 02 Jun 2020 17:58:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47396) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjP-0005Gv-Ad for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:59 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:48192 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 1jgEjO-0008SX-2y for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:59 -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-344-40lmcYYjPua1M-Q6LBeKgQ-1; Tue, 02 Jun 2020 17:45:53 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 52424107ACCA; Tue, 2 Jun 2020 21:45:52 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 18FF661987; Tue, 2 Jun 2020 21:45:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134357; 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=8rScsdZiWSfPPTJL0UGkB6AtAaEGgD4vNhN/UdNeA2U=; b=J5zBAijR2F6B7X7zQATuDwGNx8qJycT1+cU8YzncodfItf4o12sMzVtsWy+jvDcLwOoG78 sYpqDyBEOogoGh5BEVCquIZUa9z60v3ZB0RPNiVNjHFCK7ar+4ngyr1CJTuvByMI6pad6X pvq5RTLuN8lk+Jhi2WIu20lL+J/t2LA= X-MC-Unique: 40lmcYYjPua1M-Q6LBeKgQ-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 11/16] python/machine.py: use qmp.command Date: Tue, 2 Jun 2020 17:45:23 -0400 Message-Id: <20200602214528.12107-12-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 17:45:34 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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 a835b7550da..5add7a28eaf 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -25,6 +25,8 @@ import socket import tempfile from typing import ( + Any, + Dict, List, Optional, Type, @@ -450,17 +452,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): @@ -469,12 +477,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 Thu May 16 23:37:53 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=1591135002; cv=none; d=zohomail.com; s=zohoarc; b=gGtQnpdz+SIfS5zrpHF6moW+lSLlWGRaBeWXXaPcCrYiwAVPrzVDqSAQCbSUkeFLZc5dloDh4tRqAqvRAYJP9yCnpKplxUCHdqegxOCssel6DlC0rc4BGXaOO4Vafx/OpXP99akL+pzzukJoDFySCI1m2k3IN9LCfTUBj+LGRqU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591135002; 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=pLAaIb74sWexkDKSFOZp2vSJr51XcCT5DDhgIXM28S8=; b=k08UGldm7HpL0ltq6XeOLCQyNWYqZJlgCsT3YxBOtL1D4NBMXlMNoIoUvHsDwyVGfbVW8K08F+RsT2BgFvBGleh34tLfLAFDBIUEueMuhPmDpWbcuicfltLbQKjs7eaFIRXSmvU17nUG3fJyZtgjYwV2nnswLntRBtVI55VZ8rM= 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 1591135002094138.76217807848786; Tue, 2 Jun 2020 14:56:42 -0700 (PDT) Received: from localhost ([::1]:58626 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEtk-0003Kl-NC for importer@patchew.org; Tue, 02 Jun 2020 17:56:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47390) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjO-0005Gp-Tm for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45:59 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:55101 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 1jgEjO-0008SC-0A for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:45: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-42-gwRX0Lo1OT-Xv1A2qbVgMA-1; Tue, 02 Jun 2020 17:45:54 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EE09B80058E; Tue, 2 Jun 2020 21:45:53 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 889C461984; Tue, 2 Jun 2020 21:45:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134356; 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=pLAaIb74sWexkDKSFOZp2vSJr51XcCT5DDhgIXM28S8=; b=FFOboqFxlhuhFgypc3m6aO4fDn8HhXYzwzzgK7xrbsmqS0CeHF8qvDOjc2fPgsoBAD528D hxtLZqqi8XFdn8HAyl4/044/S9N+BDlFMfQFBSOBClvUWpPAxYGV7xZIsuYKGho8IG1wgY kQqbOlI2o3+JqHRIU6qzQDPuJbf1B1k= X-MC-Unique: gwRX0Lo1OT-Xv1A2qbVgMA-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 12/16] python/machine.py: Add _qmp access shim Date: Tue, 2 Jun 2020 17:45:24 -0400 Message-Id: <20200602214528.12107-13-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 17:45:34 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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 Reviewed-by: Kevin Wolf --- python/qemu/machine.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 5add7a28eaf..9c45bdfb1e4 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -117,7 +117,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 @@ -283,7 +283,7 @@ 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 @@ -450,7 +450,13 @@ def set_qmp_monitor(self, enabled=3DTrue): self._qmp_set =3D True else: self._qmp_set =3D False - self._qmp =3D None + self._qmp_connection =3D None + + @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 Thu May 16 23:37:53 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=1591135112; cv=none; d=zohomail.com; s=zohoarc; b=In9SmGueMKqI2vU5obqyNQm+uqiYRcUDj0xG3JQ9B2uDhM4Mug7+pIFZddj8ifRIMgeqKTVd63GKEzI41pnbuHH+NUQUaH3pYqql0qNGAnG94xrkyxuM+tf34117yU02ThsA8uLvprCsa/gnPdE5HOZwn7BJ2olDS1wClra9oBo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591135112; 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=XXZsgNy8ylD2Xon0Zvh4kBHu+MYqm9yocytCFoFU9TE=; b=kAaRzN31YhPBJQsEgZ2gU8+vMJUS5rwLXBSumaQsPHYxIPtq9j4LGu4UrAt+0MqJTtJOYoST5HLEIZWOmoleDa9cnZJJfhrB+HKf4LOxYu/dwvs4Z4G4z7xoL5meXyqisxE+SLkIi6n5u4uAzUwsn6rctw8wklUO33cNwAHf114= 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 1591135112300163.12853375024315; Tue, 2 Jun 2020 14:58:32 -0700 (PDT) Received: from localhost ([::1]:35144 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEvX-0005Pb-1x for importer@patchew.org; Tue, 02 Jun 2020 17:58:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47434) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjX-0005Tv-Pt for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:46:07 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:51267 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 1jgEjV-0008Tu-GB for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:46:07 -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-322-VVNjTjJqPou8lFRWT4RpNQ-1; Tue, 02 Jun 2020 17:45:58 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7FF051083E80; Tue, 2 Jun 2020 21:45:57 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6594A61984; Tue, 2 Jun 2020 21:45:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134360; 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=XXZsgNy8ylD2Xon0Zvh4kBHu+MYqm9yocytCFoFU9TE=; b=iOFEyuiEdkQnhAr+lrD583TceOVAsA9aJfK8DAC/98gHhDFHdBLoQ7ndNFsZ6JMtyIyfD5 SpLPhOcPK3ikdORQoJEL+SAPVV61PrVvp3PKVEsfGBhXfizr3kcTg+H0JmHHwyR01NOckM nkMwLtb2pJKDHJcKkZ3TNS1BGLWFJ8c= X-MC-Unique: VVNjTjJqPou8lFRWT4RpNQ-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 13/16] python/machine.py: fix _popen access Date: Tue, 2 Jun 2020 17:45:25 -0400 Message-Id: <20200602214528.12107-14-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 17:45:34 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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 9c45bdfb1e4..64197fc8492 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -113,7 +113,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. @@ -225,6 +225,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: @@ -235,7 +241,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: @@ -365,7 +371,7 @@ def wait(self): """ Wait for the VM to power off """ - self._popen.wait() + self._subp.wait() if self._qmp: self._qmp.close() self._post_shutdown() @@ -377,8 +383,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: """ @@ -395,7 +401,7 @@ def _soft_shutdown(self, has_quit: bool =3D False, time= out: int =3D 3) -> None: self._qmp.cmd('quit') 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 Thu May 16 23:37:53 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=1591134800; cv=none; d=zohomail.com; s=zohoarc; b=CfVNoWay4zbXlGqmhU2uVpmgSzkNLFEj9KR3wmsRzhIao7MyMwEfF5OtFtrYgEktnPlt5CmbjMTwpZY58gSJdYe7e2UP25JPZcHQSdq7wHYP7Srb0JGWo46xTb0BG/HaBAL2QyxegW2zSexIpyRP/LQq0z/CzBNA6R68qIUenxw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591134800; 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=uYyZQxaNvqDH/hr1wmR0qZCpuu1PPwy8ELRv1oSawx8=; b=kv4/5AGBzQmWdOHxJeMCfQupfUxAJh3+devIR4nHbdblyjgcNUBYIc7PyIf/pKAMOVhz6l+sUx5HXrYTb+efvZGEPrcx1cURnlBQ/CW45/3tt/xbVKCU09rfOaGfGycwS33NwNWL8lMTMPD9Ct4BxM/gmYggfKJ2ggDILoP7bLY= 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 1591134800236538.0888156242308; Tue, 2 Jun 2020 14:53:20 -0700 (PDT) Received: from localhost ([::1]:46758 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEqU-0006XD-Qh for importer@patchew.org; Tue, 02 Jun 2020 17:53:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47424) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjW-0005Se-IG for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:46:07 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:56304 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 1jgEjV-0008Uh-D7 for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:46:06 -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-260-H-ApTCLVOL2XoD5eVemT0g-1; Tue, 02 Jun 2020 17:45:59 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EDA41835B41; Tue, 2 Jun 2020 21:45:58 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id B6F5261984; Tue, 2 Jun 2020 21:45:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134361; 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=uYyZQxaNvqDH/hr1wmR0qZCpuu1PPwy8ELRv1oSawx8=; b=LmyHK7hfI76oAZSbo10BtvopG8ZPB3W6B1Nin7IjVpw4DQ1f9kHVUA3ba2Z1tc4QMrPeN0 PJHuKAaj3GKs4plU4dyrunlw08hOmeRxFFurFOpDMEcBPpxH/vSF70x783TzlZ2HZ6DwDd L3vCrGJNiZ/pXX/UTqWDlsuDfGG/F98= X-MC-Unique: H-ApTCLVOL2XoD5eVemT0g-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 14/16] python/qemu: make 'args' style arguments immutable Date: Tue, 2 Jun 2020 17:45:26 -0400 Message-Id: <20200602214528.12107-15-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 17:45:34 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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 64197fc8492..d32e5599ce3 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 @@ -29,6 +30,8 @@ Dict, List, Optional, + Sequence, + Tuple, Type, ) from types import TracebackType @@ -67,8 +70,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): ''' @@ -86,14 +93,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() @@ -118,7 +118,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 @@ -336,7 +336,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 @@ -356,8 +356,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 05c63a1d583..ae4661d4d3e 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 Thu May 16 23:37:53 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=1591135009; cv=none; d=zohomail.com; s=zohoarc; b=SxmncVBzpTrXPjTtuq/k7+txnQ3I1nnbsgv+j01C7WGs/R2hh0bVaGpXx5FBY1NxujFFIl2JWNmnVKWk59aIfr0w+4J+a7Xtfc5uSra/ZkR624TQtMkB+XnQRDh2uqQuEg5RCzSEi4iUZwZm37Xrqy2lD7Q7jg4O2Wx7ZSS7lAA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591135009; 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=0iGSpQJVJCdKjrWxqDYE2XmRQvkmLt6rW5uM+LnGK5c=; b=dUezWq3pl3Gyka8mrMc13HD4mCr06Vy2jsxOveMkynyV2758XxyF3H1LNrVLAgP9V9oC7jhapStTprv4xbxVgrf7eNd4g1bwi4ZiHz2LIqMm0yV8wzZ2JRpeXcgibQC0DNhk6SxBO24iJwuYHo1SJg2xolp9dDWWE0SvhepC3I4= 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 1591135009183339.95517372970585; Tue, 2 Jun 2020 14:56:49 -0700 (PDT) Received: from localhost ([::1]:59090 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgEtr-0003WL-VO for importer@patchew.org; Tue, 02 Jun 2020 17:56:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47428) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjW-0005Sh-UJ for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:46:07 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:58268 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 1jgEjV-00004t-GB for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:46:06 -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-378-CY2zF2epM6i2REwh6ocz0A-1; Tue, 02 Jun 2020 17:46:01 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D841EA0BD9; Tue, 2 Jun 2020 21:46:00 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 712AC60BE1; Tue, 2 Jun 2020 21:45:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134363; 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=0iGSpQJVJCdKjrWxqDYE2XmRQvkmLt6rW5uM+LnGK5c=; b=QEH4Q38CIiHCvoPdFdtbCROluO448/GjSUWlX4SQhH36YYW4lni+EjTSwHb4DCe53TRwzZ 7df2g8g68Intl+ye2aQVuMH16nZA/r5p6T0+kevZtCziOjog7C2pQJvBsdLy8wByhq2v5E iJ3Xf753A8IwpH7eDnYG2ISJ/5rWvqk= X-MC-Unique: CY2zF2epM6i2REwh6ocz0A-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 15/16] iotests.py: Adjust HMP kwargs typing Date: Tue, 2 Jun 2020 17:45:27 -0400 Message-Id: <20200602214528.12107-16-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 03:23:32 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H2=-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 7c1773bba37..19b22bee618 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 Thu May 16 23:37:53 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=1591135250; cv=none; d=zohomail.com; s=zohoarc; b=mh8tXTwneox91D1tDvbN4chBWgCl6vhAFbonF+uWZ4ehDx0w+n0NMtWYFukG8XTwL3Pf+hYrxKOG9Qi+VpaRtlIBLHGa3Y2KAZovBAYhwFf1aU53MyEombUFVpO2SDbmSS2nH/CBhJ32CTEX6q8z9tQ1/9/CtDHhPxlRWxtni+k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591135250; 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=faOAcm3Mwl4F6P4x7wkqEBMYBpf/XQrJY6jsw0+kYV8=; b=g5w1fn8h9m/NWvLygPdp5+DLKpixxUS7uOZQztkcWQoFOCv63EGQfc6klLDiSIgF+M0VUeCjxaHYSAfTcJbJ5PqvHCiQraJHsoiGnFcG3+9lKRIu7bEK+1LlPgRVCq11h697HpAz5fIfLRTHYTbCC3aG+xzaTDnBb89GoogCcII= 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 1591135250803731.9897877774162; Tue, 2 Jun 2020 15:00:50 -0700 (PDT) Received: from localhost ([::1]:41394 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgExl-00084X-4Q for importer@patchew.org; Tue, 02 Jun 2020 18:00:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47444) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgEjb-0005XM-2M for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:46:11 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:56558 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 1jgEjY-00005x-UH for qemu-devel@nongnu.org; Tue, 02 Jun 2020 17:46:10 -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-173-okSYV7muNjmbcgB1dpr62w-1; Tue, 02 Jun 2020 17:46:04 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D7F8B1856941; Tue, 2 Jun 2020 21:46:03 +0000 (UTC) Received: from probe.redhat.com (ovpn-112-142.rdu2.redhat.com [10.10.112.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1D4566116D; Tue, 2 Jun 2020 21:46:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591134368; 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=faOAcm3Mwl4F6P4x7wkqEBMYBpf/XQrJY6jsw0+kYV8=; b=UJAOU0Ru2X4oAkEeiICBH451zbNiEdmxNaYygzvC2GoLbEFH+REUKfzd57RdrT/ALOD3PU XIcRf4zuJ1VODyMKhpYEOpWX96rbu7aVu6dU+Pq5/FiqvoMJFTqfbUV4qqbjQzWvOVJTd5 WpOGGr2yVi+prcqWNRY4KaMJQIv/l7g= X-MC-Unique: okSYV7muNjmbcgB1dpr62w-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 16/16] python/qemu: Add mypy type annotations Date: Tue, 2 Jun 2020 17:45:28 -0400 Message-Id: <20200602214528.12107-17-jsnow@redhat.com> In-Reply-To: <20200602214528.12107-1-jsnow@redhat.com> References: <20200602214528.12107-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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/02 15:54:57 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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 | 96 ++++++++++++++++++++++++------------------ python/qemu/qmp.py | 44 ++++++++++--------- python/qemu/qtest.py | 27 +++++++----- 4 files changed, 100 insertions(+), 75 deletions(-) diff --git a/python/qemu/accel.py b/python/qemu/accel.py index 7fabe629208..4325114e51f 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 d32e5599ce3..b870ad62978 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -27,6 +27,7 @@ import tempfile from typing import ( Any, + BinaryIO, Dict, List, Optional, @@ -37,7 +38,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 @@ -60,7 +61,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:: @@ -77,7 +78,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 @@ -111,27 +113,27 @@ 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 [] =20 - def __enter__(self): + def __enter__(self) -> 'QEMUMachine': return self =20 def __exit__(self, @@ -140,14 +142,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 """ @@ -166,7 +169,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 @@ -210,7 +214,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 """ @@ -221,7 +225,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 @@ -231,19 +235,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() @@ -277,7 +281,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') @@ -295,11 +299,11 @@ def _pre_launch(self): nickname=3Dself._name ) =20 - def _post_launch(self): + def _post_launch(self) -> None: if self._qmp: self._qmp.accept() =20 - def _post_shutdown(self): + def _post_shutdown(self) -> None: self._load_io_log() =20 if self._qemu_log_file is not None: @@ -326,7 +330,7 @@ def _post_shutdown(self): =20 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 @@ -350,7 +354,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 """ @@ -371,7 +375,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 """ @@ -441,13 +445,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 @@ -487,7 +491,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. @@ -496,7 +502,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 """ @@ -504,7 +510,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 """ @@ -515,7 +521,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 @@ -545,9 +551,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 @@ -557,7 +565,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 named event from QMP with a timeout. @@ -567,7 +577,7 @@ def events_wait(self, events, timeout=3D60.0): See event_match for details. timeout: QEMUMonitorProtocol.pull_event timeout parameter. """ - 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 @@ -592,20 +602,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 @@ -614,7 +624,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 @@ -645,7 +657,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 1aefc00c93b..1ae535ddb34 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 ae4661d4d3e..df6300cd4f7 100644 --- a/python/qemu/qtest.py +++ b/python/qemu/qtest.py @@ -24,6 +24,8 @@ Optional, Sequence, TextIO, + Tuple, + Union, ) =20 from .machine import QEMUMachine @@ -43,7 +45,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: Union[Tuple[str, str], str], + server: bool =3D False): self._address =3D address self._sock =3D self._get_sock() self._sockfile: Optional[TextIO] =3D None @@ -51,14 +54,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 +70,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 +79,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 +90,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 +123,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 +135,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 +144,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