Use obj_attr_is_set to check whether an attr is set in oslo_versionedobject

hasattr() have different behaviour between python 2 and 3 [1], while we can
not fix oslo_versionedobject [2], we need to use obj_attr_is_set to
check whether an attr is set in oslo.versionedobject as their core
suggested [2].

[1]. https://docs.python.org/2/library/functions.html#hasattr
     https://docs.python.org/3/library/functions.html#hasattr
[2]. https://review.openstack.org/#/c/285647/

More detailed info, please refer to the launchpad bug page.

Change-Id: I9adbbb55e8560a499e8334e954cc904fb8ee3565
Closes-Bug: #1551394
Partially-Implements: blueprint magnum-python3
This commit is contained in:
Yang Hongyang 2016-03-01 23:09:17 +08:00
parent 5a7e48adfa
commit d56bc4f213

View File

@ -42,7 +42,7 @@ class MagnumObject(ovoo_base.VersionedObject):
def as_dict(self):
return {k: getattr(self, k)
for k in self.fields
if hasattr(self, k)}
if self.obj_attr_is_set(k)}
class MagnumObjectDictCompat(ovoo_base.VersionedObjectDictCompat):