scripts/gdb: replace "is 0" by "== 0"
PATCH 1/1 scripts/gdb: replace "is 0" by "== 0"
While for small numbers, using "node is 0" works with CPython, it is more portable to use "node == 0". Moreover, with Python 3, this triggers a syntax warning:
SyntaxWarning: "is" with a literal. Did you mean "=="?
--- scripts/gdb/linux/rbtree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/gdb/linux/rbtree.py +2 -2
@@ -17,7 +17,7 @@ def rb_first(root): raise gdb.GdbError("Must be struct rb_root not {}".format(root.type)) node = root['rb_node'] - if node is 0: + if node == 0: return None while node['rb_left']:
@@ -33,7 +33,7 @@ def rb_last(root): raise gdb.GdbError("Must be struct rb_root not {}".format(root.type)) node = root['rb_node'] - if node is 0: + if node == 0: return None while node['rb_right']:
Signature · 1 line
2.26.0.rc2
Hi Vincent,
Quoted 3 lines · Vincent Bernat
While for small numbers, using "node is 0" works with CPython, it is more portable to use "node == 0". Moreover, with Python 3, this triggers a syntax warning:
Oh, I thought python encouraged the 'is' style.
Quoted 2 lines
SyntaxWarning: "is" with a literal. Did you mean "=="?Of course that means it clearly doesn't in this case...
Quoted 1 line
Signed-off-by Vincent Bernat <[email protected]>
Change looks acceptable to me given the report, though a potential alternative below, otherwise
Quoted 9 lines
--- scripts/gdb/linux/rbtree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)scripts/gdb/linux/rbtree.py +0
-0
scripts/gdb/linux/rbtree.py def rb_first(root): · line 20
Quoted 5 lines
scripts/gdb/linux/rbtree.py +1
-1@@ -17,7 +17,7 @@ def rb_first(root): raise gdb.GdbError("Must be struct rb_root not {}".format(root.type)) node = root['rb_node'] - if node is 0: + if node == 0:
Given the nature of the statement, would
if not node:
return None
be any more appropriate ?
Quoted 13 lines
scripts/gdb/linux/rbtree.py +1
-1return None while node['rb_left']:@@ -33,7 +33,7 @@ def rb_last(root): raise gdb.GdbError("Must be struct rb_root not {}".format(root.type)) node = root['rb_node'] - if node is 0: + if node == 0: return None while node['rb_right']:
Signature · 2 lines
-- Kieran
❦ 20 mars 2020 16:47 +00, Kieran Bingham:
Quoted 1 line
Oh, I thought python encouraged the 'is' style.
Usually, only for "None", "False" or "True" (but for the latest, this doesn't usually make sense).
Quoted 6 lines
Given the nature of the statement, would
if not node:
return Nonebe any more appropriate ?
It would be more consistent with the other uses (like the while below and the ifs even further). However, I don't fully understand this code (notably the "node = node.address.cast(...)" where node is not defined yet and node is replaced by the value of "root['rb_node']" just after), so can't say for sure node cannot be equal to something eveluating to False in some conditions.
Signature · 3 lines
Writing is easy; all you do is sit staring at the blank sheet of paper until drops of blood form on your forehead. -- Gene Fowler
Quoting Vincent Bernat (2020-03-20 09:38:20)
Quoted 8 lines
While for small numbers, using "node is 0" works with CPython, it is more portable to use "node == 0". Moreover, with Python 3, this triggers a syntax warning:
SyntaxWarning: "is" with a literal. Did you mean "=="?Signed-off-by Vincent Bernat <[email protected]>---