From patchwork Fri Dec 27 13:40:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wainer dos Santos Moschetta X-Patchwork-Id: 11311165 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 421DE14DB for ; Fri, 27 Dec 2019 13:42:38 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C237D20882 for ; Fri, 27 Dec 2019 13:42:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="BF6owoGX" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C237D20882 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:35540 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ikpsy-0007wG-2M for patchwork-qemu-devel@patchwork.kernel.org; Fri, 27 Dec 2019 08:42:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58828) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ikprb-0006Hh-7n for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ikprZ-0002yh-Fi for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:10 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:30916 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ikprZ-0002yT-CO for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1577454068; 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=rsnPAQSkWvtcYpvI7N8qYJnicQPbC05o8HCtvE27HQw=; b=BF6owoGXdc8YBo1HWkJ0LiOMMF5KHX8u68s+CE2cARbdnZd8PFzv9a46AQXP3QZRiNtDtw SEjeOd2WpYhz7oGDiFDMZpIkbu4g6AiK26VtDqcNynRZYpzsv72WkvBqPAIgFgki+a6hee gEmOVxTpKOd5Z/pS06cw5FJ1MIullGc= 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-252-71KYVSqFPGW6o9-_KXWFpQ-1; Fri, 27 Dec 2019 08:41:07 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A48E6801E7A for ; Fri, 27 Dec 2019 13:41:06 +0000 (UTC) Received: from localhost.localdomain (ovpn-116-24.gru2.redhat.com [10.97.116.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4962781765; Fri, 27 Dec 2019 13:41:04 +0000 (UTC) From: Wainer dos Santos Moschetta To: qemu-devel@nongnu.org Subject: [PATCH 1/5] python/qemu: qmp: Replace socket.error with OSError Date: Fri, 27 Dec 2019 10:40:57 -0300 Message-Id: <20191227134101.244496-2-wainersm@redhat.com> In-Reply-To: <20191227134101.244496-1-wainersm@redhat.com> References: <20191227134101.244496-1-wainersm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: 71KYVSqFPGW6o9-_KXWFpQ-1 X-Mimecast-Spam-Score: 0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.61 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: ehabkost@redhat.com, crosa@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" The socket.error is deprecated from Python 3.3, instead it is made a link to OSError. This change replaces the occurences of socket.error with OSError. Signed-off-by: Wainer dos Santos Moschetta Reviewed-by: John Snow --- python/qemu/qmp.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index 5c8cf6a056..8c6c9847d0 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -47,7 +47,7 @@ class QEMUMonitorProtocol(object): or a tuple in the form ( address, port ) for a TCP connection @param server: server mode listens on the socket (bool) - @raise socket.error on socket connection errors + @raise OSError on socket connection errors @note No connection is established, this is done by the connect() or accept() methods """ @@ -107,8 +107,8 @@ class QEMUMonitorProtocol(object): self.__sock.setblocking(0) try: self.__json_read() - except socket.error as err: - if err[0] == errno.EAGAIN: + except OSError as err: + if err.errno == errno.EAGAIN: # No data available pass self.__sock.setblocking(1) @@ -133,7 +133,7 @@ class QEMUMonitorProtocol(object): Connect to the QMP Monitor and perform capabilities negotiation. @return QMP greeting dict - @raise socket.error on socket connection errors + @raise OSError on socket connection errors @raise QMPConnectError if the greeting is not received @raise QMPCapabilitiesError if fails to negotiate capabilities """ @@ -147,7 +147,7 @@ class QEMUMonitorProtocol(object): Await connection from QMP Monitor and perform capabilities negotiation. @return QMP greeting dict - @raise socket.error on socket connection errors + @raise OSError on socket connection errors @raise QMPConnectError if the greeting is not received @raise QMPCapabilitiesError if fails to negotiate capabilities """ @@ -167,10 +167,10 @@ class QEMUMonitorProtocol(object): self.logger.debug(">>> %s", qmp_cmd) try: self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8')) - except socket.error as err: - if err[0] == errno.EPIPE: + except OSError as err: + if err.errno == errno.EPIPE: return - raise socket.error(err) + raise err resp = self.__json_read() self.logger.debug("<<< %s", resp) return resp From patchwork Fri Dec 27 13:40:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wainer dos Santos Moschetta X-Patchwork-Id: 11311171 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F1E3514DB for ; Fri, 27 Dec 2019 13:42:46 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C7C7720882 for ; Fri, 27 Dec 2019 13:42:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Ex4ATej+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C7C7720882 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:35546 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ikpt7-00085l-CF for patchwork-qemu-devel@patchwork.kernel.org; Fri, 27 Dec 2019 08:42:45 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58848) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ikprd-0006Ht-5u for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ikprb-00030X-JU for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:13 -0500 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:37503 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ikprb-000300-GA for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1577454070; 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=oOjUyDHbviUeWxs2MNUwGFkBbxmqmMuSESvGijd12zQ=; b=Ex4ATej+lcLDrZJDBmUnkIGoboFfSSofPaiEe/7nLTKrhrgHviZT/iPk8MolmeZuVYRFp3 04BsEIyfmj7O4HW+qvP05walGiFyfQBPhv8b4fTBHqZKxzkfV84k8dAfp5F9VsYtFA/SDn zbXVA+ri8Fcx1JhK64FkPUJmT5Qp4bk= 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-135-vj7bqWw3M-2as7Coi2Z2tw-1; Fri, 27 Dec 2019 08:41:09 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 94E341804193 for ; Fri, 27 Dec 2019 13:41:08 +0000 (UTC) Received: from localhost.localdomain (ovpn-116-24.gru2.redhat.com [10.97.116.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1921A81C10; Fri, 27 Dec 2019 13:41:06 +0000 (UTC) From: Wainer dos Santos Moschetta To: qemu-devel@nongnu.org Subject: [PATCH 2/5] python/qemu: Delint the qmp module Date: Fri, 27 Dec 2019 10:40:58 -0300 Message-Id: <20191227134101.244496-3-wainersm@redhat.com> In-Reply-To: <20191227134101.244496-1-wainersm@redhat.com> References: <20191227134101.244496-1-wainersm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: vj7bqWw3M-2as7Coi2Z2tw-1 X-Mimecast-Spam-Score: 0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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: ehabkost@redhat.com, crosa@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" This clean up the pylint-3 report on qmp: ************* Module qemu.qmp python/qemu/qmp.py:1:0: C0111: Missing module docstring (missing-docstring) python/qemu/qmp.py:17:0: C0111: Missing class docstring (missing-docstring) python/qemu/qmp.py:21:0: C0111: Missing class docstring (missing-docstring) python/qemu/qmp.py:25:0: C0111: Missing class docstring (missing-docstring) python/qemu/qmp.py:29:0: C0111: Missing class docstring (missing-docstring) python/qemu/qmp.py:33:0: C0111: Missing class docstring (missing-docstring) python/qemu/qmp.py:33:0: R0205: Class 'QEMUMonitorProtocol' inherits from object, can be safely removed from bases in python3 (useless-object-inheritance) python/qemu/qmp.py:80:4: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements) python/qemu/qmp.py:131:4: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements) python/qemu/qmp.py:159:4: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements) python/qemu/qmp.py:245:4: C0111: Missing method docstring (missing-docstring) python/qemu/qmp.py:249:4: C0111: Missing method docstring (missing-docstring) python/qemu/qmp.py:252:4: C0111: Missing method docstring (missing-docstring) python/qemu/qmp.py:255:4: C0111: Missing method docstring (missing-docstring) Signed-off-by: Wainer dos Santos Moschetta Reviewed-by: John Snow Reviewed-by: Philippe Mathieu-Daudé --- python/qemu/qmp.py | 51 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index 8c6c9847d0..f4e04a6683 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -1,5 +1,4 @@ -# QEMU Monitor Protocol Python class -# +""" QEMU Monitor Protocol Python class """ # Copyright (C) 2009, 2010 Red Hat Inc. # # Authors: @@ -15,22 +14,34 @@ import logging class QMPError(Exception): - pass + """ + QMP base exception + """ class QMPConnectError(QMPError): - pass + """ + QMP connection exception + """ class QMPCapabilitiesError(QMPError): - pass + """ + QMP negotiate capabilities exception + """ class QMPTimeoutError(QMPError): - pass + """ + QMP timeout exception + """ -class QEMUMonitorProtocol(object): +class QEMUMonitorProtocol: + """ + Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and then + allow to handle commands and events. + """ #: Logger object for debugging messages logger = logging.getLogger('QMP') @@ -81,7 +92,7 @@ class QEMUMonitorProtocol(object): while True: data = self.__sockfile.readline() if not data: - return + return None resp = json.loads(data) if 'event' in resp: self.logger.debug("<<< %s", resp) @@ -132,7 +143,7 @@ class QEMUMonitorProtocol(object): """ Connect to the QMP Monitor and perform capabilities negotiation. - @return QMP greeting dict + @return QMP greeting dict, or None if negotiate is false @raise OSError on socket connection errors @raise QMPConnectError if the greeting is not received @raise QMPCapabilitiesError if fails to negotiate capabilities @@ -141,6 +152,7 @@ class QEMUMonitorProtocol(object): self.__sockfile = self.__sock.makefile() if negotiate: return self.__negotiate_capabilities() + return None def accept(self): """ @@ -169,7 +181,7 @@ class QEMUMonitorProtocol(object): self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8')) except OSError as err: if err.errno == errno.EPIPE: - return + return None raise err resp = self.__json_read() self.logger.debug("<<< %s", resp) @@ -243,14 +255,33 @@ class QEMUMonitorProtocol(object): self.__events = [] def close(self): + """ + Close the socket and socket file. + """ self.__sock.close() self.__sockfile.close() def settimeout(self, timeout): + """ + Set the socket timeout. + + @param timeout (float): timeout in seconds, or None. + @note This is a wrap around socket.settimeout + """ self.__sock.settimeout(timeout) def get_sock_fd(self): + """ + Get the socket file descriptor. + + @return The file descriptor number. + """ return self.__sock.fileno() def is_scm_available(self): + """ + Check if the socket allows for SCM_RIGHTS. + + @return True if SCM_RIGHTS is available, otherwise False. + """ return self.__sock.family == socket.AF_UNIX From patchwork Fri Dec 27 13:40:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wainer dos Santos Moschetta X-Patchwork-Id: 11311167 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5488817EE for ; Fri, 27 Dec 2019 13:42:38 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2A19E20CC7 for ; Fri, 27 Dec 2019 13:42:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="J9LlYsWB" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2A19E20CC7 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:35542 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ikpsy-0007yf-QC for patchwork-qemu-devel@patchwork.kernel.org; Fri, 27 Dec 2019 08:42:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58868) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ikpre-0006J0-Ic for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ikprd-00034p-HQ for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:14 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:49046 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ikprd-00032D-Dd for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1577454072; 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=UDuh+n+0bICDUrGAWdnvNOHlqbxC8cUgCjIl35Pk3/I=; b=J9LlYsWB9IbBIuePmbosoRw6sjX+81YYiDGhl+X25x7+L+RjCsLEd11Nx2D+wpkJ6NBiHS 8AMolSQIup4fLy8Xr4AsLXyEsVuBLRVDwBz49KzEJzw8oj+JsuYTBoXVs+ZF1On4WBbH6C temYaMs3CPKMh7JaZUfddS1eXmGS22o= 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-142-Vaff-dqqNX6FxXurVtQLWQ-1; Fri, 27 Dec 2019 08:41:11 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BA88D180418B for ; Fri, 27 Dec 2019 13:41:10 +0000 (UTC) Received: from localhost.localdomain (ovpn-116-24.gru2.redhat.com [10.97.116.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 30AFC81C11; Fri, 27 Dec 2019 13:41:08 +0000 (UTC) From: Wainer dos Santos Moschetta To: qemu-devel@nongnu.org Subject: [PATCH 3/5] python/qemu: qmp: Make accept()'s timeout configurable Date: Fri, 27 Dec 2019 10:40:59 -0300 Message-Id: <20191227134101.244496-4-wainersm@redhat.com> In-Reply-To: <20191227134101.244496-1-wainersm@redhat.com> References: <20191227134101.244496-1-wainersm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: Vaff-dqqNX6FxXurVtQLWQ-1 X-Mimecast-Spam-Score: 0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.61 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: ehabkost@redhat.com, crosa@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" Currently the timeout of QEMUMonitorProtocol.accept() is hard-coded to 15 seconds. This added the parameter `timeout` so the value can be configured by the user. Signed-off-by: Wainer dos Santos Moschetta Reviewed-by: Philippe Mathieu-Daudé --- python/qemu/qmp.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index f4e04a6683..914b8c6774 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -154,16 +154,17 @@ class QEMUMonitorProtocol: return self.__negotiate_capabilities() return None - def accept(self): + def accept(self, timeout=15): """ Await connection from QMP Monitor and perform capabilities negotiation. + @param timeout (float): timeout in seconds. Default is 15. @return QMP greeting dict @raise OSError on socket connection errors @raise QMPConnectError if the greeting is not received @raise QMPCapabilitiesError if fails to negotiate capabilities """ - self.__sock.settimeout(15) + self.__sock.settimeout(timeout) self.__sock, _ = self.__sock.accept() self.__sockfile = self.__sock.makefile() return self.__negotiate_capabilities() From patchwork Fri Dec 27 13:41:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wainer dos Santos Moschetta X-Patchwork-Id: 11311175 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BD5F7139A for ; Fri, 27 Dec 2019 13:44:48 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 93E5A20882 for ; Fri, 27 Dec 2019 13:44:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Nly/MPxo" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 93E5A20882 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:35562 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ikpv4-0002Bc-RG for patchwork-qemu-devel@patchwork.kernel.org; Fri, 27 Dec 2019 08:44:47 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58898) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ikprh-0006OB-AF for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ikprg-00038d-79 for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:17 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:39633 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ikprg-00037W-3o for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1577454075; 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=1l/tQ5+l+T1XzctYuN14RZwqLGUquGObj/tL5Pe29Ek=; b=Nly/MPxoma6KiJQ7nq2sVhPozUYK4QSVH1kimGlZeH4w0gS0g7RtLmnSF7AFtJRXXECl2+ B5rZjzmpPOOjY7uQR/XBnDFeXqU3yY8T/niW0LOTAgQ05tvLW5Sm1tXWudX0qg9ryLO1Yn JYiz+Sp8UvwSuPamC9rEB4s4L1qShC8= 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-326-BTa-AUmoOjWvixkFlInXAw-1; Fri, 27 Dec 2019 08:41:14 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E0F2D801E7A for ; Fri, 27 Dec 2019 13:41:12 +0000 (UTC) Received: from localhost.localdomain (ovpn-116-24.gru2.redhat.com [10.97.116.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 561CA81C11; Fri, 27 Dec 2019 13:41:11 +0000 (UTC) From: Wainer dos Santos Moschetta To: qemu-devel@nongnu.org Subject: [PATCH 4/5] python/qemu: qmp: Make QEMUMonitorProtocol a context manager Date: Fri, 27 Dec 2019 10:41:00 -0300 Message-Id: <20191227134101.244496-5-wainersm@redhat.com> In-Reply-To: <20191227134101.244496-1-wainersm@redhat.com> References: <20191227134101.244496-1-wainersm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: BTa-AUmoOjWvixkFlInXAw-1 X-Mimecast-Spam-Score: 0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.61 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: ehabkost@redhat.com, crosa@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" This implement the __enter__ and __exit__ functions on QEMUMonitorProtocol class so that it can be used on 'with' statement and the resources will be free up on block end: with QEMUMonitorProtocol(socket_path) as qmp: qmp.connect() qmp.command('query-status') Signed-off-by: Wainer dos Santos Moschetta --- python/qemu/qmp.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index 914b8c6774..6d55f53595 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -139,6 +139,15 @@ class QEMUMonitorProtocol: raise QMPConnectError("Error while reading from socket") self.__sock.settimeout(None) + def __enter__(self): + # Implement context manager enter function. + return self + + def __exit__(self, exc_type, exc_value, exc_traceback): + # Implement context manager exit function. + self.close() + return False + def connect(self, negotiate=True): """ Connect to the QMP Monitor and perform capabilities negotiation. @@ -259,8 +268,10 @@ class QEMUMonitorProtocol: """ Close the socket and socket file. """ - self.__sock.close() - self.__sockfile.close() + if self.__sock: + self.__sock.close() + if self.__sockfile: + self.__sockfile.close() def settimeout(self, timeout): """ From patchwork Fri Dec 27 13:41:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wainer dos Santos Moschetta X-Patchwork-Id: 11311177 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3494A139A for ; Fri, 27 Dec 2019 13:46:33 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0A06220882 for ; Fri, 27 Dec 2019 13:46:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ItWXsap9" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0A06220882 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:35594 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ikpwm-0003D3-4s for patchwork-qemu-devel@patchwork.kernel.org; Fri, 27 Dec 2019 08:46:32 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58918) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ikprl-0006Sd-7E for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ikprj-0003Cx-1D for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:21 -0500 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:60785 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ikpri-0003BI-UJ for qemu-devel@nongnu.org; Fri, 27 Dec 2019 08:41:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1577454077; 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=O/g0/t8tWVEaHSz/dulzIlsqxeIhd6paNZMvmjPyJhI=; b=ItWXsap9m5cWh9rAHfRvJryvBOEQzxDZcuQB62XkCFYsFct3MYmHxTyUxWZu07rv0fD+5A HrVxyUVek+XNgpX0c+30THA54CWIoep9U+LssYQnsSGZ42ccDIDW3UfgB38x4vJdm7qjZg B2JZKYVVUan2mS3uE0V5cJ1q/7hVA7I= 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-77-YwYLRECYPSSJXv11M6rf_g-1; Fri, 27 Dec 2019 08:41:16 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0EA68800D48 for ; Fri, 27 Dec 2019 13:41:15 +0000 (UTC) Received: from localhost.localdomain (ovpn-116-24.gru2.redhat.com [10.97.116.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7A49B81C00; Fri, 27 Dec 2019 13:41:13 +0000 (UTC) From: Wainer dos Santos Moschetta To: qemu-devel@nongnu.org Subject: [PATCH 5/5] python/qemu: qmp: Remove unnused attributes Date: Fri, 27 Dec 2019 10:41:01 -0300 Message-Id: <20191227134101.244496-6-wainersm@redhat.com> In-Reply-To: <20191227134101.244496-1-wainersm@redhat.com> References: <20191227134101.244496-1-wainersm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: YwYLRECYPSSJXv11M6rf_g-1 X-Mimecast-Spam-Score: 0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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: ehabkost@redhat.com, crosa@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" The `error` and `timeout` attributes in QEMUMonitorProtocol are not used, so this delete them. Signed-off-by: Wainer dos Santos Moschetta Reviewed-by: John Snow Reviewed-by: Philippe Mathieu-Daudé --- python/qemu/qmp.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py index 6d55f53595..cddb94bb3c 100644 --- a/python/qemu/qmp.py +++ b/python/qemu/qmp.py @@ -45,10 +45,6 @@ class QEMUMonitorProtocol: #: Logger object for debugging messages logger = logging.getLogger('QMP') - #: Socket's error class - error = socket.error - #: Socket's timeout - timeout = socket.timeout def __init__(self, address, server=False): """