[PATCH RFC 29/32] python//qtest.py: Check before accessing _qtest

John Snow posted 32 patches 5 years, 6 months ago
Maintainers: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>, "Philippe Mathieu-Daudé" <philmd@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Fam Zheng <fam@euphon.net>, Eduardo Habkost <ehabkost@redhat.com>, Kevin Wolf <kwolf@redhat.com>, Markus Armbruster <armbru@redhat.com>, Max Reitz <mreitz@redhat.com>, Cleber Rosa <crosa@redhat.com>
[PATCH RFC 29/32] python//qtest.py: Check before accessing _qtest
Posted by John Snow 5 years, 6 months ago
It can be None; so add assertions or exceptions where appropriate to
guard the access accordingly.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 python/qemu/lib/qtest.py | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/python/qemu/lib/qtest.py b/python/qemu/lib/qtest.py
index a8be0c782f..05c63a1d58 100644
--- a/python/qemu/lib/qtest.py
+++ b/python/qemu/lib/qtest.py
@@ -126,7 +126,8 @@ def _pre_launch(self):
         super()._pre_launch()
         self._qtest = QEMUQtestProtocol(self._qtest_path, server=True)
 
-    def _post_launch(self):
+    def _post_launch(self) -> None:
+        assert self._qtest is not None
         super()._post_launch()
         self._qtest.accept()
 
@@ -134,6 +135,13 @@ def _post_shutdown(self):
         super()._post_shutdown()
         self._remove_if_exists(self._qtest_path)
 
-    def qtest(self, cmd):
-        '''Send a qtest command to guest'''
+    def qtest(self, cmd: str) -> str:
+        """
+        Send a qtest command to the guest.
+
+        :param cmd: qtest command to send
+        :return: qtest server response
+        """
+        if self._qtest is None:
+            raise RuntimeError("qtest socket not available")
         return self._qtest.cmd(cmd)
-- 
2.21.1


Re: [PATCH RFC 29/32] python//qtest.py: Check before accessing _qtest
Posted by Philippe Mathieu-Daudé 5 years, 6 months ago
On 5/14/20 7:54 AM, John Snow wrote:
> It can be None; so add assertions or exceptions where appropriate to
> guard the access accordingly.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>   python/qemu/lib/qtest.py | 14 +++++++++++---
>   1 file changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/python/qemu/lib/qtest.py b/python/qemu/lib/qtest.py
> index a8be0c782f..05c63a1d58 100644
> --- a/python/qemu/lib/qtest.py
> +++ b/python/qemu/lib/qtest.py
> @@ -126,7 +126,8 @@ def _pre_launch(self):
>           super()._pre_launch()
>           self._qtest = QEMUQtestProtocol(self._qtest_path, server=True)
>   
> -    def _post_launch(self):
> +    def _post_launch(self) -> None:
> +        assert self._qtest is not None
>           super()._post_launch()
>           self._qtest.accept()
>   
> @@ -134,6 +135,13 @@ def _post_shutdown(self):
>           super()._post_shutdown()
>           self._remove_if_exists(self._qtest_path)
>   
> -    def qtest(self, cmd):
> -        '''Send a qtest command to guest'''
> +    def qtest(self, cmd: str) -> str:
> +        """
> +        Send a qtest command to the guest.
> +
> +        :param cmd: qtest command to send
> +        :return: qtest server response
> +        """
> +        if self._qtest is None:
> +            raise RuntimeError("qtest socket not available")
>           return self._qtest.cmd(cmd)
> 

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>


Re: [PATCH RFC 29/32] python//qtest.py: Check before accessing _qtest
Posted by Philippe Mathieu-Daudé 5 years, 5 months ago
On 5/14/20 7:54 AM, John Snow wrote:
> It can be None; so add assertions or exceptions where appropriate to
> guard the access accordingly.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  python/qemu/lib/qtest.py | 14 +++++++++++---
>  1 file changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/python/qemu/lib/qtest.py b/python/qemu/lib/qtest.py
> index a8be0c782f..05c63a1d58 100644
> --- a/python/qemu/lib/qtest.py
> +++ b/python/qemu/lib/qtest.py
> @@ -126,7 +126,8 @@ def _pre_launch(self):
>          super()._pre_launch()
>          self._qtest = QEMUQtestProtocol(self._qtest_path, server=True)
>  
> -    def _post_launch(self):
> +    def _post_launch(self) -> None:
> +        assert self._qtest is not None
>          super()._post_launch()
>          self._qtest.accept()
>  
> @@ -134,6 +135,13 @@ def _post_shutdown(self):
>          super()._post_shutdown()
>          self._remove_if_exists(self._qtest_path)
>  
> -    def qtest(self, cmd):
> -        '''Send a qtest command to guest'''
> +    def qtest(self, cmd: str) -> str:
> +        """
> +        Send a qtest command to the guest.
> +
> +        :param cmd: qtest command to send
> +        :return: qtest server response
> +        """
> +        if self._qtest is None:
> +            raise RuntimeError("qtest socket not available")
>          return self._qtest.cmd(cmd)
> 

Thanks, applied to my python-next tree:
https://gitlab.com/philmd/qemu/commits/python-next