@@ -11,23 +11,35 @@
# This work is licensed under the terms of the GNU GPL, version 2.
# See the COPYING file in the top-level directory.
+from typing import Optional
+
+from .source import QAPISourceInfo
+
class QAPIError(Exception):
- def __init__(self, info, col, msg):
- Exception.__init__(self)
+ """Base class for all exceptions from the QAPI module."""
+
+
+class QAPISourceError(QAPIError):
+ """Error class for all exceptions identifying a source location."""
+ def __init__(self,
+ info: QAPISourceInfo,
+ msg: str,
+ col: Optional[int] = None):
+ super().__init__()
self.info = info
- self.col = col
self.msg = msg
+ self.col = col
- def __str__(self):
+ def __str__(self) -> str:
loc = str(self.info)
if self.col is not None:
- assert self.info.line is not None
- loc += ':%s' % self.col
- return loc + ': ' + self.msg
+ loc += f":{self.col}"
+ return f"{loc}: {self.msg}"
-class QAPIParseError(QAPIError):
+class QAPIParseError(QAPISourceError):
+ """Error class for all QAPI schema parsing errors."""
def __init__(self, parser, msg):
col = 1
for ch in parser.src[parser.line_pos:parser.pos]:
@@ -35,9 +47,8 @@ def __init__(self, parser, msg):
col = (col + 7) % 8 + 1
else:
col += 1
- super().__init__(parser.info, col, msg)
+ super().__init__(parser.info, msg, col)
-class QAPISemError(QAPIError):
- def __init__(self, info, msg):
- super().__init__(info, None, msg)
+class QAPISemError(QAPISourceError):
+ """Error class for semantic QAPI errors."""
@@ -45,7 +45,7 @@ def generate(schema_file: str,
if match and match.end() != len(prefix):
msg = "funny character '{:s}' in prefix '{:s}'".format(
prefix[match.end()], prefix)
- raise QAPIError('', None, msg)
+ raise QAPIError(msg)
schema = QAPISchema(schema_file)
gen_types(schema, output_dir, prefix, builtins)
@@ -20,7 +20,7 @@
from typing import Optional
from .common import c_name, POINTER_SUFFIX
-from .error import QAPIError, QAPISemError
+from .error import QAPISourceError, QAPISemError
from .expr import check_exprs
from .parser import QAPISchemaParser
@@ -841,7 +841,7 @@ def _def_entity(self, ent):
other_ent = self._entity_dict.get(ent.name)
if other_ent:
if other_ent.info:
- where = QAPIError(other_ent.info, None, "previous definition")
+ where = QAPISourceError(other_ent.info, "previous definition")
raise QAPISemError(
ent.info,
"'%s' is already defined\n%s" % (ent.name, where))
Create a ubiquitous, context-free base error class that all exceptions in the qapi package should inherit from. Move the QAPISourceInfo relevant fields up into QAPIErrorLocation; making sure the order of arguments is consistent between QAPIErrorLocation and QAPISemError. --- The order of arguments for QAPIParseError is inconsistent, but handled explicitly in the __init__ method; this will be addressed in forthcoming patches, but it works correctly here. Signed-off-by: John Snow <jsnow@redhat.com> --- scripts/qapi/error.py | 35 +++++++++++++++++++++++------------ scripts/qapi/main.py | 2 +- scripts/qapi/schema.py | 4 ++-- 3 files changed, 26 insertions(+), 15 deletions(-)