pickle: Prevent access to the _Dead and CallError constructors

This should be pretty much identical the same behaviour as before, but
the extra assertion makes me feel happier.
wip-fakessh-exit-status
David Wilson 7 years ago
parent 92ae866271
commit 11acc031a9

@ -60,16 +60,22 @@ class CallError(Error):
exception message. exception message.
""" """
def __init__(self, e): def __init__(self, e):
s = '' s = '%s.%s: %s' % (type(e).__module__, type(e).__name__, e)
if not isinstance(e, basestring):
s += '%s.%s: ' % (type(e).__module__, type(e).__name__)
s += str(e)
tb = sys.exc_info()[2] tb = sys.exc_info()[2]
if tb: if tb:
s += '\n' s += '\n'
s += ''.join(traceback.format_tb(tb)) s += ''.join(traceback.format_tb(tb))
Error.__init__(self, s) Error.__init__(self, s)
def __reduce__(self):
return (_unpickle_call_error, (self[0],))
def _unpickle_call_error(s):
assert type(s) is str and len(s) < 10000
inst = CallError.__new__(CallError)
Exception.__init__(inst, s)
return inst
class ChannelError(Error): class ChannelError(Error):
"""Raised when a channel dies or has been closed.""" """Raised when a channel dies or has been closed."""
@ -87,9 +93,15 @@ class Dead(object):
def __eq__(self, other): def __eq__(self, other):
return type(other) is Dead return type(other) is Dead
def __reduce__(self):
return (_unpickle_dead, ())
def __repr__(self): def __repr__(self):
return '<Dead>' return '<Dead>'
def _unpickle_dead():
return _DEAD
#: Sentinel value used to represent :py:class:`Channel` disconnection. #: Sentinel value used to represent :py:class:`Channel` disconnection.
_DEAD = Dead() _DEAD = Dead()

@ -39,8 +39,8 @@ COMMENT_RE = re.compile(r'^[ ]*#[^\n]*$', re.M)
IOLOG_RE = re.compile(r'^[ ]*IOLOG.debug\(.+?\)$', re.M) IOLOG_RE = re.compile(r'^[ ]*IOLOG.debug\(.+?\)$', re.M)
PERMITTED_CLASSES = set([ PERMITTED_CLASSES = set([
('mitogen.core', 'CallError'), ('mitogen.core', '_unpickle_call_error'),
('mitogen.core', 'Dead'), ('mitogen.core', '_unpickle_dead'),
]) ])

Loading…
Cancel
Save