@@ -2,7 +2,7 @@
# QAPI helper library
#
# Copyright IBM, Corp. 2011
-# Copyright (c) 2013 Red Hat Inc.
+# Copyright (c) 2013-2014 Red Hat Inc.
#
# Authors:
# Anthony Liguori <aliguori@us.ibm.com>
@@ -319,10 +319,18 @@ def check_union(expr, expr_info):
def check_exprs(schema):
for expr_elem in schema.exprs:
expr = expr_elem['expr']
+ info = expr_elem['info']
+ members = expr.get('data')
+
if expr.has_key('union'):
- check_union(expr, expr_elem['info'])
+ check_union(expr, info)
if expr.has_key('event'):
- check_event(expr, expr_elem['info'])
+ check_event(expr, info)
+ if expr.has_key('enum'):
+ if not isinstance(members, list):
+ raise QAPIExprError(info,
+ "enum '%s' requires an array for 'data'"
+ % expr.get('enum'))
def parse_schema(input_file):
try:
@@ -336,7 +344,7 @@ def parse_schema(input_file):
for expr_elem in schema.exprs:
expr = expr_elem['expr']
if expr.has_key('enum'):
- add_enum(expr['enum'], expr['data'])
+ add_enum(expr['enum'], expr.get('data'))
elif expr.has_key('union'):
add_union(expr)
elif expr.has_key('type'):
@@ -1,6 +1 @@
-Traceback (most recent call last):
- File "tests/qapi-schema/test-qapi.py", line 19, in <module>
- exprs = parse_schema(sys.argv[1])
- File "scripts/qapi.py", line 339, in parse_schema
- add_enum(expr['enum'], expr['data'])
-KeyError: 'data'
+tests/qapi-schema/enum-missing-data.json:1: enum 'MyEnum' requires an array for 'data'
@@ -0,0 +1 @@
+tests/qapi-schema/enum-wrong-data.json:1: enum 'MyEnum' requires an array for 'data'
@@ -1 +1 @@
-0
+1
@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'MyEnum'), ('data', OrderedDict([('value', 'str')]))])]
-[{'enum_name': 'MyEnum', 'enum_values': OrderedDict([('value', 'str')])}]
-[]
The previous commit demonstrated that the generator choked if an enum forgot 'data', and silently ignored an enum where 'data' was the wrong type. Fix both cases to give a sane error message. * scripts/qapi.py (parse_schema): Avoid bad deref. (check_exprs): Check for array on enums. * tests/qapi-schema/enum-missing-data.err: Update expected results. * tests/qapi-schema/enum-wrong-data.*: Likewise. Signed-off-by: Eric Blake <eblake@redhat.com> --- scripts/qapi.py | 16 ++++++++++++---- tests/qapi-schema/enum-missing-data.err | 7 +------ tests/qapi-schema/enum-wrong-data.err | 1 + tests/qapi-schema/enum-wrong-data.exit | 2 +- tests/qapi-schema/enum-wrong-data.out | 3 --- 5 files changed, 15 insertions(+), 14 deletions(-)