def element_text_should_be_exact(self, locator, expected): """Verifies element identified by `locator` exactly contains text `expected`. In contrast to `element_should_contain_text`, this keyword does not try a substring match but an exact match on the element identified by `locator`. """ import pdb;pdb.Pdb(stdout=sys.__stdout__).set_trace() log.mjLog.LogReporter("AssertElement","debug","Element should contains exact text: %s => %s" % (locator,expected)) self.element = self._element_finder(locator) actual = self.element.text if expected != actual: message = "The text of element '%s' should have been '%s' but "\ "in fact it was '%s'." % (locator, expected, actual) raise AssertionError(message) log.mjLog.LogReporter("AssertElement","debug","Element contains exact text: %s => %s" % (locator,actual))
def expect(condition, debug_data, debug_interactively=False): """Expect a condition to be true, if not, either a debugger is started or an exception is raised providing debug_data. This is controlled by the debug_interactively parameter.""" # Pass check if condition: return # Handle failure if debug_interactively: print("Expectation failed, starting debugger") import pdb as dbg dbg.Pdb(skip=['daan.ml.tools.*']).set_trace() else: if callable(debug_data): debug_data = debug_data() raise ValueError("Qualitative check failed (%s)" % str(debug_data))
def config(self, **kwargs): if "callback" in kwargs: self._callback = kwargs["callback"] if "pdb" in kwargs: self.pdb = pdb.Pdb() self.pdb.reset() if "file" in kwargs: self.file = kwargs["file"] if "stack_limit" in kwargs: self.stack_limit = kwargs["stack_limit"] if "custom_printer" in kwargs: self.custom_printer = kwargs["custom_printer"]
def set_trace(self, bp, frameCount=1): #find useful frame self.currFrame = sys._getframe() interactFrame = self.currFrame while frameCount > 0: interactFrame = interactFrame.f_back frameCount -= 1 #cache this as the latest bp self.lastBp = bp.getParts() #set up and start debuggger import pdb self.pdb = pdb.Pdb() #self.pdb.do_alias('aa bpdb.addPdbAliases()') self.addPdbAliases() self.pdb.set_trace(interactFrame)
def get_intersection(nums1, nums2): pdb.Pdb(stdout=sys.__stdout__).set_trace() nums3 = [] if len(nums1) == 0 or len(nums2) == 0: return False list1_dict = get_dictionary(nums1) for num in nums2: try: if list1_dict[num]: nums3.append(num) except KeyError: LOGGER.debug("element does not exist in list1_dict {}".format(num)) return nums3
def _getDebugger(self): dbg = pdb.Pdb() try: import readline except ImportError: print "readline module not available" sys.exc_clear() for path in ('.pdbrc', 'pdbrc'): if os.path.exists(path): try: rcFile = file(path, 'r') except IOError: sys.exc_clear() else: dbg.rcLines.extend(rcFile.readlines()) return dbg
def SecondaryStructure_CV(DB, data_type, list_ID, list_y, list_FASTA, dict_id2fasta, n_folds): if data_type == 'kernel': if not os.path.isfile('data/' + DB + '/' + DB + '_K.npy'): print('data/' + DB + '/' + DB + '_K.npy', 'does not exist') else: K = np.load('data/' + DB + '/' + DB + '_K.npy') list_assignment = Khierarchical_cluster(K, n_folds) elif data_type == 'features': if not os.path.isfile('data/' + DB + '/' + DB + '_X.npy'): print('data/' + DB + '/' + DB + '_X.npy', 'does not exist') else: X = np.load('data/' + DB + '/' + DB + '_X.npy') list_assignment = Xkmeans_cluster(X, n_folds) elif data_type == 'standard': X = np.zeros((len(list_ID), 1)) list_assignment = np.zeros(X.shape[0]) skf = model_selection.KFold(n_folds, shuffle=True, random_state=92) skf.get_n_splits(X) ifold = 0 for train_index, test_index in skf.split(X): list_assignment[test_index] = ifold ifold += 1 import pdb pdb.Pdb().set_trace() c = collections.Counter(list_assignment) folds = [np.where(list_assignment == cle)[0] for cle in list(c.keys())] fo = open('data/' + DB + '/' + DB + '_folds.txt', 'w') for ifold in range(n_folds): # import pdb; pdb.Pdb().set_trace() fo.write("ifold " + str(ifold) + '\t' + str( collections.Counter( [el for ll in list_y[folds[ifold]] for el in ll]))) fo.write('\n') print("ifold " + str(ifold) + '\t' + str( collections.Counter( [el for ll in list_y[folds[ifold]] for el in ll]))) fo.close() return folds
def unexpected_exception(e, tb=None, frame=None): """Marks an exception that we might want to debug. Before logging an exception or showing a message (potentially with format_exception()), you might want to call this. It's a no-op unless debugging is enabled in the configuration, in which case it will start a debugger. """ if tb is None: tb = sys.exc_info()[2] if frame is None: tb_it = tb while tb_it.tb_next is not None: tb_it = tb_it.tb_next frame = tb_it.tb_frame # Whether to use the debugger try: from vistrails.core.configuration import get_vistrails_configuration debugger = getattr(get_vistrails_configuration(), 'developerDebugger', False) except Exception: debugger = False if not debugger: return # Removes PyQt's input hook try: from PyQt4 import QtCore except ImportError: pass else: QtCore.pyqtRemoveInputHook() # Prints the exception and traceback print >>sys.stderr, "!!!!!!!!!!" print >>sys.stderr, "Got unexpected exception, starting debugger" print_exception(None, e, tb, 3, file=sys.stderr) # Starts the debugger print >>sys.stderr, "!!!!!!!!!!" # pdb.post_mortem() p = pdb.Pdb() p.reset() p.interaction(frame, tb)
def specificity(reference_codon_sequence, comparison_codon_sequence): '''Portion correct of sites predicted to be in genes Args: - reference_codon_sequence (ndarray): the true codon sequence - comparison_codon_sequence (ndarray): the codon to compare to the reference sequence Output: - float: the specificity ''' positive_predictions = comparison_codon_sequence == 1 true_predicitons = np.equal(reference_codon_sequence, comparison_codon_sequence) true_positives = positive_predictions * true_predicitons if np.sum(positive_predictions) == 0: import pdb pdb.Pdb().set_trace() return np.sum(true_positives) / np.sum(positive_predictions)
def trace_max_mem(self, frame, event, arg): # run into PDB as soon as memory is higher than MAX_MEM if event in ('line', 'return') and frame.f_code in self.code_map: c = _get_memory(os.getpid()) if c >= self.max_mem: t = 'Current memory {0:.2f} MB exceeded the maximum '.format(c) + 'of {0:.2f} MB\n'.format(self.max_mem) sys.stdout.write(t) sys.stdout.write('Stepping into the debugger \n') frame.f_lineno -= 2 p = pdb.Pdb() p.quitting = False p.stopframe = frame p.returnframe = None p.stoplineno = frame.f_lineno - 3 p.botframe = None return p.trace_dispatch return self.trace_max_mem
def process(commands, debug): r""" Run the specified subcommands of ``worker``. EXAMPLES: We compute the orbit closure of the unfolding of a equilateral triangle, i.e., the torus:: >>> from ..test.cli import invoke >>> invoke(worker, "ngon", "-a", "1", "-a", "1", "-a", "1", "orbit-closure") [Ngon([1, 1, 1])] [FlowDecompositions] ¯\_(ツ)_/¯ (orientation: (-6, (-2*c ~ -3.4641016))) (cylinders: 1) (minimal: 0) (undetermined: 0) [Ngon([1, 1, 1])] [OrbitClosure] dimension: 2/2 [Ngon([1, 1, 1])] [OrbitClosure] GL(2,R)-orbit closure of dimension at least 2 in H_1(0) (ambient dimension 2) (dimension: 2) (directions: 1) (directions_with_cylinders: 1) (dense: True) """ if debug: import pdb import signal signal.signal(signal.SIGUSR1, lambda sig, frame: pdb.Pdb().set_trace(frame)) try: while True: objects = Worker.make_object_graph(commands) try: import asyncio asyncio.run(objects.provide(Worker).start()) except Restart as restart: commands = [ restart.rewrite_command(command, objects=objects) for command in commands ] continue break except Exception: if debug: pdb.post_mortem() raise
def log(self, message, level=5, tags="", dontprint=False, category=""): """ @param category is in dot notation e.g. appserver3.status or system.fs """ try: if level < (j.logger.consoleloglevel + 1) and not dontprint and j.application.interactive: j.console.echo(message, log=False) if j.logger.nolog: return if level < j.logger.maxlevel + 1: # send to logserver self.sendMessage(self.data2Message(1, "%s,%s,%s" % (level, category, message)), 1, True) except Exception as e: print("HAVE SERIOUS BUG IN LOGGER 4 MESSAGEHANDLER 6\n") print(e) import pdb pdb.Pdb().set_trace(None)
def set_trace(): """Pdb using original stdin and stdout. When debugging blackbox tests, sys.stdin and sys.stdout are captured for test purposes and cannot be used for interactive debugging. This class uses the origianl stdin/stdout to allow such use. Instead of doing: import pdb; pdb.set_trace() you can do: from bzrlib import debug; debug.set_trace() """ import pdb import sys pdb.Pdb(stdin=sys.__stdin__, stdout=sys.__stdout__).set_trace(sys._getframe().f_back)
def set_trace(*args, **kwargs): """Call pdb.set_trace, making sure it receives the unwrapped stdout. This is so we don't keep drawing progress bars over debugger output. """ # There's no stream attr if capture plugin is enabled: out = sys.stdout.stream if hasattr(sys.stdout, 'stream') else None # Python 2.5 can't put an explicit kwarg and **kwargs in the same function # call. kwargs['stdout'] = out debugger = pdb.Pdb(*args, **kwargs) # Ordinarily (and in a silly fashion), pdb refuses to use raw_input() if # you pass it a stream on instantiation. Fix that: debugger.use_rawinput = True debugger.set_trace(sys._getframe().f_back)
def maybe_debug(break_funcs, enable): """Context manager to wrap a block to possibly run under debugger. Arguments: break_funcs(list): functions to set up breakpoints for enable(bool): whether to actually trigger debugger, or be no-op """ if not enable: yield return debugger = pdb.Pdb() for func in break_funcs: debugger.set_break(func.func_code.co_filename, func.func_code.co_firstlineno, funcname=func.func_code.co_name) try: def dispatch_thunk(*args): """Triggers 'continue' command when debugger starts.""" val = debugger.trace_dispatch(*args) debugger.set_continue() sys.settrace(debugger.trace_dispatch) return val debugger.reset() sys.settrace(dispatch_thunk) try: yield finally: debugger.quitting = 1 sys.settrace(None) except bdb.BdbQuit: pass except Exception: traceback.print_exc() print('Uncaught exception. Entering post mortem debugging') print('Running \'cont\' or \'step\' will restart the program') t = sys.exc_info()[2] debugger.interaction(None, t)
def collect_all(self): debug = 4 if self.opts.debug_modulegraph else 0 mf = find_modules(scripts=self.collect_scripts(), includes=self.opts.includes, packages=self.opts.packages, excludes=self.opts.excludes, debug=debug) filters = [has_filename_filter] flatpackages = {} loader_files = [] self.process_recipes(mf, filters, flatpackages, loader_files) if self.opts.debug_modulegraph: import pdb pdb.Pdb().set_trace() self.filter_dependencies(mf, filters) py_files, extensions = self.finalize_modulefinder(mf) pkgdirs = self.collect_packagedirs() return py_files, pkgdirs, extensions, loader_files
def do_debug(self, line): """debug (module:function|module:class.function) Run 'pdb' in post mortem mode for the traceback captured against the nominated function.""" if not line in _tracebacks: return tb = _tracebacks[line] debugger = pdb.Pdb(stdin=self.stdin, stdout=self.stdout) debugger.reset() acquire_console(self) try: debugger.interaction(None, tb) except SystemExit: pass finally: release_console()
def run_normal(self): mf = self.get_modulefinder() filters = self.collect_filters() flatpackages = {} loader_files = [] self.process_recipes(mf, filters, flatpackages, loader_files) if self.debug_modulegraph: import pdb pdb.Pdb().set_trace() self.filter_dependencies(mf, filters) if self.graph: self.build_graph(mf, flatpackages) if self.xref: self.build_xref(mf, flatpackages) py_files, extensions = self.finalize_modulefinder(mf) pkgdirs = self.collect_packagedirs() self.create_binaries(py_files, pkgdirs, extensions, loader_files)
def handle_Test(test): dbg = pdb.Pdb() for path, line, funcname in test.breakpoints: dbg.set_break(path, line, funcname=funcname) dbg.reset() def dispatch_thunk(*args): """Allows us to continue until the actual breakpoint.""" val = dbg.trace_dispatch(*args) dbg.set_continue() sys.settrace(dbg.trace_dispatch) return val sys.settrace(dispatch_thunk) try: test.run() except pdb.bdb.BdbQuit: pass finally: dbg.quitting = 1 sys.settrace(None)
def _wrappedPdb(): """ Wrap an instance of C{pdb.Pdb} with readline support and load any .rcs. """ dbg = pdb.Pdb() try: import readline except ImportError: print("readline module not available") sys.exc_clear() for path in ('.pdbrc', 'pdbrc'): if os.path.exists(path): try: rcFile = file(path, 'r') except IOError: sys.exc_clear() else: dbg.rcLines.extend(rcFile.readlines()) return dbg
def _wrappedPdb(): """ Wrap an instance of C{pdb.Pdb} with readline support and load any .rcs. """ dbg = pdb.Pdb() try: namedModule("readline") except ImportError: print("readline module not available") for path in (".pdbrc", "pdbrc"): if os.path.exists(path): try: rcFile = open(path) except OSError: pass else: with rcFile: dbg.rcLines.extend(rcFile.readlines()) return dbg
def trace_max_mem(self, frame, event, arg): # run into PDB as soon as memory is higher than MAX_MEM if event in ('line', 'return') and frame.f_code in self.code_map: c = _get_memory(-1, self.backend, filename=frame.f_code.co_filename) if c >= self.max_mem: t = ('Current memory {0:.2f} MiB exceeded the ' 'maximum of {1:.2f} MiB\n'.format(c, self.max_mem)) sys.stdout.write(t) sys.stdout.write('Stepping into the debugger \n') frame.f_lineno -= 2 p = pdb.Pdb() p.quitting = False p.stopframe = frame p.returnframe = None p.stoplineno = frame.f_lineno - 3 p.botframe = None return p.trace_dispatch if self._original_trace_function is not None: (self._original_trace_function)(frame, event, arg) return self.trace_max_mem
def _store_mod(self, attrs, mass_key="mass"): ''' Stores a given mod data, which can either be from the name == 'mod_aminoacid_mass' or name == 'modification_info' tags. ''' if mass_key == 'massdiff': import pdb, sys; pdb.Pdb(stdout=sys.__stdout__).set_trace() mass = str(round(float(attrs[mass_key]), self._moddec)) # grab mod name and make blank list if not in # internal mods use mass # need to convert to 4 decimal accuracy since older pepXML specs # do not have identical accuracy for mods key = self._get_modkey(mass_key) mod_name = self._modifications[key][mass] # pep xml schema doesn't have uncertain mods, so... grab hit hit = self._hits[self._hit] hit['mods']['certain'].setdefault(mod_name, []) pos = self._get_pos(mass_key, attrs) hit['mods']['certain'][mod_name].append(pos)
def post_mortem_excepthook(type, value, tb): """ For post mortem exception handling, print a banner and enable post mortem debugging. """ ipython_shell = get_ipython() ipython_shell.showtraceback((type, value, tb)) p = pdb.Pdb(ipython_shell.colors) if not type == SyntaxError: # wait for stderr to print (stderr.flush does not work in this case) time.sleep(0.1) _print('*' * 40) _print('Entering post mortem debugging...') _print('*' * 40) # add ability to move between frames p.send_initial_notification = False p.reset() frame = tb.tb_next.tb_frame # wait for stdout to print time.sleep(0.1) p.interaction(frame, tb)
def output_error(self, error_tuple): if 'TESTR_PDB' in os.environ: import traceback self._stderr.write(_u('').join(traceback.format_tb(error_tuple[2]))) self._stderr.write(_u('\n')) # This is terrible: it is because on Python2.x pdb writes bytes to # its pipes, and the test suite uses io.StringIO that refuse bytes. import pdb; if sys.version_info[0]==2: if isinstance(self._stdout, io.StringIO): write = self._stdout.write def _write(text): return write(text.decode('utf8')) self._stdout.write = _write p = pdb.Pdb(stdin=self._stdin, stdout=self._stdout) p.reset() p.interaction(None, error_tuple[2]) error_type = str(error_tuple[1]) # XX: Python2. if type(error_type) is bytes: error_type = error_type.decode('utf8') self._stderr.write(error_type + _u('\n'))
def _debug_recipe(recipe_deps, recipe, test_data): """Debugs the given recipe + test case. Args: * recipe_deps (RecipeDeps) * recipe (Recipe) * test_data (TestData) """ debugger = pdb.Pdb() for func in [recipe.global_symbols['RunSteps']]: debugger.set_break(func.func_code.co_filename, func.func_code.co_firstlineno, funcname=func.func_code.co_name) try: def dispatch_thunk(frame, event, arg): """Triggers 'continue' command when debugger starts.""" val = debugger.trace_dispatch(frame, event, arg) debugger.set_continue() sys.settrace(debugger.trace_dispatch) return val debugger.reset() sys.settrace(dispatch_thunk) try: execute_test_case(recipe_deps, recipe.name, test_data) finally: debugger.quitting = 1 sys.settrace(None) except bdb.BdbQuit: pass except Exception: # pylint: disable=broad-except traceback.print_exc() print 'Uncaught exception. Entering post mortem debugging' print 'Running \'cont\' or \'step\' will restart the program' tback = sys.exc_info()[2] debugger.interaction(None, tback)
def run_test_suite(self): #Run the tests if self.debug is None: self.cov.start() tr = unittest.TextTestRunner() tr.run(self.suite) self.cov.stop() self.cov.save() #self.cov.report() self.cov.xml_report(outfile='build/coverage.xml') self.cov.html_report(directory='build/htmlcov') else: import pdb db = pdb.Pdb() if self.bSetup: db.rcLines.append('b self.setUp') db.rcLines.append('b self.{0}'.format(self.method)) if self.bTeardown: db.rcLines.append('b self.tearDown') db.rcLines.append('c') db.rcLines.append('l') db.runcall(self.suite.debug)
def signalHandler(self, signum, frame): if self._in_sigint: # ignore multiple Ctrl-C presses return if self._prompting: # shown while at prompt: always stop directly signal.default_int_handler(signum, frame) self._in_sigint = True try: self.log.info('== Keyboard interrupt (Ctrl-C) ==') self.log.info('Please enter how to proceed:') self.log.info('<I> ignore this interrupt') self.log.info('<H> stop after current scan point') self.log.info('<L> stop after current scan') self.log.info('<S> immediate stop') try: reply = input_func('---> ') except RuntimeError: # when already in readline(), this will be raised reply = 'S' self.log.log(INPUT, reply) # first two choices are hidden, but useful for debugging purposes if reply.upper() == 'R': # handle further Ctrl-C presses with KeyboardInterrupt signal.signal(signal.SIGINT, signal.default_int_handler) elif reply.upper() == 'D': # print a stacktrace and debug self.log.info(formatExtendedStack(2)) pdb.Pdb().set_trace(sys._getframe(1)) elif reply.upper() == 'I': pass elif reply.upper() == 'H': self._stoplevel = 2 elif reply.upper() == 'L': self._stoplevel = 1 else: # this will create a KeyboardInterrupt and run stop() signal.default_int_handler(signum, frame) finally: self._in_sigint = False
def breakpoint(self, name=None): """Add breakpoint, optionally named, at the place where this method is called. For the breakpoint to be activated the JUJU_DEBUG_AT environment variable must be set to "all" or to the specific name parameter provided, if any. In every other situation calling this method does nothing. The framework also provides a standard breakpoint named "hook", that will stop execution when a hook event is about to be handled. For those reasons, the "all" and "hook" breakpoint names are reserved. """ # If given, validate the name comply with all the rules if name is not None: if not isinstance(name, str): raise TypeError('breakpoint names must be strings') if name in ('hook', 'all'): raise ValueError( 'breakpoint names "all" and "hook" are reserved') if not re.match(r'^[a-z0-9]([a-z0-9\-]*[a-z0-9])?$', name): raise ValueError( 'breakpoint names must look like "foo" or "foo-bar"') indicated_breakpoints = self._juju_debug_at if not indicated_breakpoints: return if 'all' in indicated_breakpoints or name in indicated_breakpoints: self._show_debug_code_message() # If we call set_trace() directly it will open the debugger *here*, so indicating # it to use our caller's frame code_frame = inspect.currentframe().f_back pdb.Pdb().set_trace(code_frame) else: logger.warning( "Breakpoint %r skipped (not found in the requested breakpoints: %s)", name, indicated_breakpoints)
def process_view(self, request, view_func, view_args, view_kwargs): """ If running with '--pdb', set a breakpoint at the start of each of each view before it gets called. """ # Skip out unless using `runserver --pdb`, # or `pdb` is in the command line parameters type_pdb = self.get_type_pdb(request) if not type_pdb: return filename = inspect.getsourcefile(view_func) basename = os.path.basename(filename) dirname = os.path.basename(os.path.dirname(filename)) lines, lineno = inspect.getsourcelines(view_func) temporary = True cond = None funcname = view_func.__name__ print print '%s %s' % (request.method, request.get_full_path()) print 'function "%s" in %s/%s:%d' % (funcname, dirname, basename, lineno) print 'args: %s' % (view_args,) print 'kwargs: %s' % (view_kwargs,) print if type_pdb == 'ipdb' and has_ipdb(): p = get_ipdb() else: if not type_pdb == 'pdb': print 'You do not install ipdb or ipython module' p = pdb.Pdb() p.reset() p.set_break(filename, lineno + 1, temporary, cond, funcname) sys.settrace(p.trace_dispatch)