[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 10/15] qapi/parser: Fix token membership tests when token can
From: |
John Snow |
Subject: |
[PATCH v3 10/15] qapi/parser: Fix token membership tests when token can be None |
Date: |
Wed, 19 May 2021 14:39:46 -0400 |
When the token can be None (EOF), we can't use 'x in "abc"' style
membership tests to group types of tokens together, because 'None in
"abc"' is a TypeError.
Easy enough to fix. (Use a tuple: It's neither a static typing error nor
a runtime error to check for None in Tuple[str, ...])
Add tests to prevent a regression. (Note: they cannot be added prior to
this fix, as the unhandled stack trace will not match test output in the
CI system.)
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/parser.py | 5 +++--
tests/qapi-schema/meson.build | 2 ++
tests/qapi-schema/missing-array-rsqb.err | 1 +
tests/qapi-schema/missing-array-rsqb.json | 1 +
tests/qapi-schema/missing-array-rsqb.out | 0
tests/qapi-schema/missing-object-member-element.err | 1 +
tests/qapi-schema/missing-object-member-element.json | 1 +
tests/qapi-schema/missing-object-member-element.out | 0
8 files changed, 9 insertions(+), 2 deletions(-)
create mode 100644 tests/qapi-schema/missing-array-rsqb.err
create mode 100644 tests/qapi-schema/missing-array-rsqb.json
create mode 100644 tests/qapi-schema/missing-array-rsqb.out
create mode 100644 tests/qapi-schema/missing-object-member-element.err
create mode 100644 tests/qapi-schema/missing-object-member-element.json
create mode 100644 tests/qapi-schema/missing-object-member-element.out
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index 48137d3fbec..9f980f75139 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -275,7 +275,7 @@ def get_values(self):
if self.tok == ']':
self.accept()
return expr
- if self.tok not in "{['tf":
+ if self.tok not in tuple("{['tf"):
raise QAPIParseError(
self, "expected '{', '[', ']', string, or boolean")
while True:
@@ -294,7 +294,8 @@ def get_expr(self):
elif self.tok == '[':
self.accept()
expr = self.get_values()
- elif self.tok in "'tf":
+ elif self.tok in tuple("'tf"):
+ assert isinstance(self.val, (str, bool))
expr = self.val
self.accept()
else:
diff --git a/tests/qapi-schema/meson.build b/tests/qapi-schema/meson.build
index dc448e8f74d..9e8f658ce38 100644
--- a/tests/qapi-schema/meson.build
+++ b/tests/qapi-schema/meson.build
@@ -134,9 +134,11 @@ schemas = [
'indented-expr.json',
'leading-comma-list.json',
'leading-comma-object.json',
+ 'missing-array-rsqb.json',
'missing-colon.json',
'missing-comma-list.json',
'missing-comma-object.json',
+ 'missing-object-member-element.json',
'missing-type.json',
'nested-struct-data.json',
'nested-struct-data-invalid-dict.json',
diff --git a/tests/qapi-schema/missing-array-rsqb.err
b/tests/qapi-schema/missing-array-rsqb.err
new file mode 100644
index 00000000000..b5f58b8c12a
--- /dev/null
+++ b/tests/qapi-schema/missing-array-rsqb.err
@@ -0,0 +1 @@
+missing-array-rsqb.json:1:44: expected '{', '[', string, or boolean
diff --git a/tests/qapi-schema/missing-array-rsqb.json
b/tests/qapi-schema/missing-array-rsqb.json
new file mode 100644
index 00000000000..7fca1df923c
--- /dev/null
+++ b/tests/qapi-schema/missing-array-rsqb.json
@@ -0,0 +1 @@
+['Daisy,', 'Daisy,', 'Give me your answer',
diff --git a/tests/qapi-schema/missing-array-rsqb.out
b/tests/qapi-schema/missing-array-rsqb.out
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/tests/qapi-schema/missing-object-member-element.err
b/tests/qapi-schema/missing-object-member-element.err
new file mode 100644
index 00000000000..c08a3dc307f
--- /dev/null
+++ b/tests/qapi-schema/missing-object-member-element.err
@@ -0,0 +1 @@
+missing-object-member-element.json:1:8: expected '{', '[', string, or boolean
diff --git a/tests/qapi-schema/missing-object-member-element.json
b/tests/qapi-schema/missing-object-member-element.json
new file mode 100644
index 00000000000..f52d0106f31
--- /dev/null
+++ b/tests/qapi-schema/missing-object-member-element.json
@@ -0,0 +1 @@
+{'key':
diff --git a/tests/qapi-schema/missing-object-member-element.out
b/tests/qapi-schema/missing-object-member-element.out
new file mode 100644
index 00000000000..e69de29bb2d
--
2.30.2
- [PATCH v3 00/15] qapi: static typing conversion, pt5a, John Snow, 2021/05/19
- [PATCH v3 05/15] qapi/parser: Assert lexer value is a string, John Snow, 2021/05/19
- [PATCH v3 01/15] qapi/parser: Don't try to handle file errors, John Snow, 2021/05/19
- [PATCH v3 07/15] qapi/parser: assert object keys are strings, John Snow, 2021/05/19
- [PATCH v3 15/15] qapi/parser: add docstrings, John Snow, 2021/05/19
- [PATCH v3 02/15] qapi: Add test for nonexistent schema file, John Snow, 2021/05/19
- [PATCH v3 04/15] qapi/parser: factor parsing routine into method, John Snow, 2021/05/19
- [PATCH v3 03/15] qapi/source: Remove line number from QAPISourceInfo initializer, John Snow, 2021/05/19
- [PATCH v3 11/15] qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard, John Snow, 2021/05/19
- [PATCH v3 10/15] qapi/parser: Fix token membership tests when token can be None,
John Snow <=
- [PATCH v3 14/15] qapi/parser: allow 'ch' variable name, John Snow, 2021/05/19
- [PATCH v3 09/15] qapi: add must_match helper, John Snow, 2021/05/19
- [PATCH v3 08/15] qapi/parser: Use @staticmethod where appropriate, John Snow, 2021/05/19
- [PATCH v3 06/15] qapi/parser: enforce all top-level expressions must be dict in _parse(), John Snow, 2021/05/19
- [PATCH v3 12/15] qapi/parser: add type hint annotations, John Snow, 2021/05/19
- [PATCH v3 13/15] qapi/parser: Remove superfluous list comprehension, John Snow, 2021/05/19
- Re: [PATCH v3 00/15] qapi: static typing conversion, pt5a, Markus Armbruster, 2021/05/20