[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/6] qapi/error.py: refactor error classes
From: |
John Snow |
Subject: |
[PATCH 1/6] qapi/error.py: refactor error classes |
Date: |
Tue, 22 Sep 2020 17:21:10 -0400 |
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(-)
diff --git a/scripts/qapi/error.py b/scripts/qapi/error.py
index ae60d9e2fe..47f266f33d 100644
--- a/scripts/qapi/error.py
+++ b/scripts/qapi/error.py
@@ -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."""
diff --git a/scripts/qapi/main.py b/scripts/qapi/main.py
index 3f8338ade8..302b9eee81 100644
--- a/scripts/qapi/main.py
+++ b/scripts/qapi/main.py
@@ -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)
diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index 3023bab44b..121d8488d2 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -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))
--
2.26.2
- [PATCH 0/6] qapi: static typing conversion, pt4, John Snow, 2020/09/22
- [PATCH 1/6] qapi/error.py: refactor error classes,
John Snow <=
- [PATCH 2/6] qapi/parser.py: remove parser context from QAPIDoc, John Snow, 2020/09/22
- [PATCH 5/6] qapi/error.py: enable mypy checks, John Snow, 2020/09/22
- [PATCH 3/6] qapi/error.py: move QAPIParseError to parser.py, John Snow, 2020/09/22
- [PATCH 6/6] qapi/error.py: enable pylint checks, John Snow, 2020/09/22
- [PATCH 4/6] qapi/parser.py: refactor QAPIParseError, John Snow, 2020/09/22