diff options
| author | Sandy Walsh <sandy.walsh@rackspace.com> | 2011-08-22 03:09:47 +0000 |
|---|---|---|
| committer | Tarmac <> | 2011-08-22 03:09:47 +0000 |
| commit | 34f35990ed675a9360db3cbb317b9560755b22ef (patch) | |
| tree | ed3b5363369ad677d6459330a314af3fdf9df34f | |
| parent | 271817cdce37c55f29bb9782429ee8b6ad57364e (diff) | |
| parent | c11a156b1e50fde6cf3047057746564d491634e2 (diff) | |
Fixes utils.to_primitive (again) to handle modules, builtins and whatever other crap might be hiding in an object.
| -rw-r--r-- | nova/tests/test_utils.py | 10 | ||||
| -rw-r--r-- | nova/utils.py | 12 |
2 files changed, 19 insertions, 3 deletions
diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index ec5098a37..28e366a8e 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -384,3 +384,13 @@ class ToPrimitiveTestCase(test.TestCase): def test_typeerror(self): x = bytearray # Class, not instance self.assertEquals(utils.to_primitive(x), u"<type 'bytearray'>") + + def test_nasties(self): + def foo(): + pass + x = [datetime, foo, dir] + ret = utils.to_primitive(x) + self.assertEquals(len(ret), 3) + self.assertTrue(ret[0].startswith(u"<module 'datetime' from ")) + self.assertTrue(ret[1].startswith(u'<function foo at 0x')) + self.assertEquals(ret[2], u'<built-in function dir>') diff --git a/nova/utils.py b/nova/utils.py index 54126f644..b42f76457 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -547,11 +547,17 @@ def to_primitive(value, convert_instances=False, level=0): Therefore, convert_instances=True is lossy ... be aware. """ - if inspect.isclass(value): - return unicode(value) + nasty = [inspect.ismodule, inspect.isclass, inspect.ismethod, + inspect.isfunction, inspect.isgeneratorfunction, + inspect.isgenerator, inspect.istraceback, inspect.isframe, + inspect.iscode, inspect.isbuiltin, inspect.isroutine, + inspect.isabstract] + for test in nasty: + if test(value): + return unicode(value) if level > 3: - return [] + return '?' # The try block may not be necessary after the class check above, # but just in case ... |
