[libvirt PATCH] rpcgen: use proper operators when comparing types

Laine Stump posted 1 patch 5 months, 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20231104000626.581451-1-laine@redhat.com
scripts/rpcgen/rpcgen/ast.py       |  4 +-
scripts/rpcgen/rpcgen/generator.py | 26 ++++++------
scripts/rpcgen/rpcgen/lexer.py     |  2 +-
scripts/rpcgen/rpcgen/parser.py    | 68 +++++++++++++++---------------
4 files changed, 50 insertions(+), 50 deletions(-)
[libvirt PATCH] rpcgen: use proper operators when comparing types
Posted by Laine Stump 5 months, 3 weeks ago
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>
---
 scripts/rpcgen/rpcgen/ast.py       |  4 +-
 scripts/rpcgen/rpcgen/generator.py | 26 ++++++------
 scripts/rpcgen/rpcgen/lexer.py     |  2 +-
 scripts/rpcgen/rpcgen/parser.py    | 68 +++++++++++++++---------------
 4 files changed, 50 insertions(+), 50 deletions(-)

diff --git a/scripts/rpcgen/rpcgen/ast.py b/scripts/rpcgen/rpcgen/ast.py
index 3e3e089713..52d9c3ac46 100644
--- a/scripts/rpcgen/rpcgen/ast.py
+++ b/scripts/rpcgen/rpcgen/ast.py
@@ -185,9 +185,9 @@ class XDRTypeCustom(XDRType):
         self.definition = definition
 
     def is_scalar(self):
-        if type(self.definition) == XDRDefinitionEnum:
+        if type(self.definition) is XDRDefinitionEnum:
             return True
-        if type(self.definition) == XDRDefinitionTypedef:
+        if type(self.definition) is XDRDefinitionTypedef:
             return self.definition.decl.typ.is_scalar()
         return False
 
diff --git a/scripts/rpcgen/rpcgen/generator.py b/scripts/rpcgen/rpcgen/generator.py
index 73731e1497..c6e210e7b0 100644
--- a/scripts/rpcgen/rpcgen/generator.py
+++ b/scripts/rpcgen/rpcgen/generator.py
@@ -81,7 +81,7 @@ class XDRTypeDeclarationGenerator(XDRVisitor):
         )
 
     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)
         else:
             code = (
@@ -178,10 +178,10 @@ class XDRTypeDeclarationGenerator(XDRVisitor):
             + "%s    union {\n" % indent
         )
         for value in obj.cases:
-            if type(value.decl.typ) == XDRTypeVoid:
+            if type(value.decl.typ) is XDRTypeVoid:
                 continue
             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 + "%s    } %su;\n" % (indent, prefix) + "%s}" % indent
         return code
@@ -250,10 +250,10 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
         return code
 
     def generate_type_call(self, decl, field, typename, embedded=False, indent=""):
-        if type(decl.typ) == XDRTypeVoid:
+        if type(decl.typ) is XDRTypeVoid:
             return ""
-        if type(decl) == XDRDeclarationFixedArray:
-            if type(decl.typ) == XDRTypeOpaque:
+        if type(decl) is XDRDeclarationFixedArray:
+            if type(decl.typ) is XDRTypeOpaque:
                 code = "%s    if (!xdr_%s(xdrs, %s, %s))\n" % (
                     indent,
                     self.visit_object(decl.typ, context="func"),
@@ -270,7 +270,7 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
                     self.visit_object(decl.typ),
                     self.visit_object(decl.typ, context="func"),
                 )
-        elif type(decl) == XDRDeclarationVariableArray:
+        elif type(decl) is XDRDeclarationVariableArray:
             fieldRef = "."
             pointerStr = ""
             if embedded:
@@ -278,7 +278,7 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
             else:
                 fieldRef = "->"
 
-            if type(decl.typ) == XDRTypeString:
+            if type(decl.typ) is XDRTypeString:
                 code = "%s    if (!xdr_%s(xdrs, %s%s, %s))\n" % (
                     indent,
                     self.visit_object(decl.typ, context="func"),
@@ -286,7 +286,7 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
                     field,
                     decl.maxlength,
                 )
-            elif type(decl.typ) == XDRTypeOpaque:
+            elif type(decl.typ) is XDRTypeOpaque:
                 code = "%s    if (!xdr_bytes(xdrs, (char **)&%s%s%s_val, " % (
                     indent,
                     field,
@@ -311,7 +311,7 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
                         self.visit_object(decl.typ, context="func"),
                     )
                 )
-        elif type(decl) == XDRDeclarationPointer:
+        elif type(decl) is XDRDeclarationPointer:
             pointerStr = ""
             if embedded:
                 pointerStr = "&"
