From nobody Tue Feb 10 16:22:39 2026 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@gnu.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@gnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1506958927155826.0973474158969; Mon, 2 Oct 2017 08:42:07 -0700 (PDT) Received: from localhost ([::1]:52887 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dz2qy-0004Mv-7X for importer@patchew.org; Mon, 02 Oct 2017 11:41:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38684) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dz2ba-0007xv-U7 for qemu-devel@nongnu.org; Mon, 02 Oct 2017 11:26:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dz2bZ-0007ug-KP for qemu-devel@nongnu.org; Mon, 02 Oct 2017 11:26:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34010) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dz2bZ-0007te-Ci for qemu-devel@nongnu.org; Mon, 02 Oct 2017 11:26:01 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6CFE75F73B; Mon, 2 Oct 2017 15:26:00 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-91.ams2.redhat.com [10.36.116.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 03E9A99DE9; Mon, 2 Oct 2017 15:26:00 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id A986611562E0; Mon, 2 Oct 2017 17:25:52 +0200 (CEST) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6CFE75F73B Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=armbru@redhat.com From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 2 Oct 2017 17:25:33 +0200 Message-Id: <20171002152552.27999-14-armbru@redhat.com> In-Reply-To: <20171002152552.27999-1-armbru@redhat.com> References: <20171002152552.27999-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 02 Oct 2017 15:26:00 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC PATCH 13/32] qapi: Use argparse to open schema file X-BeenThere: qemu-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: marcandre.lureau@redhat.com, mdroth@linux.vnet.ibm.com Errors-To: qemu-devel-bounces+importer=patchew.org@gnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" QAPISchema.__init__() opens the schema file. Since it doesn't bother to catch exceptions, an invalid schema argument is reported like this: Traceback (most recent call last): File "../scripts/qapi-commands.py", line 318, in schema =3D QAPISchema(args.schema) File "/work/armbru/qemu/scripts/qapi.py", line 1464, in __init__ parser =3D QAPISchemaParser(open(fname, 'r')) IOError: [Errno 2] No such file or directory: 'nonexistent' Leave it to argparse, which handles the exception like this: usage: qapi-commands.py [-h] [-o OUTPUT_DIR] [-p PREFIX] schema qapi-commands.py: error: argument schema: can't open 'nonexistent': [Er= rno 2] No such file or directory: 'nonexistent' Too verbose for my taste, but let's not second-guess the standard library. Signed-off-by: Markus Armbruster Reviewed-by: Marc-Andr=C3=A9 Lureau --- scripts/qapi.py | 6 +++--- scripts/qapi2texi.py | 2 +- tests/qapi-schema/test-qapi.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/qapi.py b/scripts/qapi.py index 25f6c81b08..a33203e82d 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -1450,9 +1450,9 @@ class QAPISchemaEvent(QAPISchemaEntity): =20 =20 class QAPISchema(object): - def __init__(self, fname): + def __init__(self, file): try: - parser =3D QAPISchemaParser(open(fname, 'r')) + parser =3D QAPISchemaParser(file) self.exprs =3D check_exprs(parser.exprs) self.docs =3D parser.docs self._entity_dict =3D {} @@ -1934,7 +1934,7 @@ def common_argument_parser(builtins=3DFalse): help=3D'output directory') parser.add_argument('-p', '--prefix', default=3D'', type=3Dprefix, help=3D'prefix to add to output files') - parser.add_argument('schema', + parser.add_argument('schema', type=3Dargparse.FileType('r'), help=3D'QAPI schema source file') return parser =20 diff --git a/scripts/qapi2texi.py b/scripts/qapi2texi.py index fd90d8953e..d95d7541a3 100755 --- a/scripts/qapi2texi.py +++ b/scripts/qapi2texi.py @@ -282,7 +282,7 @@ def texi_schema(schema): def main(argv): """Takes schema argument, prints result to stdout""" parser =3D argparse.ArgumentParser() - parser.add_argument('schema', + parser.add_argument('schema', type=3Dargparse.FileType('r'), help=3D'QAPI schema source file') args =3D parser.parse_args() =20 diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py index a7e21d016f..225417d861 100644 --- a/tests/qapi-schema/test-qapi.py +++ b/tests/qapi-schema/test-qapi.py @@ -53,7 +53,7 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): print ' case %s: %s' % (v.name, v.type.name) =20 parser =3D argparse.ArgumentParser() -parser.add_argument('schema', +parser.add_argument('schema', type=3Dargparse.FileType('r'), help=3D'QAPI schema source file') args =3D parser.parse_args() =20 --=20 2.13.6