我正在修改openstack-nova的代码。
添加RESTful API后,出现此错误:
eption during message handling: AttributeError: 'SchedulerReportClient' object has no attribute 'obj_to_primitive'
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server Traceback (most recent call last):
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server File "/usr/lib/python2.7/site-packages/oslo_messaging/rpc/server.py", line 163, in _process_incoming
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server res = self.dispatcher.dispatch(message)
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server File "/usr/lib/python2.7/site-packages/oslo_messaging/rpc/dispatcher.py", line 268, in dispatch
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server return self._do_dispatch(endpoint, method, ctxt, args)
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server File "/usr/lib/python2.7/site-packages/oslo_messaging/rpc/dispatcher.py", line 195, in _do_dispatch
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server return self.serializer.serialize_entity(ctxt, result)
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server File "/openstack/lib/python2.7/site-packages/nova/rpc.py", line 130, in serialize_entity
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server return self._base.serialize_entity(context, entity)
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server File "/openstack/lib/python2.7/site-packages/nova/objects/base.py", line 237, in serialize_entity
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server entity)
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server File "/openstack/lib/python2.7/site-packages/nova/objects/base.py", line 223, in _process_iterable
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server for k, v in values.items()})
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server File "/openstack/lib/python2.7/site-packages/nova/objects/base.py", line 223, in <dictcomp>
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server for k, v in values.items()})
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server File "/openstack/lib/python2.7/site-packages/nova/objects/base.py", line 245, in serialize_entity
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server entity = entity.obj_to_primitive()
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server File "/openstack/lib/python2.7/site-packages/nova/scheduler/client/__init__.py", line 37, in __run_method
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server return getattr(self.instance, __name)(*args, **kwargs)
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server AttributeError: 'SchedulerReportClient' object has no attribute 'obj_to_primitive'
2018-08-30 23:08:10.904 1637 ERROR oslo_messaging.rpc.server
相关代码为:
def serialize_entity(self, context, entity):
if isinstance(entity, (tuple, list, set, dict)):
entity = self._process_iterable(context, self.serialize_entity, entity)
elif (hasattr(entity, 'obj_to_primitive') and callable(entity.obj_to_primitive)):
entity = entity.obj_to_primitive()
return entity
我的问题是,hasattr(entity, 'obj_to_primitive')
是对的,那么entity.obj_to_primitive()
为什么引发异常?
答案 0 :(得分:0)
查看堆栈跟踪的最后一行。您正在调用“方法”,但这只是self.instance
上相应方法的包装,而 getattr
却失败了。外部属性查找成功创建了包装器,这使hasattr
感到满意,并且包装器的确是callable
。
如果在创建包装器时计算了hasattr
,则getattr(self.instance, __name)
测试将起作用。如果包装程序的唯一目的是被称为 (而不是拥有自己的方法),它也可以执行自己的callable
检查。但是Python将自己交给了EAFP,所以就在这里。