Our QAPI parser is a hack, replace it
v2 cover Our QAPI parser is a hack, replace it
If you think I'm exaggerating, check out the list of issues in PATCH 3/9.
v2:
* Straightforward rebase
* A few more tests in 1/9, in part suggested by Eric
Markus Armbruster (9):
tests: QAPI schema parser tests
tests: Use qapi-schema-test.json as schema parser test qapi.py: Restructure lexer and parser
qapi.py: Decent syntax error reporting
qapi.py: Reject invalid characters in schema file qapi.py: Fix schema parser to check syntax systematically qapi.py: Fix diagnosing non-objects at a schema's top-level qapi.py: Rename expr_eval to expr in parse_schema() qapi.py: Permit comments starting anywhere on the line
configure | 2 +- scripts/qapi.py | 227 +++++++++++++-------- tests/Makefile | 30 ++- tests/qapi-schema/comments.exit | 1 + tests/qapi-schema/comments.json | 4 + tests/qapi-schema/comments.out | 3 + tests/qapi-schema/empty.exit | 1 + tests/qapi-schema/empty.out | 3 + tests/qapi-schema/funny-char.err | 1 + tests/qapi-schema/funny-char.exit | 1 + tests/qapi-schema/funny-char.json | 2 + tests/qapi-schema/indented-expr.exit | 1 + tests/qapi-schema/indented-expr.json | 2 + tests/qapi-schema/indented-expr.out | 3 + tests/qapi-schema/missing-colon.err | 1 + tests/qapi-schema/missing-colon.exit | 1 + tests/qapi-schema/missing-colon.json | 2 + tests/qapi-schema/missing-comma-list.err | 1 + tests/qapi-schema/missing-comma-list.exit | 1 + tests/qapi-schema/missing-comma-list.json | 2 + tests/qapi-schema/missing-comma-object.err | 1 + tests/qapi-schema/missing-comma-object.exit | 1 + tests/qapi-schema/missing-comma-object.json | 2 + tests/qapi-schema/non-objects.err | 1 + tests/qapi-schema/non-objects.exit | 1 + tests/qapi-schema/non-objects.json | 2 + tests/qapi-schema/qapi-schema-test.exit | 1 + .../qapi-schema/qapi-schema-test.json | 0 tests/qapi-schema/qapi-schema-test.out | 19 ++ tests/qapi-schema/quoted-structural-chars.err | 1 + tests/qapi-schema/quoted-structural-chars.exit | 1 + tests/qapi-schema/quoted-structural-chars.json | 1 + tests/qapi-schema/test-qapi.py | 27 +++ tests/qapi-schema/trailing-comma-list.err | 1 + tests/qapi-schema/trailing-comma-list.exit | 1 + tests/qapi-schema/trailing-comma-list.json | 2 + tests/qapi-schema/trailing-comma-object.err | 1 + tests/qapi-schema/trailing-comma-object.exit | 1 + tests/qapi-schema/trailing-comma-object.json | 2 + tests/qapi-schema/unclosed-list.err | 1 + tests/qapi-schema/unclosed-list.exit | 1 + tests/qapi-schema/unclosed-list.json | 1 + tests/qapi-schema/unclosed-object.err | 1 + tests/qapi-schema/unclosed-object.exit | 1 + tests/qapi-schema/unclosed-object.json | 1 + tests/qapi-schema/unclosed-string.err | 1 + tests/qapi-schema/unclosed-string.exit | 1 + tests/qapi-schema/unclosed-string.json | 2 + 48 files changed, 271 insertions(+), 94 deletions(-) create mode 100644 tests/qapi-schema/comments.err create mode 100644 tests/qapi-schema/comments.exit create mode 100644 tests/qapi-schema/comments.json create mode 100644 tests/qapi-schema/comments.out create mode 100644 tests/qapi-schema/empty.err create mode 100644 tests/qapi-schema/empty.exit create mode 100644 tests/qapi-schema/empty.json create mode 100644 tests/qapi-schema/empty.out create mode 100644 tests/qapi-schema/funny-char.err create mode 100644 tests/qapi-schema/funny-char.exit create mode 100644 tests/qapi-schema/funny-char.json create mode 100644 tests/qapi-schema/funny-char.out create mode 100644 tests/qapi-schema/indented-expr.err create mode 100644 tests/qapi-schema/indented-expr.exit create mode 100644 tests/qapi-schema/indented-expr.json create mode 100644 tests/qapi-schema/indented-expr.out create mode 100644 tests/qapi-schema/missing-colon.err create mode 100644 tests/qapi-schema/missing-colon.exit create mode 100644 tests/qapi-schema/missing-colon.json create mode 100644 tests/qapi-schema/missing-colon.out create mode 100644 tests/qapi-schema/missing-comma-list.err create mode 100644 tests/qapi-schema/missing-comma-list.exit create mode 100644 tests/qapi-schema/missing-comma-list.json create mode 100644 tests/qapi-schema/missing-comma-list.out create mode 100644 tests/qapi-schema/missing-comma-object.err create mode 100644 tests/qapi-schema/missing-comma-object.exit create mode 100644 tests/qapi-schema/missing-comma-object.json create mode 100644 tests/qapi-schema/missing-comma-object.out create mode 100644 tests/qapi-schema/non-objects.err create mode 100644 tests/qapi-schema/non-objects.exit create mode 100644 tests/qapi-schema/non-objects.json create mode 100644 tests/qapi-schema/non-objects.out create mode 100644 tests/qapi-schema/qapi-schema-test.err create mode 100644 tests/qapi-schema/qapi-schema-test.exit rename qapi-schema-test.json => tests/qapi-schema/qapi-schema-test.json (100%) create mode 100644 tests/qapi-schema/qapi-schema-test.out create mode 100644 tests/qapi-schema/quoted-structural-chars.err create mode 100644 tests/qapi-schema/quoted-structural-chars.exit create mode 100644 tests/qapi-schema/quoted-structural-chars.json create mode 100644 tests/qapi-schema/quoted-structural-chars.out create mode 100644 tests/qapi-schema/test-qapi.py create mode 100644 tests/qapi-schema/trailing-comma-list.err create mode 100644 tests/qapi-schema/trailing-comma-list.exit create mode 100644 tests/qapi-schema/trailing-comma-list.json create mode 100644 tests/qapi-schema/trailing-comma-list.out create mode 100644 tests/qapi-schema/trailing-comma-object.err create mode 100644 tests/qapi-schema/trailing-comma-object.exit create mode 100644 tests/qapi-schema/trailing-comma-object.json create mode 100644 tests/qapi-schema/trailing-comma-object.out create mode 100644 tests/qapi-schema/unclosed-list.err create mode 100644 tests/qapi-schema/unclosed-list.exit create mode 100644 tests/qapi-schema/unclosed-list.json create mode 100644 tests/qapi-schema/unclosed-list.out create mode 100644 tests/qapi-schema/unclosed-object.err create mode 100644 tests/qapi-schema/unclosed-object.exit create mode 100644 tests/qapi-schema/unclosed-object.json create mode 100644 tests/qapi-schema/unclosed-object.out create mode 100644 tests/qapi-schema/unclosed-string.err create mode 100644 tests/qapi-schema/unclosed-string.exit create mode 100644 tests/qapi-schema/unclosed-string.json create mode 100644 tests/qapi-schema/unclosed-string.out
Signature · 1 line
1.7.11.7
PATCH v2 1/9 tests: QAPI schema parser tests
The parser handles erroneous input badly. To be improved shortly.
--- configure | 2 +- tests/Makefile | 24 ++++++++++++++++++++++-- tests/qapi-schema/comments.exit | 1 + tests/qapi-schema/comments.json | 4 ++++ tests/qapi-schema/comments.out | 3 +++ tests/qapi-schema/empty.exit | 1 + tests/qapi-schema/empty.out | 3 +++ tests/qapi-schema/funny-char.exit | 1 + tests/qapi-schema/funny-char.json | 2 ++ tests/qapi-schema/funny-char.out | 3 +++ tests/qapi-schema/indented-expr.exit | 1 + tests/qapi-schema/indented-expr.json | 2 ++ tests/qapi-schema/indented-expr.out | 3 +++ tests/qapi-schema/missing-colon.exit | 1 + tests/qapi-schema/missing-colon.json | 2 ++ tests/qapi-schema/missing-colon.out | 3 +++ tests/qapi-schema/missing-comma-list.exit | 1 + tests/qapi-schema/missing-comma-list.json | 2 ++ tests/qapi-schema/missing-comma-list.out | 3 +++ tests/qapi-schema/missing-comma-object.exit | 1 + tests/qapi-schema/missing-comma-object.json | 2 ++ tests/qapi-schema/missing-comma-object.out | 3 +++ tests/qapi-schema/non-objects.err | 1 + tests/qapi-schema/non-objects.exit | 1 + tests/qapi-schema/non-objects.json | 2 ++ tests/qapi-schema/quoted-structural-chars.exit | 1 + tests/qapi-schema/quoted-structural-chars.json | 1 + tests/qapi-schema/quoted-structural-chars.out | 3 +++ tests/qapi-schema/test-qapi.py | 25 +++++++++++++++++++++++++ tests/qapi-schema/trailing-comma-list.exit | 1 + tests/qapi-schema/trailing-comma-list.json | 2 ++ tests/qapi-schema/trailing-comma-list.out | 3 +++ tests/qapi-schema/trailing-comma-object.exit | 1 + tests/qapi-schema/trailing-comma-object.json | 2 ++ tests/qapi-schema/trailing-comma-object.out | 3 +++ tests/qapi-schema/unclosed-list.err | 1 + tests/qapi-schema/unclosed-list.exit | 1 + tests/qapi-schema/unclosed-list.json | 1 + tests/qapi-schema/unclosed-object.err | 1 + tests/qapi-schema/unclosed-object.exit | 1 + tests/qapi-schema/unclosed-object.json | 1 + tests/qapi-schema/unclosed-string.err | 1 + tests/qapi-schema/unclosed-string.exit | 1 + tests/qapi-schema/unclosed-string.json | 2 ++ 44 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 tests/qapi-schema/comments.err create mode 100644 tests/qapi-schema/comments.exit create mode 100644 tests/qapi-schema/comments.json create mode 100644 tests/qapi-schema/comments.out create mode 100644 tests/qapi-schema/empty.err create mode 100644 tests/qapi-schema/empty.exit create mode 100644 tests/qapi-schema/empty.json create mode 100644 tests/qapi-schema/empty.out create mode 100644 tests/qapi-schema/funny-char.err create mode 100644 tests/qapi-schema/funny-char.exit create mode 100644 tests/qapi-schema/funny-char.json create mode 100644 tests/qapi-schema/funny-char.out create mode 100644 tests/qapi-schema/indented-expr.err create mode 100644 tests/qapi-schema/indented-expr.exit create mode 100644 tests/qapi-schema/indented-expr.json create mode 100644 tests/qapi-schema/indented-expr.out create mode 100644 tests/qapi-schema/missing-colon.err create mode 100644 tests/qapi-schema/missing-colon.exit create mode 100644 tests/qapi-schema/missing-colon.json create mode 100644 tests/qapi-schema/missing-colon.out create mode 100644 tests/qapi-schema/missing-comma-list.err create mode 100644 tests/qapi-schema/missing-comma-list.exit create mode 100644 tests/qapi-schema/missing-comma-list.json create mode 100644 tests/qapi-schema/missing-comma-list.out create mode 100644 tests/qapi-schema/missing-comma-object.err create mode 100644 tests/qapi-schema/missing-comma-object.exit create mode 100644 tests/qapi-schema/missing-comma-object.json create mode 100644 tests/qapi-schema/missing-comma-object.out create mode 100644 tests/qapi-schema/non-objects.err create mode 100644 tests/qapi-schema/non-objects.exit create mode 100644 tests/qapi-schema/non-objects.json create mode 100644 tests/qapi-schema/non-objects.out create mode 100644 tests/qapi-schema/quoted-structural-chars.err create mode 100644 tests/qapi-schema/quoted-structural-chars.exit create mode 100644 tests/qapi-schema/quoted-structural-chars.json create mode 100644 tests/qapi-schema/quoted-structural-chars.out create mode 100644 tests/qapi-schema/test-qapi.py create mode 100644 tests/qapi-schema/trailing-comma-list.err create mode 100644 tests/qapi-schema/trailing-comma-list.exit create mode 100644 tests/qapi-schema/trailing-comma-list.json create mode 100644 tests/qapi-schema/trailing-comma-list.out create mode 100644 tests/qapi-schema/trailing-comma-object.err create mode 100644 tests/qapi-schema/trailing-comma-object.exit create mode 100644 tests/qapi-schema/trailing-comma-object.json create mode 100644 tests/qapi-schema/trailing-comma-object.out create mode 100644 tests/qapi-schema/unclosed-list.err create mode 100644 tests/qapi-schema/unclosed-list.exit create mode 100644 tests/qapi-schema/unclosed-list.json create mode 100644 tests/qapi-schema/unclosed-list.out create mode 100644 tests/qapi-schema/unclosed-object.err create mode 100644 tests/qapi-schema/unclosed-object.exit create mode 100644 tests/qapi-schema/unclosed-object.json create mode 100644 tests/qapi-schema/unclosed-object.out create mode 100644 tests/qapi-schema/unclosed-string.err create mode 100644 tests/qapi-schema/unclosed-string.exit create mode 100644 tests/qapi-schema/unclosed-string.json create mode 100644 tests/qapi-schema/unclosed-string.out
configure +1 -1
@@ -4502,7 +4502,7 @@ if [ "$dtc_internal" = "yes" ]; then fi # build tree in object directory in case the source is not in the current directory -DIRS="tests tests/tcg tests/tcg/cris tests/tcg/lm32 tests/libqos" +DIRS="tests tests/tcg tests/tcg/cris tests/tcg/lm32 tests/libqos tests/qapi-schema" DIRS="$DIRS pc-bios/optionrom pc-bios/spapr-rtas pc-bios/s390-ccw" DIRS="$DIRS roms/seabios roms/vgabios" DIRS="$DIRS qapi-generated"
tests/Makefile +22 -2
@@ -83,6 +83,14 @@ gcov-files-arm-y += hw/tmp105.c check-qtest-ppc-y += tests/boot-order-test$(EXESUF) check-qtest-ppc64-y += tests/boot-order-test$(EXESUF) +check-qapi-schema-y := $(addprefix tests/qapi-schema/, \ + comments.json empty.json funny-char.json indented-expr.json \ + missing-colon.json missing-comma-list.json \ + missing-comma-object.json non-objects.json \ + quoted-structural-chars.json \ + trailing-comma-list.json trailing-comma-object.json \ + unclosed-list.json unclosed-object.json unclosed-string.json) + GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
@@ -171,6 +179,7 @@ check-help: @echo " make check-qtest-TARGET Run qtest tests for given target" @echo " make check-qtest Run qtest tests" @echo " make check-unit Run qobject tests" + @echo " make check-qapi-schema Run QAPI schema tests" @echo " make check-block Run block tests" @echo " make check-report.html Generates an HTML test report" @echo
@@ -233,13 +242,24 @@ check-report.html: check-report.xml check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF) qemu-io$(EXESUF) $< +.PHONY: check-tests/test-qapi.py +check-tests/test-qapi.py: tests/test-qapi.py + +.PHONY: $(patsubst %, check-%, $(check-qapi-schema-y)) +$(patsubst %, check-%, $(check-qapi-schema-y)): check-%.json: $(SRC_PATH)/%.json + $(call quiet-command, PYTHONPATH=$(SRC_PATH)/scripts $(PYTHON) $(SRC_PATH)/tests/qapi-schema/test-qapi.py <$^ >$*.out 2>$*.err; echo $$? >$*.exit, " TEST $*.out") + @diff -q $(SRC_PATH)/$*.out $*.out + @diff -q $(SRC_PATH)/$*.err $*.err + @diff -q $(SRC_PATH)/$*.exit $*.exit + # Consolidated targets -.PHONY: check-qtest check-unit check +.PHONY: check-qapi-schema check-qtest check-unit check +check-qapi-schema: $(patsubst %,check-%, $(check-qapi-schema-y)) check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS)) check-unit: $(patsubst %,check-%, $(check-unit-y)) check-block: $(patsubst %,check-%, $(check-block-y)) -check: check-unit check-qtest +check: check-qapi-schema check-unit check-qtest -include $(wildcard tests/*.d) -include $(wildcard tests/libqos/*.d)
tests/qapi-schema/comments.err new file +0 -0
tests/qapi-schema/comments.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/comments.json new file +4 -0
@@ -0,0 +1,4 @@ +# Unindented comment +{ 'enum': 'Status', # Comment to the right of code + # Indented comment + 'data': [ 'good', 'bad', 'ugly' ] }
tests/qapi-schema/comments.out new file +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +['Status'] +[]
tests/qapi-schema/empty.err new file +0 -0
tests/qapi-schema/empty.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/empty.json new file +0 -0
tests/qapi-schema/empty.out new file +3 -0
@@ -0,0 +1,3 @@ +[] +[] +[]
tests/qapi-schema/funny-char.err new file +0 -0
tests/qapi-schema/funny-char.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/funny-char.json new file +2 -0
@@ -0,0 +1,2 @@ +{ 'enum': 'Status', + 'data': [ 'good', 'bad', 'ugly' ]; }
tests/qapi-schema/funny-char.out new file +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +['Status'] +[]
tests/qapi-schema/indented-expr.err new file +0 -0
tests/qapi-schema/indented-expr.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/indented-expr.json new file +2 -0
@@ -0,0 +1,2 @@ +{ 'id' : 'eins' } + { 'id' : 'zwei' }
tests/qapi-schema/indented-expr.out new file +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('id', 'eins')])] +[] +[]
tests/qapi-schema/missing-colon.err new file +0 -0
tests/qapi-schema/missing-colon.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/missing-colon.json new file +2 -0
@@ -0,0 +1,2 @@ +{ 'enum' 'Status', + 'data': [ 'good', 'bad', 'ugly' ] }
tests/qapi-schema/missing-colon.out new file +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('enum', ','), ('data', ['good', 'bad', 'ugly'])])] +[','] +[]
tests/qapi-schema/missing-comma-list.err new file +0 -0
tests/qapi-schema/missing-comma-list.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/missing-comma-list.json new file +2 -0
@@ -0,0 +1,2 @@ +{ 'enum': 'Status', + 'data': [ 'good' 'bad', 'ugly' ] }
tests/qapi-schema/missing-comma-list.out new file +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +['Status'] +[]
tests/qapi-schema/missing-comma-object.err new file +0 -0
tests/qapi-schema/missing-comma-object.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/missing-comma-object.json new file +2 -0
@@ -0,0 +1,2 @@ +{ 'enum': 'Status' + 'data': [ 'good', 'bad', 'ugly' ] }
tests/qapi-schema/missing-comma-object.out new file +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +['Status'] +[]
tests/qapi-schema/non-objects.err new file +1 -0
@@ -0,0 +1 @@ +Crashed: <type 'exceptions.AttributeError'>
tests/qapi-schema/non-objects.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/non-objects.json new file +2 -0
@@ -0,0 +1,2 @@ +'string' +[ ]
tests/qapi-schema/non-objects.out new file +0 -0
tests/qapi-schema/quoted-structural-chars.err new file +0 -0
tests/qapi-schema/quoted-structural-chars.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/quoted-structural-chars.json new file +1 -0
@@ -0,0 +1 @@ +'{' 'key1' ':' 'value1' ',' 'key2' ':' '[' ']' '}'
tests/qapi-schema/quoted-structural-chars.out new file +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('key1', 'value1'), ('key2', [])])] +[] +[]
tests/qapi-schema/test-qapi.py new file +25 -0
@@ -0,0 +1,25 @@ +# +# QAPI parser test harness +# +# Copyright (c) 2013 Red Hat Inc. +# +# Authors: +# Markus Armbruster <[email protected]> +# +# This work is licensed under the terms of the GNU GPL, version 2 or later. +# See the COPYING file in the top-level directory. +# + +from qapi import * +from pprint import pprint +import sys + +try: + exprs = parse_schema(sys.stdin) +except: + print >>sys.stderr, "Crashed:", sys.exc_info()[0] + exit(1) + +pprint(exprs) +pprint(enum_types) +pprint(struct_types)
tests/qapi-schema/trailing-comma-list.err new file +0 -0
tests/qapi-schema/trailing-comma-list.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/trailing-comma-list.json new file +2 -0
@@ -0,0 +1,2 @@ +{ 'enum': 'Status', + 'data': [ 'good', 'bad', 'ugly', ] }
tests/qapi-schema/trailing-comma-list.out new file +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +['Status'] +[]
tests/qapi-schema/trailing-comma-object.err new file +0 -0
tests/qapi-schema/trailing-comma-object.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/trailing-comma-object.json new file +2 -0
@@ -0,0 +1,2 @@ +{ 'enum': 'Status', + 'data': [ 'good', 'bad', 'ugly' ], }
tests/qapi-schema/trailing-comma-object.out new file +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +['Status'] +[]
tests/qapi-schema/unclosed-list.err new file +1 -0
@@ -0,0 +1 @@ +Crashed: <type 'exceptions.IndexError'>
tests/qapi-schema/unclosed-list.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/unclosed-list.json new file +1 -0
@@ -0,0 +1 @@ +{ 'key': [ 'value' }
tests/qapi-schema/unclosed-list.out new file +0 -0
tests/qapi-schema/unclosed-object.err new file +1 -0
@@ -0,0 +1 @@ +Crashed: <type 'exceptions.IndexError'>
tests/qapi-schema/unclosed-object.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/unclosed-object.json new file +1 -0
@@ -0,0 +1 @@ +{ 'key': [ 'value' ]
tests/qapi-schema/unclosed-object.out new file +0 -0
tests/qapi-schema/unclosed-string.err new file +1 -0
@@ -0,0 +1 @@ +Crashed: <type 'exceptions.Exception'>
tests/qapi-schema/unclosed-string.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/unclosed-string.json new file +2 -0
@@ -0,0 +1,2 @@ +{ 'text': 'lorem ips +}
tests/qapi-schema/unclosed-string.out new file +0 -0
Signature · 1 line
1.7.11.7
Re: [Qemu-devel] [PATCH v2 1/9] tests: QAPI schema parser tests
Quoted 13 lines · Markus Armbruster
The parser handles erroneous input badly. To be improved shortly.
Signed-off-by Markus Armbruster <[email protected]>tests/Makefile +0
-0@@ -233,13 +242,24 @@ check-report.html: check-report.xml check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF) qemu-io$(EXESUF) $<
tests/Makefile check-report.html: check-report.xml · line 254
Quoted 10 lines
tests/Makefile +10
-0+.PHONY: check-tests/test-qapi.py +check-tests/test-qapi.py: tests/test-qapi.py + +.PHONY: $(patsubst %, check-%, $(check-qapi-schema-y)) +$(patsubst %, check-%, $(check-qapi-schema-y)): check-%.json: $(SRC_PATH)/%.json + $(call quiet-command, PYTHONPATH=$(SRC_PATH)/scripts $(PYTHON) $(SRC_PATH)/tests/qapi-schema/test-qapi.py <$^ >$*.out 2>$*.err; echo $$? >$*.exit, " TEST $*.out") + @diff -q $(SRC_PATH)/$*.out $*.out + @diff -q $(SRC_PATH)/$*.err $*.err + @diff -q $(SRC_PATH)/$*.exit $*.exit +
Unfortunately, this doesn't catch errors/differences when someone builds qemu in the source tree, and runs "make check" there. In that case, whatever output "test-qapi.py" produces, overwrites the in-tree file, and the diff commands compare files with themselves.
Also, why is 2/9 a good idea, namely, using "qapi-schema-test.json" as a test file itself? Whoever adds a new test, for anything that uses the schema, now has to update the .out file as well. (And, due to the above, only realizes this burden when someone else tries to make-check his/her code outside of the tree...)
Thanks,
Laszlo
Re: [Qemu-devel] [PATCH v2 1/9] tests: QAPI schema parser tests
Quoted 14 lines · Laszlo Ersek
Quoted 13 lines · Markus Armbruster
The parser handles erroneous input badly. To be improved shortly.
Signed-off-by Markus Armbruster <[email protected]>tests/Makefile +0
-0@@ -233,13 +242,24 @@ check-report.html: check-report.xml check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF) qemu-io$(EXESUF) $<
Quoted 15 lines
tests/Makefile check-report.html: check-report.xml · line 254
Quoted 10 lines
tests/Makefile +10
-0+.PHONY: check-tests/test-qapi.py +check-tests/test-qapi.py: tests/test-qapi.py + +.PHONY: $(patsubst %, check-%, $(check-qapi-schema-y)) +$(patsubst %, check-%, $(check-qapi-schema-y)): check-%.json: $(SRC_PATH)/%.json + $(call quiet-command, PYTHONPATH=$(SRC_PATH)/scripts $(PYTHON) $(SRC_PATH)/tests/qapi-schema/test-qapi.py <$^ >$*.out 2>$*.err; echo $$? >$*.exit, " TEST $*.out") + @diff -q $(SRC_PATH)/$*.out $*.out + @diff -q $(SRC_PATH)/$*.err $*.err + @diff -q $(SRC_PATH)/$*.exit $*.exit +Unfortunately, this doesn't catch errors/differences when someone builds qemu in the source tree, and runs "make check" there. In that case, whatever output "
test-qapi.py" produces, overwrites the in-tree file, and the diff commands compare files with themselves.
D'uh! Will fix. Thanks!
Quoted 3 lines
Also, why is 2/9 a good idea, namely, using "qapi-schema-test.json" as a test file itself? Whoever adds a new test, for anything that uses the schema, now has to update the .out file as well.
The parser needs to be tested with input that exercises all schema features. Since such a test already existed, I extended it to additionally test the parser.
If this coupling turns out to be inconvenient, we can split the test in
two: qapi-schema-test.json goes back to just its original purpose, and a (possibly simplified) copy is used for testing the parser.
Drawback: when you add schema features, you have to update both tests to cover them. Neglecting to add any tests is obvious in review. Forgetting one of two not so much.
Quoted 3 lines
(And, due to the above,
only realizes this burden when someone else tries to make-check his/her code outside of the tree...)
Anyone posting patches without running "make check" gets exactly what he asked for: embarrassment :)
PATCH v2 2/9 tests: Use qapi-schema-test.json as schema parser test
In reply to Markus Armbruster--- tests/Makefile | 8 ++++---- tests/qapi-schema/qapi-schema-test.exit | 1 + .../qapi-schema/qapi-schema-test.json | 0 tests/qapi-schema/qapi-schema-test.out | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 tests/qapi-schema/qapi-schema-test.err create mode 100644 tests/qapi-schema/qapi-schema-test.exit rename qapi-schema-test.json => tests/qapi-schema/qapi-schema-test.json (100%) create mode 100644 tests/qapi-schema/qapi-schema-test.out
tests/Makefile +4 -4
@@ -87,7 +87,7 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \ comments.json empty.json funny-char.json indented-expr.json \ missing-colon.json missing-comma-list.json \ missing-comma-object.json non-objects.json \ - quoted-structural-chars.json \ + qapi-schema-test.json quoted-structural-chars.json \ trailing-comma-list.json trailing-comma-object.json \ unclosed-list.json unclosed-object.json unclosed-string.json)
@@ -125,13 +125,13 @@ tests/test-cutils$(EXESUF): tests/test-cutils.o util/cutils.o tests/test-int128$(EXESUF): tests/test-int128.o tests/test-qapi-types.c tests/test-qapi-types.h :\ -$(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py +$(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py $(gen-out-type) -o tests -p "test-" < $<, " GEN $@") tests/test-qapi-visit.c tests/test-qapi-visit.h :\ -$(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-visit.py +$(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-visit.py $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py $(gen-out-type) -o tests -p "test-" < $<, " GEN $@") tests/test-qmp-commands.h tests/test-qmp-marshal.c :\ -$(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-commands.py +$(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-commands.py $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py $(gen-out-type) -o tests -p "test-" < $<, " GEN $@") tests/test-string-output-visitor$(EXESUF): tests/test-string-output-visitor.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
tests/qapi-schema/qapi-schema-test.err new file +0 -0
tests/qapi-schema/qapi-schema-test.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/qapi-schema-test.json renamed from qapi-schema-test.json +0 -0
tests/qapi-schema/qapi-schema-test.out new file +19 -0
@@ -0,0 +1,19 @@ +[OrderedDict([('enum', 'EnumOne'), ('data', ['value1', 'value2', 'value3'])]), + OrderedDict([('type', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]), + OrderedDict([('type', 'UserDefOne'), ('data', OrderedDict([('integer', 'int'), ('string', 'str'), ('*enum1', 'EnumOne')]))]), + OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string', 'str'), ('dict', OrderedDict([('string', 'str'), ('dict', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]), + OrderedDict([('type', 'UserDefNested'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef1', 'UserDefOne'), ('string2', 'str')])), ('*dict3', OrderedDict([('userdef2', 'UserDefOne'), ('string3', 'str')]))]))]))]), + OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]), + OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]), + OrderedDict([('union', 'UserDefUnion'), ('data', OrderedDict([('a', 'UserDefA'), ('b', 'UserDefB')]))]), + OrderedDict([('union', 'UserDefNativeListUnion'), ('data', OrderedDict([('integer', ['int']), ('s8', ['int8']), ('s16', ['int16']), ('s32', ['int32']), ('s64', ['int64']), ('u8', ['uint8']), ('u16', ['uint16']), ('u32', ['uint32']), ('u64', ['uint64']), ('number', ['number']), ('boolean', ['bool']), ('string', ['str'])]))]), + OrderedDict([('command', 'user_def_cmd'), ('data', OrderedDict())]), + OrderedDict([('command', 'user_def_cmd1'), ('data', OrderedDict([('ud1a', 'UserDefOne')]))]), + OrderedDict([('command', 'user_def_cmd2'), ('data', OrderedDict([('ud1a', 'UserDefOne'), ('ud1b', 'UserDefOne')])), ('returns', 'UserDefTwo')])] +['EnumOne', 'UserDefUnionKind', 'UserDefNativeListUnionKind'] +[OrderedDict([('type', 'NestedEnumsOne'), ('data', OrderedDict([('enum1', 'EnumOne'), ('*enum2', 'EnumOne'), ('enum3', 'EnumOne'), ('*enum4', 'EnumOne')]))]), + OrderedDict([('type', 'UserDefOne'), ('data', OrderedDict([('integer', 'int'), ('string', 'str'), ('*enum1', 'EnumOne')]))]), + OrderedDict([('type', 'UserDefTwo'), ('data', OrderedDict([('string', 'str'), ('dict', OrderedDict([('string', 'str'), ('dict', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')])), ('*dict2', OrderedDict([('userdef', 'UserDefOne'), ('string', 'str')]))]))]))]), + OrderedDict([('type', 'UserDefNested'), ('data', OrderedDict([('string0', 'str'), ('dict1', OrderedDict([('string1', 'str'), ('dict2', OrderedDict([('userdef1', 'UserDefOne'), ('string2', 'str')])), ('*dict3', OrderedDict([('userdef2', 'UserDefOne'), ('string3', 'str')]))]))]))]), + OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]), + OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))])]
Signature · 1 line
1.7.11.7
PATCH v2 3/9 qapi.py: Restructure lexer and parser
In reply to Markus ArmbrusterThe parser has a rather unorthodox structure:
Until EOF:
Read a section:
Generator function get_expr() yields one section after the other, as a string. An unindented, non-empty line that isn't a comment starts a new section.
Lexing:
Split section into a list of tokens (strings), with help of generator function tokenize().
Parsing:
Parse the first expression from the list of tokens, with parse(), throw away any remaining tokens.
In parse_schema(): record value of an enum, union or struct key (if any) in the appropriate global table, append expression to the list of expressions.
Return list of expressions.
Known issues:
(1) Indentation is significant, unlike in real JSON.
(2) Neither lexer nor parser have any idea of source positions. Error reporting is hard, let's go shopping.
(3) The one error we bother to detect, we "report" via raise.
(4) The lexer silently ignores invalid characters.
(5) If everything in a section gets ignored, the parser crashes.
(6) The lexer treats a string containing a structural character exactly like the structural character.
(7) Tokens trailing the first expression in a section are silently ignored.
(8) The parser accepts any token in place of a colon.
(9) The parser treats comma as optional.
(10) parse() crashes on unexpected EOF.
(11) parse_schema() crashes when a section's expression isn't a JSON object.
Replace this piece of original art by a thoroughly unoriginal design. Takes care of (1), (2), (5), (6) and (7), and lays the groundwork for addressing the others. Generated source files remain unchanged.
--- scripts/qapi.py | 163 +++++++++++++------------ tests/qapi-schema/indented-expr.out | 2 +- tests/qapi-schema/missing-colon.out | 4 +- tests/qapi-schema/quoted-structural-chars.err | 1 + tests/qapi-schema/quoted-structural-chars.exit | 2 +- tests/qapi-schema/quoted-structural-chars.out | 3 - 6 files changed, 88 insertions(+), 87 deletions(-)
scripts/qapi.py +83 -80
@@ -2,9 +2,11 @@ # QAPI helper library # # Copyright IBM, Corp. 2011 +# Copyright (c) 2013 Red Hat Inc. # # Authors: # Anthony Liguori <[email protected]> +# Markus Armbruster <[email protected]> # # This work is licensed under the terms of the GNU GPLv2. # See the COPYING.LIB file in the top-level directory.
@@ -32,91 +34,92 @@ builtin_type_qtypes = { 'uint64': 'QTYPE_QINT', } -def tokenize(data): - while len(data): - ch = data[0] - data = data[1:] - if ch in ['{', '}', ':', ',', '[', ']']: - yield ch - elif ch in ' \n': - None - elif ch == "'": - string = '' - esc = False - while True: - if (data == ''): - raise Exception("Mismatched quotes") - ch = data[0] - data = data[1:] - if esc: - string += ch - esc = False - elif ch == "\\": - esc = True - elif ch == "'": - break - else: - string += ch - yield string - -def parse(tokens): - if tokens[0] == '{': - ret = OrderedDict() - tokens = tokens[1:] - while tokens[0] != '}': - key = tokens[0] - tokens = tokens[1:] - - tokens = tokens[1:] # : - - value, tokens = parse(tokens) - - if tokens[0] == ',': - tokens = tokens[1:] - - ret[key] = value - tokens = tokens[1:] - return ret, tokens - elif tokens[0] == '[': - ret = [] - tokens = tokens[1:] - while tokens[0] != ']': - value, tokens = parse(tokens) - if tokens[0] == ',': - tokens = tokens[1:] - ret.append(value) - tokens = tokens[1:] - return ret, tokens - else: - return tokens[0], tokens[1:] - -def evaluate(string): - return parse(map(lambda x: x, tokenize(string)))[0] - -def get_expr(fp): - expr = '' - - for line in fp: - if line.startswith('#') or line == '\n': - continue - - if line.startswith(' '): - expr += line - elif expr: - yield expr - expr = line +class QAPISchema: + + def __init__(self, fp): + self.fp = fp + self.src = fp.read() + if self.src == '' or self.src[-1] != '\n': + self.src += '\n' + self.cursor = 0 + self.exprs = [] + self.accept() + + while self.tok != None: + self.exprs.append(self.get_expr()) + + def accept(self): + while True: + bol = self.cursor == 0 or self.src[self.cursor-1] == '\n' + self.tok = self.src[self.cursor] + self.cursor += 1 + self.val = None + + if self.tok == '#' and bol: + self.cursor = self.src.find('\n', self.cursor) + elif self.tok in ['{', '}', ':', ',', '[', ']']: + return + elif self.tok == "'": + string = '' + esc = False + while True: + ch = self.src[self.cursor] + self.cursor += 1 + if ch == '\n': + raise Exception("Mismatched quotes") + if esc: + string += ch + esc = False + elif ch == "\\": + esc = True + elif ch == "'": + self.val = string + return + else: + string += ch + elif self.tok == '\n': + if self.cursor == len(self.src): + self.tok = None + return + + def get_members(self): + expr = OrderedDict() + while self.tok != '}': + key = self.val + self.accept() + self.accept() # : + expr[key] = self.get_expr() + if self.tok == ',': + self.accept() + self.accept() + return expr + + def get_values(self): + expr = [] + while self.tok != ']': + expr.append(self.get_expr()) + if self.tok == ',': + self.accept() + self.accept() + return expr + + def get_expr(self): + if self.tok == '{': + self.accept() + expr = self.get_members() + elif self.tok == '[': + self.accept() + expr = self.get_values() else: - expr += line - - if expr: - yield expr + expr = self.val + self.accept() + return expr def parse_schema(fp): + schema = QAPISchema(fp) exprs = [] - for expr in get_expr(fp): - expr_eval = evaluate(expr) - + for expr_eval in schema.exprs: if expr_eval.has_key('enum'): add_enum(expr_eval['enum']) elif expr_eval.has_key('union'):
tests/qapi-schema/indented-expr.out +1 -1
@@ -1,3 +1,3 @@ -[OrderedDict([('id', 'eins')])] +[OrderedDict([('id', 'eins')]), OrderedDict([('id', 'zwei')])] [] []
tests/qapi-schema/missing-colon.out +2 -2
@@ -1,3 +1,3 @@ -[OrderedDict([('enum', ','), ('data', ['good', 'bad', 'ugly'])])] -[','] +[OrderedDict([('enum', None), ('data', ['good', 'bad', 'ugly'])])] +[None] []
tests/qapi-schema/quoted-structural-chars.err +1 -0
@@ -0,0 +1 @@ +Crashed: <type 'exceptions.AttributeError'>
tests/qapi-schema/quoted-structural-chars.exit +1 -1
@@ -1 +1 @@ -0 +1
tests/qapi-schema/quoted-structural-chars.out +0 -3
@@ -1,3 +0,0 @@ -[OrderedDict([('key1', 'value1'), ('key2', [])])] -[] -[]
Signature · 1 line
1.7.11.7
PATCH v2 4/9 qapi.py: Decent syntax error reporting
In reply to Markus Armbruster--- scripts/qapi.py | 29 +++++++++++++++++++++++++++-- tests/qapi-schema/test-qapi.py | 2 ++ tests/qapi-schema/unclosed-string.err | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-)
scripts/qapi.py +27 -2
@@ -12,6 +12,7 @@ # See the COPYING.LIB file in the top-level directory. from ordereddict import OrderedDict +import sys builtin_types = [ 'str', 'int', 'number', 'bool',
@@ -34,6 +35,23 @@ builtin_type_qtypes = { 'uint64': 'QTYPE_QINT', } +class QAPISchemaError(Exception): + def __init__(self, schema, msg): + self.fp = schema.fp + self.msg = msg + self.line = self.col = 1 + for ch in schema.src[0:schema.pos]: + if ch == '\n': + self.line += 1 + self.col = 1 + elif ch == '\t': + self.col = (self.col + 7) % 8 + 1 + else: + self.col += 1 + + def __str__(self): + return "%s:%s:%s: %s" % (self.fp.name, self.line, self.col, self.msg) + class QAPISchema: def __init__(self, fp):
@@ -52,6 +70,7 @@ class QAPISchema: while True: bol = self.cursor == 0 or self.src[self.cursor-1] == '\n' self.tok = self.src[self.cursor] + self.pos = self.cursor self.cursor += 1 self.val = None
@@ -66,7 +85,8 @@ class QAPISchema: ch = self.src[self.cursor] self.cursor += 1 if ch == '\n': - raise Exception("Mismatched quotes") + raise QAPISchemaError(self, + 'Missing terminating "\'"') if esc: string += ch esc = False
@@ -116,7 +136,12 @@ class QAPISchema: return expr def parse_schema(fp): - schema = QAPISchema(fp) + try: + schema = QAPISchema(fp) + except QAPISchemaError as e: + print >>sys.stderr, e + exit(1) + exprs = [] for expr_eval in schema.exprs:
tests/qapi-schema/test-qapi.py +2 -0
@@ -16,6 +16,8 @@ import sys try: exprs = parse_schema(sys.stdin) +except SystemExit: + raise except: print >>sys.stderr, "Crashed:", sys.exc_info()[0] exit(1)
tests/qapi-schema/unclosed-string.err +1 -1
@@ -1 +1 @@ -Crashed: <type 'exceptions.Exception'> +<stdin>:1:11: Missing terminating "'"
Signature · 1 line
1.7.11.7
PATCH v2 5/9 qapi.py: Reject invalid characters in schema file
In reply to Markus Armbruster--- scripts/qapi.py | 2 ++ tests/qapi-schema/comments.err | 1 + tests/qapi-schema/comments.exit | 2 +- tests/qapi-schema/comments.out | 3 --- tests/qapi-schema/funny-char.err | 1 + tests/qapi-schema/funny-char.exit | 2 +- tests/qapi-schema/funny-char.out | 3 --- 7 files changed, 6 insertions(+), 8 deletions(-)
scripts/qapi.py +2 -0
@@ -101,6 +101,8 @@ class QAPISchema: if self.cursor == len(self.src): self.tok = None return + elif not self.tok.isspace(): + raise QAPISchemaError(self, 'Stray "%s"' % self.tok) def get_members(self): expr = OrderedDict()
tests/qapi-schema/comments.err +1 -0
@@ -0,0 +1 @@ +<stdin>:2:33: Stray "#"
tests/qapi-schema/comments.exit +1 -1
@@ -1 +1 @@ -0 +1
tests/qapi-schema/comments.out +0 -3
@@ -1,3 +0,0 @@ -[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] -['Status'] -[]
tests/qapi-schema/funny-char.err +1 -0
@@ -0,0 +1 @@ +<stdin>:2:36: Stray ";"
tests/qapi-schema/funny-char.exit +1 -1
@@ -1 +1 @@ -0 +1
tests/qapi-schema/funny-char.out +0 -3
@@ -1,3 +0,0 @@ -[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] -['Status'] -[]
Signature · 1 line
1.7.11.7
PATCH v2 6/9 qapi.py: Fix schema parser to check syntax systematically
In reply to Markus ArmbrusterFixes at least the following parser bugs:
* accepts any token in place of a colon
* treats comma as optional
* crashes when closing braces or brackets are missing
--- scripts/qapi.py | 40 +++++++++++++++++++++------- tests/qapi-schema/missing-colon.err | 1 + tests/qapi-schema/missing-colon.exit | 2 +- tests/qapi-schema/missing-colon.out | 3 --- tests/qapi-schema/missing-comma-list.err | 1 + tests/qapi-schema/missing-comma-list.exit | 2 +- tests/qapi-schema/missing-comma-list.out | 3 --- tests/qapi-schema/missing-comma-object.err | 1 + tests/qapi-schema/missing-comma-object.exit | 2 +- tests/qapi-schema/missing-comma-object.out | 3 --- tests/qapi-schema/trailing-comma-list.err | 1 + tests/qapi-schema/trailing-comma-list.exit | 2 +- tests/qapi-schema/trailing-comma-list.out | 3 --- tests/qapi-schema/trailing-comma-object.err | 1 + tests/qapi-schema/trailing-comma-object.exit | 2 +- tests/qapi-schema/trailing-comma-object.out | 3 --- tests/qapi-schema/unclosed-list.err | 2 +- tests/qapi-schema/unclosed-object.err | 2 +- 18 files changed, 42 insertions(+), 32 deletions(-)
scripts/qapi.py +30 -10
@@ -106,24 +106,42 @@ class QAPISchema: def get_members(self): expr = OrderedDict() - while self.tok != '}': + if self.tok == '}': + self.accept() + return expr + if self.tok != "'": + raise QAPISchemaError(self, 'Expected string or "}"') + while True: key = self.val self.accept() - self.accept() # : + if self.tok != ':': + raise QAPISchemaError(self, 'Expected ":"') + self.accept() expr[key] = self.get_expr() - if self.tok == ',': + if self.tok == '}': self.accept() - self.accept() - return expr + return expr + if self.tok != ',': + raise QAPISchemaError(self, 'Expected "," or "}"') + self.accept() + if self.tok != "'": + raise QAPISchemaError(self, 'Expected string') def get_values(self): expr = [] - while self.tok != ']': + if self.tok == ']': + self.accept() + return expr + if not self.tok in [ '{', '[', "'" ]: + raise QAPISchemaError(self, 'Expected "{", "[", "]" or string') + while True: expr.append(self.get_expr()) - if self.tok == ',': + if self.tok == ']': self.accept() - self.accept() - return expr + return expr + if self.tok != ',': + raise QAPISchemaError(self, 'Expected "," or "]"') + self.accept() def get_expr(self): if self.tok == '{':
@@ -132,9 +150,11 @@ class QAPISchema: elif self.tok == '[': self.accept() expr = self.get_values() - else: + elif self.tok == "'": expr = self.val self.accept() + else: + raise QAPISchemaError(self, 'Expected "{", "[" or string') return expr def parse_schema(fp):
tests/qapi-schema/missing-colon.err +1 -0
@@ -0,0 +1 @@ +<stdin>:1:10: Expected ":"
tests/qapi-schema/missing-colon.exit +1 -1
@@ -1 +1 @@ -0 +1
tests/qapi-schema/missing-colon.out +0 -3
@@ -1,3 +0,0 @@ -[OrderedDict([('enum', None), ('data', ['good', 'bad', 'ugly'])])] -[None] -[]
tests/qapi-schema/missing-comma-list.err +1 -0
@@ -0,0 +1 @@ +<stdin>:2:20: Expected "," or "]"
tests/qapi-schema/missing-comma-list.exit +1 -1
@@ -1 +1 @@ -0 +1
tests/qapi-schema/missing-comma-list.out +0 -3
@@ -1,3 +0,0 @@ -[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] -['Status'] -[]
tests/qapi-schema/missing-comma-object.err +1 -0
@@ -0,0 +1 @@ +<stdin>:2:3: Expected "," or "}"
tests/qapi-schema/missing-comma-object.exit +1 -1
@@ -1 +1 @@ -0 +1
tests/qapi-schema/missing-comma-object.out +0 -3
@@ -1,3 +0,0 @@ -[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] -['Status'] -[]
tests/qapi-schema/trailing-comma-list.err +1 -0
@@ -0,0 +1 @@ +<stdin>:2:36: Expected "{", "[" or string
tests/qapi-schema/trailing-comma-list.exit +1 -1
@@ -1 +1 @@ -0 +1
tests/qapi-schema/trailing-comma-list.out +0 -3
@@ -1,3 +0,0 @@ -[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] -['Status'] -[]
tests/qapi-schema/trailing-comma-object.err +1 -0
@@ -0,0 +1 @@ +<stdin>:2:38: Expected string
tests/qapi-schema/trailing-comma-object.exit +1 -1
@@ -1 +1 @@ -0 +1
tests/qapi-schema/trailing-comma-object.out +0 -3
@@ -1,3 +0,0 @@ -[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] -['Status'] -[]
tests/qapi-schema/unclosed-list.err +1 -1
@@ -1 +1 @@ -Crashed: <type 'exceptions.IndexError'> +<stdin>:1:20: Expected "," or "]"
tests/qapi-schema/unclosed-object.err +1 -1
@@ -1 +1 @@ -Crashed: <type 'exceptions.IndexError'> +<stdin>:1:21: Expected "," or "}"
Signature · 1 line
1.7.11.7
PATCH v2 7/9 qapi.py: Fix diagnosing non-objects at a schema's top-level
In reply to Markus ArmbrusterReport syntax error instead of crashing.
--- scripts/qapi.py | 10 ++++++---- tests/qapi-schema/non-objects.err | 2 +- tests/qapi-schema/quoted-structural-chars.err | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-)
scripts/qapi.py +6 -4
@@ -64,7 +64,7 @@ class QAPISchema: self.accept() while self.tok != None: - self.exprs.append(self.get_expr()) + self.exprs.append(self.get_expr(False)) def accept(self): while True:
@@ -117,7 +117,7 @@ class QAPISchema: if self.tok != ':': raise QAPISchemaError(self, 'Expected ":"') self.accept() - expr[key] = self.get_expr() + expr[key] = self.get_expr(True) if self.tok == '}': self.accept() return expr
@@ -135,7 +135,7 @@ class QAPISchema: if not self.tok in [ '{', '[', "'" ]: raise QAPISchemaError(self, 'Expected "{", "[", "]" or string') while True: - expr.append(self.get_expr()) + expr.append(self.get_expr(True)) if self.tok == ']': self.accept() return expr
@@ -143,7 +143,9 @@ class QAPISchema: raise QAPISchemaError(self, 'Expected "," or "]"') self.accept() - def get_expr(self): + def get_expr(self, nested): + if self.tok != '{' and not nested: + raise QAPISchemaError(self, 'Expected "{"') if self.tok == '{': self.accept() expr = self.get_members()
tests/qapi-schema/non-objects.err +1 -1
@@ -1 +1 @@ -Crashed: <type 'exceptions.AttributeError'> +<stdin>:1:1: Expected "{"
tests/qapi-schema/quoted-structural-chars.err +1 -1
@@ -1 +1 @@ -Crashed: <type 'exceptions.AttributeError'> +<stdin>:1:1: Expected "{"
Signature · 1 line
1.7.11.7
PATCH v2 8/9 qapi.py: Rename expr_eval to expr in parse_schema()
In reply to Markus Armbruster--- scripts/qapi.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
scripts/qapi.py +9 -9
@@ -168,15 +168,15 @@ def parse_schema(fp): exprs = [] - for expr_eval in schema.exprs: - if expr_eval.has_key('enum'): - add_enum(expr_eval['enum']) - elif expr_eval.has_key('union'): - add_union(expr_eval) - add_enum('%sKind' % expr_eval['union']) - elif expr_eval.has_key('type'): - add_struct(expr_eval) - exprs.append(expr_eval) + for expr in schema.exprs: + if expr.has_key('enum'): + add_enum(expr['enum']) + elif expr.has_key('union'): + add_union(expr) + add_enum('%sKind' % expr['union']) + elif expr.has_key('type'): + add_struct(expr) + exprs.append(expr) return exprs
Signature · 1 line
1.7.11.7
PATCH v2 9/9 qapi.py: Permit comments starting anywhere on the line
In reply to Markus Armbruster--- scripts/qapi.py | 3 +-- tests/qapi-schema/comments.err | 1 - tests/qapi-schema/comments.exit | 2 +- tests/qapi-schema/comments.out | 3 +++ 4 files changed, 5 insertions(+), 4 deletions(-)
scripts/qapi.py +1 -2
@@ -68,13 +68,12 @@ class QAPISchema: def accept(self): while True: - bol = self.cursor == 0 or self.src[self.cursor-1] == '\n' self.tok = self.src[self.cursor] self.pos = self.cursor self.cursor += 1 self.val = None - if self.tok == '#' and bol: + if self.tok == '#': self.cursor = self.src.find('\n', self.cursor) elif self.tok in ['{', '}', ':', ',', '[', ']']: return
tests/qapi-schema/comments.err +0 -1
@@ -1 +0,0 @@ -<stdin>:2:33: Stray "#"
tests/qapi-schema/comments.exit +1 -1
@@ -1 +1 @@ -1 +0
tests/qapi-schema/comments.out +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +['Status'] +[]
Signature · 1 line
1.7.11.7
Anthony Liguori
AppliedApplied. Thanks.
Regards,
Anthony Liguori