No module name for builtin type and exception names
Make utils.reflection.get_class_name() return just type name (without module) for builtin exceptions and types. This makes output less clumsy and makes this function return same strings for python 2 and 3. Change-Id: Ibc599ed34c804cf2cf2ab67e7466a48ebe7f6df3
This commit is contained in:
@@ -202,7 +202,7 @@ class EngineTaskTest(EngineTestBase):
|
||||
'flow FAILURE',
|
||||
'flow REVERTING',
|
||||
'fail REVERTING',
|
||||
'fail reverted(Failure: exceptions.RuntimeError: Woot!)',
|
||||
'fail reverted(Failure: RuntimeError: Woot!)',
|
||||
'fail REVERTED',
|
||||
'fail PENDING',
|
||||
'flow REVERTED'])
|
||||
@@ -382,7 +382,7 @@ class EngineLinearFlowTest(EngineTestBase):
|
||||
engine.run()
|
||||
self.assertEquals(
|
||||
self.values,
|
||||
['fail reverted(Failure: exceptions.RuntimeError: Woot!)'])
|
||||
['fail reverted(Failure: RuntimeError: Woot!)'])
|
||||
|
||||
def test_correctly_reverts_children(self):
|
||||
flow = lf.Flow('root-1').add(
|
||||
@@ -398,7 +398,7 @@ class EngineLinearFlowTest(EngineTestBase):
|
||||
self.assertEquals(
|
||||
self.values,
|
||||
['task1', 'task2',
|
||||
'fail reverted(Failure: exceptions.RuntimeError: Woot!)',
|
||||
'fail reverted(Failure: RuntimeError: Woot!)',
|
||||
'task2 reverted(5)', 'task1 reverted(5)'])
|
||||
|
||||
|
||||
@@ -540,7 +540,7 @@ class EngineGraphFlowTest(EngineTestBase):
|
||||
self.assertEquals(
|
||||
self.values,
|
||||
['task1', 'task2',
|
||||
'task3 reverted(Failure: exceptions.RuntimeError: Woot!)',
|
||||
'task3 reverted(Failure: RuntimeError: Woot!)',
|
||||
'task2 reverted(5)', 'task1 reverted(5)'])
|
||||
|
||||
def test_graph_flow_four_tasks_revert_failure(self):
|
||||
@@ -634,7 +634,7 @@ class SuspendFlowTest(EngineTestBase):
|
||||
self.assertEquals(
|
||||
self.values,
|
||||
['a', 'b',
|
||||
'c reverted(Failure: exceptions.RuntimeError: Woot!)',
|
||||
'c reverted(Failure: RuntimeError: Woot!)',
|
||||
'b reverted(5)'])
|
||||
with self.assertRaisesRegexp(RuntimeError, '^Woot'):
|
||||
engine.run()
|
||||
@@ -643,7 +643,7 @@ class SuspendFlowTest(EngineTestBase):
|
||||
self.values,
|
||||
['a',
|
||||
'b',
|
||||
'c reverted(Failure: exceptions.RuntimeError: Woot!)',
|
||||
'c reverted(Failure: RuntimeError: Woot!)',
|
||||
'b reverted(5)',
|
||||
'a reverted(5)'])
|
||||
|
||||
@@ -852,7 +852,7 @@ class MultiThreadedEngineTest(EngineTaskTest,
|
||||
'task1', 'task1 reverted(5)',
|
||||
'task2', 'task2 reverted(5)',
|
||||
'task3', 'task3 reverted(5)',
|
||||
'fail reverted(Failure: exceptions.RuntimeError: Woot!)'
|
||||
'fail reverted(Failure: RuntimeError: Woot!)'
|
||||
])
|
||||
self.assertIsSubset(possible_result, result)
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
import sys
|
||||
|
||||
from taskflow import test
|
||||
from taskflow.tests import utils as test_utils
|
||||
from taskflow.utils import lock_utils
|
||||
from taskflow.utils import misc
|
||||
from taskflow.utils import reflection
|
||||
@@ -149,9 +150,13 @@ class AcceptsKwargsTest(test.TestCase):
|
||||
|
||||
class GetClassNameTest(test.TestCase):
|
||||
|
||||
def test_std_class(self):
|
||||
def test_std_exception(self):
|
||||
name = reflection.get_class_name(RuntimeError)
|
||||
self.assertEquals(name, 'exceptions.RuntimeError')
|
||||
self.assertEquals(name, 'RuntimeError')
|
||||
|
||||
def test_global_class(self):
|
||||
name = reflection.get_class_name(misc.Failure)
|
||||
self.assertEquals(name, 'taskflow.utils.misc.Failure')
|
||||
|
||||
def test_class(self):
|
||||
name = reflection.get_class_name(Class)
|
||||
@@ -163,27 +168,19 @@ class GetClassNameTest(test.TestCase):
|
||||
|
||||
def test_int(self):
|
||||
name = reflection.get_class_name(42)
|
||||
self.assertEquals(name, '__builtin__.int')
|
||||
self.assertEquals(name, 'int')
|
||||
|
||||
|
||||
class GetAllClassNamesTest(test.TestCase):
|
||||
|
||||
def test_std_class(self):
|
||||
names = list(reflection.get_all_class_names(RuntimeError))
|
||||
self.assertEquals(names, [
|
||||
'exceptions.RuntimeError',
|
||||
'exceptions.StandardError',
|
||||
'exceptions.Exception',
|
||||
'exceptions.BaseException',
|
||||
'__builtin__.object'])
|
||||
self.assertEquals(names, test_utils.RUNTIME_ERROR_CLASSES)
|
||||
|
||||
def test_std_class_up_to(self):
|
||||
names = list(reflection.get_all_class_names(RuntimeError,
|
||||
up_to=Exception))
|
||||
self.assertEquals(names, [
|
||||
'exceptions.RuntimeError',
|
||||
'exceptions.StandardError',
|
||||
'exceptions.Exception'])
|
||||
self.assertEquals(names, test_utils.RUNTIME_ERROR_CLASSES[:-2])
|
||||
|
||||
|
||||
class ExcInfoUtilsTest(test.TestCase):
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
from taskflow import exceptions
|
||||
from taskflow import test
|
||||
from taskflow.tests import utils as test_utils
|
||||
|
||||
from taskflow.utils import misc
|
||||
|
||||
@@ -37,20 +38,18 @@ class GeneralFailureObjTestsMixin(object):
|
||||
|
||||
def test_str(self):
|
||||
self.assertEquals(str(self.fail_obj),
|
||||
'Failure: exceptions.RuntimeError: Woot!')
|
||||
'Failure: RuntimeError: Woot!')
|
||||
|
||||
def test_exception_types(self):
|
||||
self.assertEquals(list(self.fail_obj),
|
||||
['exceptions.RuntimeError',
|
||||
'exceptions.StandardError',
|
||||
'exceptions.Exception'])
|
||||
test_utils.RUNTIME_ERROR_CLASSES[:-2])
|
||||
|
||||
def test_check_str(self):
|
||||
val = 'exceptions.StandardError'
|
||||
val = 'Exception'
|
||||
self.assertEquals(self.fail_obj.check(val), val)
|
||||
|
||||
def test_check_str_not_there(self):
|
||||
val = 'exceptions.ValueError'
|
||||
val = 'ValueError'
|
||||
self.assertEquals(self.fail_obj.check(val), None)
|
||||
|
||||
def test_check_type(self):
|
||||
@@ -115,7 +114,7 @@ class FailureObjectTestCase(test.TestCase):
|
||||
misc.Failure(
|
||||
exception_str='Woot!',
|
||||
traceback_str=None,
|
||||
exc_type_names=['exceptions.Exception'],
|
||||
exc_type_names=['Exception'],
|
||||
hi='hi there')
|
||||
expected = "Failure.__init__ got unexpected keyword argument: 'hi'"
|
||||
self.assertEquals(str(ctx.exception), expected)
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import six
|
||||
|
||||
from taskflow import task
|
||||
|
||||
ARGS_KEY = '__args__'
|
||||
@@ -75,17 +77,19 @@ class ProvidesRequiresTask(task.Task):
|
||||
ARGS_KEY: args,
|
||||
})
|
||||
if self.return_tuple:
|
||||
outs = []
|
||||
for i in xrange(0, len(self.provides)):
|
||||
outs.append(i)
|
||||
return tuple(outs)
|
||||
return tuple(range(len(self.provides)))
|
||||
else:
|
||||
outs = {}
|
||||
for k in self.provides:
|
||||
outs[k] = k
|
||||
return outs
|
||||
return dict((k, k) for k in self.provides)
|
||||
|
||||
|
||||
class DummyTask(task.Task):
|
||||
def execute(self, context, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
if six.PY3:
|
||||
RUNTIME_ERROR_CLASSES = ['RuntimeError', 'Exception',
|
||||
'BaseException', 'object']
|
||||
else:
|
||||
RUNTIME_ERROR_CLASSES = ['RuntimeError', 'StandardError', 'Exception',
|
||||
'BaseException', 'object']
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
# under the License.
|
||||
|
||||
import inspect
|
||||
import six
|
||||
import types
|
||||
|
||||
|
||||
@@ -25,9 +26,12 @@ def get_class_name(obj):
|
||||
|
||||
If object is a type, fully qualified name of the type is returned.
|
||||
Else, fully qualified name of the type of the object is returned.
|
||||
For builtin types, just name is returned.
|
||||
"""
|
||||
if not isinstance(obj, type):
|
||||
if not isinstance(obj, six.class_types):
|
||||
obj = type(obj)
|
||||
if obj.__module__ in ('builtins', '__builtin__', 'exceptions'):
|
||||
return obj.__name__
|
||||
return '.'.join((obj.__module__, obj.__name__))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user