[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 10/21] qapi/parser: Fix token membership tests when token can
From: |
John Snow |
Subject: |
[PATCH v2 10/21] qapi/parser: Fix token membership tests when token can be None |
Date: |
Tue, 11 May 2021 18:05:50 -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 39de24785ac..959214b7042 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 v2 03/21] qapi/source: Remove line number from QAPISourceInfo initializer, (continued)
- [PATCH v2 03/21] qapi/source: Remove line number from QAPISourceInfo initializer, John Snow, 2021/05/11
- [PATCH v2 07/21] qapi/parser: assert object keys are strings, John Snow, 2021/05/11
- [PATCH v2 05/21] qapi/parser: Assert lexer value is a string, John Snow, 2021/05/11
- [PATCH v2 04/21] qapi/parser: factor parsing routine into method, John Snow, 2021/05/11
- [PATCH v2 09/21] qapi: add must_match helper, John Snow, 2021/05/11
- [PATCH v2 08/21] qapi/parser: Use @staticmethod where appropriate, John Snow, 2021/05/11
- [PATCH v2 10/21] qapi/parser: Fix token membership tests when token can be None,
John Snow <=
- [PATCH v2 11/21] qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard, John Snow, 2021/05/11
- [PATCH v2 12/21] qapi/parser: add type hint annotations, John Snow, 2021/05/11
- [PATCH v2 13/21] qapi/parser: Remove superfluous list comprehension, John Snow, 2021/05/11
- [PATCH v2 15/21] qapi/parser: add docstrings, John Snow, 2021/05/11
- [PATCH v2 14/21] qapi/parser: allow 'ch' variable name, John Snow, 2021/05/11
- [PATCH v2 16/21] CHECKPOINT, John Snow, 2021/05/11