Received: from eggs.gnu.org ([2001:4830:134:3::10]:56761) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c7P2a-0002uL-41 for qemu-devel@nongnu.org; Thu, 17 Nov 2016 10:55:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c7P2V-0005Kf-DD for qemu-devel@nongnu.org; Thu, 17 Nov 2016 10:55:56 -0500 Received: from mx1.redhat.com ([209.132.183.28]:39828) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c7P2V-0005KA-5D for qemu-devel@nongnu.org; Thu, 17 Nov 2016 10:55:51 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 58BB27EBB2 for ; Thu, 17 Nov 2016 15:55:50 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Thu, 17 Nov 2016 19:54:59 +0400 Message-Id: <20161117155504.21843-13-marcandre.lureau@redhat.com> In-Reply-To: <20161117155504.21843-1-marcandre.lureau@redhat.com> References: <20161117155504.21843-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v5 12/17] qapi: rename QAPIExprError/QAPILineError List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: eblake@redhat.com, armbru@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= There is nothing specific about expressions in this exception, the following patch will use it without expressions. Signed-off-by: Marc-Andr=C3=A9 Lureau --- scripts/qapi.py | 146 ++++++++++++++++++++++++++++----------------------= ------ 1 file changed, 73 insertions(+), 73 deletions(-) diff --git a/scripts/qapi.py b/scripts/qapi.py index 21bc32f..4d1b0e4 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -110,11 +110,11 @@ class QAPISchemaError(Exception): "%s:%d:%d: %s" % (self.fname, self.line, self.col, self.msg) =20 =20 -class QAPIExprError(Exception): - def __init__(self, expr_info, msg): +class QAPILineError(Exception): + def __init__(self, info, msg): Exception.__init__(self) - assert expr_info - self.info =3D expr_info + assert info + self.info =3D info self.msg =3D msg =20 def __str__(self): @@ -140,24 +140,24 @@ class QAPISchemaParser(object): self.accept() =20 while self.tok is not None: - expr_info =3D {'file': fname, 'line': self.line, - 'parent': self.incl_info} + info =3D {'file': fname, 'line': self.line, + 'parent': self.incl_info} expr =3D self.get_expr(False) if isinstance(expr, dict) and "include" in expr: if len(expr) !=3D 1: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Invalid 'include' directive") include =3D expr["include"] if not isinstance(include, str): - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Value of 'include' must be a st= ring") incl_abs_fname =3D os.path.join(os.path.dirname(abs_fnam= e), include) # catch inclusion cycle - inf =3D expr_info + inf =3D info while inf: if incl_abs_fname =3D=3D os.path.abspath(inf['file']= ): - raise QAPIExprError(expr_info, "Inclusion loop f= or %s" + raise QAPILineError(info, "Inclusion loop for %s= " % include) inf =3D inf['parent'] # skip multiple include of the same file @@ -166,14 +166,14 @@ class QAPISchemaParser(object): try: fobj =3D open(incl_abs_fname, 'r') except IOError as e: - raise QAPIExprError(expr_info, + raise QAPILineError(info, '%s: %s' % (e.strerror, include)= ) exprs_include =3D QAPISchemaParser(fobj, previously_incl= uded, - expr_info) + info) self.exprs.extend(exprs_include.exprs) else: expr_elem =3D {'expr': expr, - 'info': expr_info} + 'info': info} self.exprs.append(expr_elem) =20 def accept(self): @@ -375,18 +375,18 @@ valid_name =3D re.compile('^(__[a-zA-Z0-9.-]+_)?' '[a-zA-Z][a-zA-Z0-9_-]*$') =20 =20 -def check_name(expr_info, source, name, allow_optional=3DFalse, +def check_name(info, source, name, allow_optional=3DFalse, enum_member=3DFalse): global valid_name membername =3D name =20 if not isinstance(name, str): - raise QAPIExprError(expr_info, + raise QAPILineError(info, "%s requires a string name" % source) if name.startswith('*'): membername =3D name[1:] if not allow_optional: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "%s does not allow optional name '%s'" % (source, name)) # Enum members can start with a digit, because the generated C @@ -397,7 +397,7 @@ def check_name(expr_info, source, name, allow_optiona= l=3DFalse, # and 'q_obj_*' implicit type names. if not valid_name.match(membername) or \ c_name(membername, False).startswith('q_'): - raise QAPIExprError(expr_info, + raise QAPILineError(info, "%s uses invalid name '%s'" % (source, name)= ) =20 =20 @@ -407,11 +407,11 @@ def add_name(name, info, meta, implicit=3DFalse): # FIXME should reject names that differ only in '_' vs. '.' # vs. '-', because they're liable to clash in generated C. if name in all_names: - raise QAPIExprError(info, + raise QAPILineError(info, "%s '%s' is already defined" % (all_names[name], name)) if not implicit and (name.endswith('Kind') or name.endswith('List'))= : - raise QAPIExprError(info, + raise QAPILineError(info, "%s '%s' should not end in '%s'" % (meta, name, name[-4:])) all_names[name] =3D meta @@ -465,7 +465,7 @@ def is_enum(name): return find_enum(name) is not None =20 =20 -def check_type(expr_info, source, value, allow_array=3DFalse, +def check_type(info, source, value, allow_array=3DFalse, allow_dict=3DFalse, allow_optional=3DFalse, allow_metas=3D[]): global all_names @@ -476,10 +476,10 @@ def check_type(expr_info, source, value, allow_arra= y=3DFalse, # Check if array type for value is okay if isinstance(value, list): if not allow_array: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "%s cannot be an array" % source) if len(value) !=3D 1 or not isinstance(value[0], str): - raise QAPIExprError(expr_info, + raise QAPILineError(info, "%s: array type must contain single type= name" % source) value =3D value[0] @@ -487,58 +487,58 @@ def check_type(expr_info, source, value, allow_arra= y=3DFalse, # Check if type name for value is okay if isinstance(value, str): if value not in all_names: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "%s uses unknown type '%s'" % (source, value)) if not all_names[value] in allow_metas: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "%s cannot use %s type '%s'" % (source, all_names[value], value)) return =20 if not allow_dict: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "%s should be a type name" % source) =20 if not isinstance(value, OrderedDict): - raise QAPIExprError(expr_info, + raise QAPILineError(info, "%s should be a dictionary or type name" % s= ource) =20 # value is a dictionary, check that each member is okay for (key, arg) in value.items(): - check_name(expr_info, "Member of %s" % source, key, + check_name(info, "Member of %s" % source, key, allow_optional=3Dallow_optional) if c_name(key, False) =3D=3D 'u' or c_name(key, False).startswit= h('has_'): - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Member of %s uses reserved name '%s'" % (source, key)) # Todo: allow dictionaries to represent default values of # an optional argument. - check_type(expr_info, "Member '%s' of %s" % (key, source), arg, + check_type(info, "Member '%s' of %s" % (key, source), arg, allow_array=3DTrue, allow_metas=3D['built-in', 'union', 'alternate', 'str= uct', 'enum']) =20 =20 -def check_command(expr, expr_info): +def check_command(expr, info): name =3D expr['command'] boxed =3D expr.get('boxed', False) =20 args_meta =3D ['struct'] if boxed: args_meta +=3D ['union', 'alternate'] - check_type(expr_info, "'data' for command '%s'" % name, + check_type(info, "'data' for command '%s'" % name, expr.get('data'), allow_dict=3Dnot boxed, allow_optional=3D= True, allow_metas=3Dargs_meta) returns_meta =3D ['union', 'struct'] if name in returns_whitelist: returns_meta +=3D ['built-in', 'alternate', 'enum'] - check_type(expr_info, "'returns' for command '%s'" % name, + check_type(info, "'returns' for command '%s'" % name, expr.get('returns'), allow_array=3DTrue, allow_optional=3DTrue, allow_metas=3Dreturns_meta) =20 =20 -def check_event(expr, expr_info): +def check_event(expr, info): global events name =3D expr['event'] boxed =3D expr.get('boxed', False) @@ -547,12 +547,12 @@ def check_event(expr, expr_info): if boxed: meta +=3D ['union', 'alternate'] events.append(name) - check_type(expr_info, "'data' for event '%s'" % name, + check_type(info, "'data' for event '%s'" % name, expr.get('data'), allow_dict=3Dnot boxed, allow_optional=3D= True, allow_metas=3Dmeta) =20 =20 -def check_union(expr, expr_info): +def check_union(expr, info): name =3D expr['union'] base =3D expr.get('base') discriminator =3D expr.get('discriminator') @@ -565,18 +565,18 @@ def check_union(expr, expr_info): enum_define =3D None allow_metas =3D ['built-in', 'union', 'alternate', 'struct', 'en= um'] if base is not None: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Simple union '%s' must not have a base" % name) =20 # Else, it's a flat union. else: # The object must have a string or dictionary 'base'. - check_type(expr_info, "'base' for union '%s'" % name, + check_type(info, "'base' for union '%s'" % name, base, allow_dict=3DTrue, allow_optional=3DTrue, allow_metas=3D['struct']) if not base: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Flat union '%s' must have a base" % name) base_members =3D find_base_members(base) @@ -584,11 +584,11 @@ def check_union(expr, expr_info): =20 # The value of member 'discriminator' must name a non-optional # member of the base struct. - check_name(expr_info, "Discriminator of flat union '%s'" % name, + check_name(info, "Discriminator of flat union '%s'" % name, discriminator) discriminator_type =3D base_members.get(discriminator) if not discriminator_type: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Discriminator '%s' is not a member of b= ase " "struct '%s'" % (discriminator, base)) @@ -596,26 +596,26 @@ def check_union(expr, expr_info): allow_metas =3D ['struct'] # Do not allow string discriminator if not enum_define: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Discriminator '%s' must be of enumerati= on " "type" % discriminator) =20 # Check every branch; don't allow an empty union if len(members) =3D=3D 0: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Union '%s' cannot have empty 'data'" % name= ) for (key, value) in members.items(): - check_name(expr_info, "Member of union '%s'" % name, key) + check_name(info, "Member of union '%s'" % name, key) =20 # Each value must name a known type - check_type(expr_info, "Member '%s' of union '%s'" % (key, name), + check_type(info, "Member '%s' of union '%s'" % (key, name), value, allow_array=3Dnot base, allow_metas=3Dallow_me= tas) =20 # If the discriminator names an enum type, then all members # of 'data' must also be members of the enum type. if enum_define: if key not in enum_define['enum_values']: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Discriminator value '%s' is not fou= nd in " "enum '%s'" % (key, enum_define["enum_name"])) @@ -624,64 +624,64 @@ def check_union(expr, expr_info): if enum_define: for value in enum_define['enum_values']: if value not in members.keys(): - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Union '%s' data missing '%s' branch= " % (name, value)) =20 =20 -def check_alternate(expr, expr_info): +def check_alternate(expr, info): name =3D expr['alternate'] members =3D expr['data'] types_seen =3D {} =20 # Check every branch; require at least two branches if len(members) < 2: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Alternate '%s' should have at least two bra= nches " "in 'data'" % name) for (key, value) in members.items(): - check_name(expr_info, "Member of alternate '%s'" % name, key) + check_name(info, "Member of alternate '%s'" % name, key) =20 # Ensure alternates have no type conflicts. - check_type(expr_info, "Member '%s' of alternate '%s'" % (key, na= me), + check_type(info, "Member '%s' of alternate '%s'" % (key, name), value, allow_metas=3D['built-in', 'union', 'struct', 'enum']= ) qtype =3D find_alternate_member_qtype(value) if not qtype: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Alternate '%s' member '%s' cannot use " "type '%s'" % (name, key, value)) if qtype in types_seen: - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Alternate '%s' member '%s' can't " "be distinguished from member '%s'" % (name, key, types_seen[qtype])) types_seen[qtype] =3D key =20 =20 -def check_enum(expr, expr_info): +def check_enum(expr, info): name =3D expr['enum'] members =3D expr.get('data') prefix =3D expr.get('prefix') =20 if not isinstance(members, list): - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Enum '%s' requires an array for 'data'" % n= ame) if prefix is not None and not isinstance(prefix, str): - raise QAPIExprError(expr_info, + raise QAPILineError(info, "Enum '%s' requires a string for 'prefix'" %= name) for member in members: - check_name(expr_info, "Member of enum '%s'" % name, member, + check_name(info, "Member of enum '%s'" % name, member, enum_member=3DTrue) =20 =20 -def check_struct(expr, expr_info): +def check_struct(expr, info): name =3D expr['struct'] members =3D expr['data'] =20 - check_type(expr_info, "'data' for struct '%s'" % name, members, + check_type(info, "'data' for struct '%s'" % name, members, allow_dict=3DTrue, allow_optional=3DTrue) - check_type(expr_info, "'base' for struct '%s'" % name, expr.get('bas= e'), + check_type(info, "'base' for struct '%s'" % name, expr.get('base'), allow_metas=3D['struct']) =20 =20 @@ -690,25 +690,25 @@ def check_keys(expr_elem, meta, required, optional=3D= []): info =3D expr_elem['info'] name =3D expr[meta] if not isinstance(name, str): - raise QAPIExprError(info, + raise QAPILineError(info, "'%s' key must have a string value" % meta) required =3D required + [meta] for (key, value) in expr.items(): if key not in required and key not in optional: - raise QAPIExprError(info, + raise QAPILineError(info, "Unknown key '%s' in %s '%s'" % (key, meta, name)) if (key =3D=3D 'gen' or key =3D=3D 'success-response') and value= is not False: - raise QAPIExprError(info, + raise QAPILineError(info, "'%s' of %s '%s' should only use false v= alue" % (key, meta, name)) if key =3D=3D 'boxed' and value is not True: - raise QAPIExprError(info, + raise QAPILineError(info, "'%s' of %s '%s' should only use true va= lue" % (key, meta, name)) for key in required: if key not in expr: - raise QAPIExprError(info, + raise QAPILineError(info, "Key '%s' is missing from %s '%s'" % (key, meta, name)) =20 @@ -743,7 +743,7 @@ def check_exprs(exprs): check_keys(expr_elem, 'event', [], ['data', 'boxed']) add_name(expr['event'], info, 'event') else: - raise QAPIExprError(expr_elem['info'], + raise QAPILineError(expr_elem['info'], "Expression is missing metatype") =20 # Try again for hidden UnionKind enum @@ -978,7 +978,7 @@ class QAPISchemaObjectType(QAPISchemaType): =20 def check(self, schema): if self.members is False: # check for cycles - raise QAPIExprError(self.info, + raise QAPILineError(self.info, "Object %s contains itself" % self.name) if self.members: return @@ -1051,10 +1051,10 @@ class QAPISchemaMember(object): def check_clash(self, info, seen): cname =3D c_name(self.name) if cname.lower() !=3D cname and self.owner not in case_whitelist= : - raise QAPIExprError(info, + raise QAPILineError(info, "%s should not use uppercase" % self.des= cribe()) if cname in seen: - raise QAPIExprError(info, + raise QAPILineError(info, "%s collides with %s" % (self.describe(), seen[cname].describe= ())) seen[cname] =3D self @@ -1201,13 +1201,13 @@ class QAPISchemaCommand(QAPISchemaEntity): self.arg_type.check(schema) if self.boxed: if self.arg_type.is_empty(): - raise QAPIExprError(self.info, + raise QAPILineError(self.info, "Cannot use 'boxed' with empty t= ype") else: assert not isinstance(self.arg_type, QAPISchemaAlternate= Type) assert not self.arg_type.variants elif self.boxed: - raise QAPIExprError(self.info, + raise QAPILineError(self.info, "Use of 'boxed' requires 'data'") if self._ret_type_name: self.ret_type =3D schema.lookup_type(self._ret_type_name) @@ -1235,13 +1235,13 @@ class QAPISchemaEvent(QAPISchemaEntity): self.arg_type.check(schema) if self.boxed: if self.arg_type.is_empty(): - raise QAPIExprError(self.info, + raise QAPILineError(self.info, "Cannot use 'boxed' with empty t= ype") else: assert not isinstance(self.arg_type, QAPISchemaAlternate= Type) assert not self.arg_type.variants elif self.boxed: - raise QAPIExprError(self.info, + raise QAPILineError(self.info, "Use of 'boxed' requires 'data'") =20 def visit(self, visitor): @@ -1258,7 +1258,7 @@ class QAPISchema(object): self._predefining =3D False self._def_exprs() self.check() - except (QAPISchemaError, QAPIExprError) as err: + except (QAPISchemaError, QAPILineError) as err: print >>sys.stderr, err exit(1) =20 --=20 2.10.0