Skip to contentSkip to conversation

event_match: always match on None value

PATCH Applied John Snow 2 participants 2 messages May 28, 2019

lore
Share & tools
lore

John Snow

author
#0 lore raw

PATCH v2 1/1 event_match: always match on None value

Before, event_match didn't always recurse if the event value was not a dictionary, and would instead check for equality immediately.

By delaying equality checking to post-recursion, we can allow leaf values like "5" to match "None" and take advantage of the generic None-returns-True clause.

This makes the matching a little more obviously consistent at the expense of being able to check for explicit None values, which is probably not that important given what this function is used for.

---
v2: actually let the for loop finish ... Thanks Max

---
 python/qemu/__init__.py | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
python/qemu/__init__.py +14 -10
index 98ed8a2e28..dbaf8a5311 100644
@@ -409,27 +409,31 @@ class QEMUMachine(object):
 
         The match criteria takes the form of a matching subdict. The event is
         checked to be a superset of the subdict, recursively, with matching
-        values whenever those values are not None.
+        values whenever the subdict values are not None.
+
+        This has a limitation that you cannot explicitly check for None values.
 
         Examples, with the subdict queries on the left:
          - None matches any object.
          - {"foo": None} matches {"foo": {"bar": 1}}
-         - {"foo": {"baz": None}} does not match {"foo": {"bar": 1}}
-         - {"foo": {"baz": 2}} matches {"foo": {"bar": 1, "baz": 2}}
+         - {"foo": None} matches {"foo": 5}
+         - {"foo": {"abc": None}} does not match {"foo": {"bar": 1}}
+         - {"foo": {"rab": 2}} matches {"foo": {"bar": 1, "rab": 2}}
         """
         if match is None:
             return True
 
-        for key in match:
-            if key in event:
-                if isinstance(event[key], dict):
+        try:
+            for key in match:
+                if key in event:
                     if not QEMUMachine.event_match(event[key], match[key]):
                         return False
-                elif event[key] != match[key]:
+                else:
                     return False
-            else:
-                return False
-        return True
+            return True
+        except TypeError:
+            # either match or event wasn't iterable (not a dict)
+            return match == event
 
     def event_wait(self, name, timeout=60.0, match=None):
         """
Signature · 1 line
2.20.1
Applied
#1 lore raw
Quoted 11 lines · John Snow

Before, event_match didn't always recurse if the event value was not a dictionary, and would instead check for equality immediately.

By delaying equality checking to post-recursion, we can allow leaf values like "5" to match "None" and take advantage of the generic None-returns-True clause.

This makes the matching a little more obviously consistent at the expense of being able to check for explicit None values, which is probably not that important given what this function is used for.

Quoted 6 lines

---
v2: actually let the for loop finish ... Thanks Max

---
 python/qemu/__init__.py | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

Thanks, applied to my block-on-kevin branch:

https://git.xanclic.moe/XanClic/qemu/commits/branch/block-on-kevin

...which is also the place where your backup/aio_context series currently resides. I couldn’t put it in the pull request I just sent because it depends on patches from Kevin’s branch.

But know that your patches aren’t forgotten!

Max