qapi/parser: Fix token membership tests when token can be None
authorJohn Snow <jsnow@redhat.com>
Wed, 19 May 2021 18:39:46 +0000 (14:39 -0400)
committerMarkus Armbruster <armbru@redhat.com>
Thu, 20 May 2021 09:28:28 +0000 (11:28 +0200)
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>
Message-Id: <20210519183951.3946870-11-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
scripts/qapi/parser.py
tests/qapi-schema/meson.build
tests/qapi-schema/missing-array-rsqb.err [new file with mode: 0644]
tests/qapi-schema/missing-array-rsqb.json [new file with mode: 0644]
tests/qapi-schema/missing-array-rsqb.out [new file with mode: 0644]
tests/qapi-schema/missing-object-member-element.err [new file with mode: 0644]
tests/qapi-schema/missing-object-member-element.json [new file with mode: 0644]
tests/qapi-schema/missing-object-member-element.out [new file with mode: 0644]

index 48137d3fbec6c0d8a73c5093feaf575251421937..9f980f751397dfa98436c582d5b44ddec9c0c84f 100644 (file)
@@ -275,7 +275,7 @@ class QAPISchemaParser:
         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 @@ class QAPISchemaParser:
         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:
index dc448e8f74d55d0548fc047624205ad4ccbdd920..9e8f658ce384aa89e9b7a9bb1b0b48cfceee3d2d 100644 (file)
@@ -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 (file)
index 0000000..b5f58b8
--- /dev/null
@@ -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 (file)
index 0000000..7fca1df
--- /dev/null
@@ -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 (file)
index 0000000..e69de29
diff --git a/tests/qapi-schema/missing-object-member-element.err b/tests/qapi-schema/missing-object-member-element.err
new file mode 100644 (file)
index 0000000..c08a3dc
--- /dev/null
@@ -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 (file)
index 0000000..f52d010
--- /dev/null
@@ -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 (file)
index 0000000..e69de29