Skip to content

Commit eae587e

Browse files
committed
Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2021-09-13' into staging
QAPI patches patches for 2021-09-13 # gpg: Signature made Mon 13 Sep 2021 08:53:42 BST # gpg: using RSA key 354BC8B3D7EB2A6B68674E5F3870B400EB918653 # gpg: issuer "[email protected]" # gpg: Good signature from "Markus Armbruster <[email protected]>" [full] # gpg: aka "Markus Armbruster <[email protected]>" [full] # Primary key fingerprint: 354B C8B3 D7EB 2A6B 6867 4E5F 3870 B400 EB91 8653 * remotes/armbru/tags/pull-qapi-2021-09-13: qapi: Fix bogus error for 'if': { 'not': '' } tests/qapi-schema: Cover 'not' condition with empty argument qapi: Bury some unused code in class Indentation qapi: Drop Indentation.__bool__() qapi: Fix a botched type annotation Signed-off-by: Peter Maydell <[email protected]>
2 parents 99c4498 + 62f2758 commit eae587e

File tree

6 files changed

+25
-21
lines changed

6 files changed

+25
-21
lines changed

scripts/qapi/common.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,20 @@ class Indentation:
132132
def __init__(self, initial: int = 0) -> None:
133133
self._level = initial
134134

135-
def __int__(self) -> int:
136-
return self._level
137-
138135
def __repr__(self) -> str:
139136
return "{}({:d})".format(type(self).__name__, self._level)
140137

141138
def __str__(self) -> str:
142139
"""Return the current indentation as a string of spaces."""
143140
return ' ' * self._level
144141

145-
def __bool__(self) -> bool:
146-
"""True when there is a non-zero indentation."""
147-
return bool(self._level)
148-
149142
def increase(self, amount: int = 4) -> None:
150143
"""Increase the indentation level by ``amount``, default 4."""
151144
self._level += amount
152145

153146
def decrease(self, amount: int = 4) -> None:
154147
"""Decrease the indentation level by ``amount``, default 4."""
155-
if self._level < amount:
156-
raise ArithmeticError(
157-
f"Can't remove {amount:d} spaces from {self!r}")
148+
assert amount <= self._level
158149
self._level -= amount
159150

160151

@@ -169,8 +160,9 @@ def cgen(code: str, **kwds: object) -> str:
169160
Obey `indent`, and strip `EATSPACE`.
170161
"""
171162
raw = code % kwds
172-
if indent:
173-
raw = re.sub(r'^(?!(#|$))', str(indent), raw, flags=re.MULTILINE)
163+
pfx = str(indent)
164+
if pfx:
165+
raw = re.sub(r'^(?!(#|$))', pfx, raw, flags=re.MULTILINE)
174166
return re.sub(re.escape(EATSPACE) + r' *', '', raw)
175167

176168

@@ -205,7 +197,8 @@ def gen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]],
205197
cond_fmt: str, not_fmt: str,
206198
all_operator: str, any_operator: str) -> str:
207199

208-
def do_gen(ifcond: Union[str, Dict[str, Any]], need_parens: bool):
200+
def do_gen(ifcond: Union[str, Dict[str, Any]],
201+
need_parens: bool) -> str:
209202
if isinstance(ifcond, str):
210203
return cond_fmt % ifcond
211204
assert isinstance(ifcond, dict) and len(ifcond) == 1

scripts/qapi/expr.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,22 @@ def _check_if(cond: Union[str, object]) -> None:
293293
info,
294294
"'if' condition of %s has conflicting keys" % source)
295295

296-
oper, operands = next(iter(cond.items()))
296+
if 'not' in cond:
297+
_check_if(cond['not'])
298+
elif 'all' in cond:
299+
_check_infix('all', cond['all'])
300+
else:
301+
_check_infix('any', cond['any'])
302+
303+
def _check_infix(operator: str, operands: object) -> None:
304+
if not isinstance(operands, list):
305+
raise QAPISemError(
306+
info,
307+
"'%s' condition of %s must be an array"
308+
% (operator, source))
297309
if not operands:
298310
raise QAPISemError(
299311
info, "'if' condition [] of %s is useless" % source)
300-
301-
if oper == "not":
302-
_check_if(operands)
303-
return
304-
if oper in ("all", "any") and not isinstance(operands, list):
305-
raise QAPISemError(
306-
info, "'%s' condition of %s must be an array" % (oper, source))
307312
for operand in operands:
308313
_check_if(operand)
309314

tests/qapi-schema/bad-if-not.err

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bad-if-not.json: In struct 'TestIfStruct':
2+
bad-if-not.json:2: 'if' condition '' of struct is not a valid identifier

tests/qapi-schema/bad-if-not.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# check 'if not' with empy argument
2+
{ 'struct': 'TestIfStruct', 'data': { 'foo': 'int' },
3+
'if': { 'not': '' } }

tests/qapi-schema/bad-if-not.out

Whitespace-only changes.

tests/qapi-schema/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ schemas = [
4343
'bad-if-key.json',
4444
'bad-if-keys.json',
4545
'bad-if-list.json',
46+
'bad-if-not.json',
4647
'bad-type-bool.json',
4748
'bad-type-dict.json',
4849
'bad-type-int.json',

0 commit comments

Comments
 (0)