rpcgen: use proper operators when comparing types

flake8 (run on all python scripts as a part of the syntax checks)
version 6.1.0 (on macOS 14) issued many complaints like this on the
new rpcgen python scripts:

[...]libvirt/scripts/rpcgen/rpcgen/lexer.py:57:17: E721 do not compare types, for exact checks use `is` / `is not`, for instance checks use `isinstance()`

This patch changes all [type] == [type] to use "is" instead of "==",
and similarly to use "is not" instead of "!=".

(flake8 5.03, e.g. on Fedora 38, is just fine with using "==" and "!=",
but python on both likes "is" and "is not")

Fixes: commit v9.9.0-24-g8ec79e5e14
Fixes: commit v9.9.0-22-gca3f025011
Fixes: commit v9.9.0-21-g031efb691f
Fixes: commit v9.9.0-20-g8c8b97685b

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Laine Stump 2023-11-03 19:40:32 -04:00
parent 72723b2b90
commit 9953ab321e
4 changed files with 50 additions and 50 deletions

View File

@ -185,9 +185,9 @@ class XDRTypeCustom(XDRType):
self.definition = definition self.definition = definition
def is_scalar(self): def is_scalar(self):
if type(self.definition) == XDRDefinitionEnum: if type(self.definition) is XDRDefinitionEnum:
return True return True
if type(self.definition) == XDRDefinitionTypedef: if type(self.definition) is XDRDefinitionTypedef:
return self.definition.decl.typ.is_scalar() return self.definition.decl.typ.is_scalar()
return False return False

View File

@ -81,7 +81,7 @@ class XDRTypeDeclarationGenerator(XDRVisitor):
) )
def visit_declaration_variablearray(self, obj, indent, context): def visit_declaration_variablearray(self, obj, indent, context):
if type(obj.typ) == XDRTypeString: if type(obj.typ) is XDRTypeString:
return "%schar *%s" % (indent, obj.identifier) return "%schar *%s" % (indent, obj.identifier)
else: else:
code = ( code = (
@ -178,10 +178,10 @@ class XDRTypeDeclarationGenerator(XDRVisitor):
+ "%s union {\n" % indent + "%s union {\n" % indent
) )
for value in obj.cases: for value in obj.cases:
if type(value.decl.typ) == XDRTypeVoid: if type(value.decl.typ) is XDRTypeVoid:
continue continue
code = code + self.visit_object(value, indent + " ") + ";\n" code = code + self.visit_object(value, indent + " ") + ";\n"
if obj.default is not None and type(obj.default.typ) != XDRTypeVoid: if obj.default is not None and type(obj.default.typ) is not XDRTypeVoid:
code = code + self.visit_object(obj.default, indent + " ") + ";\n" code = code + self.visit_object(obj.default, indent + " ") + ";\n"
code = code + "%s } %su;\n" % (indent, prefix) + "%s}" % indent code = code + "%s } %su;\n" % (indent, prefix) + "%s}" % indent
return code return code
@ -250,10 +250,10 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
return code return code
def generate_type_call(self, decl, field, typename, embedded=False, indent=""): def generate_type_call(self, decl, field, typename, embedded=False, indent=""):
if type(decl.typ) == XDRTypeVoid: if type(decl.typ) is XDRTypeVoid:
return "" return ""
if type(decl) == XDRDeclarationFixedArray: if type(decl) is XDRDeclarationFixedArray:
if type(decl.typ) == XDRTypeOpaque: if type(decl.typ) is XDRTypeOpaque:
code = "%s if (!xdr_%s(xdrs, %s, %s))\n" % ( code = "%s if (!xdr_%s(xdrs, %s, %s))\n" % (
indent, indent,
self.visit_object(decl.typ, context="func"), self.visit_object(decl.typ, context="func"),
@ -270,7 +270,7 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
self.visit_object(decl.typ), self.visit_object(decl.typ),
self.visit_object(decl.typ, context="func"), self.visit_object(decl.typ, context="func"),
) )
elif type(decl) == XDRDeclarationVariableArray: elif type(decl) is XDRDeclarationVariableArray:
fieldRef = "." fieldRef = "."
pointerStr = "" pointerStr = ""
if embedded: if embedded:
@ -278,7 +278,7 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
else: else:
fieldRef = "->" fieldRef = "->"
if type(decl.typ) == XDRTypeString: if type(decl.typ) is XDRTypeString:
code = "%s if (!xdr_%s(xdrs, %s%s, %s))\n" % ( code = "%s if (!xdr_%s(xdrs, %s%s, %s))\n" % (
indent, indent,
self.visit_object(decl.typ, context="func"), self.visit_object(decl.typ, context="func"),
@ -286,7 +286,7 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
field, field,
decl.maxlength, decl.maxlength,
) )
elif type(decl.typ) == XDRTypeOpaque: elif type(decl.typ) is XDRTypeOpaque:
code = "%s if (!xdr_bytes(xdrs, (char **)&%s%s%s_val, " % ( code = "%s if (!xdr_bytes(xdrs, (char **)&%s%s%s_val, " % (
indent, indent,
field, field,
@ -311,7 +311,7 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
self.visit_object(decl.typ, context="func"), self.visit_object(decl.typ, context="func"),
) )
) )
elif type(decl) == XDRDeclarationPointer: elif type(decl) is XDRDeclarationPointer:
pointerStr = "" pointerStr = ""
if embedded: if embedded:
pointerStr = "&" pointerStr = "&"
@ -327,9 +327,9 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
else: else:
pointerStr = "" pointerStr = ""
isFixedArray = ( isFixedArray = (
type(decl.typ) == XDRTypeCustom type(decl.typ) is XDRTypeCustom
and type(decl.typ.definition) == XDRDefinitionTypedef and type(decl.typ.definition) is XDRDefinitionTypedef
and type(decl.typ.definition.decl) == XDRDeclarationFixedArray and type(decl.typ.definition.decl) is XDRDeclarationFixedArray
) )
if embedded and not isFixedArray: if embedded and not isFixedArray:

