| 181 | loop.default_exception_handler(context) |
| 182 | |
| 183 | def _format_loop_exception(self, context, n): |
| 184 | message = context.get('message', 'Unhandled exception in event loop') |
| 185 | exception = context.get('exception') |
| 186 | if exception is not None: |
| 187 | exc_info = (type(exception), exception, exception.__traceback__) |
| 188 | else: |
| 189 | exc_info = None |
| 190 | |
| 191 | lines = [] |
| 192 | for key in sorted(context): |
| 193 | if key in {'message', 'exception'}: |
| 194 | continue |
| 195 | value = context[key] |
| 196 | if key == 'source_traceback': |
| 197 | tb = ''.join(traceback.format_list(value)) |
| 198 | value = 'Object created at (most recent call last):\n' |
| 199 | value += tb.rstrip() |
| 200 | else: |
| 201 | try: |
| 202 | value = repr(value) |
| 203 | except Exception as ex: |
| 204 | value = ('Exception in __repr__ {!r}; ' |
| 205 | 'value type: {!r}'.format(ex, type(value))) |
| 206 | lines.append('[{}]: {}\n\n'.format(key, value)) |
| 207 | |
| 208 | if exc_info is not None: |
| 209 | lines.append('[exception]:\n') |
| 210 | formatted_exc = textwrap.indent( |
| 211 | ''.join(traceback.format_exception(*exc_info)), ' ') |
| 212 | lines.append(formatted_exc) |
| 213 | |
| 214 | details = textwrap.indent(''.join(lines), ' ') |
| 215 | return '{:02d}. {}:\n{}\n'.format(n, message, details) |
| 216 | |
| 217 | |
| 218 | _default_cluster = None |