issue #72: add CallError.type_name, mitogen.core.qualname().

issue72
David Wilson 6 years ago
parent e45e5d3e06
commit e8ab8d9352

@ -334,12 +334,13 @@ Helper Functions
================ ================
.. currentmodule:: mitogen.core .. currentmodule:: mitogen.core
.. autofunction:: to_text
.. autofunction:: has_parent_authority .. autofunction:: has_parent_authority
.. autofunction:: io_op
.. autofunction:: qualname
.. autofunction:: set_block
.. autofunction:: set_cloexec .. autofunction:: set_cloexec
.. autofunction:: set_nonblock .. autofunction:: set_nonblock
.. autofunction:: set_block .. autofunction:: to_text
.. autofunction:: io_op
.. currentmodule:: mitogen.parent .. currentmodule:: mitogen.parent
.. autofunction:: close_nonstandard_fds .. autofunction:: close_nonstandard_fds

@ -236,6 +236,13 @@ class Kwargs(dict):
return (Kwargs, (dict(self),)) return (Kwargs, (dict(self),))
def qualname(type_):
"""
Given a type, return its fully qualified name as a string.
"""
return '%s.%s' % (type_.__module__, type_.__name__)
class CallError(Error): class CallError(Error):
"""Serializable :class:`Error` subclass raised when """Serializable :class:`Error` subclass raised when
:py:meth:`Context.call() <mitogen.parent.Context.call>` fails. A copy of :py:meth:`Context.call() <mitogen.parent.Context.call>` fails. A copy of
@ -246,7 +253,7 @@ class CallError(Error):
Error.__init__(self, fmt, *args) Error.__init__(self, fmt, *args)
else: else:
e = fmt e = fmt
fmt = '%s.%s: %s' % (type(e).__module__, type(e).__name__, e) fmt = '%s: %s' % (qualname(type(e)), e)
args = () args = ()
tb = sys.exc_info()[2] tb = sys.exc_info()[2]
if tb: if tb:
@ -254,6 +261,16 @@ class CallError(Error):
fmt += ''.join(traceback.format_tb(tb)) fmt += ''.join(traceback.format_tb(tb))
Error.__init__(self, fmt) Error.__init__(self, fmt)
@property
def type_name(self):
"""
Return the fully qualified name of the original exception type, or
:data:`None` if the :class:`CallError` was raised explcitly.
"""
type_name, sep, _ = self.args[0].partition(':')
if sep:
return type_name
def __reduce__(self): def __reduce__(self):
return (_unpickle_call_error, (self.args[0],)) return (_unpickle_call_error, (self.args[0],))
@ -1205,8 +1222,7 @@ class Stream(BasicStream):
self.transmit_side = Side(self, os.dup(wfd)) self.transmit_side = Side(self, os.dup(wfd))
def __repr__(self): def __repr__(self):
cls = type(self) return '%s(%r)' % (qualname(type(self)), self.name)
return '%s.%s(%r)' % (cls.__module__, cls.__name__, self.name)
class Context(object): class Context(object):

@ -24,14 +24,16 @@ class ConstructorTest(unittest2.TestCase):
def test_from_exc(self): def test_from_exc(self):
ve = plain_old_module.MyError('eek') ve = plain_old_module.MyError('eek')
e = self.klass(ve) e = self.klass(ve)
self.assertEquals(e.type_name, 'plain_old_module.MyError')
self.assertEquals(e.args[0], 'plain_old_module.MyError: eek') self.assertEquals(e.args[0], 'plain_old_module.MyError: eek')
def test_form_base_exc(self): def test_form_base_exc(self):
ve = SystemExit('eek') ve = SystemExit('eek')
type_name = mitogen.core.qualname(SystemExit) # varies across 2/3.
e = self.klass(ve) e = self.klass(ve)
self.assertEquals(e.args[0], self.assertEquals(e.type_name, type_name)
# varies across 2/3. self.assertEquals(e.args[0], '%s: eek' % (type_name,))
'%s.%s: eek' % (type(ve).__module__, type(ve).__name__))
def test_from_exc_tb(self): def test_from_exc_tb(self):
try: try:
@ -40,6 +42,7 @@ class ConstructorTest(unittest2.TestCase):
ve = sys.exc_info()[1] ve = sys.exc_info()[1]
e = self.klass(ve) e = self.klass(ve)
self.assertTrue(e.type_name, 'plain_old_module.MyError')
self.assertTrue(e.args[0].startswith('plain_old_module.MyError: eek')) self.assertTrue(e.args[0].startswith('plain_old_module.MyError: eek'))
self.assertTrue('test_from_exc_tb' in e.args[0]) self.assertTrue('test_from_exc_tb' in e.args[0])
@ -50,17 +53,20 @@ class PickleTest(unittest2.TestCase):
def test_string_noargs(self): def test_string_noargs(self):
e = self.klass('%s%s') e = self.klass('%s%s')
e2 = pickle.loads(pickle.dumps(e)) e2 = pickle.loads(pickle.dumps(e))
self.assertEquals(e2.type_name, None)
self.assertEquals(e2.args[0], '%s%s') self.assertEquals(e2.args[0], '%s%s')
def test_string_args(self): def test_string_args(self):
e = self.klass('%s%s', 1, 1) e = self.klass('%s%s', 1, 1)
e2 = pickle.loads(pickle.dumps(e)) e2 = pickle.loads(pickle.dumps(e))
self.assertEquals(e2.type_name, None)
self.assertEquals(e2.args[0], '11') self.assertEquals(e2.args[0], '11')
def test_from_exc(self): def test_from_exc(self):
ve = plain_old_module.MyError('eek') ve = plain_old_module.MyError('eek')
e = self.klass(ve) e = self.klass(ve)
e2 = pickle.loads(pickle.dumps(e)) e2 = pickle.loads(pickle.dumps(e))
self.assertEquals(e2.type_name, 'plain_old_module.MyError')
self.assertEquals(e2.args[0], 'plain_old_module.MyError: eek') self.assertEquals(e2.args[0], 'plain_old_module.MyError: eek')
def test_from_exc_tb(self): def test_from_exc_tb(self):
@ -71,6 +77,7 @@ class PickleTest(unittest2.TestCase):
e = self.klass(ve) e = self.klass(ve)
e2 = pickle.loads(pickle.dumps(e)) e2 = pickle.loads(pickle.dumps(e))
self.assertEquals(e2.type_name, 'plain_old_module.MyError')
self.assertTrue(e2.args[0].startswith('plain_old_module.MyError: eek')) self.assertTrue(e2.args[0].startswith('plain_old_module.MyError: eek'))
self.assertTrue('test_from_exc_tb' in e2.args[0]) self.assertTrue('test_from_exc_tb' in e2.args[0])

Loading…
Cancel
Save