2019-10-18 10:43:44 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright IBM, Corp. 2011
|
2021-04-21 21:20:32 +03:00
|
|
|
# Copyright (c) 2013-2021 Red Hat Inc.
|
2019-10-18 10:43:44 +03:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
# Markus Armbruster <armbru@redhat.com>
|
|
|
|
# Eric Blake <eblake@redhat.com>
|
|
|
|
# Marc-André Lureau <marcandre.lureau@redhat.com>
|
2021-04-21 21:20:32 +03:00
|
|
|
# John Snow <jsnow@redhat.com>
|
2019-10-18 10:43:44 +03:00
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2.
|
|
|
|
# See the COPYING file in the top-level directory.
|
|
|
|
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Normalize and validate (context-free) QAPI schema expression structures.
|
|
|
|
|
|
|
|
`QAPISchemaParser` parses a QAPI schema into abstract syntax trees
|
|
|
|
consisting of dict, list, str, bool, and int nodes. This module ensures
|
|
|
|
that these nested structures have the correct type(s) and key(s) where
|
|
|
|
appropriate for the QAPI context-free grammar.
|
|
|
|
|
|
|
|
The QAPI schema expression language allows for certain syntactic sugar;
|
|
|
|
this module also handles the normalization process of these nested
|
|
|
|
structures.
|
|
|
|
|
|
|
|
See `check_exprs` for the main entry point.
|
|
|
|
|
|
|
|
See `schema.QAPISchema` for processing into native Python data
|
|
|
|
structures and contextual semantic validation.
|
|
|
|
"""
|
|
|
|
|
2020-10-09 19:15:29 +03:00
|
|
|
import re
|
2021-04-21 21:20:25 +03:00
|
|
|
from typing import (
|
|
|
|
Dict,
|
|
|
|
Iterable,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Union,
|
|
|
|
cast,
|
|
|
|
)
|
2020-10-09 19:15:29 +03:00
|
|
|
|
qapi: Prefer explicit relative imports
All of the QAPI include statements are changed to be package-aware, as
explicit relative imports.
A quirk of Python packages is that the name of the package exists only
*outside* of the package. This means that to a module inside of the qapi
folder, there is inherently no such thing as the "qapi" package. The
reason these imports work is because the "qapi" package exists in the
context of the caller -- the execution shim, where sys.path includes a
directory that has a 'qapi' folder in it.
When we write "from qapi import sibling", we are NOT referencing the folder
'qapi', but rather "any package named qapi in sys.path". If you should
so happen to have a 'qapi' package in your path, it will use *that*
package.
When we write "from .sibling import foo", we always reference explicitly
our sibling module; guaranteeing consistency in *where* we are importing
these modules from.
This can be useful when working with virtual environments and packages
in development mode. In development mode, a package is installed as a
series of symlinks that forwards to your same source files. The problem
arises because code quality checkers will follow "import qapi.x" to the
"installed" version instead of the sibling file and -- even though they
are the same file -- they have different module paths, and this causes
cyclic import problems, false positive type mismatch errors, and more.
It can also be useful when dealing with hierarchical packages, e.g. if
we allow qemu.core.qmp, qemu.qapi.parser, etc.
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
Reviewed-by: Cleber Rosa <crosa@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20201009161558.107041-6-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2020-10-09 19:15:27 +03:00
|
|
|
from .common import c_name
|
|
|
|
from .error import QAPISemError
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
from .parser import QAPIExpression
|
2021-04-21 21:20:19 +03:00
|
|
|
from .source import QAPISourceInfo
|
|
|
|
|
|
|
|
|
2021-04-21 21:20:30 +03:00
|
|
|
# See check_name_str(), below.
|
2021-03-23 12:40:07 +03:00
|
|
|
valid_name = re.compile(r'(__[a-z0-9.-]+_)?'
|
|
|
|
r'(x-)?'
|
|
|
|
r'([a-z][a-z0-9_-]*)$', re.IGNORECASE)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
2021-04-21 21:20:25 +03:00
|
|
|
def check_name_is_str(name: object,
|
|
|
|
info: QAPISourceInfo,
|
|
|
|
source: str) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Ensure that ``name`` is a ``str``.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``name`` fails validation.
|
|
|
|
"""
|
2019-10-18 10:43:44 +03:00
|
|
|
if not isinstance(name, str):
|
|
|
|
raise QAPISemError(info, "%s requires a string name" % source)
|
|
|
|
|
|
|
|
|
2021-04-21 21:20:25 +03:00
|
|
|
def check_name_str(name: str, info: QAPISourceInfo, source: str) -> str:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Ensure that ``name`` is a valid QAPI name.
|
|
|
|
|
|
|
|
A valid name consists of ASCII letters, digits, ``-``, and ``_``,
|
|
|
|
starting with a letter. It may be prefixed by a downstream prefix
|
|
|
|
of the form __RFQDN_, or the experimental prefix ``x-``. If both
|
|
|
|
prefixes are present, the __RFDQN_ prefix goes first.
|
|
|
|
|
|
|
|
A valid name cannot start with ``q_``, which is reserved.
|
|
|
|
|
|
|
|
:param name: Name to check.
|
|
|
|
:param info: QAPI schema source file information.
|
|
|
|
:param source: Error string describing what ``name`` belongs to.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``name`` fails validation.
|
|
|
|
:return: The stem of the valid name, with no prefixes.
|
|
|
|
"""
|
2019-10-18 10:43:44 +03:00
|
|
|
# Reserve the entire 'q_' namespace for c_name(), and for 'q_empty'
|
|
|
|
# and 'q_obj_*' implicit type names.
|
2021-03-23 12:40:07 +03:00
|
|
|
match = valid_name.match(name)
|
|
|
|
if not match or c_name(name, False).startswith('q_'):
|
2019-10-18 10:43:44 +03:00
|
|
|
raise QAPISemError(info, "%s has an invalid name" % source)
|
2021-03-23 12:40:07 +03:00
|
|
|
return match.group(3)
|
|
|
|
|
|
|
|
|
2021-04-21 21:20:25 +03:00
|
|
|
def check_name_upper(name: str, info: QAPISourceInfo, source: str) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Ensure that ``name`` is a valid event name.
|
|
|
|
|
|
|
|
This means it must be a valid QAPI name as checked by
|
|
|
|
`check_name_str()`, but where the stem prohibits lowercase
|
|
|
|
characters and ``-``.
|
|
|
|
|
|
|
|
:param name: Name to check.
|
|
|
|
:param info: QAPI schema source file information.
|
|
|
|
:param source: Error string describing what ``name`` belongs to.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``name`` fails validation.
|
|
|
|
"""
|
2021-03-23 12:40:08 +03:00
|
|
|
stem = check_name_str(name, info, source)
|
2021-03-23 12:40:10 +03:00
|
|
|
if re.search(r'[a-z-]', stem):
|
|
|
|
raise QAPISemError(
|
|
|
|
info, "name of %s must not use lowercase or '-'" % source)
|
2021-03-23 12:40:07 +03:00
|
|
|
|
|
|
|
|
2021-04-21 21:20:25 +03:00
|
|
|
def check_name_lower(name: str, info: QAPISourceInfo, source: str,
|
|
|
|
permit_upper: bool = False,
|
|
|
|
permit_underscore: bool = False) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Ensure that ``name`` is a valid command or member name.
|
|
|
|
|
|
|
|
This means it must be a valid QAPI name as checked by
|
|
|
|
`check_name_str()`, but where the stem prohibits uppercase
|
|
|
|
characters and ``_``.
|
|
|
|
|
|
|
|
:param name: Name to check.
|
|
|
|
:param info: QAPI schema source file information.
|
|
|
|
:param source: Error string describing what ``name`` belongs to.
|
|
|
|
:param permit_upper: Additionally permit uppercase.
|
|
|
|
:param permit_underscore: Additionally permit ``_``.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``name`` fails validation.
|
|
|
|
"""
|
2021-03-23 12:40:08 +03:00
|
|
|
stem = check_name_str(name, info, source)
|
2021-03-23 12:40:19 +03:00
|
|
|
if ((not permit_upper and re.search(r'[A-Z]', stem))
|
|
|
|
or (not permit_underscore and '_' in stem)):
|
2021-03-23 12:40:08 +03:00
|
|
|
raise QAPISemError(
|
2021-03-23 12:40:19 +03:00
|
|
|
info, "name of %s must not use uppercase or '_'" % source)
|
2021-03-23 12:40:07 +03:00
|
|
|
|
|
|
|
|
2021-04-21 21:20:25 +03:00
|
|
|
def check_name_camel(name: str, info: QAPISourceInfo, source: str) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Ensure that ``name`` is a valid user-defined type name.
|
|
|
|
|
|
|
|
This means it must be a valid QAPI name as checked by
|
|
|
|
`check_name_str()`, but where the stem must be in CamelCase.
|
|
|
|
|
|
|
|
:param name: Name to check.
|
|
|
|
:param info: QAPI schema source file information.
|
|
|
|
:param source: Error string describing what ``name`` belongs to.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``name`` fails validation.
|
|
|
|
"""
|
2021-03-23 12:40:08 +03:00
|
|
|
stem = check_name_str(name, info, source)
|
2021-03-23 12:40:11 +03:00
|
|
|
if not re.match(r'[A-Z][A-Za-z0-9]*[a-z][A-Za-z0-9]*$', stem):
|
|
|
|
raise QAPISemError(info, "name of %s must use CamelCase" % source)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
2021-04-21 21:20:25 +03:00
|
|
|
def check_defn_name_str(name: str, info: QAPISourceInfo, meta: str) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Ensure that ``name`` is a valid definition name.
|
|
|
|
|
|
|
|
Based on the value of ``meta``, this means that:
|
|
|
|
- 'event' names adhere to `check_name_upper()`.
|
|
|
|
- 'command' names adhere to `check_name_lower()`.
|
|
|
|
- Else, meta is a type, and must pass `check_name_camel()`.
|
2021-09-17 17:31:13 +03:00
|
|
|
These names must not end with ``List``.
|
2021-04-21 21:20:30 +03:00
|
|
|
|
|
|
|
:param name: Name to check.
|
|
|
|
:param info: QAPI schema source file information.
|
|
|
|
:param meta: Meta-type name of the QAPI expression.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``name`` fails validation.
|
|
|
|
"""
|
2021-03-23 12:40:07 +03:00
|
|
|
if meta == 'event':
|
|
|
|
check_name_upper(name, info, meta)
|
|
|
|
elif meta == 'command':
|
2021-03-23 12:40:21 +03:00
|
|
|
check_name_lower(
|
|
|
|
name, info, meta,
|
|
|
|
permit_underscore=name in info.pragma.command_name_exceptions)
|
2021-03-23 12:40:07 +03:00
|
|
|
else:
|
|
|
|
check_name_camel(name, info, meta)
|
2021-09-17 17:31:13 +03:00
|
|
|
if name.endswith('List'):
|
2021-04-21 21:20:29 +03:00
|
|
|
raise QAPISemError(
|
2021-09-17 17:31:13 +03:00
|
|
|
info, "%s name should not end in 'List'" % meta)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
2023-02-15 03:00:10 +03:00
|
|
|
def check_keys(value: Dict[str, object],
|
2021-04-21 21:20:25 +03:00
|
|
|
info: QAPISourceInfo,
|
|
|
|
source: str,
|
2023-02-15 03:00:08 +03:00
|
|
|
required: List[str],
|
|
|
|
optional: List[str]) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Ensure that a dict has a specific set of keys.
|
|
|
|
|
|
|
|
:param value: The dict to check.
|
|
|
|
:param info: QAPI schema source file information.
|
|
|
|
:param source: Error string describing this ``value``.
|
|
|
|
:param required: Keys that *must* be present.
|
|
|
|
:param optional: Keys that *may* be present.
|
|
|
|
|
|
|
|
:raise QAPISemError: When unknown keys are present.
|
|
|
|
"""
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2021-04-21 21:20:25 +03:00
|
|
|
def pprint(elems: Iterable[str]) -> str:
|
2019-10-18 10:43:44 +03:00
|
|
|
return ', '.join("'" + e + "'" for e in sorted(elems))
|
|
|
|
|
|
|
|
missing = set(required) - set(value)
|
|
|
|
if missing:
|
|
|
|
raise QAPISemError(
|
|
|
|
info,
|
|
|
|
"%s misses key%s %s"
|
|
|
|
% (source, 's' if len(missing) > 1 else '',
|
|
|
|
pprint(missing)))
|
2021-04-21 21:20:24 +03:00
|
|
|
allowed = set(required) | set(optional)
|
2019-10-18 10:43:44 +03:00
|
|
|
unknown = set(value) - allowed
|
|
|
|
if unknown:
|
|
|
|
raise QAPISemError(
|
|
|
|
info,
|
|
|
|
"%s has unknown key%s %s\nValid keys are %s."
|
|
|
|
% (source, 's' if len(unknown) > 1 else '',
|
|
|
|
pprint(unknown), pprint(allowed)))
|
|
|
|
|
|
|
|
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
def check_flags(expr: QAPIExpression) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Ensure flag members (if present) have valid values.
|
|
|
|
|
|
|
|
:param expr: The expression to validate.
|
|
|
|
|
|
|
|
:raise QAPISemError:
|
|
|
|
When certain flags have an invalid value, or when
|
|
|
|
incompatible flags are present.
|
|
|
|
"""
|
2021-04-21 21:20:31 +03:00
|
|
|
for key in ('gen', 'success-response'):
|
2019-10-18 10:43:44 +03:00
|
|
|
if key in expr and expr[key] is not False:
|
|
|
|
raise QAPISemError(
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
expr.info, "flag '%s' may only use false value" % key)
|
2021-04-21 21:20:31 +03:00
|
|
|
for key in ('boxed', 'allow-oob', 'allow-preconfig', 'coroutine'):
|
2019-10-18 10:43:44 +03:00
|
|
|
if key in expr and expr[key] is not True:
|
|
|
|
raise QAPISemError(
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
expr.info, "flag '%s' may only use true value" % key)
|
2020-10-05 18:58:49 +03:00
|
|
|
if 'allow-oob' in expr and 'coroutine' in expr:
|
|
|
|
# This is not necessarily a fundamental incompatibility, but
|
|
|
|
# we don't have a use case and the desired semantics isn't
|
|
|
|
# obvious. The simplest solution is to forbid it until we get
|
|
|
|
# a use case for it.
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
raise QAPISemError(
|
|
|
|
expr.info, "flags 'allow-oob' and 'coroutine' are incompatible")
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
2023-02-15 03:00:10 +03:00
|
|
|
def check_if(expr: Dict[str, object],
|
|
|
|
info: QAPISourceInfo, source: str) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
2021-08-04 11:31:01 +03:00
|
|
|
Validate the ``if`` member of an object.
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2021-08-04 11:31:01 +03:00
|
|
|
The ``if`` member may be either a ``str`` or a dict.
|
2021-04-21 21:20:30 +03:00
|
|
|
|
|
|
|
:param expr: The expression containing the ``if`` member to validate.
|
|
|
|
:param info: QAPI schema source file information.
|
|
|
|
:param source: Error string describing ``expr``.
|
|
|
|
|
|
|
|
:raise QAPISemError:
|
|
|
|
When the "if" member fails validation, or when there are no
|
|
|
|
non-empty conditions.
|
2021-08-04 11:31:01 +03:00
|
|
|
:return: None
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
2021-04-21 21:20:26 +03:00
|
|
|
|
2021-08-04 11:31:01 +03:00
|
|
|
def _check_if(cond: Union[str, object]) -> None:
|
|
|
|
if isinstance(cond, str):
|
2021-08-31 15:38:06 +03:00
|
|
|
if not re.fullmatch(r'[A-Z][A-Z0-9_]*', cond):
|
2021-08-04 11:31:01 +03:00
|
|
|
raise QAPISemError(
|
|
|
|
info,
|
2021-08-04 11:31:05 +03:00
|
|
|
"'if' condition '%s' of %s is not a valid identifier"
|
2021-08-04 11:31:01 +03:00
|
|
|
% (cond, source))
|
|
|
|
return
|
2021-04-21 21:20:26 +03:00
|
|
|
|
2021-08-04 11:31:01 +03:00
|
|
|
if not isinstance(cond, dict):
|
2021-04-21 21:20:26 +03:00
|
|
|
raise QAPISemError(
|
|
|
|
info,
|
2021-08-04 11:31:01 +03:00
|
|
|
"'if' condition of %s must be a string or an object" % source)
|
2021-08-31 15:38:09 +03:00
|
|
|
check_keys(cond, info, "'if' condition of %s" % source, [],
|
|
|
|
["all", "any", "not"])
|
2021-08-04 11:31:01 +03:00
|
|
|
if len(cond) != 1:
|
2021-04-21 21:20:26 +03:00
|
|
|
raise QAPISemError(
|
|
|
|
info,
|
2021-08-31 15:38:09 +03:00
|
|
|
"'if' condition of %s has conflicting keys" % source)
|
2021-08-04 11:31:01 +03:00
|
|
|
|
2021-09-08 07:54:28 +03:00
|
|
|
if 'not' in cond:
|
|
|
|
_check_if(cond['not'])
|
|
|
|
elif 'all' in cond:
|
|
|
|
_check_infix('all', cond['all'])
|
|
|
|
else:
|
|
|
|
_check_infix('any', cond['any'])
|
|
|
|
|
|
|
|
def _check_infix(operator: str, operands: object) -> None:
|
|
|
|
if not isinstance(operands, list):
|
|
|
|
raise QAPISemError(
|
|
|
|
info,
|
|
|
|
"'%s' condition of %s must be an array"
|
|
|
|
% (operator, source))
|
2021-08-04 11:31:01 +03:00
|
|
|
if not operands:
|
|
|
|
raise QAPISemError(
|
|
|
|
info, "'if' condition [] of %s is useless" % source)
|
|
|
|
for operand in operands:
|
|
|
|
_check_if(operand)
|
|
|
|
|
|
|
|
ifcond = expr.get('if')
|
|
|
|
if ifcond is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
_check_if(ifcond)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
2021-04-21 21:20:25 +03:00
|
|
|
def normalize_members(members: object) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Normalize a "members" value.
|
|
|
|
|
|
|
|
If ``members`` is a dict, for every value in that dict, if that
|
|
|
|
value is not itself already a dict, normalize it to
|
|
|
|
``{'type': value}``.
|
|
|
|
|
|
|
|
:forms:
|
|
|
|
:sugared: ``Dict[str, Union[str, TypeRef]]``
|
|
|
|
:canonical: ``Dict[str, TypeRef]``
|
|
|
|
|
|
|
|
:param members: The members value to normalize.
|
|
|
|
|
|
|
|
:return: None, ``members`` is normalized in-place as needed.
|
|
|
|
"""
|
2021-04-21 21:20:18 +03:00
|
|
|
if isinstance(members, dict):
|
2019-10-18 10:43:44 +03:00
|
|
|
for key, arg in members.items():
|
|
|
|
if isinstance(arg, dict):
|
|
|
|
continue
|
|
|
|
members[key] = {'type': arg}
|
|
|
|
|
|
|
|
|
2023-03-16 10:13:15 +03:00
|
|
|
def check_type_name(value: Optional[object],
|
|
|
|
info: QAPISourceInfo, source: str) -> None:
|
2023-03-16 10:13:17 +03:00
|
|
|
if value is not None and not isinstance(value, str):
|
|
|
|
raise QAPISemError(info, "%s should be a type name" % source)
|
2023-03-16 10:13:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
def check_type_name_or_array(value: Optional[object],
|
|
|
|
info: QAPISourceInfo, source: str) -> None:
|
2023-03-16 10:13:17 +03:00
|
|
|
if value is None or isinstance(value, str):
|
2021-04-21 21:20:21 +03:00
|
|
|
return
|
|
|
|
|
2023-03-16 10:13:18 +03:00
|
|
|
if not isinstance(value, list):
|
|
|
|
raise QAPISemError(info,
|
|
|
|
"%s should be a type name or array" % source)
|
|
|
|
|
|
|
|
if len(value) != 1 or not isinstance(value[0], str):
|
|
|
|
raise QAPISemError(info,
|
|
|
|
"%s: array type must contain single type name" %
|
|
|
|
source)
|
2023-03-16 10:13:15 +03:00
|
|
|
|
|
|
|
|
2023-03-16 10:13:19 +03:00
|
|
|
def check_type_implicit(value: Optional[object],
|
|
|
|
info: QAPISourceInfo, source: str,
|
|
|
|
parent_name: Optional[str]) -> None:
|
2023-03-16 10:13:15 +03:00
|
|
|
"""
|
|
|
|
Normalize and validate an optional implicit struct type.
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2023-03-16 10:13:19 +03:00
|
|
|
Accept ``None`` or a ``dict`` defining an implicit struct type.
|
|
|
|
The latter is normalized in place.
|
2023-03-16 10:13:15 +03:00
|
|
|
|
|
|
|
:param value: The value to check.
|
|
|
|
:param info: QAPI schema source file information.
|
|
|
|
:param source: Error string describing this ``value``.
|
|
|
|
:param parent_name:
|
|
|
|
When the value of ``parent_name`` is in pragma
|
|
|
|
``member-name-exceptions``, an implicit struct type may
|
|
|
|
violate the member naming rules.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``value`` fails validation.
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
return
|
|
|
|
|
2021-04-21 21:20:18 +03:00
|
|
|
if not isinstance(value, dict):
|
2019-10-18 10:43:44 +03:00
|
|
|
raise QAPISemError(info,
|
|
|
|
"%s should be an object or type name" % source)
|
|
|
|
|
2023-03-16 10:13:15 +03:00
|
|
|
permissive = parent_name in info.pragma.member_name_exceptions
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
for (key, arg) in value.items():
|
|
|
|
key_source = "%s member '%s'" % (source, key)
|
2021-03-23 12:40:04 +03:00
|
|
|
if key.startswith('*'):
|
|
|
|
key = key[1:]
|
2021-03-23 12:40:19 +03:00
|
|
|
check_name_lower(key, info, key_source,
|
2021-03-23 12:40:23 +03:00
|
|
|
permit_upper=permissive,
|
|
|
|
permit_underscore=permissive)
|
2019-10-18 10:43:44 +03:00
|
|
|
if c_name(key, False) == 'u' or c_name(key, False).startswith('has_'):
|
|
|
|
raise QAPISemError(info, "%s uses reserved name" % key_source)
|
2020-03-17 14:54:45 +03:00
|
|
|
check_keys(arg, info, key_source, ['type'], ['if', 'features'])
|
2019-10-18 10:43:44 +03:00
|
|
|
check_if(arg, info, key_source)
|
2020-03-17 14:54:45 +03:00
|
|
|
check_features(arg.get('features'), info)
|
2023-03-16 10:13:15 +03:00
|
|
|
check_type_name_or_array(arg['type'], info, key_source)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
2023-03-16 10:13:19 +03:00
|
|
|
def check_type_name_or_implicit(value: Optional[object],
|
|
|
|
info: QAPISourceInfo, source: str,
|
|
|
|
parent_name: Optional[str]) -> None:
|
|
|
|
if value is None or isinstance(value, str):
|
|
|
|
return
|
|
|
|
|
|
|
|
check_type_implicit(value, info, source, parent_name)
|
|
|
|
|
|
|
|
|
2021-04-21 21:20:25 +03:00
|
|
|
def check_features(features: Optional[object],
|
|
|
|
info: QAPISourceInfo) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Normalize and validate the ``features`` member.
|
|
|
|
|
|
|
|
``features`` may be a ``list`` of either ``str`` or ``dict``.
|
|
|
|
Any ``str`` element will be normalized to ``{'name': element}``.
|
|
|
|
|
|
|
|
:forms:
|
|
|
|
:sugared: ``List[Union[str, Feature]]``
|
|
|
|
:canonical: ``List[Feature]``
|
|
|
|
|
|
|
|
:param features: The features member value to validate.
|
|
|
|
:param info: QAPI schema source file information.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``features`` fails validation.
|
|
|
|
:return: None, ``features`` is normalized in-place as needed.
|
|
|
|
"""
|
2019-10-18 11:14:51 +03:00
|
|
|
if features is None:
|
|
|
|
return
|
|
|
|
if not isinstance(features, list):
|
|
|
|
raise QAPISemError(info, "'features' must be an array")
|
2019-10-24 14:02:33 +03:00
|
|
|
features[:] = [f if isinstance(f, dict) else {'name': f}
|
|
|
|
for f in features]
|
2021-04-21 21:20:27 +03:00
|
|
|
for feat in features:
|
2019-10-18 11:14:51 +03:00
|
|
|
source = "'features' member"
|
2021-04-21 21:20:27 +03:00
|
|
|
assert isinstance(feat, dict)
|
|
|
|
check_keys(feat, info, source, ['name'], ['if'])
|
|
|
|
check_name_is_str(feat['name'], info, source)
|
|
|
|
source = "%s '%s'" % (source, feat['name'])
|
2022-05-10 09:16:44 +03:00
|
|
|
check_name_lower(feat['name'], info, source)
|
2021-04-21 21:20:27 +03:00
|
|
|
check_if(feat, info, source)
|
2019-10-18 11:14:51 +03:00
|
|
|
|
|
|
|
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
def check_enum(expr: QAPIExpression) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Normalize and validate this expression as an ``enum`` definition.
|
|
|
|
|
|
|
|
:param expr: The expression to validate.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``expr`` is not a valid ``enum``.
|
|
|
|
:return: None, ``expr`` is normalized in-place as needed.
|
|
|
|
"""
|
2019-10-18 10:43:44 +03:00
|
|
|
name = expr['enum']
|
|
|
|
members = expr['data']
|
|
|
|
prefix = expr.get('prefix')
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
info = expr.info
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
if not isinstance(members, list):
|
|
|
|
raise QAPISemError(info, "'data' must be an array")
|
|
|
|
if prefix is not None and not isinstance(prefix, str):
|
|
|
|
raise QAPISemError(info, "'prefix' must be a string")
|
|
|
|
|
2021-03-23 12:40:24 +03:00
|
|
|
permissive = name in info.pragma.member_name_exceptions
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2019-10-24 14:02:34 +03:00
|
|
|
members[:] = [m if isinstance(m, dict) else {'name': m}
|
|
|
|
for m in members]
|
2019-10-18 10:43:44 +03:00
|
|
|
for member in members:
|
|
|
|
source = "'data' member"
|
2021-10-25 07:24:02 +03:00
|
|
|
check_keys(member, info, source, ['name'], ['if', 'features'])
|
2021-06-16 10:21:21 +03:00
|
|
|
member_name = member['name']
|
2021-03-23 12:40:06 +03:00
|
|
|
check_name_is_str(member_name, info, source)
|
|
|
|
source = "%s '%s'" % (source, member_name)
|
|
|
|
# Enum members may start with a digit
|
|
|
|
if member_name[0].isdigit():
|
2021-04-21 21:20:16 +03:00
|
|
|
member_name = 'd' + member_name # Hack: hide the digit
|
2021-03-23 12:40:19 +03:00
|
|
|
check_name_lower(member_name, info, source,
|
2021-03-23 12:40:24 +03:00
|
|
|
permit_upper=permissive,
|
|
|
|
permit_underscore=permissive)
|
2019-10-18 10:43:44 +03:00
|
|
|
check_if(member, info, source)
|
2021-10-25 07:24:02 +03:00
|
|
|
check_features(member.get('features'), info)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
def check_struct(expr: QAPIExpression) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Normalize and validate this expression as a ``struct`` definition.
|
|
|
|
|
|
|
|
:param expr: The expression to validate.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``expr`` is not a valid ``struct``.
|
|
|
|
:return: None, ``expr`` is normalized in-place as needed.
|
|
|
|
"""
|
2021-04-21 21:20:23 +03:00
|
|
|
name = cast(str, expr['struct']) # Checked in check_exprs
|
2019-10-18 10:43:44 +03:00
|
|
|
members = expr['data']
|
|
|
|
|
2023-03-16 10:13:19 +03:00
|
|
|
check_type_implicit(members, expr.info, "'data'", name)
|
2023-03-16 10:13:15 +03:00
|
|
|
check_type_name(expr.get('base'), expr.info, "'base'")
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
def check_union(expr: QAPIExpression) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Normalize and validate this expression as a ``union`` definition.
|
|
|
|
|
|
|
|
:param expr: The expression to validate.
|
|
|
|
|
|
|
|
:raise QAPISemError: when ``expr`` is not a valid ``union``.
|
|
|
|
:return: None, ``expr`` is normalized in-place as needed.
|
|
|
|
"""
|
2021-04-21 21:20:23 +03:00
|
|
|
name = cast(str, expr['union']) # Checked in check_exprs
|
2021-09-17 17:31:32 +03:00
|
|
|
base = expr['base']
|
|
|
|
discriminator = expr['discriminator']
|
2019-10-18 10:43:44 +03:00
|
|
|
members = expr['data']
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
info = expr.info
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2023-03-16 10:13:15 +03:00
|
|
|
check_type_name_or_implicit(base, info, "'base'", name)
|
2021-09-17 17:31:32 +03:00
|
|
|
check_name_is_str(discriminator, info, "'discriminator'")
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2021-04-21 21:20:22 +03:00
|
|
|
if not isinstance(members, dict):
|
|
|
|
raise QAPISemError(info, "'data' must be an object")
|
|
|
|
|
2019-10-18 10:43:44 +03:00
|
|
|
for (key, value) in members.items():
|
|
|
|
source = "'data' member '%s'" % key
|
|
|
|
check_keys(value, info, source, ['type'], ['if'])
|
|
|
|
check_if(value, info, source)
|
2023-03-16 10:13:15 +03:00
|
|
|
check_type_name(value['type'], info, source)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
def check_alternate(expr: QAPIExpression) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Normalize and validate this expression as an ``alternate`` definition.
|
|
|
|
|
|
|
|
:param expr: The expression to validate.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``expr`` is not a valid ``alternate``.
|
|
|
|
:return: None, ``expr`` is normalized in-place as needed.
|
|
|
|
"""
|
2019-10-18 10:43:44 +03:00
|
|
|
members = expr['data']
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
info = expr.info
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2020-03-04 18:59:32 +03:00
|
|
|
if not members:
|
2019-10-18 10:43:44 +03:00
|
|
|
raise QAPISemError(info, "'data' must not be empty")
|
2021-04-21 21:20:22 +03:00
|
|
|
|
|
|
|
if not isinstance(members, dict):
|
|
|
|
raise QAPISemError(info, "'data' must be an object")
|
|
|
|
|
2019-10-18 10:43:44 +03:00
|
|
|
for (key, value) in members.items():
|
|
|
|
source = "'data' member '%s'" % key
|
2021-03-23 12:40:25 +03:00
|
|
|
check_name_lower(key, info, source)
|
2019-10-18 10:43:44 +03:00
|
|
|
check_keys(value, info, source, ['type'], ['if'])
|
|
|
|
check_if(value, info, source)
|
2023-03-16 10:13:15 +03:00
|
|
|
check_type_name_or_array(value['type'], info, source)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
def check_command(expr: QAPIExpression) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Normalize and validate this expression as a ``command`` definition.
|
|
|
|
|
|
|
|
:param expr: The expression to validate.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``expr`` is not a valid ``command``.
|
|
|
|
:return: None, ``expr`` is normalized in-place as needed.
|
|
|
|
"""
|
2019-10-18 10:43:44 +03:00
|
|
|
args = expr.get('data')
|
|
|
|
rets = expr.get('returns')
|
|
|
|
boxed = expr.get('boxed', False)
|
|
|
|
|
2023-03-16 10:13:15 +03:00
|
|
|
if boxed:
|
|
|
|
if args is None:
|
|
|
|
raise QAPISemError(expr.info, "'boxed': true requires 'data'")
|
|
|
|
check_type_name(args, expr.info, "'data'")
|
|
|
|
else:
|
|
|
|
check_type_name_or_implicit(args, expr.info, "'data'", None)
|
|
|
|
check_type_name_or_array(rets, expr.info, "'returns'")
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
def check_event(expr: QAPIExpression) -> None:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Normalize and validate this expression as an ``event`` definition.
|
|
|
|
|
|
|
|
:param expr: The expression to validate.
|
|
|
|
|
|
|
|
:raise QAPISemError: When ``expr`` is not a valid ``event``.
|
|
|
|
:return: None, ``expr`` is normalized in-place as needed.
|
|
|
|
"""
|
2019-10-18 10:43:44 +03:00
|
|
|
args = expr.get('data')
|
|
|
|
boxed = expr.get('boxed', False)
|
|
|
|
|
2023-03-16 10:13:15 +03:00
|
|
|
if boxed:
|
|
|
|
if args is None:
|
|
|
|
raise QAPISemError(expr.info, "'boxed': true requires 'data'")
|
|
|
|
check_type_name(args, expr.info, "'data'")
|
|
|
|
else:
|
|
|
|
check_type_name_or_implicit(args, expr.info, "'data'", None)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
def check_exprs(exprs: List[QAPIExpression]) -> List[QAPIExpression]:
|
2021-04-21 21:20:30 +03:00
|
|
|
"""
|
|
|
|
Validate and normalize a list of parsed QAPI schema expressions.
|
|
|
|
|
|
|
|
This function accepts a list of expressions and metadata as returned
|
|
|
|
by the parser. It destructively normalizes the expressions in-place.
|
|
|
|
|
|
|
|
:param exprs: The list of expressions to normalize and validate.
|
|
|
|
|
|
|
|
:raise QAPISemError: When any expression fails validation.
|
|
|
|
:return: The same list of expressions (now modified).
|
|
|
|
"""
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
for expr in exprs:
|
|
|
|
info = expr.info
|
|
|
|
doc = expr.doc
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
if 'include' in expr:
|
|
|
|
continue
|
|
|
|
|
2021-08-31 15:38:08 +03:00
|
|
|
metas = expr.keys() & {'enum', 'struct', 'union', 'alternate',
|
|
|
|
'command', 'event'}
|
|
|
|
if len(metas) != 1:
|
|
|
|
raise QAPISemError(
|
|
|
|
info,
|
|
|
|
"expression must have exactly one key"
|
|
|
|
" 'enum', 'struct', 'union', 'alternate',"
|
|
|
|
" 'command', 'event'")
|
|
|
|
meta = metas.pop()
|
2019-10-18 10:43:44 +03:00
|
|
|
|
2021-04-21 21:20:23 +03:00
|
|
|
check_name_is_str(expr[meta], info, "'%s'" % meta)
|
|
|
|
name = cast(str, expr[meta])
|
2019-10-18 10:43:44 +03:00
|
|
|
info.set_defn(meta, name)
|
|
|
|
check_defn_name_str(name, info, meta)
|
|
|
|
|
|
|
|
if doc:
|
|
|
|
if doc.symbol != name:
|
|
|
|
raise QAPISemError(
|
|
|
|
info, "documentation comment is for '%s'" % doc.symbol)
|
|
|
|
doc.check_expr(expr)
|
|
|
|
elif info.pragma.doc_required:
|
|
|
|
raise QAPISemError(info,
|
|
|
|
"documentation comment required")
|
|
|
|
|
|
|
|
if meta == 'enum':
|
|
|
|
check_keys(expr, info, meta,
|
2020-03-17 14:54:37 +03:00
|
|
|
['enum', 'data'], ['if', 'features', 'prefix'])
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
check_enum(expr)
|
2019-10-18 10:43:44 +03:00
|
|
|
elif meta == 'union':
|
|
|
|
check_keys(expr, info, meta,
|
2021-09-17 17:31:32 +03:00
|
|
|
['union', 'base', 'discriminator', 'data'],
|
|
|
|
['if', 'features'])
|
2019-10-18 10:43:44 +03:00
|
|
|
normalize_members(expr.get('base'))
|
|
|
|
normalize_members(expr['data'])
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
check_union(expr)
|
2019-10-18 10:43:44 +03:00
|
|
|
elif meta == 'alternate':
|
|
|
|
check_keys(expr, info, meta,
|
2020-03-17 14:54:37 +03:00
|
|
|
['alternate', 'data'], ['if', 'features'])
|
2019-10-18 10:43:44 +03:00
|
|
|
normalize_members(expr['data'])
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
check_alternate(expr)
|
2019-10-18 10:43:44 +03:00
|
|
|
elif meta == 'struct':
|
|
|
|
check_keys(expr, info, meta,
|
|
|
|
['struct', 'data'], ['base', 'if', 'features'])
|
|
|
|
normalize_members(expr['data'])
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
check_struct(expr)
|
2019-10-18 10:43:44 +03:00
|
|
|
elif meta == 'command':
|
|
|
|
check_keys(expr, info, meta,
|
|
|
|
['command'],
|
2019-10-18 11:14:51 +03:00
|
|
|
['data', 'returns', 'boxed', 'if', 'features',
|
2019-10-18 10:43:44 +03:00
|
|
|
'gen', 'success-response', 'allow-oob',
|
2020-10-05 18:58:49 +03:00
|
|
|
'allow-preconfig', 'coroutine'])
|
2019-10-18 10:43:44 +03:00
|
|
|
normalize_members(expr.get('data'))
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
check_command(expr)
|
2019-10-18 10:43:44 +03:00
|
|
|
elif meta == 'event':
|
|
|
|
check_keys(expr, info, meta,
|
2020-03-17 14:54:37 +03:00
|
|
|
['event'], ['data', 'boxed', 'if', 'features'])
|
2019-10-18 10:43:44 +03:00
|
|
|
normalize_members(expr.get('data'))
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
check_event(expr)
|
2019-10-18 10:43:44 +03:00
|
|
|
else:
|
|
|
|
assert False, 'unexpected meta type'
|
|
|
|
|
|
|
|
check_if(expr, info, meta)
|
2020-03-17 14:54:37 +03:00
|
|
|
check_features(expr.get('features'), info)
|
qapi/parser: add QAPIExpression type
This patch creates a new type, QAPIExpression, which represents a parsed
expression complete with QAPIDoc and QAPISourceInfo.
This patch turns parser.exprs into a list of QAPIExpression instead,
and adjusts expr.py to match.
This allows the types we specify in parser.py to be "remembered" all the
way through expr.py and into schema.py. Several assertions around
packing and unpacking this data can be removed as a result.
It also corrects a harmless typing error. Before the patch,
check_exprs() allegedly takes a List[_JSONObject]. It actually takes
a list of dicts of the form
{'expr': E, 'info': I, 'doc': D}
where E is of type _ExprValue, I is of type QAPISourceInfo, and D is
of type QAPIDoc. Key 'doc' is optional. This is not a _JSONObject!
Passes type checking anyway, because _JSONObject is Dict[str, object].
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20230215000011.1725012-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Commit message amended to point out the typing fix]
2023-02-15 03:00:09 +03:00
|
|
|
check_flags(expr)
|
2019-10-18 10:43:44 +03:00
|
|
|
|
|
|
|
return exprs
|