@@ -327,9 +327,9 @@ class XDRMarshallImplementationGenerator(XDRVisitor):
         else:
             pointerStr = ""
             isFixedArray = (
-                type(decl.typ) == XDRTypeCustom
-                and type(decl.typ.definition) == XDRDefinitionTypedef
-                and type(decl.typ.definition.decl) == XDRDeclarationFixedArray
+                type(decl.typ) is XDRTypeCustom
+                and type(decl.typ.definition) is XDRDefinitionTypedef
+                and type(decl.typ.definition.decl) is XDRDeclarationFixedArray
             )
 
             if embedded and not isFixedArray:
diff --git a/scripts/rpcgen/rpcgen/lexer.py b/scripts/rpcgen/rpcgen/lexer.py
index 989c2ae216..a27d7005b8 100644
--- a/scripts/rpcgen/rpcgen/lexer.py
+++ b/scripts/rpcgen/rpcgen/lexer.py
@@ -55,7 +55,7 @@ class XDRToken(abc.ABC):
 
     def __eq__(self, other):
         return (
-            type(self) == type(other)
+            type(self) is type(other)
             and self.line == other.line
             and self.column == other.column
             and self.value == other.value
diff --git a/scripts/rpcgen/rpcgen/parser.py b/scripts/rpcgen/rpcgen/parser.py
index 7efbe5468e..c01ae56755 100644
--- a/scripts/rpcgen/rpcgen/parser.py
+++ b/scripts/rpcgen/rpcgen/parser.py
@@ -162,10 +162,10 @@ class XDRParser:
         if token is None:
             return None
 
-        if type(token) == XDRTokenCEscape:
+        if type(token) is XDRTokenCEscape:
             return XDRDefinitionCEscape(token.value[1:])
 
-        if type(token) != XDRTokenIdentifier:
+        if type(token) is not XDRTokenIdentifier:
             raise Exception("Expected identifier, but got %s" % token)
 
         defs = {
@@ -186,18 +186,18 @@ class XDRParser:
         definition = func()
 
         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)
 
         return definition
 
     def parse_definition_const(self):
         ident = self.lexer.next()
-        if type(ident) != XDRTokenIdentifier:
+        if type(ident) is not XDRTokenIdentifier:
             raise Exception("Expected identifier, but got %s" % ident)
 
         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)
 
         const = self.lexer.next()
@@ -217,7 +217,7 @@ class XDRParser:
 
     def parse_definition_enum(self):
         name = self.lexer.next()
-        if type(name) != XDRTokenIdentifier:
+        if type(name) is not XDRTokenIdentifier:
             raise Exception("Expected identifier, but got %s" % name)
 
         body = self.parse_enum_body()
@@ -231,7 +231,7 @@ class XDRParser:
 
     def parse_definition_struct(self):
         name = self.lexer.next()
-        if type(name) != XDRTokenIdentifier:
+        if type(name) is not XDRTokenIdentifier:
             raise Exception("Expected identifier, but got %s" % name)
 
         body = self.parse_struct_body()
@@ -245,7 +245,7 @@ class XDRParser:
 
     def parse_definition_union(self):
         name = self.lexer.next()
-        if type(name) != XDRTokenIdentifier:
+        if type(name) is not XDRTokenIdentifier:
             raise Exception("Expected identifier, but got %s" % name)
 
         body = self.parse_union_body()
@@ -260,23 +260,23 @@ class XDRParser:
     def parse_declaration(self):
         typ = self.parse_type()
 
-        if type(typ) == XDRTypeVoid:
+        if type(typ) is XDRTypeVoid:
             return XDRDeclarationScalar(typ, None)
 
         ident = self.lexer.next()
 
         pointer = False
-        if type(ident) == XDRTokenPunctuation:
+        if type(ident) is XDRTokenPunctuation:
             if ident.value != "*":
                 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")
 
             pointer = True
             ident = self.lexer.next()
 
         bracket = self.lexer.peek()
-        if type(bracket) == XDRTokenPunctuation:
+        if type(bracket) is XDRTokenPunctuation:
             if bracket.value == "[":
                 _ = self.lexer.next()
                 value = self.lexer.next()
@@ -284,10 +284,10 @@ class XDRParser:
                     raise Exception("Expected constant, but got %s" % value)
 
                 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)
 
-                if type(typ) == XDRTypeString:
+                if type(typ) is XDRTypeString:
                     raise Exception("Fixed array invalid for 'string' type")
                 return XDRDeclarationFixedArray(typ, ident.value, value.value)
             elif bracket.value == "<":
@@ -298,7 +298,7 @@ class XDRParser:
                     value = self.lexer.next().value
 
                 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)
 
                 return XDRDeclarationVariableArray(typ, ident.value, value)
