def _print_exception(self, context, type_, value, traceback): ultratb.VerboseTB(call_pdb=True)(type_, value, traceback) resp = input('Debugger exited. Do you want to quit raiden? [Y/n] ' ).strip().lower() if not resp or resp.startswith('y'): os.kill(os.getpid(), signal.SIGTERM)
def _print_exception(self, context, type_, value, traceback): ultratb.VerboseTB(call_pdb=True)(type_, value, traceback) resp = raw_input( "{c.OKGREEN}Debugger exited. " "{c.OKBLUE}Do you want to quit pyethapp?{c.ENDC} [{c.BOLD}Y{c.ENDC}/n] " .format(c=bcolors)).strip().lower() if not resp or resp.startswith("y"): os.kill(os.getpid(), signal.SIGTERM)
def __call__(self, etype, evalue, etb): """Handle an exception, call for compatible with sys.excepthook""" # do not allow the crash handler to be called twice without reinstalling it # this prevents unlikely errors in the crash handling from entering an # infinite loop. sys.excepthook = sys.__excepthook__ # Report tracebacks shouldn't use color in general (safer for users) color_scheme = 'NoColor' # Use this ONLY for developer debugging (keep commented out for release) #color_scheme = 'Linux' # dbg try: rptdir = self.app.ipython_dir except: rptdir = os.getcwd() if rptdir is None or not os.path.isdir(rptdir): rptdir = os.getcwd() report_name = os.path.join(rptdir, self.crash_report_fname) # write the report filename into the instance dict so it can get # properly expanded out in the user message template self.crash_report_fname = report_name self.info['crash_report_fname'] = report_name TBhandler = ultratb.VerboseTB( color_scheme=color_scheme, long_header=1, call_pdb=self.call_pdb, ) if self.call_pdb: TBhandler(etype, evalue, etb) return else: traceback = TBhandler.text(etype, evalue, etb, context=31) # print traceback to screen if self.show_crash_traceback: print(traceback, file=sys.stderr) # and generate a complete report on disk try: report = open(report_name, 'w') except: print('Could not create crash report on disk.', file=sys.stderr) return with report: # Inform user on stderr of what happened print('\n' + '*' * 70 + '\n', file=sys.stderr) print(self.message_template.format(**self.info), file=sys.stderr) # Construct report on disk report.write(self.make_report(traceback)) input("Hit <Enter> to quit (your terminal may close):")
def raise_exception(cls, exception, level: int = 1): # NOTE: ultratb is imported locally to avoid cyclic imports with, say, # ipdb or anything else that has an IPython dependency. from IPython.core import ultratb if level == 1: sys.excepthook = ultratb.ColorTB(tb_offset=-5) if level == 2: sys.excepthook = ultratb.ColorTB() elif level == 3: sys.excepthook = ultratb.VerboseTB() raise exception
def __call__(self, etype, evalue, etb): """Handle an exception, call for compatible with sys.excepthook""" # Report tracebacks shouldn't use color in general (safer for users) color_scheme = 'NoColor' # Use this ONLY for developer debugging (keep commented out for release) #color_scheme = 'Linux' # dbg try: rptdir = self.app.ipython_dir except: rptdir = os.getcwd() if rptdir is None or not os.path.isdir(rptdir): rptdir = os.getcwd() report_name = os.path.join(rptdir, self.crash_report_fname) # write the report filename into the instance dict so it can get # properly expanded out in the user message template self.crash_report_fname = report_name TBhandler = ultratb.VerboseTB( color_scheme=color_scheme, long_header=1, call_pdb=self.call_pdb, ) if self.call_pdb: TBhandler(etype, evalue, etb) return else: traceback = TBhandler.text(etype, evalue, etb, context=31) # print traceback to screen if self.show_crash_traceback: print >> sys.stderr, traceback # and generate a complete report on disk try: report = open(report_name, 'w') except: print >> sys.stderr, 'Could not create crash report on disk.' return # Inform user on stderr of what happened msg = itpl('\n' + '*' * 70 + '\n' + self.message_template) print >> sys.stderr, msg # Construct report on disk report.write(self.make_report(traceback)) report.close() raw_input( "Hit <Enter> to quit this message (your terminal may close):")
def print_error(self, err, func_vars, obj, src): traceback.tb = ultratb.VerboseTB(tb_offset=1) traceback.tb() enhancer = Enhancer(err, func_vars, obj, src) help_txt = enhancer.get_enhancement() if self.helpful and help_txt: display( Markdown( "<div class='alert alert-block alert-info'><img class='pull-left obligator' src='obligator-64.png' alt='�' /><h2 style='color:blue'>Maybe this can help? </h2></div>" )) print(self.format_return_text(help_txt)) return help_txt else: return None
def print_error(err, name): from IPython.core.display import display, Markdown traceback.tb = ultratb.VerboseTB(tb_offset=1) traceback.tb() help_txt = get_help(err) if helpful and help_txt: display( Markdown( "<div class='alert alert-block alert-info'><img class='pull-left obligator' src='obligator-64.png' alt='�' /><h2 style='color:blue'>Maybe this can help? </h2></div>" )) print(help_txt) b = make_feedback_button(err, name) if b != None: display(b) return help_txt else: return None
def do_debug_patching(): import sys from igit.util.misc import is_pycharm # TODO: learn how to call pdb on exception just using ipdb, not relying on ipython # (currently, whenever calling ipdb, it uses ipython → startup scripts → igit) # this will enable having env vars IGIT_CALL_PDB_ON_EXC=TRUE and IGIT_MODE="" and still # calling pdb on exception (right now they're dependent) IGIT_CALL_PDB_ON_EXC = eval(os.environ.get('IGIT_CALL_PDB_ON_EXC', '')) IGIT_EXCEPTHOOK = os.environ.get('IGIT_EXCEPTHOOK', '') IGIT_DEBUGGER = os.environ.get('IGIT_DEBUGGER', '') using_pycharm = is_pycharm() actually_call_pdb = IGIT_CALL_PDB_ON_EXC and not using_pycharm logging.info('IGIT_MODE == DEBUG (inside do_debug_patching())', 'IGIT_CALL_PDB_ON_EXC:', IGIT_CALL_PDB_ON_EXC, 'actually calling pdb:', actually_call_pdb, 'IGIT_EXCEPTHOOK:', IGIT_EXCEPTHOOK, 'IGIT_DEBUGGER:', IGIT_DEBUGGER, 'using_pycharm:', using_pycharm) if IGIT_EXCEPTHOOK == 'VerboseTB': from IPython.core import ultratb sys.excepthook = ultratb.VerboseTB(include_vars=True, call_pdb=actually_call_pdb) elif actually_call_pdb: raise EnvironmentError("\n\t".join(( f"env mismatch: 'IGIT_EXCEPTHOOK' is bad ({repr(IGIT_EXCEPTHOOK)}) and 'actually_call_pdb' is True", f"which means IGIT_CALL_PDB_ON_EXC is True and we're not using pycharm", 'whether to call pdb on exception is depndent on patching excepthook currently' ))) if actually_call_pdb: if IGIT_DEBUGGER == 'ipdb': from ipdb import set_trace from functools import partial sys.breakpointhook = partial(set_trace, context=30) elif IGIT_DEBUGGER: raise EnvironmentError( f"env: IGIT_DEBUGGER is something but not ipdb: {repr(IGIT_DEBUGGER)}" )
def main(): hooks = { 'default': lambda: sys.excepthook, 'cgitb': lambda: cgitb.Hook(format='text'), 'ultratb_color': lambda: ultratb.ColorTB(ostream=sys.stderr), 'ultratb_verbose': lambda: ultratb.VerboseTB(ostream=sys.stderr), } parser = argparse.ArgumentParser() parser.add_argument('hook', choices=sorted(hooks), default='default') args = parser.parse_args() sys.excepthook = hooks[args.hook]() print('__name__ =', __name__) print('__file__ =', __file__) time.time = lambda: 1531996624.0 # Freeze time sys.executable = '<traceback test>' one()
def activate_ultratb(): from IPython.core import ultratb sys.excepthook = ultratb.VerboseTB(call_pdb=True, tb_offset=6)
#!/usr/bin/env python # -*- coding: utf-8 -*- """Compute analytical solutions of the homogeneous half-space corresponding to electrode positions as recovered from a CRTomo FE-grid. Also, export potentials at each node for each current injection. TODO ---- * properly check/fix and describe output files """ import sys import IPython.core.ultratb as ultratb sys.excepthook = ultratb.VerboseTB(call_pdb=True, ) import os from optparse import OptionParser import numpy as np import crtomo.grid as CRGrid import crtomo.analytical_solution as am def handle_cmd_options(): parser = OptionParser() parser.add_option("-e", "--elem", dest="elem_file", type="string", help="elem.dat file (default: elem.dat)", default="elem.dat") parser.add_option("-t",
import itertools from fmtutil.row.parse_verbose_argvars import ParsedTuple from pathlib import Path from pdb import set_trace as st from typing import Dict, List, Any, Iterable from toolz.functoolz import compose_left from dataclasses import dataclass, field, InitVar from collections import OrderedDict, namedtuple from types import SimpleNamespace from functools import partial from textwrap import TextWrapper from beautifultable import BeautifulTable from itertools import accumulate from IPython.core import ultratb sys.excepthook = ultratb.VerboseTB() import better_exceptions # better_exceptions.hook() import traceback as tb # exc = sys.exc_info() # exc_tb = exc[2] # tb.print_exception(*exc) # tb.print_tb(exc_tb) # def colorizetxt(txt,bgc=None,fgc=None): # d = { # "blu":f'\x1b[0;34m', # "grn":f'\x1b[0;32m', # "cyn":f'\x1b[0;36m' # } # reset = f'\x1b[0m'
def execProcess(session, message_queue, output_handler, resource_limits, sysargs, fs_secret): """ Run the code, outputting into a pipe. Meant to be run as a separate process. :arg str session: the ID of the session running the code :arg multiprocessing.Queue message_queue: a queue through which this process will be passed input messages :arg device_process.OutputIPython output_handler: the context wrapper in which to execute the code :arg list resource_limits: list of tuples of the form ``(resource, limit)``, to be passed as arguments to :func:`resource.setrlimit`. """ # we need a new context since we just forked global fs fs.new_context() from Queue import Empty global user_code # Since the user can set a timeout, we safeguard by having a maximum timeout MAX_TIMEOUT = 60 timeout = 0.1 upload_recv, upload_send = Pipe() file_parent, file_child = Pipe() file_upload_process = Process(target=upload_files, args=(upload_recv, file_child, session, fs_secret['upload'])) file_upload_process.start() fs_hmac = hmac.new(fs_secret[''], digestmod=sha1) del fs_secret if resource_limits is None: resource_limits = [] from resource import setrlimit for r, l in resource_limits: setrlimit(r, l) while True: try: msg = message_queue.get(timeout=timeout) except Empty: break if msg[0] == "exec": msg = msg[1] else: break # Now msg is an IPython message for the user session if msg['msg_type'] != "execute_request": raise ValueError("Received invalid message: %s" % (msg, )) # TODO: we probably ought not prepend our own code, in case the user has some # "from __future__ import ..." statements, which *must* occur at the top of the code block # alternatively, we could move any such statements above our statements code = line_prefix.sub("", msg['content']['code'].encode('utf8')) CONFIG.EMBEDDED_MODE["sage_mode"] = sage_mode = msg['content'][ 'sage_mode'] if enable_sage and sage_mode: from sage.misc.preparser import preparse_file code = user_code_sage + "\n" + preparse_file(code) elif sage_mode: code = "print 'NOTE: Sage Mode is unavailable, which may cause errors if using Sage-specific syntax.'\n" + user_code + code else: code = user_code + code code = displayhook_hack(code) # always add a newline to avoid this bug in Python versions < 2.7: http://bugs.python.org/issue1184112 code += '\n' log("Executing: %r" % code) output_handler.set_parent_header(msg['header']) old_files = dict([(f, os.stat(f).st_mtime) for f in os.listdir(os.getcwd())]) if 'files' in msg['content']: for filename in msg['content']['files']: with open(filename, 'w') as f: fs.copy_file(f, filename=filename, session=session, hmac=fs_hmac) old_files[filename] = -1 file_parent.send(True) with output_handler: try: import user_convenience locals = { '_sagecell': user_convenience.UserConvenience(output_handler, upload_send), '_sage_messages': output_handler, '_sage_upload_file_pipe': upload_send } if enable_sage and sage_mode: locals['sage'] = sage exec code in locals # I've commented out fields we aren't using below to # save bandwidth output_handler.message_queue.raw_message( "execute_reply", { "status": "ok", #"payload":[], #"user_expressions":{}, #"user_variables":{} }) # technically should send back an execution_state: idle message too except: (etype, evalue, etb) = sys.exc_info() try: from IPython.core import ultratb as ultraTB except ImportError: # Workaround for IPython 0.10 from IPython import ultraTB err = ultraTB.VerboseTB(include_vars=0, tb_offset=1) try: # Check whether the exception has any further details error_value = evalue[0] except: error_value = "" #TODO: docs have this as exc_name and exc_value, #but it seems like IPython returns ename and #evalue! err_msg = { "ename": etype.__name__, "evalue": str(error_value), "traceback": err.text(etype, evalue, etb, context=3), "status": "error" } output_handler.message_queue.raw_message( "execute_reply", err_msg) upload_send.send_bytes(json.dumps({'msg_type': 'end_exec'})) new_files = file_parent.recv() old_files.update(new_files) # TODO: security implications here calling something that the user had access to. timeout = max( 0, min(float(interact_sagecell.__sage_cell_timeout__), MAX_TIMEOUT)) file_list = [] for filename in os.listdir(os.getcwd()): if filename not in old_files or old_files[filename] != os.stat( filename).st_mtime: file_list.append(filename) try: with open(filename) as f: fs.create_file(f, session=session, filename=filename, hmac=fs_hmac) except Exception as e: sys.stdout.write("An exception occurred: %s\n" % (e, )) if len(file_list) > 0: output_handler.message_queue.message('files', {'files': file_list}) log("Done executing code: %r" % code) upload_send.send_bytes(json.dumps({'msg_type': 'end_session'})) file_upload_process.join()
def track_rat(fname, corners=None, length=-1): if length <= 0: length = np.inf #Import video folder, file = os.path.split(fname) file = os.path.splitext(file)[0] cap = cv2.VideoCapture(fname) ret, frame = cap.read() frame = equalize_image(frame) fps = cap.get(cv2.CAP_PROP_FPS) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #Define cropped region region = define_crop_region(frame) frame_cropped = crop(frame, region) #Identify corners of Open Field if corners is not None: for pt in corners: cv2.circle(frame_cropped, tuple(pt), 10, (0, 255, 0), -1) perspective_transform, frame_cropped = fix_perspective(frame_cropped) #Output tracked video fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') ensure_dir(folder + '/Tracked/') out = cv2.VideoWriter(folder + '/Tracked/' + file + '_tracked.avi', fourcc, 2 * fps, (frame_cropped.shape[1], frame_cropped.shape[0])) #Position Matrix num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) rat_pos = np.zeros((num_frames, 3), dtype=float) rat_pos[:, 0] = np.arange(num_frames, dtype=float) / fps rat_pos[:, 1:] = np.NaN frame_num = 0 tot_time = 0 try: bar = progress_bar(num_frames - 1) bar.start() while ret and tot_time <= length + 2: frame = equalize_image(frame) frame_num = int(cap.get(cv2.CAP_PROP_POS_FRAMES)) - 1 frame_cropped = crop(frame, region) frame_cropped = cv2.warpPerspective(frame_cropped, perspective_transform, (600, 600), borderValue=(255, 255, 255)) cx, cy, rat = find_rat(frame_cropped) if rat is None: rat_pos[frame_num, 1] = np.NaN rat_pos[frame_num, 2] = np.NaN annotated = frame_cropped.copy() else: rat_pos[frame_num, 1] = cx rat_pos[frame_num, 2] = cy tot_time += 1 / fps if frame_num % 2 == 0: pts = rat_pos.copy()[0:frame_num, 1:] pts = pts[~np.isnan(pts).any(axis=1)] pts = pts.reshape((-1, 1, 2)).astype(np.int32) annotated = cv2.polylines(frame_cropped.copy(), [pts], False, (180, 119, 31), 2) cv2.drawContours(annotated, [rat], -1, (14, 127, 255), 3) if frame_num % 2 == 0: out.write(annotated) ret, frame = cap.read() bar.update(frame_num) bar.finish() cap.release() out.release() #rat_pos = rat_pos[~np.isnan(rat_pos).any(axis=1)] #Trim out any NaN rat_pos = rat_pos[ 2 * int(fps):, :] #Trim out the first half second with the hand return rat_pos except Exception as err: print('Fuckkkkkk') cap.release() out.release() #print(err) #traceback.print_exc() err_tb = tb.VerboseTB() print(err_tb.text(*sys.exc_info()))
def enable_nice_traceback(): # also look for "ultraTB" in "IPython.CrashHandler" import IPython.core.ultratb as utb sys.excepthook = utb.VerboseTB( include_vars=0) #VerboseTB, ColorTB, AutoFormattedTB, FormattedTB
def main(): """ Parses command line arguments and executes commands """ # Setup recipe importer sys.meta_path.append(RecipeFinder) # Setup argument parser parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() parser.add_argument('--log_level', dest='log_level', metavar='LOG_LEVEL', type=int, default=20, help='log level, 10 for debug messages, 50 for only critical,' ' default: 20, ') parser.add_argument('--config', dest='config_files', action='append', default=[], help='config file for setup, defining which jobs to run') # Not fully supported at the moment # parser.add_argument('--settings', dest='settings_file', # default=gs.GLOBAL_SETTINGS_FILE_DEFAULT, # help='settings file, aka how to run the jobs') # parser.add_argument('-s', '--setting', dest='commandline_settings', action='append', # default=[], help='overwrite global settings directly via commandline') parser_manager = subparsers.add_parser('manager', aliases=['m'], conflict_handler='resolve', help="Load config files and start manager loop") parser_manager.set_defaults(func=manager) parser_manager.add_argument("-r", dest="run", default=False, action='store_true', help="Run the given task") parser_manager.add_argument("-co", dest="clear_once", action="store_true", default=False, help="Move jobs aside that are in an error " "state when the manager runs the first time") parser_manager.add_argument("--http", dest="http_port", default=None, type=int, help="Enables http server, takes " "port as argument") parser_manager.add_argument("--fs", "--filesystem", dest="filesystem", default=None, help="Start filesystem in given directory") parser_manager.add_argument('argv', metavar='ARGV', type=str, nargs='*', help='an additional way do ' 'define config files') parser_console = subparsers.add_parser('console', aliases=['c'], usage='sis console [-h] [--load LOAD_SIS_GRAPH] [ARGV [ARGV ...]]\n\n' 'Open console to debug sisyphus graph or job', help="Start console to interactively work on sis graph. Things like: " "Rerunning tasks, cleaning up the work directory, and debugging " "is best done here") parser_console.add_argument("--load", dest="load", default=[], action='append', help="load graph and start console") parser_console.add_argument("--skip_config", dest="not_load_config", default=False, action='store_true', help="do not load config files before starting the console") parser_console.add_argument("-c", dest="commands", default=[], action='append', help="Run commands after loading console") parser_console.add_argument('argv', metavar='ARGV', type=str, nargs='*', help='an additional way do define config files') parser_console.set_defaults(func=console) parser_worker = subparsers.add_parser(gs.CMD_WORKER, help='Start worker to compute job (for internally usage)') parser_worker.set_defaults(func=worker) parser_worker.add_argument('jobdir', metavar='JOBDIR', type=str, help='Job directory of the executed function') parser_worker.add_argument('task_name', metavar='TASK_NAME', type=str, help='Task name') parser_worker.add_argument('task_id', metavar='TASK_ID', type=int, nargs='?', default=None, help='Task id, if not set trying to ' 'read it from environment variables') parser_worker.add_argument('--force_resume', "--fr", default=False, action="store_true", help='force resume of non resumable tasks, good for debugging') parser_worker.add_argument('--engine', default='short', help='The engine running the Job') # Currently disabled parser, maybe used again in the future # # Disabled, since it's not used for now # parser_unittest = subparsers.add_parser('unittest', help='Run unittest tasks') # parser_unittest.set_defaults(func=unittest) # parser_unittest.add_argument("--do_not_run", dest="run", default=True, action='store_false', # help="Do not run the given task") # parser_unittest.add_argument('argv', metavar='ARGV', type=str, nargs='*', # help='an additional way do define config files') # Disabled since it's currently not working # parser_notebook = subparsers.add_parser('notebook', # usage='sis notebook [-h]\n\n' # 'Open notebook session', # help="Start notebook to interactively work on sis graph") # parser_notebook.add_argument("--load", dest="load", default=[], action='append', # help="load graph and start console") # parser_notebook.add_argument("--file", "-f", dest="filename", default='default', # help="load (and create if needed) this notebook file") # parser_notebook.set_defaults(func=helper.notebook) # Disabled since it's currently not working # parser_connect = subparsers.add_parser('connect', usage='sis connect [-h] [connect_file]\n\n' # 'Opens a console connected to given kernel', # help='Opens a console connected with given kernel') # parser_connect.add_argument('argv', metavar='ARGV', type=str, nargs='?', # default=[], help='connection_file') # parser_connect.set_defaults(func=helper.connect) args = parser.parse_args() if not hasattr(args, 'func'): parser.print_help() return # add argv to config_files if manager or console is called if args.func in [manager, console]: args.config_files += args.argv # Setup logging logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s', level=args.log_level) # Changing settings via commandline is currently not supported # Needs to ensure all parameters are passed correctly to worker, ignored since nobody requested it so far # update_global_settings_from_file(args.settings_file) # update_global_settings_from_list(args.commandline_settings) update_global_settings_from_file(gs.GLOBAL_SETTINGS_FILE_DEFAULT) if gs.USE_VERBOSE_TRACEBACK: if gs.VERBOSE_TRACEBACK_TYPE == "ipython": from IPython.core import ultratb sys.excepthook = ultratb.VerboseTB() elif gs.VERBOSE_TRACEBACK_TYPE == "better_exchook": # noinspection PyPackageRequirements import better_exchook better_exchook.install() better_exchook.replace_traceback_format_tb() else: raise Exception("invalid VERBOSE_TRACEBACK_TYPE %r" % gs.VERBOSE_TRACEBACK_TYPE) if gs.USE_SIGNAL_HANDLERS: from sisyphus.tools import maybe_install_signal_handers maybe_install_signal_handers() if args.func != manager: gs.JOB_AUTO_CLEANUP = False try: args.func(args) except BaseException as exc: if not isinstance(exc, SystemExit): logging.error("Main thread unhandled exception:") sys.excepthook(*sys.exc_info()) import threading non_daemon_threads = { thread for thread in threading.enumerate() if not thread.daemon and thread is not threading.main_thread()} if non_daemon_threads: logging.warning("Main thread exit. Still running non-daemon threads: %r" % non_daemon_threads)
def debug (e, c, t) : if CONFIG.debug : vtb = ultratb.VerboseTB(color_scheme="Linux") print("\n".join(vtb.structured_traceback(e, c, t))) if ipdb is not None : ipdb.post_mortem(t)