From nobody Mon Feb 9 08:11:33 2026 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=1600811382; cv=none; d=zohomail.com; s=zohoarc; b=TzMP6CGBykGhIErnVx1gtJwlAlK8bIfrl4umBvAB4NN94s2GZaUnvxWMbzStPVZV9/xQg3wugRIkN2s0aa1E0Kt+EhnSi9o4DCQ5R3TLVCNVxTGyKTu2KpJUAFcVkYURiOGkSVcyc+mu0lPYk7dQGiKStAHLcOR23+34hnNhZQs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600811382; 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=d1w7A9tf/5Qzt1yH408ACdCWC7OW5p+S8aaxkJNuoPE=; b=h5r/we5K+91VFqSu9reftQF52udBZ3BC54EeBpQmU155QgE/0J2f8jU6nnRu2tUXcEvYQaCjY6bni2DU2DhMStuEqf4q2rEx6TULq38ecmLKlbg0E+KpjgRBmr9DnkzdhmSnIZLfjl/1zm490uiOu0wkPt6+WEcQdC0mts20+Eg= 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 1600811382137255.57908703808982; Tue, 22 Sep 2020 14:49:42 -0700 (PDT) Received: from localhost ([::1]:37452 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKqAO-00075s-Rf for importer@patchew.org; Tue, 22 Sep 2020 17:49:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41948) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKpj9-0003L8-4R for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:21:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:30953) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKpj5-000750-JM for qemu-devel@nongnu.org; Tue, 22 Sep 2020 17:21:30 -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-102-g7yzsh0ENwqX1za9HGQXlw-1; Tue, 22 Sep 2020 17:21:22 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E1FE01DE09; Tue, 22 Sep 2020 21:21:21 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1FB0D5DE86; Tue, 22 Sep 2020 21:21:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600809686; 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=d1w7A9tf/5Qzt1yH408ACdCWC7OW5p+S8aaxkJNuoPE=; b=ZLqGX4uNnQwFkGCCuOEd2ryka/C/DNnHVQDI0SjdpnKJ+IPwAVmlhd3syXnMPcZM85AxLB v+8c/JEn8CwXKy69grz5qihFFTyC29DekGYczcNSmwq+XTekoQFUN7wVIWMHg3yb2ccqz9 xXc0XSL5Bm6Tku8M83sK5CbmSqbhprE= X-MC-Unique: g7yzsh0ENwqX1za9HGQXlw-1 From: John Snow To: Markus Armbruster Subject: [PATCH 2/6] qapi/parser.py: remove parser context from QAPIDoc Date: Tue, 22 Sep 2020 17:21:11 -0400 Message-Id: <20200922212115.4084301-3-jsnow@redhat.com> In-Reply-To: <20200922212115.4084301-1-jsnow@redhat.com> References: <20200922212115.4084301-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com 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=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 02:07:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no 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: Michael Roth , qemu-devel@nongnu.org, John Snow , Eduardo Habkost , Cleber Rosa 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" We only need the parser context for the purpose of raising exceptions. What we can do instead is to raise a unique document parsing error and affix additional context from the caller. This way, the document parsing can be isolated. Signed-off-by: John Snow --- scripts/qapi/parser.py | 56 ++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py index 327cf05736..cb2595ce60 100644 --- a/scripts/qapi/parser.py +++ b/scripts/qapi/parser.py @@ -18,10 +18,14 @@ import re from collections import OrderedDict =20 -from .error import QAPIParseError, QAPISemError +from .error import QAPIError, QAPIParseError, QAPISemError from .source import QAPISourceInfo =20 =20 +class QAPIDocError(QAPIError): + """Documentation parsing error.""" + + class QAPISchemaParser: =20 def __init__(self, fname, previously_included=3DNone, incl_info=3DNone= ): @@ -265,13 +269,13 @@ def get_expr(self, nested): self, "expected '{', '[', string, boolean or 'null'") return expr =20 - def get_doc(self, info): + def _get_doc(self, info): if self.val !=3D '##': raise QAPIParseError( self, "junk after '##' at start of documentation comment") =20 docs =3D [] - cur_doc =3D QAPIDoc(self, info) + cur_doc =3D QAPIDoc(info) self.accept(False) while self.tok =3D=3D '#': if self.val.startswith('##'): @@ -292,12 +296,22 @@ def get_doc(self, info): if cur_doc.body.text: cur_doc.end_comment() docs.append(cur_doc) - cur_doc =3D QAPIDoc(self, info) + cur_doc =3D QAPIDoc(info) cur_doc.append(self.val) self.accept(False) =20 raise QAPIParseError(self, "documentation comment must end with '#= #'") =20 + def get_doc(self, info): + try: + return self._get_doc(info) + except QAPIDocError as err: + # Tie the Doc parsing error to our parsing state. The + # resulting error position depends on the state of the + # parser. It happens to be the beginning of the comment. + # More or less servicable, but action at a distance. + raise QAPIParseError(self, str(err)) from err + =20 class QAPIDoc: """ @@ -335,12 +349,7 @@ def __init__(self, name): def connect(self, member): self.member =3D member =20 - def __init__(self, parser, info): - # self._parser is used to report errors with QAPIParseError. The - # resulting error position depends on the state of the parser. - # It happens to be the beginning of the comment. More or less - # servicable, but action at a distance. - self._parser =3D parser + def __init__(self, info): self.info =3D info self.symbol =3D None self.body =3D QAPIDoc.Section() @@ -377,7 +386,7 @@ def append(self, line): return =20 if line[0] !=3D ' ': - raise QAPIParseError(self._parser, "missing space after #") + raise QAPIDocError("missing space after #") line =3D line[1:] self._append_line(line) =20 @@ -411,11 +420,11 @@ def _append_body_line(self, line): # recognized, and get silently treated as ordinary text if not self.symbol and not self.body.text and line.startswith('@'): if not line.endswith(':'): - raise QAPIParseError(self._parser, "line should end with '= :'") + raise QAPIDocError("line should end with ':'") self.symbol =3D line[1:-1] # FIXME invalid names other than the empty string aren't flagg= ed if not self.symbol: - raise QAPIParseError(self._parser, "invalid name") + raise QAPIDocError("invalid name") elif self.symbol: # This is a definition documentation block if name.startswith('@') and name.endswith(':'): @@ -498,9 +507,8 @@ def _append_various_line(self, line): name =3D line.split(' ', 1)[0] =20 if name.startswith('@') and name.endswith(':'): - raise QAPIParseError(self._parser, - "'%s' can't follow '%s' section" - % (name, self.sections[0].name)) + raise QAPIDocError("'%s' can't follow '%s' section" + % (name, self.sections[0].name)) if self._is_section_tag(name): line =3D line[len(name)+1:] self._start_section(name[:-1]) @@ -514,10 +522,9 @@ def _append_various_line(self, line): def _start_symbol_section(self, symbols_dict, name): # FIXME invalid names other than the empty string aren't flagged if not name: - raise QAPIParseError(self._parser, "invalid parameter name") + raise QAPIDocError("invalid parameter name") if name in symbols_dict: - raise QAPIParseError(self._parser, - "'%s' parameter name duplicated" % name) + raise QAPIDocError("'%s' parameter name duplicated" % name) assert not self.sections self._end_section() self._section =3D QAPIDoc.ArgSection(name) @@ -531,8 +538,7 @@ def _start_features_section(self, name): =20 def _start_section(self, name=3DNone): if name in ('Returns', 'Since') and self.has_section(name): - raise QAPIParseError(self._parser, - "duplicated '%s' section" % name) + raise QAPIDocError("duplicated '%s' section" % name) self._end_section() self._section =3D QAPIDoc.Section(name) self.sections.append(self._section) @@ -541,17 +547,15 @@ def _end_section(self): if self._section: text =3D self._section.text =3D self._section.text.strip() if self._section.name and (not text or text.isspace()): - raise QAPIParseError( - self._parser, + raise QAPIDocError( "empty doc section '%s'" % self._section.name) self._section =3D None =20 def _append_freeform(self, line): match =3D re.match(r'(@\S+:)', line) if match: - raise QAPIParseError(self._parser, - "'%s' not allowed in free-form documentat= ion" - % match.group(1)) + raise QAPIDocError("'%s' not allowed in free-form documentatio= n" + % match.group(1)) self._section.append(line) =20 def connect_member(self, member): --=20 2.26.2