Make code py3-compatible (global callable())

global function callable(f) is removed in python3.
It can be replaced with
isinstance(f, collections.Callable)
This patch addresses the change.

Ref : https://docs.python.org/3.1/whatsnew/3.0.html

Change-Id: I47a50fffac14668f90aac043ee22a91bdb7dca41
This commit is contained in:
whoami-rajat 2018-08-13 04:58:59 +00:00
parent 1579981969
commit 043ac5e574
5 changed files with 14 additions and 7 deletions

View File

@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import collections
import functools import functools
import inspect import inspect
import math import math
@ -1088,7 +1089,7 @@ class ControllerMetaclass(type):
versioned_methods.append(getattr(base, VER_METHOD_ATTR)) versioned_methods.append(getattr(base, VER_METHOD_ATTR))
for key, value in cls_dict.items(): for key, value in cls_dict.items():
if not callable(value): if not isinstance(value, collections.Callable):
continue continue
if getattr(value, 'wsgi_action', None): if getattr(value, 'wsgi_action', None):
actions[value.wsgi_action] = key actions[value.wsgi_action] = key

View File

@ -54,7 +54,7 @@
from __future__ import print_function from __future__ import print_function
import collections
import logging as python_logging import logging as python_logging
import prettytable import prettytable
import sys import sys
@ -765,7 +765,8 @@ def methods_of(obj):
""" """
result = [] result = []
for i in dir(obj): for i in dir(obj):
if callable(getattr(obj, i)) and not i.startswith('_'): if isinstance(getattr(obj, i),
collections.Callable) and not i.startswith('_'):
result.append((i, getattr(obj, i))) result.append((i, getattr(obj, i)))
return result return result

View File

@ -14,6 +14,7 @@
"""Cinder common internal object model""" """Cinder common internal object model"""
import collections
import contextlib import contextlib
import datetime import datetime
@ -160,7 +161,8 @@ class CinderObjectRegistry(base.VersionedObjectRegistry):
setattr(objects, cls.obj_name(), cls) setattr(objects, cls.obj_name(), cls)
# If registering class has a callable initialization method, call it. # If registering class has a callable initialization method, call it.
if callable(getattr(cls, 'cinder_ovo_cls_init', None)): if isinstance(getattr(cls, 'cinder_ovo_cls_init', None),
collections.Callable):
cls.cinder_ovo_cls_init() cls.cinder_ovo_cls_init()
@ -567,7 +569,7 @@ class CinderObjectSerializer(base.VersionedObjectSerializer):
entity = self._process_iterable(context, self.serialize_entity, entity = self._process_iterable(context, self.serialize_entity,
entity) entity)
elif (hasattr(entity, 'obj_to_primitive') and elif (hasattr(entity, 'obj_to_primitive') and
callable(entity.obj_to_primitive)): isinstance(entity.obj_to_primitive, collections.Callable)):
# NOTE(dulek): Backport outgoing object to the capped version. # NOTE(dulek): Backport outgoing object to the capped version.
backport_ver = self._get_capped_obj_version(entity) backport_ver = self._get_capped_obj_version(entity)
entity = entity.obj_to_primitive(backport_ver, self.manifest) entity = entity.obj_to_primitive(backport_ver, self.manifest)

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import collections
import inspect import inspect
import decorator import decorator
@ -224,7 +225,8 @@ class CinderCleanableObject(base.CinderPersistentObject):
# If we don't have optional decorator arguments the argument in # If we don't have optional decorator arguments the argument in
# decorator_args is the function we have to decorate # decorator_args is the function we have to decorate
if len(decorator_args) == 1 and callable(decorator_args[0]): if len(decorator_args) == 1 and isinstance(
decorator_args[0], collections.Callable):
function = decorator_args[0] function = decorator_args[0]
decorator_args = None decorator_args = None
return _decorator(function) return _decorator(function)

View File

@ -15,6 +15,7 @@
Mock unit tests for the NetApp block storage driver interfaces Mock unit tests for the NetApp block storage driver interfaces
""" """
import collections
from cinder import test from cinder import test
from cinder.volume.drivers.netapp.dataontap import block_cmode from cinder.volume.drivers.netapp.dataontap import block_cmode
@ -54,4 +55,4 @@ class NetAppBlockStorageDriverInterfaceTestCase(test.TestCase):
def _get_local_functions(self, obj): def _get_local_functions(self, obj):
"""Get function names of an object without superclass functions.""" """Get function names of an object without superclass functions."""
return set([key for key, value in type(obj).__dict__.items() return set([key for key, value in type(obj).__dict__.items()
if callable(value)]) if isinstance(value, collections.Callable)])