From nobody Fri Nov 14 18:03:34 2025 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=1589436166; cv=none; d=zohomail.com; s=zohoarc; b=ZNSh/BG6QyrgdS/qmvBdZp5FGEQzYnFHCa7IyX7JeChZptAwUvVCL8YYLekW4RPw99LyPkU7rRCoop3Ip3doXrYkiuvBGfoHHXXC6BoB/0zBEeNXfAOmk6G0uWYknt1KO2aw7GmH82/fEs56K3yJb5PBp5DFsjEfTVHSPniT2jU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589436166; 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=qAjh6Nw65iVmFi5ZvQEKe7Hzst3+3DUhD23waIF+BD4=; b=KzGqLoSMn2WyJS2xKZ3Yv+6JlhrXe/8pz9BklFh36Oo3cQPUWNbecrL0Zk0F0G/a3oVQbWFQc6fFm6fzXTICKx1Ll2N1lMN0AFYHJZS0XViRGPFO+nZ5mXLtffPwMEh/janOMsDpCWNBWTaDmm37YnGCGDl8PkB76v+Ps4cn2J8= 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 1589436166496738.875986676358; Wed, 13 May 2020 23:02:46 -0700 (PDT) Received: from localhost ([::1]:33356 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jZ6xB-00062V-45 for importer@patchew.org; Thu, 14 May 2020 02:02:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36674) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jZ6pm-00067v-Gb for qemu-devel@nongnu.org; Thu, 14 May 2020 01:55:06 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:51729 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 1jZ6pl-0001ox-E6 for qemu-devel@nongnu.org; Thu, 14 May 2020 01:55: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-181-FTgmNXz1NDeUt3KrOWLf5A-1; Thu, 14 May 2020 01:55:02 -0400 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 A8F09BFC2; Thu, 14 May 2020 05:55:01 +0000 (UTC) Received: from probe.redhat.com (ovpn-113-9.rdu2.redhat.com [10.10.113.9]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4CC7B6A977; Thu, 14 May 2020 05:55:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1589435704; 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=qAjh6Nw65iVmFi5ZvQEKe7Hzst3+3DUhD23waIF+BD4=; b=M6Iltt/2lZ1S/8CrpGP0w1ESUUrKRyoOuPOqBo+WyEkTIk1p0UQZtU3A9TZJfoeo73lXQn fN8p8BX9zZrsad3EPXEndJ1eLGY5zXkG9Y9Lwpq7IbvjMzseoS1dJ40RDGGyV20pn3zSTP lEWHLmx+I0Mu98TZPjwxF7vLjNEFt4c= X-MC-Unique: FTgmNXz1NDeUt3KrOWLf5A-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH RFC 30/32] python/qemu/lib: make 'args' style arguments immutable Date: Thu, 14 May 2020 01:54:01 -0400 Message-Id: <20200514055403.18902-31-jsnow@redhat.com> In-Reply-To: <20200514055403.18902-1-jsnow@redhat.com> References: <20200514055403.18902-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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/05/13 22:25:46 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: Fam Zheng , Kevin Wolf , Vladimir Sementsov-Ogievskiy , Eduardo Habkost , qemu-block@nongnu.org, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Markus Armbruster , Max Reitz , John Snow , Cleber Rosa , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= 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 --- python/qemu/lib/machine.py | 30 +++++++++++++++++------------- python/qemu/lib/qtest.py | 16 ++++++++++++---- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/python/qemu/lib/machine.py b/python/qemu/lib/machine.py index fb1a02b53c..ec2bb28b86 100644 --- a/python/qemu/lib/machine.py +++ b/python/qemu/lib/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 @@ -323,7 +323,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 @@ -343,8 +343,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/lib/qtest.py b/python/qemu/lib/qtest.py index 05c63a1d58..ae4661d4d3 100644 --- a/python/qemu/lib/qtest.py +++ b/python/qemu/lib/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.1