diff mbox series

[ovs-dev] python: Raise AttributeError from uuid_to_row

Message ID 20220430200952.863496-1-twilson@redhat.com
State Superseded
Headers show
Series [ovs-dev] python: Raise AttributeError from uuid_to_row | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

Terry Wilson April 30, 2022, 8:09 p.m. UTC
Prior to 4e3966e64, when calling _uuid_to_row, it would raise an
AttributeError when trying to access base.ref_table.rows if the
referenced table was not registered. When called from
Row.__getattr__(), this would appropriately raise an AttributeError.

After 4e3966e64, a KeyError would be raised, which is not expected
from a getattr() or hasattr() call, which could break existing
code.

Fixes: 4e3966e64 (python: Politely handle misuse of table.condition.)
Signed-off-by: Terry Wilson <twilson@redhat.com>
---
 python/ovs/db/idl.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Terry Wilson April 30, 2022, 8:32 p.m. UTC | #1
On Sat, Apr 30, 2022 at 3:10 PM Terry Wilson <twilson@redhat.com> wrote:
>
> Prior to 4e3966e64, when calling _uuid_to_row, it would raise an
> AttributeError when trying to access base.ref_table.rows if the
> referenced table was not registered. When called from
> Row.__getattr__(), this would appropriately raise an AttributeError.
>
> After 4e3966e64, a KeyError would be raised, which is not expected
> from a getattr() or hasattr() call, which could break existing
> code.
>
> Fixes: 4e3966e64 (python: Politely handle misuse of table.condition.)
> Signed-off-by: Terry Wilson <twilson@redhat.com>
> ---
>  python/ovs/db/idl.py | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/python/ovs/db/idl.py b/python/ovs/db/idl.py
> index c98985773..ca61c5f73 100644
> --- a/python/ovs/db/idl.py
> +++ b/python/ovs/db/idl.py
> @@ -1299,7 +1299,12 @@ class Row(object):
>
>      def _uuid_to_row(self, atom, base):
>          if base.ref_table:
> -            return self._idl.tables[base.ref_table.name].rows.get(atom)
> +            try:
> +                table = self._idl.tables[base.ref_table.name]
> +            except KeyError as e:

Part of me thinks that we could just return atom here to return the
UUID if the table wasn't registered, but I went with trying to restore
the previous behavior.

> +                raise AttributeError(
> +                    f"Table {base.ref_table.name} is not registered") from e
> +            return table.rows.get(atom)
>          else:
>              return atom
>
> --
> 2.35.1
>
diff mbox series

Patch

diff --git a/python/ovs/db/idl.py b/python/ovs/db/idl.py
index c98985773..ca61c5f73 100644
--- a/python/ovs/db/idl.py
+++ b/python/ovs/db/idl.py
@@ -1299,7 +1299,12 @@  class Row(object):
 
     def _uuid_to_row(self, atom, base):
         if base.ref_table:
-            return self._idl.tables[base.ref_table.name].rows.get(atom)
+            try:
+                table = self._idl.tables[base.ref_table.name]
+            except KeyError as e:
+                raise AttributeError(
+                    f"Table {base.ref_table.name} is not registered") from e
+            return table.rows.get(atom)
         else:
             return atom