All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Cleber Rosa" <crosa@redhat.com>, "Kevin Wolf" <kwolf@redhat.com>,
	kvm@vger.kernel.org, "Richard Henderson" <rth@twiddle.net>,
	"Fam Zheng" <fam@euphon.net>,
	"Eduardo Habkost" <ehabkost@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Max Reitz" <mreitz@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	qemu-block@nongnu.org, "John Snow" <jsnow@redhat.com>
Subject: [PULL 13/25] python/qemu: fix socket.makefile() typing
Date: Sun, 31 May 2020 18:38:34 +0200	[thread overview]
Message-ID: <20200531163846.25363-14-philmd@redhat.com> (raw)
In-Reply-To: <20200531163846.25363-1-philmd@redhat.com>

From: John Snow <jsnow@redhat.com>

Note:

A bug in typeshed (https://github.com/python/typeshed/issues/3977)
misinterprets the type of makefile(). Work around this by explicitly
stating that we are opening a text-mode file.

Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20200514055403.18902-13-jsnow@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 python/qemu/qmp.py   | 10 +++++++---
 python/qemu/qtest.py | 12 ++++++++----
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py
index 6ae7693965..73d49050ed 100644
--- a/python/qemu/qmp.py
+++ b/python/qemu/qmp.py
@@ -11,6 +11,10 @@
 import errno
 import socket
 import logging
+from typing import (
+    Optional,
+    TextIO,
+)
 
 
 class QMPError(Exception):
@@ -61,7 +65,7 @@ def __init__(self, address, server=False, nickname=None):
         self.__events = []
         self.__address = address
         self.__sock = self.__get_sock()
-        self.__sockfile = None
+        self.__sockfile: Optional[TextIO] = None
         self._nickname = nickname
         if self._nickname:
             self.logger = logging.getLogger('QMP').getChild(self._nickname)
@@ -157,7 +161,7 @@ def connect(self, negotiate=True):
         @raise QMPCapabilitiesError if fails to negotiate capabilities
         """
         self.__sock.connect(self.__address)
-        self.__sockfile = self.__sock.makefile()
+        self.__sockfile = self.__sock.makefile(mode='r')
         if negotiate:
             return self.__negotiate_capabilities()
         return None
@@ -180,7 +184,7 @@ def accept(self, timeout=15.0):
         """
         self.__sock.settimeout(timeout)
         self.__sock, _ = self.__sock.accept()
-        self.__sockfile = self.__sock.makefile()
+        self.__sockfile = self.__sock.makefile(mode='r')
         return self.__negotiate_capabilities()
 
     def cmd_obj(self, qmp_cmd):
diff --git a/python/qemu/qtest.py b/python/qemu/qtest.py
index 7943487c2b..4c88590eb0 100644
--- a/python/qemu/qtest.py
+++ b/python/qemu/qtest.py
@@ -19,6 +19,7 @@
 
 import socket
 import os
+from typing import Optional, TextIO
 
 from .machine import QEMUMachine
 
@@ -40,7 +41,7 @@ class QEMUQtestProtocol:
     def __init__(self, address, server=False):
         self._address = address
         self._sock = self._get_sock()
-        self._sockfile = None
+        self._sockfile: Optional[TextIO] = None
         if server:
             self._sock.bind(self._address)
             self._sock.listen(1)
@@ -59,7 +60,7 @@ def connect(self):
         @raise socket.error on socket connection errors
         """
         self._sock.connect(self._address)
-        self._sockfile = self._sock.makefile()
+        self._sockfile = self._sock.makefile(mode='r')
 
     def accept(self):
         """
@@ -68,7 +69,7 @@ def accept(self):
         @raise socket.error on socket connection errors
         """
         self._sock, _ = self._sock.accept()
-        self._sockfile = self._sock.makefile()
+        self._sockfile = self._sock.makefile(mode='r')
 
     def cmd(self, qtest_cmd):
         """
@@ -76,6 +77,7 @@ def cmd(self, qtest_cmd):
 
         @param qtest_cmd: qtest command text to be sent
         """
+        assert self._sockfile is not None
         self._sock.sendall((qtest_cmd + "\n").encode('utf-8'))
         resp = self._sockfile.readline()
         return resp
@@ -83,7 +85,9 @@ def cmd(self, qtest_cmd):
     def close(self):
         """Close this socket."""
         self._sock.close()
-        self._sockfile.close()
+        if self._sockfile:
+            self._sockfile.close()
+            self._sockfile = None
 
     def settimeout(self, timeout):
         """Set a timeout, in seconds."""
-- 
2.21.3


WARNING: multiple messages have this Message-ID (diff)
From: "Philippe Mathieu-Daudé" <philmd@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Kevin Wolf" <kwolf@redhat.com>, "Fam Zheng" <fam@euphon.net>,
	"Eduardo Habkost" <ehabkost@redhat.com>,
	kvm@vger.kernel.org, "Philippe Mathieu-Daudé" <philmd@redhat.com>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Max Reitz" <mreitz@redhat.com>, "John Snow" <jsnow@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Cleber Rosa" <crosa@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	qemu-block@nongnu.org, "Alex Bennée" <alex.bennee@linaro.org>,
	"Richard Henderson" <rth@twiddle.net>
Subject: [PULL 13/25] python/qemu: fix socket.makefile() typing
Date: Sun, 31 May 2020 18:38:34 +0200	[thread overview]
Message-ID: <20200531163846.25363-14-philmd@redhat.com> (raw)
In-Reply-To: <20200531163846.25363-1-philmd@redhat.com>

From: John Snow <jsnow@redhat.com>

Note:

A bug in typeshed (https://github.com/python/typeshed/issues/3977)
misinterprets the type of makefile(). Work around this by explicitly
stating that we are opening a text-mode file.

Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20200514055403.18902-13-jsnow@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 python/qemu/qmp.py   | 10 +++++++---
 python/qemu/qtest.py | 12 ++++++++----
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py
index 6ae7693965..73d49050ed 100644
--- a/python/qemu/qmp.py
+++ b/python/qemu/qmp.py
@@ -11,6 +11,10 @@
 import errno
 import socket
 import logging
+from typing import (
+    Optional,
+    TextIO,
+)
 
 
 class QMPError(Exception):
@@ -61,7 +65,7 @@ def __init__(self, address, server=False, nickname=None):
         self.__events = []
         self.__address = address
         self.__sock = self.__get_sock()
-        self.__sockfile = None
+        self.__sockfile: Optional[TextIO] = None
         self._nickname = nickname
         if self._nickname:
             self.logger = logging.getLogger('QMP').getChild(self._nickname)
@@ -157,7 +161,7 @@ def connect(self, negotiate=True):
         @raise QMPCapabilitiesError if fails to negotiate capabilities
         """
         self.__sock.connect(self.__address)
-        self.__sockfile = self.__sock.makefile()
+        self.__sockfile = self.__sock.makefile(mode='r')
         if negotiate:
             return self.__negotiate_capabilities()
         return None
@@ -180,7 +184,7 @@ def accept(self, timeout=15.0):
         """
         self.__sock.settimeout(timeout)
         self.__sock, _ = self.__sock.accept()
-        self.__sockfile = self.__sock.makefile()
+        self.__sockfile = self.__sock.makefile(mode='r')
         return self.__negotiate_capabilities()
 
     def cmd_obj(self, qmp_cmd):
diff --git a/python/qemu/qtest.py b/python/qemu/qtest.py
index 7943487c2b..4c88590eb0 100644
--- a/python/qemu/qtest.py
+++ b/python/qemu/qtest.py
@@ -19,6 +19,7 @@
 
 import socket
 import os
+from typing import Optional, TextIO
 
 from .machine import QEMUMachine
 
@@ -40,7 +41,7 @@ class QEMUQtestProtocol:
     def __init__(self, address, server=False):
         self._address = address
         self._sock = self._get_sock()
-        self._sockfile = None
+        self._sockfile: Optional[TextIO] = None
         if server:
             self._sock.bind(self._address)
             self._sock.listen(1)
@@ -59,7 +60,7 @@ def connect(self):
         @raise socket.error on socket connection errors
         """
         self._sock.connect(self._address)
-        self._sockfile = self._sock.makefile()
+        self._sockfile = self._sock.makefile(mode='r')
 
     def accept(self):
         """
@@ -68,7 +69,7 @@ def accept(self):
         @raise socket.error on socket connection errors
         """
         self._sock, _ = self._sock.accept()
-        self._sockfile = self._sock.makefile()
+        self._sockfile = self._sock.makefile(mode='r')
 
     def cmd(self, qtest_cmd):
         """
@@ -76,6 +77,7 @@ def cmd(self, qtest_cmd):
 
         @param qtest_cmd: qtest command text to be sent
         """
+        assert self._sockfile is not None
         self._sock.sendall((qtest_cmd + "\n").encode('utf-8'))
         resp = self._sockfile.readline()
         return resp
@@ -83,7 +85,9 @@ def cmd(self, qtest_cmd):
     def close(self):
         """Close this socket."""
         self._sock.close()
-        self._sockfile.close()
+        if self._sockfile:
+            self._sockfile.close()
+            self._sockfile = None
 
     def settimeout(self, timeout):
         """Set a timeout, in seconds."""
-- 
2.21.3



  parent reply	other threads:[~2020-05-31 16:40 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-31 16:38 [PULL 00/25] python-next patches for 2020-05-31 Philippe Mathieu-Daudé
2020-05-31 16:38 ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 01/25] scripts/qemugdb: Remove shebang header Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 02/25] scripts/qemu-gdb: Use Python 3 interpreter Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 03/25] scripts/qmp: " Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 04/25] scripts/kvm/vmxcap: Use Python 3 interpreter and add pseudo-main() Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 05/25] scripts/modules/module_block: Use Python 3 interpreter & add pseudo-main Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 06/25] scripts/qmp: Fix shebang and imports Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 07/25] python: remove more instances of sys.version_info Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 08/25] python/qemu/machine: add kill() method Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 09/25] python/qemu/machine: remove logging configuration Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 10/25] python/qemu: delint and add pylintrc Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 11/25] python/qemu: delint; add flake8 config Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 12/25] python/qemu: remove Python2 style super() calls Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` Philippe Mathieu-Daudé [this message]
2020-05-31 16:38   ` [PULL 13/25] python/qemu: fix socket.makefile() typing Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 14/25] python/qemu: Adjust traceback typing Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 15/25] python/qemu/qmp: use True/False for non/blocking modes Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 16/25] python/qemu/qmp: assert sockfile is not None Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 17/25] python/qemu/qtest: Check before accessing _qtest Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 18/25] tests/vm: Pass --debug through for vm-boot-ssh Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 19/25] tests/vm: Add ability to select QEMU from current build Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 20/25] tests/vm: allow wait_ssh() to specify command Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 21/25] tests/migration/guestperf: Use Python 3 interpreter Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 22/25] tests/acceptance/migration.py: Wait for both sides Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 23/25] tests/acceptance: allow console interaction with specific VMs Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 24/25] tests/acceptance: refactor boot_linux_console test to allow code reuse Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-05-31 16:38 ` [PULL 25/25] tests/acceptance: refactor boot_linux " Philippe Mathieu-Daudé
2020-05-31 16:38   ` Philippe Mathieu-Daudé
2020-06-01 12:03 ` [PULL 00/25] python-next patches for 2020-05-31 Peter Maydell
2020-06-01 12:03   ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200531163846.25363-14-philmd@redhat.com \
    --to=philmd@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=armbru@redhat.com \
    --cc=crosa@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=fam@euphon.net \
    --cc=jsnow@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.