From nobody Mon Feb 9 15:10:30 2026 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=1622680723; cv=none; d=zohomail.com; s=zohoarc; b=b4cSNbmMu9uHlyBOi379dCC02N+DBWXrj1IpTWJOBk9ONnUPNVDkBFy4i2UpOcRHa4hnWlXJ5aDsLI2F4Fx8AGA+tJY3wh5o13EEe6qoxrZ4cg1KudlXt/yf6MhL7msMZzO2ZLX3PqR1hu24WpFpOCsP/xg2CKjTPxrcc5cB/W8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622680723; 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=diSv2aOelwwD3NmaQRLGaZ5QS8R3qiDmQc3DVTR0XyQ=; b=PGfEQKedL8yGuo493uxVLgFYS13Vkb8xnUpKBI8pzIgpw0UF5yCOAUfzOcxc81kJd1G/wrjrmVxN7L8nIY7yU2SKHKcFBooh04NlcS3qr78nZRlIugmGrB5yu7BPdFmgakLzlXHbUJB9/MQyE0KGQ1LH2yvGtAagRbW+B6DEZEk= 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 162268072349012.425923326887869; Wed, 2 Jun 2021 17:38:43 -0700 (PDT) Received: from localhost ([::1]:52066 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lobNi-0006k0-B6 for importer@patchew.org; Wed, 02 Jun 2021 20:38:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55020) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lobMW-00048H-PT for qemu-devel@nongnu.org; Wed, 02 Jun 2021 20:37:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:40593) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lobMU-0004Um-CL for qemu-devel@nongnu.org; Wed, 02 Jun 2021 20:37:28 -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-346-v6lq18vSMNilZ8TWuNNUjA-1; Wed, 02 Jun 2021 20:37:24 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 54415343A2; Thu, 3 Jun 2021 00:37:23 +0000 (UTC) Received: from scv.redhat.com (ovpn-116-137.rdu2.redhat.com [10.10.116.137]) by smtp.corp.redhat.com (Postfix) with ESMTP id 92A5B10013D6; Thu, 3 Jun 2021 00:37:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622680645; 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=diSv2aOelwwD3NmaQRLGaZ5QS8R3qiDmQc3DVTR0XyQ=; b=XKCZ7o0CizJUjQaIiqToFj/9ncVS3akSO82UJz6YKFKdfGyb/TsrkPztRactkhqKLHCfid 9h7iEz1ooUAmDtJ9muRB7MAfixKjbHBFf/w8VmsE8918VIr1gSAUldUtguGLWj2K6BB47H Ed0l/mK6wt7YjIyOIwUGYo8diHXg1Kg= X-MC-Unique: v6lq18vSMNilZ8TWuNNUjA-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v3 03/19] python/qmp: add parse_address classmethod Date: Wed, 2 Jun 2021 20:37:03 -0400 Message-Id: <20210603003719.1321369-4-jsnow@redhat.com> In-Reply-To: <20210603003719.1321369-1-jsnow@redhat.com> References: <20210603003719.1321369-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.371, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no 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: Markus Armbruster , John Snow , "Niteesh G . S ." , Eduardo Habkost , Cleber Rosa 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 takes the place of qmp-shell's __get_address function. It also allows other utilities to share the same parser and syntax for specifying QMP locations. Signed-off-by: John Snow --- python/qemu/qmp/__init__.py | 26 ++++++++++++++++++++++++++ scripts/qmp/qmp-shell | 21 ++------------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/python/qemu/qmp/__init__.py b/python/qemu/qmp/__init__.py index 5fb970f8a80..822c793c320 100644 --- a/python/qemu/qmp/__init__.py +++ b/python/qemu/qmp/__init__.py @@ -92,6 +92,12 @@ def __init__(self, reply: QMPMessage): self.reply =3D reply =20 =20 +class QMPBadPortError(QMPError): + """ + Unable to parse socket address: Port was non-numerical. + """ + + class QEMUMonitorProtocol: """ Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and = then @@ -219,6 +225,26 @@ def __exit__(self, # Implement context manager exit function. self.close() =20 + @classmethod + def parse_address(cls, address: str) -> SocketAddrT: + """ + Parse a string into a QMP address. + + Figure out if the argument is in the port:host form. + If it's not, it's probably a file path. + """ + components =3D address.split(':') + if len(components) =3D=3D 2: + try: + port =3D int(components[1]) + except ValueError: + msg =3D f"Bad port: '{components[1]}' in '{address}'." + raise QMPBadPortError(msg) from None + return (components[0], port) + + # Treat as filepath. + return address + def connect(self, negotiate: bool =3D True) -> Optional[QMPMessage]: """ Connect to the QMP Monitor and perform capabilities negotiation. diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell index b4d06096abd..d5ae8a9b212 100755 --- a/scripts/qmp/qmp-shell +++ b/scripts/qmp/qmp-shell @@ -89,8 +89,6 @@ class QMPCompleter(list): class QMPShellError(Exception): pass =20 -class QMPShellBadPort(QMPShellError): - pass =20 class FuzzyJSON(ast.NodeTransformer): '''This extension of ast.NodeTransformer filters literal "true/false/n= ull" @@ -109,7 +107,7 @@ class FuzzyJSON(ast.NodeTransformer): # _execute_cmd()). Let's design a better one. class QMPShell(qmp.QEMUMonitorProtocol): def __init__(self, address, pretty=3DFalse): - super(QMPShell, self).__init__(self.__get_address(address)) + super(QMPShell, self).__init__(self.parse_address(address)) self._greeting =3D None self._completer =3D None self._pretty =3D pretty @@ -118,21 +116,6 @@ class QMPShell(qmp.QEMUMonitorProtocol): self._histfile =3D os.path.join(os.path.expanduser('~'), '.qmp-shell_history') =20 - def __get_address(self, arg): - """ - Figure out if the argument is in the port:host form, if it's not i= t's - probably a file path. - """ - addr =3D arg.split(':') - if len(addr) =3D=3D 2: - try: - port =3D int(addr[1]) - except ValueError: - raise QMPShellBadPort - return ( addr[0], port ) - # socket path - return arg - def _fill_completion(self): cmds =3D self.cmd('query-commands') if 'error' in cmds: @@ -437,7 +420,7 @@ def main(): =20 if qemu is None: fail_cmdline() - except QMPShellBadPort: + except qmp.QMPBadPortError: die('bad port number in command-line') =20 try: --=20 2.31.1