Skip to content

Commit

Permalink
Fully qualify class names in repr(), add selected state information
Browse files Browse the repository at this point in the history
This is intended to disambiguate objects in log and debug output.
  • Loading branch information
moreati committed Jul 4, 2023
1 parent ec212a1 commit 4189606
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 46 deletions.
5 changes: 4 additions & 1 deletion ansible_mitogen/planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ def get_module_source(self):
return self._module_source

def __repr__(self):
return 'Invocation(module_name=%s)' % (self.module_name,)
return '%s.%s(module_name=%r)' % (
__name__, self.__class__.__name__,
self.module_name,
)


class Planner(object):
Expand Down
2 changes: 1 addition & 1 deletion ansible_mitogen/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def __init__(self, path):
LOG.debug('%r installed; existing keys: %r', self, self._keys)

def __repr__(self):
return 'EnvironmentFileWatcher(%r)' % (self.path,)
return '%s.%s(%r)' % (__name__, self.__class__.__name__, self.path)

def _stat(self):
try:
Expand Down
59 changes: 39 additions & 20 deletions mitogen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def __init__(self, dct):
self[k] = v

def __repr__(self):
return 'Kwargs(%s)' % (dict.__repr__(self),)
return '%s(%s)' % (self.__class__.__name__, dict.__repr__(self),)

def __reduce__(self):
return (Kwargs, (dict(self),))
Expand Down Expand Up @@ -979,7 +979,8 @@ def unpickle(self, throw=True, throw_dead=True):
return obj

def __repr__(self):
return 'Message(%r, %r, %r, %r, %r, %r..%d)' % (
return '%s.%s(dst_id=%r, src_id=%r, auth_id=%r, handle=%r, reply_to=%r, data=%r..%d)' % (
__name__, self.__class__.__name__,
self.dst_id, self.src_id, self.auth_id, self.handle,
self.reply_to, (self.data or '')[:50], len(self.data)
)
Expand Down Expand Up @@ -1025,7 +1026,10 @@ def close(self):
)

def __repr__(self):
return 'Sender(%r, %r)' % (self.context, self.dst_handle)
return '%s.%s(%r, %r)' % (
__name__, self.__class__.__name__,
self.context, self.dst_handle,
)

def __reduce__(self):
return _unpickle_sender, (self.context.context_id, self.dst_handle)
Expand Down Expand Up @@ -1087,7 +1091,10 @@ def __init__(self, router, handle=None, persist=True,
)

def __repr__(self):
return 'Receiver(%r, %r)' % (self.router, self.handle)
return '%s.%s(%r, %r)' % (
__name__, self.__class__.__name__,
self.router, self.handle,
)

def __enter__(self):
return self
Expand Down Expand Up @@ -1237,7 +1244,8 @@ def close(self):
Sender.close(self)

def __repr__(self):
return 'Channel(%s, %s)' % (
return '%s.%s(%s, %s)' % (
__name__, self.__class__.__name__,
Sender.__repr__(self),
Receiver.__repr__(self)
)
Expand Down Expand Up @@ -1351,7 +1359,7 @@ def _install_handler(self, router):
)

def __repr__(self):
return 'Importer'
return '%s.%s()' % (__name__, self.__class__.__name__)

def builtin_find_module(self, fullname):
# imp.find_module() will always succeed for __main__, because it is a
Expand Down Expand Up @@ -1719,7 +1727,10 @@ def accept(self, rfp, wfp):
self.transmit_side = Side(self, wfp)

def __repr__(self):
return "<Stream %s #%04x>" % (self.name, id(self) & 0xffff,)
return "<%s.%s %r #%04x>" % (
__name__, self.__class__.__name__,
self.name, id(self) & 0xffff,
)

def on_receive(self, broker):
"""
Expand Down Expand Up @@ -1812,8 +1823,8 @@ def build_stream(cls, *args, **kwargs):
return stream

def __repr__(self):
return '%s(%s)' % (
self.__class__.__name__,
return '%s.%s(%r)' % (
__name__, self.__class__.__name__,
self.stream and self.stream.name,
)

Expand Down Expand Up @@ -2008,8 +2019,9 @@ def __init__(self, stream, fp, cloexec=True, keep_alive=True, blocking=False):
set_nonblock(self.fd)

def __repr__(self):
return '<Side of %s fd %s>' % (
self.stream.name or repr(self.stream),
return '<%s.%s of %r fd %r>' % (
__name__, self.__class__.__name__,
self.stream.name or self.stream,
self.fd
)

Expand Down Expand Up @@ -2334,7 +2346,10 @@ def send_await(self, msg, deadline=None):
return data

def __repr__(self):
return 'Context(%s, %r)' % (self.context_id, self.name)
return '%s.%s(%r, %r)' % (
__name__, self.__class__.__name__,
self.context_id, self.name,
)


def _unpickle_context(context_id, name, router=None):
Expand Down Expand Up @@ -2397,7 +2412,10 @@ def __init__(self):
self._wfds = {}

def __repr__(self):
return '%s' % (type(self).__name__,)
return '<%s.%s with %d readers %d writers>' % (
__name__, self.__class__.__name__,
len(self._rfds), len(self._wfds),
)

def _update(self, fd):
"""
Expand Down Expand Up @@ -2769,7 +2787,8 @@ def _wake(self, wsock, cookie):
assert written == len(cookie) and not disconnected

