[lxc-devel] [pylxd/master] Warn but don't raise exception on unknown attributes.
rockstar on Github
lxc-bot at linuxcontainers.org
Wed Sep 7 16:13:39 UTC 2016
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 435 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160907/9d2aa3b0/attachment.bin>
-------------- next part --------------
From 3b5c583401910f5cdfcca45c5a33346fc27eaed4 Mon Sep 17 00:00:00 2001
From: Paul Hummer <paul.hummer at canonical.com>
Date: Wed, 7 Sep 2016 10:09:46 -0600
Subject: [PATCH] Warn but don't raise exception on unknown attributes.
LXD adding new properties shouldn't cause current applications to fail,
but it is appropriate for pylxd to emit warnings about them.
---
pylxd/model.py | 11 ++++++++++-
pylxd/tests/test_model.py | 12 ++++++++----
2 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/pylxd/model.py b/pylxd/model.py
index ec14639..3ca5ebf 100644
--- a/pylxd/model.py
+++ b/pylxd/model.py
@@ -11,6 +11,8 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
+import warnings
+
import six
from pylxd.deprecation import deprecated
@@ -102,7 +104,14 @@ def __init__(self, client, **kwargs):
self.client = client
for key, val in kwargs.items():
- setattr(self, key, val)
+ try:
+ setattr(self, key, val)
+ except AttributeError:
+ warnings.warn(
+ 'Attempted to set unknown attribute "{}" '
+ 'on instance of "{}"'.format(
+ key, self.__class__.__name__
+ ))
del self.__dirty__[:]
def __getattribute__(self, name):
diff --git a/pylxd/tests/test_model.py b/pylxd/tests/test_model.py
index a76b1fa..2f6e7f1 100644
--- a/pylxd/tests/test_model.py
+++ b/pylxd/tests/test_model.py
@@ -69,10 +69,14 @@ def test_init(self):
self.assertEqual('an-item', item.name)
def test_init_unknown_attribute(self):
- """Unknown attributes raise an exception."""
- self.assertRaises(
- AttributeError,
- Item, self.client, name='an-item', nonexistent='SRSLY')
+ """Unknown attributes aren't set."""
+ item = Item(self.client, name='an-item', nonexistent='SRSLY')
+
+ try:
+ item.nonexistent
+ self.fail('item.nonexistent did not raise AttributeError')
+ except AttributeError:
+ pass
def test_unknown_attribute(self):
"""Setting unknown attributes raise an exception."""
More information about the lxc-devel
mailing list