hasattr throws wrong exception when using python3.4

Bug #1551394 reported by Yang Hongyang
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Magnum
Fix Released
Medium
Yang Hongyang

Bug Description

Magnum python34 test fails with:
2016-02-29 16:15:58.511 | magnum.tests.unit.api.test_validation.TestValidation.test_enforce_volume_driver_types_remove_update
2016-02-29 16:15:58.511 | ---------------------------------------------------------------------------------------------------
2016-02-29 16:15:58.511 |
2016-02-29 16:15:58.511 | Captured traceback:
2016-02-29 16:15:58.511 | ~~~~~~~~~~~~~~~~~~~
2016-02-29 16:15:58.511 | b'Traceback (most recent call last):'
2016-02-29 16:15:58.511 | b' File "/home/jenkins/workspace/gate-magnum-python34/magnum/tests/unit/api/test_validation.py", line 413, in test_enforce_volume_driver_types_remove_update'
2016-02-29 16:15:58.511 | b" op='remove')"
2016-02-29 16:15:58.511 | b' File "/home/jenkins/workspace/gate-magnum-python34/.tox/py34/lib/python3.4/site-packages/mock/mock.py", line 1305, in patched'
2016-02-29 16:15:58.511 | b' return func(*args, **keywargs)'
2016-02-29 16:15:58.512 | b' File "/home/jenkins/workspace/gate-magnum-python34/magnum/tests/unit/api/test_validation.py", line 384, in _test_enforce_volume_driver_types_update'
2016-02-29 16:15:58.512 | b' test(self, baymodel_ident, patch)'
2016-02-29 16:15:58.512 | b' File "<decorator-gen-381>", line 2, in test'
2016-02-29 16:15:58.512 | b' File "/home/jenkins/workspace/gate-magnum-python34/magnum/api/validation.py", line 143, in wrapper'
2016-02-29 16:15:58.512 | b' baymodel_dict = api_utils.apply_jsonpatch(baymodel.as_dict(),'
2016-02-29 16:15:58.512 | b' File "/home/jenkins/workspace/gate-magnum-python34/magnum/objects/base.py", line 44, in as_dict'
2016-02-29 16:15:58.512 | b' for k in self.fields'
2016-02-29 16:15:58.512 | b' File "/home/jenkins/workspace/gate-magnum-python34/magnum/objects/base.py", line 45, in <dictcomp>'
2016-02-29 16:15:58.512 | b' if hasattr(self, k)}'
2016-02-29 16:15:58.512 | b' File "/home/jenkins/workspace/gate-magnum-python34/.tox/py34/lib/python3.4/site-packages/oslo_versionedobjects/base.py", line 67, in getter'
2016-02-29 16:15:58.512 | b' self.obj_load_attr(name)'
2016-02-29 16:15:58.513 | b' File "/home/jenkins/workspace/gate-magnum-python34/.tox/py34/lib/python3.4/site-packages/oslo_versionedobjects/base.py", line 580, in obj_load_attr'
2016-02-29 16:15:58.513 | b' _("Cannot load \'%s\' in the base class") % attrname)'
2016-02-29 16:15:58.513 | b"NotImplementedError: Cannot load 'id' in the base class"
2016-02-29 16:15:58.513 | b''

This is because we use hasattr() to generate attr dict for an object, the oslo_versionedobject returns exception NotImplementedError, which is ok in python 2.7 because it only check getattr() returns an exception or not:
https://docs.python.org/2/library/functions.html#hasattr
hasattr(object, name)
The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)

But it will fail on python3 because in python3 it checks getattr() raises an AttributeError or not:
https://docs.python.org/3/library/functions.html#hasattr
hasattr(object, name)
The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)

affects: magnum → oslo.versionedobjects
Changed in oslo.versionedobjects:
assignee: nobody → Yang Hongyang (hongyang-yang)
Revision history for this message
Davanum Srinivas (DIMS) (dims-v) wrote :
Changed in oslo.versionedobjects:
status: New → In Progress
importance: Undecided → Medium
Revision history for this message
Yang Hongyang (hongyang-yang) wrote :

@dims-v, right.

affects: oslo.versionedobjects → magnum
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to magnum (master)

Fix proposed to branch: master
Review: https://review.openstack.org/286654

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to magnum (master)

Reviewed: https://review.openstack.org/286654
Committed: https://git.openstack.org/cgit/openstack/magnum/commit/?id=d56bc4f21348181fcd8103699192bd31ac4bd805
Submitter: Jenkins
Branch: master

commit d56bc4f21348181fcd8103699192bd31ac4bd805
Author: Yang Hongyang <email address hidden>
Date: Tue Mar 1 23:09:17 2016 +0800

    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

Changed in magnum:
status: In Progress → Fix Released
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/magnum 2.0.0

This issue was fixed in the openstack/magnum 2.0.0 release.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.