@@ -310,12 +310,12 @@ class XDRParser:
 
     def parse_type(self):
         typ = self.lexer.next()
-        if type(typ) != XDRTokenIdentifier:
+        if type(typ) is not XDRTokenIdentifier:
             raise Exception("Expected identifier, but got %s" % typ)
 
         if typ.value == "unsigned":
             typ = self.lexer.peek()
-            if type(typ) != XDRTokenIdentifier:
+            if type(typ) is not XDRTokenIdentifier:
                 raise Exception("Expected identifier, but got %s" % typ)
 
             if typ.value == "char":
@@ -366,25 +366,25 @@ class XDRParser:
 
     def parse_enum_body(self):
         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)
 
         values = []
         while True:
             ident = self.lexer.next()
-            if type(ident) != XDRTokenIdentifier:
+            if type(ident) is not XDRTokenIdentifier:
                 raise Exception("Expected identifier, but got %s" % ident)
 
             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)
 
             value = self.lexer.next()
-            if type(value) != XDRTokenConstant:
+            if type(value) is not XDRTokenConstant:
                 raise Exception("Expected constant, but got %s" % ident)
 
             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):
         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)
 
         fields = []
@@ -412,11 +412,11 @@ class XDRParser:
             fields.append(field)
 
             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)
 
             end = self.lexer.peek()
-            if type(end) == XDRTokenPunctuation and end.value == "}":
+            if type(end) is XDRTokenPunctuation and end.value == "}":
                 break
 
         # discard the '}' we peeked at to end the loop
@@ -429,28 +429,28 @@ class XDRParser:
 
     def parse_union_body(self):
         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)
 
         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)
 
         discriminator = self.parse_declaration()
 
         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)
 
         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)
 
         default = None
         cases = []
         while True:
             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",
                 "case",
             ]:
@@ -463,7 +463,7 @@ class XDRParser:
                     raise Exception("Expected constant, but got %s" % value)
 
                 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)
 
                 decl = self.parse_declaration()
@@ -475,17 +475,17 @@ class XDRParser:
                     raise Exception("Duplicate 'default' clause")
 
                 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)
 
                 default = self.parse_declaration()
 
             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)
 
             end = self.lexer.peek()
-            if type(end) == XDRTokenPunctuation and end.value == "}":
+            if type(end) is XDRTokenPunctuation and end.value == "}":
                 break
 
         # discard the '}' we peeked at to end the loop
-- 
2.41.0
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org
Re: [libvirt PATCH] rpcgen: use proper operators when comparing types
Posted by Daniel P. Berrangé 5 months, 3 weeks ago
On Fri, Nov 03, 2023 at 08:04:13PM -0400, Laine Stump wrote:
> 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")

Doh, we have  '--no-suite syntax-check' because had a dedicated
codestyle job, so I didn't detect this problem with newest flake8.

> 
> 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>
> ---
>  scripts/rpcgen/rpcgen/ast.py       |  4 +-
>  scripts/rpcgen/rpcgen/generator.py | 26 ++++++------
>  scripts/rpcgen/rpcgen/lexer.py     |  2 +-
>  scripts/rpcgen/rpcgen/parser.py    | 68 +++++++++++++++---------------
>  4 files changed, 50 insertions(+), 50 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org
Re: [libvirt PATCH] rpcgen: use proper operators when comparing types
Posted by Michal Prívozník 5 months, 3 weeks ago
On 11/4/23 01:04, Laine Stump wrote:
> 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>
> ---
>  scripts/rpcgen/rpcgen/ast.py       |  4 +-
>  scripts/rpcgen/rpcgen/generator.py | 26 ++++++------
>  scripts/rpcgen/rpcgen/lexer.py     |  2 +-
>  scripts/rpcgen/rpcgen/parser.py    | 68 +++++++++++++++---------------
>  4 files changed, 50 insertions(+), 50 deletions(-)
> 
> diff --git a/scripts/rpcgen/rpcgen/ast.py b/scripts/rpcgen/rpcgen/ast.py
> index 3e3e089713..52d9c3ac46 100644
> --- a/scripts/rpcgen/rpcgen/ast.py
> +++ b/scripts/rpcgen/rpcgen/ast.py
> @@ -185,9 +185,9 @@ class XDRTypeCustom(XDRType):
>          self.definition = definition
>  
>      def is_scalar(self):
> -        if type(self.definition) == XDRDefinitionEnum:
> +        if type(self.definition) is XDRDefinitionEnum:

D'oh! And I just started rewriting this into isinstace() and only after
that I noticed this patch. I guess we don't need to worry about subclases.

Michal
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org