[ovs-dev] python: KeyError shouldn't be raised from __getattr__

Message ID 051beba9afdec61ca2c0eacd6fd66f26644c9649.1520851941.git.tredaelli@redhat.com
State Accepted
Delegated to: Russell Bryant
Headers show
Series
  • [ovs-dev] python: KeyError shouldn't be raised from __getattr__
Related show

Commit Message

Timothy Redaelli March 12, 2018, 10:52 a.m.
On Python 3 hasattr only intercepts AttributeError exception.
On Python2, instead, hasattr intercepts all the exceptions.

This means __getattr__ shouldn't return KeyError when the attribute
doesn't exists, but it should raise AttributeError instead.

Fixes: 2d54d8011e14 ("Python-IDL: getattr after mutate fix")
Signed-off-by: Timothy Redaelli <tredaelli@redhat.com>
---
 python/ovs/db/idl.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Comments

Russell Bryant March 14, 2018, 3:35 p.m. | #1
I've applied this to master and branch-2.9.  Thanks!

On Mon, Mar 12, 2018 at 6:52 AM, Timothy Redaelli <tredaelli@redhat.com> wrote:
> On Python 3 hasattr only intercepts AttributeError exception.
> On Python2, instead, hasattr intercepts all the exceptions.
>
> This means __getattr__ shouldn't return KeyError when the attribute
> doesn't exists, but it should raise AttributeError instead.
>
> Fixes: 2d54d8011e14 ("Python-IDL: getattr after mutate fix")
> Signed-off-by: Timothy Redaelli <tredaelli@redhat.com>
> ---
>  python/ovs/db/idl.py | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/python/ovs/db/idl.py b/python/ovs/db/idl.py
> index 5a4d129c0..773a604ed 100644
> --- a/python/ovs/db/idl.py
> +++ b/python/ovs/db/idl.py
> @@ -774,7 +774,11 @@ class Row(object):
>          assert self._changes is not None
>          assert self._mutations is not None
>
> -        column = self._table.columns[column_name]
> +        try:
> +            column = self._table.columns[column_name]
> +        except KeyError:
> +            raise AttributeError("%s instance has no attribute '%s'" %
> +                                 (self.__class__.__name__, column_name))
>          datum = self._changes.get(column_name)
>          inserts = None
>          if '_inserts' in self._mutations.keys():
> --
> 2.14.3
>
> _______________________________________________
> dev mailing list
> dev@openvswitch.org
> https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Patch

diff --git a/python/ovs/db/idl.py b/python/ovs/db/idl.py
index 5a4d129c0..773a604ed 100644
--- a/python/ovs/db/idl.py
+++ b/python/ovs/db/idl.py
@@ -774,7 +774,11 @@  class Row(object):
         assert self._changes is not None
         assert self._mutations is not None
 
-        column = self._table.columns[column_name]
+        try:
+            column = self._table.columns[column_name]
+        except KeyError:
+            raise AttributeError("%s instance has no attribute '%s'" %
+                                 (self.__class__.__name__, column_name))
         datum = self._changes.get(column_name)
         inserts = None
         if '_inserts' in self._mutations.keys():