def __repr__(self):
return 'Latch(%#x, size=%d, t=%r)' % (
return '%s.%s(%#x, size=%d, thread=%r)' % (
__name__, self.__class__.__name__,
id(self),
len(self._queue),
threading__thread_name(threading__current_thread()),
Expand Down Expand Up @@ -2798,9 +2817,9 @@ def __init__(self, broker):
self._deferred = collections.deque()

def __repr__(self):
return 'Waker(fd=%r/%r)' % (
self.stream.receive_side and self.stream.receive_side.fd,
self.stream.transmit_side and self.stream.transmit_side.fd,
return '%s.%s(%r, %r)' % (
__name__, self.__class__.__name__,
self.stream.receive_side, self.stream.transmit_side,
)

@property
Expand Down Expand Up @@ -2990,7 +3009,7 @@ def __init__(self, broker):
self.add_handler(self._on_del_route, DEL_ROUTE)

def __repr__(self):
return 'Router(%r)' % (self.broker,)
return '%s.%s(%r)' % (__name__, self.__class__.__name__, self.broker)

def _setup_logging(self):
"""
Expand Down Expand Up @@ -3626,7 +3645,7 @@ def join(self):
self._thread.join()

def __repr__(self):
return 'Broker(%04x)' % (id(self) & 0xffff,)
return '%s.%s(%r)' % (__name__, self.__class__.__name__, self.poller)


class Dispatcher(object):
Expand All @@ -3643,7 +3662,7 @@ class Dispatcher(object):
_service_recv = None

def __repr__(self):
return 'Dispatcher'
return '%s.%s(%r)' % (__name__, self.__class__.__name__, self.econtext)

def __init__(self, econtext):
self.econtext = econtext
Expand Down
11 changes: 7 additions & 4 deletions mitogen/fakessh.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ def on_receive(self, stream, broker):
self.on_disconnect(broker)

def __repr__(self):
return 'IoPump(%r, %r)' % (
self.receive_side.fp.fileno(),
self.transmit_side.fp.fileno(),
return '%s.%s(%r, %r)' % (
__name__, self.__class__.__name__,
self.receive_side, self.transmit_side,
)


Expand Down Expand Up @@ -191,7 +191,10 @@ def __init__(self, router, stdin, stdout, proc=None):
pmon.add(proc.pid, self._on_proc_exit)

def __repr__(self):
return 'Process(%r, %r)' % (self.stdin, self.stdout)
return '%s.%s(%r, %r, %r)' % (
__name__, self.__class__.__name__,
self.router, self.stdin, self.stdout,
)

def _on_proc_exit(self, status):
LOG.debug('%r._on_proc_exit(%r)', self, status)
Expand Down
10 changes: 5 additions & 5 deletions mitogen/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def _on_forward_log(self, msg):
logger.handle(record)

def __repr__(self):
return 'LogForwarder(%r)' % (self._router,)
return '%s.%s(%r)' % (__name__, self.__class__.__name__, self._router)


class FinderMethod(object):
Expand All @@ -461,7 +461,7 @@ class FinderMethod(object):
simple task, right? Naive young fellow, welcome to the real world.
"""
def __repr__(self):
return '%s()' % (type(self).__name__,)
return '%s.%s()' % (__name__, self.__class__.__name__)

def find(self, fullname):
"""
Expand Down Expand Up @@ -813,7 +813,7 @@ def __init__(self):
self._related_cache = {}

def __repr__(self):
return 'ModuleFinder()'
return '%s.%s()' % (__name__, self.__class__.__name__)

def add_source_override(self, fullname, path, source, is_pkg):
"""
Expand Down Expand Up @@ -995,7 +995,7 @@ def __init__(self, router):
)

def __repr__(self):
return 'ModuleResponder'
return '%s.%s(%r)' % (__name__, self.__class__.__name__, self._router)

def add_source_override(self, fullname, path, source, is_pkg):
"""
Expand Down Expand Up @@ -1412,7 +1412,7 @@ def __init__(self, router):
)

def __repr__(self):
return 'IdAllocator(%r)' % (self.router,)
return '%s.%s(%r)' % (__name__, self.__class__.__name__, self.router)

def allocate(self):
"""
Expand Down
25 changes: 18 additions & 7 deletions mitogen/parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,10 @@ def __init__(self, when, func):
self.func = func

def __repr__(self):
return 'Timer(%r, %r)' % (self.when, self.func)
return '%s.%s(%r, %r)' % (
__name__, self.__class__.__name__,
self.when, self.func,
)

def __eq__(self, other):
return self.when == other.when
Expand Down Expand Up @@ -1399,7 +1402,9 @@ def __init__(self, options, router):
self._router = router

def __repr__(self):
return 'Connection(%r)' % (self.stdio_stream,)
return '%s.%s(stdio_stream=%r)' % (
__name__, self.__class__.__name__, self.stdio_stream,
)

# Minimised, gzipped, base64'd and passed to 'python -c'. It forks, dups
# file descriptor 0 as 100, creates a pipe, then execs a new interpreter
Expand Down Expand Up @@ -1832,7 +1837,7 @@ def make_chain_id(cls):
)

def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self.context)
return '%s.%s(%r)' % (__name__, self.__class__.__name__, self.context)

