def _exception(self, exc_type, exc_val, exc_tb, message, fargs, data): exc_type = exc_type or DefaultException # have to capture the time now in case the on_exception sinks # take their sweet time etime = time.time() exc_info = ExceptionInfo.from_exc_info(exc_type, exc_val, exc_tb) if not message: cp = exc_info.tb_info.frames[-1] t = "%s raised exception: " exc_repr = "%s(%r)" errno = getattr(exc_val, 'errno', None) if errno and str(errno) not in exc_repr: t += exc_repr + ' (errno %s)' % exc_val.errno else: t += exc_repr t += " from %s on line %s of file '%s'" if self.data_map: t += ' - ({data_map_repr})' message = t % (self.name, exc_info.exc_type, exc_info.exc_msg, cp.func_name, cp.lineno, cp.module_path) exc_event = ExceptionEvent(self, etime, message, fargs, exc_info) self.exc_events.append(exc_event) self.logger.on_exception(exc_event, exc_type, exc_val, exc_tb) return self._end('exception', message, fargs, data, etime, exc_info)
def invoke(self, *args): """ Executes Click's CLI, print output and return results. """ result = self.runner.invoke(cli, args) self.print_cli_output(['mpm'] + list(args), result.output) # Print some more debug info. print(result) if result.exception: print( ExceptionInfo.from_exc_info(*result.exc_info).get_formatted()) return result
def invoke(self, *args): """ Executes CLI, print output and return results. """ result = self.runner.invoke(cli, args) # Simulate CLI output. print('-' * 70) print("$ mdedupe {}".format(' '.join(args))) print(result.output) print('-' * 70) # Print some more debug info. print(result) if result.exception: print(ExceptionInfo.from_exc_info( *result.exc_info).get_formatted()) return result
def _run(*args, color=False): # We allow for nested iterables and None values as args for # convenience. We just need to flatten and filters them out. args = list(filter(None.__ne__, flatten(args))) if args: assert same(map(type, args), str) result = runner.invoke(mdedup, args, color=color) print_cli_output([CLI_NAME] + args, result.output) # Print some more debug info. print(result) if result.exception: print( ExceptionInfo.from_exc_info(*result.exc_info).get_formatted()) return result
def test_exception_info(): # test ExceptionInfo and TracebackInfo and hooks, via StringIOs builtin_exc_hook = sys.excepthook fix_print_exception() tbi_str = '' def test(): raise ValueError('yay fun') fake_stderr1 = StringIO() fake_stderr2 = StringIO() sys.stderr = fake_stderr1 try: test() except: _, _, exc_traceback = sys.exc_info() tbi = TracebackInfo.from_traceback(exc_traceback) exc_info = ExceptionInfo.from_exc_info(*sys.exc_info()) exc_info2 = ExceptionInfo.from_current() tbi_str = str(tbi) print_exception(*sys.exc_info(), file=fake_stderr2) new_exc_hook_res = fake_stderr2.getvalue() builtin_exc_hook(*sys.exc_info()) builtin_exc_hook_res = fake_stderr1.getvalue() finally: sys.stderr = sys.__stderr__ # Single frame single_frame_str = tbi.frames[-1].tb_frame_str() assert 'in test' in single_frame_str assert 'yay fun' in single_frame_str # Traceback info assert len(tbi_str.splitlines()) == 5 assert 'yay fun' in tbi_str # Full except hook output assert 'ValueError: yay fun' in new_exc_hook_res assert "ValueError('yay fun')" in new_exc_hook_res assert len(new_exc_hook_res) > len(tbi_str) assert new_exc_hook_res == builtin_exc_hook_res
def _run(*args, **kwargs): color = kwargs.get('color', False) # We allow for nested iterables and None values as args for # convenience. We just need to flatten and filters them out. args = list(filter(None.__ne__, flatten(args))) if args: assert set(map(type, args)) == {str} result = runner.invoke(cli, args, color=color) # Strip colors out of results. result.stdout_bytes = strip_ansi(result.stdout_bytes) result.stderr_bytes = strip_ansi(result.stderr_bytes) print_cli_output(['mpm'] + args, result.output) # Print some more debug info. print(result) if result.exception: print(ExceptionInfo.from_exc_info( *result.exc_info).get_formatted()) return result