[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 04/26] qapi/source.py: Add default arguments to QAPISourceInfo
From: |
John Snow |
Subject: |
[PATCH 04/26] qapi/source.py: Add default arguments to QAPISourceInfo |
Date: |
Tue, 22 Sep 2020 18:35:03 -0400 |
The parser tries to create an object as QAPISourceInfo(None, None,
None). Add some defaults to QAPISourceInfo such that we don't need to
pass explicit as many explicit nothings.
Having the defaults nearby the code in source.py also helps remind us
that they might be unset.
Using a numerical 0 to mean "no line" is nicer to type than using None
for the same.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/parser.py | 2 +-
scripts/qapi/source.py | 7 +++++--
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index d0f35fe03e..db4e9ae872 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -67,7 +67,7 @@ def __init__(self, fname, previously_included=None,
incl_info=None):
with open(self._fname, 'r', encoding='utf-8') as fp:
self.src = fp.read()
except IOError as e:
- raise QAPISemError(incl_info or QAPISourceInfo(None, None, None),
+ raise QAPISemError(incl_info or QAPISourceInfo(None),
"can't read %s file '%s': %s"
% ("include" if incl_info else "schema",
self._fname,
diff --git a/scripts/qapi/source.py b/scripts/qapi/source.py
index ba991d798f..d1de9e36b1 100644
--- a/scripts/qapi/source.py
+++ b/scripts/qapi/source.py
@@ -30,7 +30,10 @@ def __init__(self) -> None:
class QAPISourceInfo:
T = TypeVar('T', bound='QAPISourceInfo')
- def __init__(self: T, fname: str, line: int, parent: Optional[T]):
+ def __init__(self: T,
+ fname: str,
+ line: int = 0,
+ parent: Optional[T] = None):
self.fname = fname
self.line = line
self.parent = parent
@@ -53,7 +56,7 @@ def loc(self) -> str:
if self.fname is None:
return sys.argv[0]
ret = self.fname
- if self.line is not None:
+ if self.line:
ret += ':%d' % self.line
return ret
--
2.26.2
- [PATCH 00/26] qapi: static typing conversion, pt5, John Snow, 2020/09/22
- [PATCH 03/26] qapi/parser.py: use 'with' statement for opening files, John Snow, 2020/09/22
- [PATCH 02/26] qapi/parser.py: group variable declarations in __init__, John Snow, 2020/09/22
- [PATCH 04/26] qapi/source.py: Add default arguments to QAPISourceInfo,
John Snow <=
- [PATCH 01/26] qapi/parser.py: refactor parsing routine into method, John Snow, 2020/09/22
- [PATCH 05/26] qapi/parser.py: start source info at line 0, John Snow, 2020/09/22
- [PATCH 07/26] qapi/parser.py: fully remove 'null' constant, John Snow, 2020/09/22
- [PATCH 08/26] qapi/parser.py: Assert lexer value is a string, John Snow, 2020/09/22
- [PATCH 06/26] qapi/parser.py: raise QAPIParseError during file opening, John Snow, 2020/09/22
- [PATCH 09/26] qapi/parser.py: assert get_expr returns object in outer loop, John Snow, 2020/09/22
- [PATCH 10/26] qapi/parser.py: assert object keys are strings, John Snow, 2020/09/22
- [PATCH 11/26] qapi/parser.py: Convert several methods to @classmethod, John Snow, 2020/09/22
- [PATCH 12/26] qapi/parser.py: add casts to pragma checks, John Snow, 2020/09/22
- [PATCH 13/26] qapi/parser.py: add type hint annotations, John Snow, 2020/09/22