def __enter__(self):
return self
Expand Down Expand Up @@ -2112,7 +2117,10 @@ def __init__(self, router, parent=None):
)

def __repr__(self):
return 'RouteMonitor()'
return '%s.%s(%r, %r)' % (
__name__, self.__class__.__name__,
self.router, self.parent,
)

def _send_one(self, stream, handle, target_id, name):
"""
Expand Down Expand Up @@ -2653,8 +2661,8 @@ def __init__(self, pid, stdin, stdout, stderr=None):
self.stderr = stderr

def __repr__(self):
return '%s %s pid %d' % (
type(self).__name__,
return '%s.%s(%r, %r)' % (
__name__, self.__class__.__name__,
self.name,
self.pid,
)
Expand Down Expand Up @@ -2711,7 +2719,10 @@ def __init__(self, router, parent_context, importer):
)

def __repr__(self):
return 'ModuleForwarder'
return '%s.%s(%r, %r, %r)' % (
__name__, self.__class__.__name__,
self.router, self.parent_context, self.importer,
)

def _on_forward_module(self, msg):
if msg.is_dead:
Expand Down
9 changes: 6 additions & 3 deletions mitogen/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def __init__(self, service):
self.service = service

def __repr__(self):
return '%s(%s)' % (type(self).__name__, self.service)
return '%s.%s(%r)' % (__name__, self.__class__.__name__, self.service)

unauthorized_msg = (
'Caller is not authorized to invoke %r of service %r'
Expand Down Expand Up @@ -448,7 +448,9 @@ def __init__(self, router):
self.select = mitogen.select.Select()

def __repr__(self):
return '%s()' % (self.__class__.__name__,)
return '%s.%s(%r, %r)' % (
__name__, self.__class__.__name__, self.router, self.select,
)

def on_message(self, event):
"""
Expand Down Expand Up @@ -661,7 +663,8 @@ def _worker_main(self):
raise

def __repr__(self):
return 'Pool(%04x, size=%d, th=%r)' % (
return '%s.%s(%04x, size=%d, th=%r)' % (
__name__, self.__class__.__name__,
id(self) & 0xffff,
len(self._threads),
get_thread_name(),
Expand Down
5 changes: 5 additions & 0 deletions tests/importer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def tearDown(self):
super(ImporterMixin, self).tearDown()


class ReprTest(ImporterMixin, testlib.TestCase):
def test_repr(self):
self.assertEqual('mitogen.core.Importer()', repr(self.importer))


class LoadModuleTest(ImporterMixin, testlib.TestCase):
data = zlib.compress(b("data = 1\n\n"))
path = 'fake_module.py'
Expand Down
18 changes: 15 additions & 3 deletions tests/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,18 @@ def test_py36_sender(self):
class ReprTest(testlib.TestCase):
klass = mitogen.core.Message

def test_repr(self):
# doesn't crash
repr(self.klass.pickled('test'))
def test_repr_data(self):
msg = self.klass(dst_id=1, src_id=2, auth_id=3, handle=4, reply_to=5, data=b'abcdef')
if mitogen.core.PY3:
expected = "mitogen.core.Message(dst_id=1, src_id=2, auth_id=3, handle=4, reply_to=5, data=b'abcdef'..6)"
else:
expected = "mitogen.core.Message(dst_id=1, src_id=2, auth_id=3, handle=4, reply_to=5, data='abcdef'..6)"
self.assertEqual(expected, repr(msg))

def test_repr_pickled(self):
msg = self.klass.pickled(u'test')
if mitogen.core.PY3:
expected = r"mitogen.core.Message(dst_id=None, src_id=0, auth_id=0, handle=None, reply_to=None, data=b'\x80\x02X\x04\x00\x00\x00testq\x00.'..14)"
else:
expected = r"mitogen.core.Message(dst_id=None, src_id=0, auth_id=0, handle=None, reply_to=None, data='\x80\x02X\x04\x00\x00\x00testq\x01.'..14)"
self.assertEqual(expected, repr(msg))
Loading

0 comments on commit 4189606

Please sign in to comment.