~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

Viewing changes to cloudinit/type_utils.py

  • Committer: Barry Warsaw
  • Date: 2015-01-21 22:56:53 UTC
  • mto: This revision was merged to the branch mainline in revision 1054.
  • Revision ID: barry@python.org-20150121225653-331tcuhwd1szidkh
Largely merge lp:~harlowja/cloud-init/py2-3 albeit manually because it seemed
to be behind trunk.

`tox -e py27` passes full test suite.  Now to work on replacing mocker.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
import types
24
24
 
 
25
import six
 
26
 
 
27
 
 
28
if six.PY3:
 
29
    _NAME_TYPES = (
 
30
        types.ModuleType,
 
31
        types.FunctionType,
 
32
        types.LambdaType,
 
33
        type,
 
34
    )
 
35
else:
 
36
    _NAME_TYPES = (
 
37
        types.TypeType,
 
38
        types.ModuleType,
 
39
        types.FunctionType,
 
40
        types.LambdaType,
 
41
        types.ClassType,
 
42
    )
 
43
 
25
44
 
26
45
def obj_name(obj):
27
 
    if isinstance(obj, (types.TypeType,
28
 
                        types.ModuleType,
29
 
                        types.FunctionType,
30
 
                        types.LambdaType)):
31
 
        return str(obj.__name__)
32
 
    return obj_name(obj.__class__)
 
46
    if isinstance(obj, _NAME_TYPES):
 
47
        return six.text_type(obj.__name__)
 
48
    else:
 
49
        if not hasattr(obj, '__class__'):
 
50
            return repr(obj)
 
51
        else:
 
52
            return obj_name(obj.__class__)