def override_stdouts(): """Override `sys.stdout` and `sys.stderr` with `StringIO`.""" prev_out, prev_err = sys.stdout, sys.stderr mystdout, mystderr = StringIO(), StringIO() sys.stdout = sys.__stdout__ = mystdout sys.stderr = sys.__stderr__ = mystderr yield mystdout, mystderr sys.stdout = sys.__stdout__ = prev_out sys.stderr = sys.__stderr__ = prev_err
def wrap_logger(logger, loglevel=logging.ERROR): old_handlers = get_handlers(logger) sio = StringIO() siohandler = logging.StreamHandler(sio) set_handlers(logger, [siohandler]) yield sio set_handlers(logger, old_handlers)
def _mocked(url, *args, **kwargs): response_data, headers = response_method(url) return addinfourl(StringIO(response_data), headers, url)
def cry(): # pragma: no cover """Return stacktrace of all active threads. From https://gist.github.com/737056 """ tmap = {} main_thread = None # get a map of threads by their ID so we can print their names # during the traceback dump for t in threading.enumerate(): if getattr(t, "ident", None): tmap[t.ident] = t else: main_thread = t out = StringIO() sep = "=" * 49 + "\n" for tid, frame in sys._current_frames().iteritems(): thread = tmap.get(tid, main_thread) out.write("%s\n" % (thread.getName(),)) out.write(sep) traceback.print_stack(frame, file=out) out.write(sep) out.write("LOCAL VARIABLES\n") out.write(sep) pprint(frame.f_locals, stream=out) out.write("\n\n") return out.getvalue()
def cry(): """Return stacktrace of all active threads. From https://gist.github.com/737056 """ tmap = {} main_thread = None # get a map of threads by their ID so we can print their names # during the traceback dump for t in threading.enumerate(): if getattr(t, "ident", None): tmap[t.ident] = t else: main_thread = t out = StringIO() for tid, frame in sys._current_frames().iteritems(): thread = tmap.get(tid, main_thread) out.write("%s\n" % (thread.getName(), )) out.write("=================================================\n") traceback.print_stack(frame, file=out) out.write("=================================================\n") out.write("LOCAL VARIABLES\n") out.write("=================================================\n") pprint(frame.f_locals, stream=out) out.write("\n\n") return out.getvalue()