View File

@ -55,7 +55,7 @@ class XDRToken(abc.ABC):
def __eq__(self, other): def __eq__(self, other):
return ( return (
type(self) == type(other) type(self) is type(other)
and self.line == other.line and self.line == other.line
and self.column == other.column and self.column == other.column
and self.value == other.value and self.value == other.value

View File

@ -162,10 +162,10 @@ class XDRParser:
if token is None: if token is None:
return None return None
if type(token) == XDRTokenCEscape: if type(token) is XDRTokenCEscape:
return XDRDefinitionCEscape(token.value[1:]) return XDRDefinitionCEscape(token.value[1:])
if type(token) != XDRTokenIdentifier: if type(token) is not XDRTokenIdentifier:
raise Exception("Expected identifier, but got %s" % token) raise Exception("Expected identifier, but got %s" % token)
defs = { defs = {
@ -186,18 +186,18 @@ class XDRParser:
definition = func() definition = func()
semi = self.lexer.next() semi = self.lexer.next()
if type(semi) != XDRTokenPunctuation or semi.value != ";": if type(semi) is not XDRTokenPunctuation or semi.value != ";":
raise Exception("Expected ';', but got %s" % semi) raise Exception("Expected ';', but got %s" % semi)
return definition return definition
def parse_definition_const(self): def parse_definition_const(self):
ident = self.lexer.next() ident = self.lexer.next()
if type(ident) != XDRTokenIdentifier: if type(ident) is not XDRTokenIdentifier:
raise Exception("Expected identifier, but got %s" % ident) raise Exception("Expected identifier, but got %s" % ident)
assign = self.lexer.next() assign = self.lexer.next()
if type(assign) != XDRTokenPunctuation or assign.value != "=": if type(assign) is not XDRTokenPunctuation or assign.value != "=":
raise Exception("Expected '=', but got %s" % assign) raise Exception("Expected '=', but got %s" % assign)
const = self.lexer.next() const = self.lexer.next()
@ -217,7 +217,7 @@ class XDRParser:
def parse_definition_enum(self): def parse_definition_enum(self):
name = self.lexer.next() name = self.lexer.next()
if type(name) != XDRTokenIdentifier: if type(name) is not XDRTokenIdentifier:
raise Exception("Expected identifier, but got %s" % name) raise Exception("Expected identifier, but got %s" % name)
body = self.parse_enum_body() body = self.parse_enum_body()
@ -231,7 +231,7 @@ class XDRParser:
def parse_definition_struct(self): def parse_definition_struct(self):
name = self.lexer.next() name = self.lexer.next()
if type(name) != XDRTokenIdentifier: if type(name) is not XDRTokenIdentifier:
raise Exception("Expected identifier, but got %s" % name) raise Exception("Expected identifier, but got %s" % name)
body = self.parse_struct_body() body = self.parse_struct_body()
@ -245,7 +245,7 @@ class XDRParser:
def parse_definition_union(self): def parse_definition_union(self):
name = self.lexer.next() name = self.lexer.next()
if type(name) != XDRTokenIdentifier: if type(name) is not XDRTokenIdentifier:
raise Exception("Expected identifier, but got %s" % name) raise Exception("Expected identifier, but got %s" % name)
body = self.parse_union_body() body = self.parse_union_body()
@ -260,23 +260,23 @@ class XDRParser:
def parse_declaration(self): def parse_declaration(self):
typ = self.parse_type() typ = self.parse_type()
if type(typ) == XDRTypeVoid: if type(typ) is XDRTypeVoid:
return XDRDeclarationScalar(typ, None) return XDRDeclarationScalar(typ, None)
ident = self.lexer.next() ident = self.lexer.next()
pointer = False pointer = False
if type(ident) == XDRTokenPunctuation: if type(ident) is XDRTokenPunctuation:
if ident.value != "*": if ident.value != "*":
raise Exception("Expected '*' or identifer, but got %s" % ident) raise Exception("Expected '*' or identifer, but got %s" % ident)
if type(typ) == XDRTypeString or type(typ) == XDRTypeOpaque: if type(typ) is XDRTypeString or type(typ) is XDRTypeOpaque:
raise Exception("Pointer invalid for 'string' and 'opaque' types") raise Exception("Pointer invalid for 'string' and 'opaque' types")
pointer = True pointer = True
ident = self.lexer.next() ident = self.lexer.next()
bracket = self.lexer.peek() bracket = self.lexer.peek()
if type(bracket) == XDRTokenPunctuation: if type(bracket) is XDRTokenPunctuation:
if bracket.value == "[": if bracket.value == "[":
_ = self.lexer.next() _ = self.lexer.next()
value = self.lexer.next() value = self.lexer.next()
@ -284,10 +284,10 @@ class XDRParser:
raise Exception("Expected constant, but got %s" % value) raise Exception("Expected constant, but got %s" % value)
close = self.lexer.next() close = self.lexer.next()
if type(close) != XDRTokenPunctuation or close.value != "]": if type(close) is not XDRTokenPunctuation or close.value != "]":
raise Exception("Expected ']', but got %s" % value) raise Exception("Expected ']', but got %s" % value)
if type(typ) == XDRTypeString: if type(typ) is XDRTypeString:
raise Exception("Fixed array invalid for 'string' type") raise Exception("Fixed array invalid for 'string' type")
return XDRDeclarationFixedArray(typ, ident.value, value.value) return XDRDeclarationFixedArray(typ, ident.value, value.value)
elif bracket.value == "<": elif bracket.value == "<":
@ -298,7 +298,7 @@ class XDRParser:
value = self.lexer.next().value value = self.lexer.next().value
close = self.lexer.next() close = self.lexer.next()
if type(close) != XDRTokenPunctuation or close.value != ">": if type(close) is not XDRTokenPunctuation or close.value != ">":
raise Exception("Expected '>', but got %s" % close) raise Exception("Expected '>', but got %s" % close)
return XDRDeclarationVariableArray(typ, ident.value, value) return XDRDeclarationVariableArray(typ, ident.value, value)
@ -310,12 +310,12 @@ class XDRParser:
def parse_type(self): def parse_type(self):
typ = self.lexer.next() typ = self.lexer.next()
if type(typ) != XDRTokenIdentifier: if type(typ) is not XDRTokenIdentifier:
raise Exception("Expected identifier, but got %s" % typ) raise Exception("Expected identifier, but got %s" % typ)
if typ.value == "unsigned": if typ.value == "unsigned":
typ = self.lexer.peek() typ = self.lexer.peek()
if type(typ) != XDRTokenIdentifier: if type(typ) is not XDRTokenIdentifier:
raise Exception("Expected identifier, but got %s" % typ) raise Exception("Expected identifier, but got %s" % typ)
if typ.value == "char": if typ.value == "char":
@ -366,25 +366,25 @@ class XDRParser:
def parse_enum_body(self): def parse_enum_body(self):
body = self.lexer.next() body = self.lexer.next()
if type(body) != XDRTokenPunctuation or body.value != "{": if type(body) is not XDRTokenPunctuation or body.value != "{":
raise Exception("Expected '{', but got %s" % body) raise Exception("Expected '{', but got %s" % body)
values = [] values = []
while True: while True:
ident = self.lexer.next() ident = self.lexer.next()
if type(ident) != XDRTokenIdentifier: if type(ident) is not XDRTokenIdentifier:
raise Exception("Expected identifier, but got %s" % ident) raise Exception("Expected identifier, but got %s" % ident)
equal = self.lexer.next() equal = self.lexer.next()
if type(equal) != XDRTokenPunctuation or equal.value != "=": if type(equal) is not XDRTokenPunctuation or equal.value != "=":
raise Exception("Expected '=', but got %s" % ident) raise Exception("Expected '=', but got %s" % ident)
value = self.lexer.next() value = self.lexer.next()
if type(value) != XDRTokenConstant: if type(value) is not XDRTokenConstant:
raise Exception("Expected constant, but got %s" % ident) raise Exception("Expected constant, but got %s" % ident)
separator = self.lexer.next() separator = self.lexer.next()
if type(separator) != XDRTokenPunctuation and separator.value not in [ if type(separator) is not XDRTokenPunctuation and separator.value not in [
"}", "}",
",", ",",
]: ]:
@ -403,7 +403,7 @@ class XDRParser:
def parse_struct_body(self): def parse_struct_body(self):
body = self.lexer.next() body = self.lexer.next()
if type(body) != XDRTokenPunctuation or body.value != "{": if type(body) is not XDRTokenPunctuation or body.value != "{":
raise Exception("Expected '{', but got %s" % body) raise Exception("Expected '{', but got %s" % body)
fields = [] fields = []
@ -412,11 +412,11 @@ class XDRParser:
fields.append(field) fields.append(field)
separator = self.lexer.next() separator = self.lexer.next()
if type(separator) != XDRTokenPunctuation and separator.value != ";": if type(separator) is not XDRTokenPunctuation and separator.value != ";":
raise Exception("Expected ';', but got %s" % separator) raise Exception("Expected ';', but got %s" % separator)
end = self.lexer.peek() end = self.lexer.peek()
if type(end) == XDRTokenPunctuation and end.value == "}": if type(end) is XDRTokenPunctuation and end.value == "}":
break break
# discard the '}' we peeked at to end the loop # discard the '}' we peeked at to end the loop
@ -429,28 +429,28 @@ class XDRParser:
def parse_union_body(self): def parse_union_body(self):
ident = self.lexer.next() ident = self.lexer.next()
if type(ident) != XDRTokenIdentifier or ident.value != "switch": if type(ident) is not XDRTokenIdentifier or ident.value != "switch":
raise Exception("Expected 'switch', but got %s" % ident) raise Exception("Expected 'switch', but got %s" % ident)
bracket = self.lexer.next() bracket = self.lexer.next()
if type(bracket) != XDRTokenPunctuation or bracket.value != "(": if type(bracket) is not XDRTokenPunctuation or bracket.value != "(":
raise Exception("Expected '(', but got %s" % bracket) raise Exception("Expected '(', but got %s" % bracket)
discriminator = self.parse_declaration() discriminator = self.parse_declaration()
bracket = self.lexer.next() bracket = self.lexer.next()
if type(bracket) != XDRTokenPunctuation or bracket.value != ")": if type(bracket) is not XDRTokenPunctuation or bracket.value != ")":
raise Exception("Expected ')', but got %s" % bracket) raise Exception("Expected ')', but got %s" % bracket)
bracket = self.lexer.next() bracket = self.lexer.next()
if type(bracket) != XDRTokenPunctuation or bracket.value != "{": if type(bracket) is not XDRTokenPunctuation or bracket.value != "{":
raise Exception("Expected '{', but got %s" % bracket) raise Exception("Expected '{', but got %s" % bracket)
default = None default = None
cases = [] cases = []
while True: while True:
ident = self.lexer.next() ident = self.lexer.next()
if type(ident) != XDRTokenIdentifier or ident.value not in [ if type(ident) is not XDRTokenIdentifier or ident.value not in [
"default", "default",
"case", "case",
]: ]:
@ -463,7 +463,7 @@ class XDRParser:
raise Exception("Expected constant, but got %s" % value) raise Exception("Expected constant, but got %s" % value)
sep = self.lexer.next() sep = self.lexer.next()
if type(sep) != XDRTokenPunctuation or sep.value != ":": if type(sep) is not XDRTokenPunctuation or sep.value != ":":
raise Exception("Expected ':', but got %s" % value) raise Exception("Expected ':', but got %s" % value)
decl = self.parse_declaration() decl = self.parse_declaration()
@ -475,17 +475,17 @@ class XDRParser:
raise Exception("Duplicate 'default' clause") raise Exception("Duplicate 'default' clause")
sep = self.lexer.next() sep = self.lexer.next()
if type(sep) != XDRTokenPunctuation or sep.value != ":": if type(sep) is not XDRTokenPunctuation or sep.value != ":":
raise Exception("Expected ':', but got %s" % value) raise Exception("Expected ':', but got %s" % value)
default = self.parse_declaration() default = self.parse_declaration()
separator = self.lexer.next() separator = self.lexer.next()
if type(separator) != XDRTokenPunctuation and separator.value != ";": if type(separator) is not XDRTokenPunctuation and separator.value != ";":
raise Exception("Expected ';', but got %s" % bracket) raise Exception("Expected ';', but got %s" % bracket)
end = self.lexer.peek() end = self.lexer.peek()
if type(end) == XDRTokenPunctuation and end.value == "}": if type(end) is XDRTokenPunctuation and end.value == "}":
break break
# discard the '}' we peeked at to end the loop # discard the '}' we peeked at to end the loop