From nobody Fri Dec 19 22:01:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539613079334657.300175701412; Mon, 15 Oct 2018 07:17:59 -0700 (PDT) Received: from localhost ([::1]:52562 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gC3gr-0001Ii-Bu for importer@patchew.org; Mon, 15 Oct 2018 10:17:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48714) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gC3eT-00006o-1w for qemu-devel@nongnu.org; Mon, 15 Oct 2018 10:15:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gC3eK-0007QL-OJ for qemu-devel@nongnu.org; Mon, 15 Oct 2018 10:15:18 -0400 Received: from mx1.redhat.com ([209.132.183.28]:30643) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gC3eH-0007Ns-Jr; Mon, 15 Oct 2018 10:15:09 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id ED08030B96D4; Mon, 15 Oct 2018 14:15:08 +0000 (UTC) Received: from localhost (ovpn-204-236.brq.redhat.com [10.40.204.236]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 53082451D; Mon, 15 Oct 2018 14:15:08 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 15 Oct 2018 16:14:49 +0200 Message-Id: <20181015141453.32632-6-mreitz@redhat.com> In-Reply-To: <20181015141453.32632-1-mreitz@redhat.com> References: <20181015141453.32632-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Mon, 15 Oct 2018 14:15:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 5/9] iotests: Different iterator behavior in Python 3 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Cleber Rosa , qemu-devel@nongnu.org, Eduardo Habkost , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In Python 3, several functions now return iterators instead of lists. This includes range(), items(), map(), and filter(). This means that if we really want a list, we have to wrap those instances with list(). On the other hand, sometimes we do just want an iterator, in which case we have sometimes used xrange() and iteritems() which no longer exist in Python 3. Just change these calls to be range() and items(), which costs a bit of performance in Python 2, but will do the right thing in Python 3 (which is what is important). In one instance, we only wanted the first instance of the result of a filter() call. Instead of using next(filter()) which would work only in Python 3, or list(filter())[0] which would work everywhere but is a bit weird, this instance is changed to a single-line for with next() wrapped around, which works both in 2.7 and 3. Signed-off-by: Max Reitz Reviewed-by: Cleber Rosa Reviewed-by: Eduardo Habkost --- tests/qemu-iotests/044 | 12 ++++++------ tests/qemu-iotests/056 | 2 +- tests/qemu-iotests/065 | 4 ++-- tests/qemu-iotests/124 | 4 ++-- tests/qemu-iotests/139 | 2 +- tests/qemu-iotests/163 | 6 +++--- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/qemu-iotests/044 b/tests/qemu-iotests/044 index 7ef5e46fe9..e2d6c9b189 100755 --- a/tests/qemu-iotests/044 +++ b/tests/qemu-iotests/044 @@ -52,23 +52,23 @@ class TestRefcountTableGrowth(iotests.QMPTestCase): # Write a refcount table fd.seek(off_reftable) =20 - for i in xrange(0, h.refcount_table_clusters): + for i in range(0, h.refcount_table_clusters): sector =3D b''.join(struct.pack('>Q', off_refblock + i * 64 * 512 + j * 512) - for j in xrange(0, 64)) + for j in range(0, 64)) fd.write(sector) =20 # Write the refcount blocks assert(fd.tell() =3D=3D off_refblock) sector =3D b''.join(struct.pack('>H', 1) for j in range(0, 64 = * 256)) - for block in xrange(0, h.refcount_table_clusters): + for block in range(0, h.refcount_table_clusters): fd.write(sector) =20 # Write the L1 table assert(fd.tell() =3D=3D off_l1) assert(off_l2 + 512 * h.l1_size =3D=3D off_data) table =3D b''.join(struct.pack('>Q', (1 << 63) | off_l2 + 512 = * j) - for j in xrange(0, h.l1_size)) + for j in range(0, h.l1_size)) fd.write(table) =20 # Write the L2 tables @@ -79,14 +79,14 @@ class TestRefcountTableGrowth(iotests.QMPTestCase): off =3D off_data while remaining > 1024 * 512: pytable =3D list((1 << 63) | off + 512 * j - for j in xrange(0, 1024)) + for j in range(0, 1024)) table =3D struct.pack('>1024Q', *pytable) fd.write(table) remaining =3D remaining - 1024 * 512 off =3D off + 1024 * 512 =20 table =3D b''.join(struct.pack('>Q', (1 << 63) | off + 512 * j) - for j in xrange(0, remaining // 512)) + for j in range(0, remaining // 512)) fd.write(table) =20 =20 diff --git a/tests/qemu-iotests/056 b/tests/qemu-iotests/056 index 223292175a..3df323984d 100755 --- a/tests/qemu-iotests/056 +++ b/tests/qemu-iotests/056 @@ -32,7 +32,7 @@ target_img =3D os.path.join(iotests.test_dir, 'target.img= ') def img_create(img, fmt=3Diotests.imgfmt, size=3D'64M', **kwargs): fullname =3D os.path.join(iotests.test_dir, '%s.%s' % (img, fmt)) optargs =3D [] - for k,v in kwargs.iteritems(): + for k,v in kwargs.items(): optargs =3D optargs + ['-o', '%s=3D%s' % (k,v)] args =3D ['create', '-f', fmt] + optargs + [fullname, size] iotests.qemu_img(*args) diff --git a/tests/qemu-iotests/065 b/tests/qemu-iotests/065 index 72aa9707c7..a339bf6069 100755 --- a/tests/qemu-iotests/065 +++ b/tests/qemu-iotests/065 @@ -59,7 +59,7 @@ class TestQemuImgInfo(TestImageInfoSpecific): :data.index('')] for field in data: self.assertTrue(re.match('^ {4}[^ ]', field) is not None) - data =3D map(lambda line: line.strip(), data) + data =3D list(map(lambda line: line.strip(), data)) self.assertEqual(data, self.human_compare) =20 class TestQMP(TestImageInfoSpecific): @@ -80,7 +80,7 @@ class TestQMP(TestImageInfoSpecific): =20 def test_qmp(self): result =3D self.vm.qmp('query-block')['return'] - drive =3D filter(lambda drive: drive['device'] =3D=3D 'drive0', re= sult)[0] + drive =3D next(drive for drive in result if drive['device'] =3D=3D= 'drive0') data =3D drive['inserted']['image']['format-specific'] self.assertEqual(data['type'], iotests.imgfmt) self.assertEqual(data['data'], self.compare) diff --git a/tests/qemu-iotests/124 b/tests/qemu-iotests/124 index 3ea4ac53f5..9f189e3b54 100755 --- a/tests/qemu-iotests/124 +++ b/tests/qemu-iotests/124 @@ -39,7 +39,7 @@ def try_remove(img): def transaction_action(action, **kwargs): return { 'type': action, - 'data': dict((k.replace('_', '-'), v) for k, v in kwargs.iteritems= ()) + 'data': dict((k.replace('_', '-'), v) for k, v in kwargs.items()) } =20 =20 @@ -134,7 +134,7 @@ class TestIncrementalBackupBase(iotests.QMPTestCase): def img_create(self, img, fmt=3Diotests.imgfmt, size=3D'64M', parent=3DNone, parentFormat=3DNone, **kwargs): optargs =3D [] - for k,v in kwargs.iteritems(): + for k,v in kwargs.items(): optargs =3D optargs + ['-o', '%s=3D%s' % (k,v)] args =3D ['create', '-f', fmt] + optargs + [img, size] if parent: diff --git a/tests/qemu-iotests/139 b/tests/qemu-iotests/139 index cc7fe337f3..e00f10b8c8 100755 --- a/tests/qemu-iotests/139 +++ b/tests/qemu-iotests/139 @@ -51,7 +51,7 @@ class TestBlockdevDel(iotests.QMPTestCase): # Check whether a BlockDriverState exists def checkBlockDriverState(self, node, must_exist =3D True): result =3D self.vm.qmp('query-named-block-nodes') - nodes =3D filter(lambda x: x['node-name'] =3D=3D node, result['ret= urn']) + nodes =3D list(filter(lambda x: x['node-name'] =3D=3D node, result= ['return'])) self.assertLessEqual(len(nodes), 1) self.assertEqual(must_exist, len(nodes) =3D=3D 1) =20 diff --git a/tests/qemu-iotests/163 b/tests/qemu-iotests/163 index 5fd424761b..35c1a2bafc 100755 --- a/tests/qemu-iotests/163 +++ b/tests/qemu-iotests/163 @@ -41,7 +41,7 @@ class ShrinkBaseClass(iotests.QMPTestCase): div_roundup =3D lambda n, d: (n + d - 1) // d =20 def split_by_n(data, n): - for x in xrange(0, len(data), n): + for x in range(0, len(data), n): yield struct.unpack('>Q', data[x:x + n])[0] & l1_mask =20 def check_l1_table(h, l1_data): @@ -135,8 +135,8 @@ class ShrinkBaseClass(iotests.QMPTestCase): self.image_verify() =20 def test_random_write(self): - offs_list =3D range(0, size_to_int(self.image_len), - size_to_int(self.chunk_size)) + offs_list =3D list(range(0, size_to_int(self.image_len), + size_to_int(self.chunk_size))) random.shuffle(offs_list) for offs in offs_list: qemu_io('-c', 'write -P 0xff %d %s' % (offs, self.chunk_size), --=20 2.17.1