def f1(a,b): print a+b cf = inspect.currentframe() print inspect.getargvalues(cf) while cf.f_back.f_back: cf = cf.f_back print cf.f_locals arg = inspect.getargvalues(cf) print inspect.formatargvalues(arg)
def call_spec_string(): """Return the argument names and values of the method or function it was called from as a string Returns: str: The argument string, e.g: (name='hallo', codenames=['aa', 'bb'], port=8000) """ frame = sys._getframe(1) argvals = inspect.getargvalues(frame) if argvals.args[0] == 'self': return inspect.formatargvalues(argvals.args[1:], *argvals[1:]) else: return inspect.formatargvalues(*argvals)
def user_line(self, frame): """ Public method reimplemented to handle the program about to execute a particular line. @param frame the frame object """ line = frame.f_lineno # We never stop on line 0. if line == 0: return fn = self._dbgClient.absPath(self.fix_frame_filename(frame)) # See if we are skipping at the start of a newly loaded program. if self._dbgClient.mainFrame is None: if fn != self._dbgClient.getRunning(): return self._dbgClient.mainFrame = frame self.currentFrame = frame self.currentFrameLocals = frame.f_locals # remember the locals because it is reinitialized when accessed fr = frame stack = [] while fr is not None: # Reset the trace function so we can be sure # to trace all functions up the stack... This gets around # problems where an exception/breakpoint has occurred # but we had disabled tracing along the way via a None # return from dispatch_call fr.f_trace = self.trace_dispatch fname = self._dbgClient.absPath(self.fix_frame_filename(fr)) if not fname.startswith("<"): fline = fr.f_lineno ffunc = fr.f_code.co_name if ffunc == '?': ffunc = '' if ffunc and not ffunc.startswith("<"): argInfo = inspect.getargvalues(fr) fargs = inspect.formatargvalues(argInfo[0], argInfo[1], argInfo[2], argInfo[3]) else: fargs = "" stack.append([fname, fline, ffunc, fargs]) if fr == self._dbgClient.mainFrame: fr = None else: fr = fr.f_back self.__isBroken = True self._dbgClient.write('%s%s\n' % (ResponseLine, unicode(stack))) self._dbgClient.eventLoop()
def log_api_call(function_call_tup=None, log_level=logging.INFO, status_code=200, exception=None): ''' Inspect the call stack frame record to get the function name and arguments. Log this info (with blanked out password) along with the API call's response status code and possibly an exception ''' if not function_call_tup: try: function_call_tup = inspect.stack()[1] except IndexError: function_call_tup = (None, ) * 6 if function_call_tup[0]: args = inspect.getargvalues(function_call_tup[0]) # we don't want passwords in logs locals_copy = args.locals.copy() if 'password' in locals_copy: locals_copy['password'] = '******' arg_str = inspect.formatargvalues(*(args[:-1] + (locals_copy, ))) else: arg_str = '' kwargs = {} if log_level == logging.ERROR: kwargs['exc_info'] = exception logger.log(log_level, '%(function_name)s%(arg_str)s: %(status_code)s', {'function_name': function_call_tup[3], 'arg_str': arg_str, 'status_code': status_code}, **kwargs)
def frame_parser(frame_record): """ Parse given frame and return formatted values. :param frame_record: frame :return: tuple (fname, call, code, objects, local_vars) """ frame, fname, lnum, func, lines, index = frame_record fname = fname and os.path.abspath(fname) or '?' args, varargs, varkw, local_vars = inspect.getargvalues(frame) call = '' if func != '?': call = func + inspect.formatargvalues( args, varargs, varkw, local_vars, formatvalue=lambda value: '=' + pydoc.text.repr(value)) highlight = {} def reader(lnum=[lnum]): highlight[lnum[0]] = 1 try: return linecache.getline(fname, lnum[0]) finally: lnum[0] += 1 code = format_code(lines, lnum, index) objects = cgitb.scanvars(reader, frame, local_vars) return (fname, call, code, objects, local_vars)
def test_previous_frame(self): args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back) self.assertEqual(args, ['a', 'b', 'c', 'd', 'e', 'f']) self.assertEqual(varargs, 'g') self.assertEqual(varkw, 'h') self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), '(a=7, b=8, c=9, d=3, e=4, f=5, *g=(), **h={})')
def getStack(self): """ Public method to get the stack. @return list of lists with file name (string), line number (integer) and function name (string) """ fr = self.cFrame stack = [] while fr is not None: fname = self._dbgClient.absPath(self.fix_frame_filename(fr)) if not fname.startswith("<"): fline = fr.f_lineno ffunc = fr.f_code.co_name if ffunc == '?': ffunc = '' if ffunc and not ffunc.startswith("<"): argInfo = inspect.getargvalues(fr) fargs = inspect.formatargvalues(argInfo[0], argInfo[1], argInfo[2], argInfo[3]) else: fargs = "" stack.append([fname, fline, ffunc, fargs]) if fr == self._dbgClient.mainFrame: fr = None else: fr = fr.f_back return stack
def analyse(exctyp, value, tb): import tokenize, keyword trace = StringIO() nlines = 3 frecs = inspect.getinnerframes(tb, nlines) trace.write("Traceback (most recent call last):\n") for frame, fname, lineno, funcname, context, cindex in frecs: trace.write(' File "%s", line %d, ' % (fname, lineno)) args, varargs, varkw, lcls = inspect.getargvalues(frame) def readline(lno=[lineno], *args): if args: print(args) try: return linecache.getline(fname, lno[0]) finally: lno[0] += 1 all, prev, name, scope = {}, None, "", None for ttype, tstr, stup, etup, line in tokenize.generate_tokens(readline): if ttype == tokenize.NAME and tstr not in keyword.kwlist: if name: if name[-1] == ".": try: val = getattr(prev, tstr) except AttributeError: # XXX skip the rest of this identifier only break name += tstr else: assert not name and not scope scope, val = lookup(tstr, frame, lcls) name = tstr if hasattr(val, "shape") and len(val): prev = val elif val: prev = val # print ' found', scope, 'name', name, 'val', val, 'in', prev, 'for token', tstr elif tstr == ".": if prev: name += "." else: if name: all[name] = (scope, prev) prev, name, scope = None, "", None if ttype == tokenize.NEWLINE: break trace.write( funcname + inspect.formatargvalues(args, varargs, varkw, lcls, formatvalue=lambda v: "=" + pydoc.text.repr(v)) + "\n" ) trace.write("".join([" " + x.replace("\t", " ") for x in [a for a in context if a.strip()]])) if len(all): trace.write(" variables: %s\n" % str(all)) trace.write("%s: %s" % (exctyp.__name__, value)) return trace
def _get_frame_args(frame): """Get the formatted arguments and class (if available) for a frame""" arginfo = inspect.getargvalues(frame) try: if not arginfo.args: return '', None # There have been reports from the field of python 2.6 which doesn't # return a namedtuple here but simply a tuple so fallback gracefully if # args isn't present. except AttributeError: return '', None firstarg = arginfo.args[0] if firstarg == 'self': self = arginfo.locals['self'] cls = self.__class__.__name__ arginfo.args.pop(0) del arginfo.locals['self'] else: cls = None formatted = inspect.formatargvalues(*arginfo) return formatted, cls
def _get_args_from_frame(frames, frame_num): if len(frames) > frame_num and frames[frame_num] and frames[frame_num][0]: argvalues = inspect.getargvalues(frames[frame_num][0]) formated_args = inspect.formatargvalues(*argvalues) # remove the first 'self' arg from the log as it adds no information formated_args = re.sub(r'\(self=.*?, ', "(", formated_args) return formated_args
def autolog(message=None, level=logging.DEBUG): func = inspect.currentframe().f_back.f_code frame = inspect.stack()[1] values = inspect.formatargvalues(*inspect.getargvalues(frame[0])) module = inspect.getmodule(frame[0]) logger = logging.getLogger(module.__name__) logger.log(level, '%s [%s] : %s' % (func.co_name, values, message))
def test_frame(self): args, varargs, varkw, locals = inspect.getargvalues(mod.fr) self.assertEqual(args, ["x", "y"]) self.assertEqual(varargs, None) self.assertEqual(varkw, None) self.assertEqual(locals, {"x": 11, "p": 11, "y": 14}) self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), "(x=11, y=14)")
def format_args(self, args): """Format the arguments""" #strip off surrounding parens, limit to 500 characters try: return inspect.formatargvalues(*args)[1:-1][:500] except Exception: return "<ERROR>"
def wrapped(*args, **kwargs): name = f.__module__ logger = logging.getLogger(name) level = logging.DEBUG s = inspect.currentframe().f_back to_print = ["\t%s:%s %s. Args: args=%s, kwargs=%s" % (pathname, linenum, func_name, args, kwargs)] while s: if True or s.f_globals["__name__"].startswith("synapse"): filename, lineno, function, _, _ = inspect.getframeinfo(s) args_string = inspect.formatargvalues(*inspect.getargvalues(s)) to_print.append("\t%s:%d %s. Args: %s" % (filename, lineno, function, args_string)) s = s.f_back msg = "\nTraceback for %s:\n" % (func_name,) + "\n".join(to_print) record = logging.LogRecord( name=name, level=level, pathname=pathname, lineno=lineno, msg=msg, args=None, exc_info=None ) logger.handle(record) return f(*args, **kwargs)
def log_func(): def kv_to_str(k, v): return str(k) + ", " + str(v) # get the stack frame of the previous function frame = inspect.stack()[1] # function name func_name = frame[3] # convert argument dict to a comma separated string arg_info = inspect.getargvalues(frame[0]) # remove 'self' argument if 'self' in arg_info.args: arg_info.args.remove('self') del arg_info.locals['self'] # remove 'cls' argument if 'cls' in arg_info.args: arg_info.args.remove('cls') del arg_info.locals['cls'] # format the args args = inspect.formatargvalues(*arg_info) logging.info("%s%s" % (func_name, args))
def analyze (self, exctyp, value, tb): """ Analyzes the exception into a human readable stack trace """ import tokenize, keyword trace = StringIO() nlines = 3 frecs = inspect.getinnerframes (tb, nlines) trace.write ('Traceback (most recent call last):\n') for frame, fname, lineno, funcname, context, cindex in frecs: trace.write (' File "%s", line %d, ' % (fname, lineno)) args, varargs, varkw, lcls = inspect.getargvalues (frame) def readline (lno=[lineno], *args): if args: print args try: return linecache.getline (fname, lno[0]) finally: lno[0] += 1 all, prev, name, scope = {}, None, '', None for ttype, tstr, stup, etup, line in tokenize.generate_tokens (readline): if ttype == tokenize.NAME and tstr not in keyword.kwlist: if name: if name[-1] == '.': try: val = getattr (prev, tstr) except AttributeError: # XXX skip the rest of this identifier only break name += tstr else: assert not name and not scope scope, val = self.lookup(tstr, frame, lcls) name = tstr if val is not None: prev = val elif tstr == '.': if prev: name += '.' else: if name: all[name] = (scope, prev) prev, name, scope = None, '', None if ttype == tokenize.NEWLINE: break try: details = inspect.formatargvalues (args, varargs, varkw, lcls, formatvalue=lambda v: '=' + pydoc.text.repr (v)) except: # seen that one on Windows (actual exception was KeyError: self) details = '(no details)' trace.write (funcname + details + '\n') if context is None: context = ['<source context missing>\n'] trace.write (''.join ([' ' + x.replace ('\t', ' ') for x in filter (lambda a: a.strip(), context)])) if len (all): trace.write (' variables: %s\n' % str (all)) trace.write ('%s: %s' % (exctyp.__name__, value)) return trace
def test_previous_frame(self): args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back) self.assertEqual(args, ["a", "b", "c", "d", ["e", ["f"]]]) self.assertEqual(varargs, "g") self.assertEqual(varkw, "h") self.assertEqual( inspect.formatargvalues(args, varargs, varkw, locals), "(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})" )
def test_frame(self): args, varargs, varkw, locals = inspect.getargvalues(mod.fr) self.assertEqual(args, ['x', 'y']) self.assertEqual(varargs, None) self.assertEqual(varkw, None) self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14}) self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), '(x=11, y=14)')
def text(einfo, context = 5): """Return a plain text document describing a given traceback.""" etype, evalue, etb = einfo if type(etype) is types.ClassType: etype = etype.__name__ pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable date = time.ctime(time.time()) head = '%s\n%s\n%s\n' % (str(etype), pyver, date) + '\nA problem occurred in a Python script. Here is the sequence of\nfunction calls leading up to the error, in the order they occurred.\n' frames = [] records = inspect.getinnerframes(etb, context) for frame, file, lnum, func, lines, index in records: file = file and os.path.abspath(file) or '?' args, varargs, varkw, locals = inspect.getargvalues(frame) call = '' if func != '?': call = 'in ' + func + inspect.formatargvalues(args, varargs, varkw, locals, formatvalue=lambda value: '=' + pydoc.text.repr(value)) highlight = {} def reader(lnum = [lnum]): highlight[lnum[0]] = 1 try: return linecache.getline(file, lnum[0]) finally: lnum[0] += 1 vars = scanvars(reader, frame, locals) rows = [' %s %s' % (file, call)] if index is not None: i = lnum - index for line in lines: num = '%5d ' % i rows.append(num + line.rstrip()) i += 1 done, dump = {}, [] for name, where, value in vars: if name in done: continue done[name] = 1 if value is not __UNDEF__: if where == 'global': name = 'global ' + name elif where != 'local': name = where + name.split('.')[-1] dump.append('%s = %s' % (name, pydoc.text.repr(value))) else: dump.append(name + ' undefined') rows.append('\n'.join(dump)) frames.append('\n%s\n' % '\n'.join(rows)) exception = ['%s: %s' % (str(etype), str(evalue))] if isinstance(evalue, BaseException): for name in dir(evalue): value = pydoc.text.repr(getattr(evalue, name)) exception.append('\n%s%s = %s' % (' ', name, value)) return head + ''.join(frames) + ''.join(exception) + '\n\nThe above is a description of an error in a Python program. Here is\nthe original traceback:\n\n%s\n' % ''.join(traceback.format_exception(etype, evalue, etb))
def do_args(self, frame, name): shortname = name if not self.ar: return self.out_result(name, shortname) from inspect import getargvalues from inspect import formatargvalues values = getargvalues(frame) global Nope Nope = False def formatvarargs(data): #print "data1 = %s" % data return str(data) def formatarg(data): global Nope #print "data2 = %s" % data if str(data) == "self": Nope = True return "" return "%s=" % data def formatvarkw(data): #print "data3 = %s" % data return str(data) def joinme(data, data1): #print "data4 = %s%s" % (str(data), str(data1)) pass def formatvalue(data): global Nope #print "data5 = %s" % data if Nope: Nope = False return "" # remove address infor from funtion name data = str(data) res = data.find(" at 0x") if not res == -1: data = data[:res] data = data.replace("<function ","") return data fout = formatargvalues(values.args, values.varargs, values.keywords, values.locals, formatarg, formatvarargs, formatvarkw, formatvalue, joinme) name = name.replace("()", "") s = name + fout return self.out_result(s, shortname)
def analyse(exctyp, value, tb): import tokenize, keyword trace = StringIO() nlines = 3 frecs = inspect.getinnerframes(tb, nlines) trace.write('Traceback (most recent call last):\n') for frame, fname, lineno, funcname, context, cindex in frecs: trace.write(' File "%s", line %d, ' % (fname, lineno)) args, varargs, varkw, lcls = inspect.getargvalues(frame) def readline(lno=[lineno], *args): if args: print args try: return linecache.getline(fname, lno[0]) finally: lno[0] += 1 all, prev, name, scope = {}, None, '', None for ttype, tstr, stup, etup, line in tokenize.generate_tokens(readline): if ttype == tokenize.NAME and tstr not in keyword.kwlist: if name: if name[-1] == '.': try: val = getattr(prev, tstr) except AttributeError: # XXX skip the rest of this identifier only break name += tstr else: assert not name and not scope scope, val = lookup(tstr, frame, lcls) name = tstr try: if val: prev = val except: pass #print ' found', scope, 'name', name, 'val', val, 'in', prev, 'for token', tstr elif tstr == '.': if prev: name += '.' else: if name: all[name] = (scope, prev) prev, name, scope = None, '', None if ttype == tokenize.NEWLINE: break trace.write(funcname + inspect.formatargvalues(args, varargs, varkw, lcls, formatvalue=lambda v: '=' + pydoc.text.repr(v)) + '\n') trace.write(''.join([' ' + x.replace('\t', ' ') for x in filter(lambda a: a.strip(), context)])) if len(all): trace.write(' variables: %s\n' % pformat(all, indent=3)) trace.write('%s: %s' % (exctyp.__name__, value)) return trace
def traceit(frame, event, arg): if event == "call": stack = traceback.extract_stack(frame) filename, linenumber, funcname, text = stack[-1] args = inspect.getargvalues(frame) indent = len(stack) * " " print "%s%s%s" % (indent, funcname, inspect.formatargvalues(*args)) return traceit
def formatArguments(self): """ Return formated arguments list """ if self.func == '?': return '' def formatValue(value): return '=' + self.formatter.repr(value) args, varargs, varkw, locals = inspect.getargvalues(self.frame) return inspect.formatargvalues(args, varargs, varkw, locals, formatvalue=formatValue)
def trace_info(self): """Get generator that exception stack trace info of validator. This generator will generate info about the validator or the rule(subclass of :class:`~structures.StructureRule`) that exception is raised. The type of info is :class:`dict`, and keys are: classname Class name of validator. args Validator's call arguments info (format by :func:`inspect.formatargvalues`). value Validatee value. validator Validator object. .. note:: The info is generated in order from the caller. :return: Generator object. """ frame_records = inspect.trace() processed = set() for frame_record in frame_records: arginfo = inspect.getargvalues(frame_record[0]) local = arginfo[3] if len(arginfo[0]) < 1: continue validator = local[arginfo[0][0]] # maybe validator's self expected_types = [ValidatorBaseInterface] try: from structures import StructureRule except: pass else: expected_types.append(StructureRule) if not isinstance(validator, tuple(expected_types)): continue if id(validator) in processed: continue info_dict = { 'classname': validator.__class__.__name__, 'args': inspect.formatargvalues(*arginfo), 'value': local[arginfo[0][1]], 'validator': validator } yield info_dict processed.add(id(validator))
def logcall(): """Performs a logging.debug line with the current function stack's function name with arguments. Useful as a sort of inline tracing. Use noisy() to ensure the visibility of these logging calls.""" logging.debug("%s%s" % ( sys._getframe().f_back.f_code.co_name, inspect.formatargvalues( *inspect.getargvalues( sys._getframe().f_back ) ) ))
def analyse (exctyp, value, tb): import tokenize, keyword trace = StringIO() nlines = 1 frecs = inspect.getinnerframes (tb, nlines) trace.write ('Variables:\n') for frame, fname, lineno, funcname, _context, _cindex in frecs: trace.write (' File "%s", line %d, ' % (fname, lineno)) args, varargs, varkw, lcls = inspect.getargvalues (frame) def readline (lno=[lineno], *args): if args: print args try: return linecache.getline (fname, lno[0]) finally: lno[0] += 1 all, prev, name, scope = {}, None, '', None for ttype, tstr, _stup, _etup, _line in tokenize.generate_tokens (readline): if ttype == tokenize.NAME and tstr not in keyword.kwlist: if name: if name[-1] == '.': try: val = getattr (prev, tstr) except AttributeError: # XXX skip the rest of this identifier only break name += tstr else: assert not name and not scope scope, val = lookup (tstr, frame, lcls) name = tstr if val is not None: prev = val #print ' found', scope, 'name', name, 'val', val, 'in', prev, 'for token', tstr elif tstr == '.': if prev: name += '.' else: if name: all[name] = prev prev, name, scope = None, '', None if ttype == tokenize.NEWLINE: break trace.write (funcname + inspect.formatargvalues (args, varargs, varkw, lcls, formatvalue=lambda v: '=' + safe_repr (v)) + '\n') if len (all): trace.write (' %s\n' % str (all)) trace.write('\n') traceback.print_exception (exctyp, value, tb, None, trace) return trace.getvalue()
def _function_data(fn, fn_args, fn_kwargs): func_code = fn.func_code line_number = func_code.co_firstlineno arg_names = func_code.co_varnames arg_locals = dict(zip(arg_names, fn_args)) arg_locals.update(fn_kwargs) arg_names = tuple(set(arg_names) & set(arg_locals.keys())) args = inspect.formatargvalues(arg_names, None, None, arg_locals) return fn.__name__, func_code.co_filename, line_number, args
def stack_trace(): stack = inspect.stack() str = "" for frame in stack[1:]: (frame_obj, filename, line, funcname, context, context_index) = frame try: args = inspect.formatargvalues(*inspect.getargvalues(frame_obj)) except Exception: args = "<unavailable>" frame_desc = "%s(%s)\t\t%s(%s)\n" % (filename, line, funcname, args) str += frame_desc return str
def _format_args(self, frame): """ Extract and format function/method parameters from frame. :param frame: Frame object :returns: string representing function args, like 'a=5, b=0' """ (args, varargs, varkw, frame_locals) = inspect.getargvalues(frame) try: prettyargs = inspect.formatargvalues(args, varargs, varkw, frame_locals, formatvalue=self._format_value) except: #pylint: disable=bare-except prettyargs = '(?)' return prettyargs
def getargspec(func): """Get argument specifications""" try: func=func.im_func except: pass try: return inspect.formatargspec(*inspect.getargspec(func)).replace('self, ','')+'\n\n' except: pass try: return inspect.formatargvalues(*inspect.getargvalues(func)).replace('self, ','')+'\n\n' except: return ''
def text(self, etype, evalue, etb, context=5): """Return a nice text document describing the traceback.""" # some locals Colors = self.Colors # just a shorthand + quicker name lookup ColorsNormal = Colors.Normal # used a lot indent_size = 8 # we need some space to put line numbers before indent = ' ' * indent_size numbers_width = indent_size - 1 # leave space between numbers & code text_repr = pydoc.text.repr exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal) em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal) undefined = '%sundefined%s' % (Colors.em, ColorsNormal) # some internal-use functions def eqrepr(value, repr=text_repr): return '=%s' % repr(value) def nullrepr(value, repr=text_repr): return '' # meat of the code begins if type(etype) is types.ClassType: etype = etype.__name__ if self.long_header: # Header with the exception type, python version, and date pyver = 'Python ' + string.split( sys.version)[0] + ': ' + sys.executable date = time.ctime(time.time()) head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-' * 75, ColorsNormal, exc, ' ' * (75 - len(str(etype)) - len(pyver)), pyver, string.rjust(date, 75)) head += "\nA problem occured executing Python code. Here is the sequence of function"\ "\ncalls leading up to the error, with the most recent (innermost) call last." else: # Simplified header head = '%s%s%s\n%s%s' % ( Colors.topline, '-' * 75, ColorsNormal, exc, string.rjust('Traceback (most recent call last)', 75 - len(str(etype)))) frames = [] # Flush cache before calling inspect. This helps alleviate some of the # problems with python 2.3's inspect.py. linecache.checkcache() # Drop topmost frames if requested try: records = inspect.getinnerframes(etb, context)[self.tb_offset:] except: # FIXME: I've been getting many crash reports from python 2.3 # users, traceable to inspect.py. If I can find a small test-case # to reproduce this, I should either write a better workaround or # file a bug report against inspect (if that's the real problem). # So far, I haven't been able to find an isolated example to # reproduce the problem. inspect_error() traceback.print_exc(file=genutils.Term.cerr) info( '\nUnfortunately, your original traceback can not be constructed.\n' ) return '' # build some color string templates outside these nested loops tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal) tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ (Colors.vName, Colors.valEm, ColorsNormal) tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, Colors.vName, ColorsNormal) tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal) # now, loop over all records printing context and info abspath = os.path.abspath for frame, file, lnum, func, lines, index in records: #print '*** record:',file,lnum,func,lines,index # dbg try: file = file and abspath(file) or '?' except OSError: # if file is '<console>' or something not in the filesystem, # the abspath call will throw an OSError. Just ignore it and # keep the original file string. pass link = tpl_link % file try: args, varargs, varkw, locals = inspect.getargvalues(frame) except: # This can happen due to a bug in python2.3. We should be # able to remove this try/except when 2.4 becomes a # requirement. Bug details at http://python.org/sf/1005466 inspect_error() traceback.print_exc(file=genutils.Term.cerr) info("\nIPython's exception reporting continues...\n") if func == '?': call = '' else: # Decide whether to include variable details or not var_repr = self.include_vars and eqrepr or nullrepr try: call = tpl_call % ( func, inspect.formatargvalues( args, varargs, varkw, locals, formatvalue=var_repr)) except KeyError: # Very odd crash from inspect.formatargvalues(). The # scenario under which it appeared was a call to # view(array,scale) in NumTut.view.view(), where scale had # been defined as a scalar (it should be a tuple). Somehow # inspect messes up resolving the argument list of view() # and barfs out. At some point I should dig into this one # and file a bug report about it. inspect_error() traceback.print_exc(file=genutils.Term.cerr) info("\nIPython's exception reporting continues...\n") call = tpl_call_fail % func # Initialize a list of names on the current line, which the # tokenizer below will populate. names = [] def tokeneater(token_type, token, start, end, line): """Stateful tokeneater which builds dotted names. The list of names it appends to (from the enclosing scope) can contain repeated composite names. This is unavoidable, since there is no way to disambguate partial dotted structures until the full list is known. The caller is responsible for pruning the final list of duplicates before using it.""" # build composite names if token == '.': try: names[-1] += '.' # store state so the next token is added for x.y.z names tokeneater.name_cont = True return except IndexError: pass if token_type == tokenize.NAME and token not in keyword.kwlist: if tokeneater.name_cont: # Dotted names names[-1] += token tokeneater.name_cont = False else: # Regular new names. We append everything, the caller # will be responsible for pruning the list later. It's # very tricky to try to prune as we go, b/c composite # names can fool us. The pruning at the end is easy # to do (or the caller can print a list with repeated # names if so desired. names.append(token) elif token_type == tokenize.NEWLINE: raise IndexError # we need to store a bit of state in the tokenizer to build # dotted names tokeneater.name_cont = False def linereader(file=file, lnum=[lnum], getline=linecache.getline): line = getline(file, lnum[0]) lnum[0] += 1 return line # Build the list of names on this line of code where the exception # occurred. try: # This builds the names list in-place by capturing it from the # enclosing scope. tokenize.tokenize(linereader, tokeneater) except IndexError: # signals exit of tokenizer pass except tokenize.TokenError, msg: _m = ("An unexpected error occurred while tokenizing input\n" "The following traceback may be corrupted or invalid\n" "The error message is: %s\n" % msg) error(_m) # prune names list of duplicates, but keep the right order unique_names = uniq_stable(names) # Start loop over vars lvals = [] if self.include_vars: for name_full in unique_names: name_base = name_full.split('.', 1)[0] if name_base in frame.f_code.co_varnames: if locals.has_key(name_base): try: value = repr(eval(name_full, locals)) except: value = undefined else: value = undefined name = tpl_local_var % name_full else: if frame.f_globals.has_key(name_base): try: value = repr(eval(name_full, frame.f_globals)) except: value = undefined else: value = undefined name = tpl_global_var % name_full lvals.append(tpl_name_val % (name, value)) if lvals: lvals = '%s%s' % (indent, em_normal.join(lvals)) else: lvals = '' level = '%s %s\n' % (link, call) excerpt = [] if index is not None: i = lnum - index for line in lines: if i == lnum: # This is the line with the error pad = numbers_width - len(str(i)) if pad >= 3: marker = '-' * (pad - 3) + '-> ' elif pad == 2: marker = '> ' elif pad == 1: marker = '>' else: marker = '' num = '%s%s' % (marker, i) line = tpl_line_em % (num, line) else: num = '%*s' % (numbers_width, i) line = tpl_line % (num, line) excerpt.append(line) if self.include_vars and i == lnum: excerpt.append('%s\n' % lvals) i += 1 frames.append('%s%s' % (level, ''.join(excerpt)))
def analyse(exctyp, value, tb): import tokenize, keyword trace = StringIO() nlines = 3 frecs = inspect.getinnerframes(tb, nlines) trace.write('Traceback (most recent call last):\n') for frame, fname, lineno, funcname, context, cindex in frecs: trace.write(' File "%s", line %d, ' % (fname, lineno)) args, varargs, varkw, lcls = inspect.getargvalues(frame) def readline(lno=[lineno], *args): if args: print args try: return linecache.getline(fname, lno[0]) finally: lno[0] += 1 all, prev, name, scope = {}, None, '', None for ttype, tstr, stup, etup, line in tokenize.generate_tokens( readline): if ttype == tokenize.NAME and tstr not in keyword.kwlist: if name: if name[-1] == '.': try: val = getattr(prev, tstr) except AttributeError: # XXX skip the rest of this identifier only break name += tstr else: assert not name and not scope scope, val = lookup(tstr, frame, lcls) name = tstr if val: prev = val #print ' found', scope, 'name', name, 'val', val, 'in', prev, 'for token', tstr elif tstr == '.': if prev: name += '.' else: if name: all[name] = (scope, prev) prev, name, scope = None, '', None if ttype == tokenize.NEWLINE: break trace.write(funcname + inspect.formatargvalues( args, varargs, varkw, lcls, formatvalue=lambda v: '=' + pydoc.text.repr(v)) + '\n') trace.write(''.join([ ' ' + x.replace('\t', ' ') for x in filter(lambda a: a.strip(), context) ])) if len(all): trace.write(' variables: %s\n' % str(all)) trace.write('%s: %s' % (exctyp.__name__, value)) return trace
def text(self, etype, evalue, etb, context=5): """Return a nice text document describing the traceback.""" import sys, os, types, string, time, traceback import keyword, tokenize, linecache, inspect, pydoc if type(etype) is types.ClassType: etype = etype.__name__ # Header with the exception type, python version, and date pyver = 'Python ' + string.split( sys.version)[0] + ': ' + sys.executable date = time.ctime(time.time()) exc = "%s%s%s" % (Colors.excNameColor, str(etype), Colors.Normal) head = '%s%s%s\n%s%s%s\n%s' % ( Colors.toplineColor, '-' * 75, Colors.Normal, exc, ' ' * (75 - len(str(etype)) - len(pyver)), pyver, string.rjust(date, 75)) head += "\nA problem occured in a Python script. Here is the sequence of function"\ "\ncalls leading up to the error, with the most recent (innermost) call last." indent = ' ' * 6 frames = [] records = inspect.getinnerframes(etb, context) for frame, file, lnum, func, lines, index in records: file = file and os.path.abspath(file) or '?' link = Colors.filenameColorEm + file + Colors.Normal args, varargs, varkw, locals = inspect.getargvalues(frame) if func == '?': call = '' else: def eqrepr(value, repr=pydoc.text.repr): return '=' + repr(value) call = 'in %s%s%s%s%s' % ( Colors.vNameColor, func, Colors.valColorEm, inspect.formatargvalues( args, varargs, varkw, locals, formatvalue=eqrepr), Colors.Normal) names = [] def tokeneater(type, token, start, end, line, names=names, kwlist=keyword.kwlist, NAME=tokenize.NAME, NEWLINE=tokenize.NEWLINE): if type == NAME and token not in kwlist: if token not in names: names.append(token) if type == NEWLINE: raise IndexError def linereader(file=file, lnum=[lnum], getline=linecache.getline): line = getline(file, lnum[0]) lnum[0] = lnum[0] + 1 return line try: tokenize.tokenize(linereader, tokeneater) except IndexError: pass lvals = [] for name in names: if name in frame.f_code.co_varnames: if locals.has_key(name): value = pydoc.text.repr(locals[name]) else: value = '%sundefined%s' % (Colors.emColor, Colors.Normal) name = '%s%s%s' % (Colors.vNameColor, name, Colors.Normal) else: if frame.f_globals.has_key(name): value = pydoc.text.repr(frame.f_globals[name]) else: value = '%sundefined%s' % (Colors.emColor, Colors.Normal) name = '%sglobal%s %s%s%s' % ( Colors.emColor, Colors.Normal, Colors.vNameColor, name, Colors.Normal) lvals.append('%s %s= %s%s' % (name, Colors.valColorEm, value, Colors.Normal)) if lvals: lvals = string.join( lvals, '%s,%s ' % (Colors.valColorEm, Colors.Normal)) lvals = indent + lvals else: lvals = '' level = link + ' ' + call + '\n' excerpt = [] if index is not None: i = lnum - index for line in lines: num = ' ' * (5 - len(str(i))) + str(i) if i == lnum: # This is the line with the error line = '%s%s%s %s%s' % (Colors.linenoColorEm, num, Colors.lineColor, line, Colors.Normal) else: line = '%s%s%s %s' % (Colors.linenoColor, num, Colors.Normal, line) excerpt.append(line) if i == lnum: excerpt.append(lvals + '\n') i = i + 1 frames.append(level + string.join(excerpt, '')) exception = [ '%s%s%s: %s' % (Colors.excNameColor, str(etype), Colors.Normal, str(evalue)) ] if type(evalue) is types.InstanceType: for name in dir(evalue): value = pydoc.text.repr(getattr(evalue, name)) exception.append('\n%s%s = %s' % (indent, name, value)) return head + '\n\n' + string.join(frames, '\n') + '\n' + \ string.join(exception, '')
def get_snapshot(exception, context=10): """ Return a dict describing a given traceback (based on cgitb.text) """ etype, evalue, etb = sys.exc_info() if isinstance(etype, six.class_types): etype = etype.__name__ # creates a snapshot dict with some basic information s = { 'pyver': 'Python {version:s}: {executable:s} (prefix: {prefix:s})'.format( version=sys.version.split()[0], executable=sys.executable, prefix=sys.prefix), 'timestamp': cstr(datetime.datetime.now()), 'traceback': traceback.format_exc(), 'frames': [], 'etype': cstr(etype), 'evalue': cstr(repr(evalue)), 'exception': {}, 'locals': {} } # start to process frames records = inspect.getinnerframes(etb, 5) for frame, file, lnum, func, lines, index in records: file = file and os.path.abspath(file) or '?' args, varargs, varkw, locals = inspect.getargvalues(frame) call = '' if func != '?': call = inspect.formatargvalues( args, varargs, varkw, locals, formatvalue=lambda value: '={}'.format(pydoc.text.repr(value))) # basic frame information f = { 'file': file, 'func': func, 'call': call, 'lines': {}, 'lnum': lnum } def reader(lnum=[lnum]): try: return linecache.getline(file, lnum[0]) finally: lnum[0] += 1 vars = cgitb.scanvars(reader, frame, locals) # if it is a view, replace with generated code # if file.endswith('html'): # lmin = lnum > context and (lnum - context) or 0 # lmax = lnum + context # lines = code.split("\n")[lmin:lmax] # index = min(context, lnum) - 1 if index is not None: i = lnum - index for line in lines: f['lines'][i] = line.rstrip() i += 1 # dump local variable (referenced in current line only) f['dump'] = {} for name, where, value in vars: if name in f['dump']: continue if value is not cgitb.__UNDEF__: if where == 'global': name = 'global {name:s}'.format(name=name) elif where != 'local': name = where + ' ' + name.split('.')[-1] f['dump'][name] = pydoc.text.repr(value) else: f['dump'][name] = 'undefined' s['frames'].append(f) # add exception type, value and attributes if isinstance(evalue, BaseException): for name in dir(evalue): # prevent py26 DeprecationWarning if (name != 'messages' or sys.version_info < (2.6)) and not name.startswith('__'): value = pydoc.text.repr(getattr(evalue, name)) # render multilingual string properly if type(value) == str and value.startswith(b"u'"): value = eval(value) s['exception'][name] = encode(value) # add all local values (of last frame) to the snapshot for name, value in locals.items(): if type(value) == str and value.startswith(b"u'"): value = eval(value) s['locals'][name] = pydoc.text.repr(value) return s
def localize(event, waveform='o2-uberbank', f_low=30.0, min_distance=None, max_distance=None, prior_distance_power=None, cosmology=False, method='toa_phoa_snr', nside=-1, chain_dump=None, enable_snr_series=True, f_high_truncate=0.95): """Convenience function to produce a sky map from LIGO-LW rows. Note that min_distance and max_distance should be in Mpc. Returns a 'NESTED' ordering HEALPix image as a Numpy array. """ frame = inspect.currentframe() argstr = inspect.formatargvalues(*inspect.getargvalues(frame)) start_time = lal.GPSTimeNow() singles = event.singles if not enable_snr_series: singles = [single for single in singles if single.snr is not None] ifos = [single.detector for single in singles] # Extract SNRs from table. snrs = np.ma.asarray([ np.ma.masked if single.snr is None else single.snr for single in singles ]) # Look up physical parameters for detector. detectors = [ lalsimulation.DetectorPrefixToLALDetector(str(ifo)) for ifo in ifos ] responses = np.asarray([det.response for det in detectors]) locations = np.asarray([det.location for det in detectors]) # Power spectra for each detector. psds = [single.psd for single in singles] psds = [ timing.InterpolatedPSD(filter.abscissa(psd), psd.data.data, f_high_truncate=f_high_truncate) for psd in psds ] log.debug('calculating templates') H = filter.sngl_inspiral_psd(waveform, f_min=f_low, **event.template_args) log.debug('calculating noise PSDs') HS = [filter.signal_psd_series(H, S) for S in psds] # Signal models for each detector. log.debug('calculating Fisher matrix elements') signal_models = [timing.SignalModel(_) for _ in HS] # Get SNR=1 horizon distances for each detector. horizons = np.asarray([ signal_model.get_horizon_distance() for signal_model in signal_models ]) weights = np.ma.asarray([ 1 / np.square(signal_model.get_crb_toa_uncert(snr)) for signal_model, snr in zip(signal_models, snrs) ]) # Center detector array. locations -= np.sum(locations * weights.reshape(-1, 1), axis=0) / np.sum(weights) if cosmology: log.warn('Enabling cosmological prior. ' 'This feature is UNREVIEWED.') if enable_snr_series: log.warn('Enabling input of SNR time series. ' 'This feature is UNREVIEWED.') snr_series = [single.snr_series for single in singles] if all(s is None for s in snr_series): snr_series = None else: snr_series = None # Maximum barycentered arrival time error: # |distance from array barycenter to furthest detector| / c + 5 ms. # For LHO+LLO, this is 15.0 ms. # For an arbitrary terrestrial detector network, the maximum is 26.3 ms. max_abs_t = np.max(np.sqrt(np.sum(np.square(locations / lal.C_SI), axis=1))) + 0.005 if snr_series is None: log.warn( "No SNR time series found, so we are creating a zero-noise " "SNR time series from the whitened template's autocorrelation " "sequence. The sky localization uncertainty may be " "underestimated.") acors, sample_rates = zip( *[filter.autocorrelation(_, max_abs_t) for _ in HS]) sample_rate = sample_rates[0] deltaT = 1 / sample_rate nsamples = len(acors[0]) assert all(sample_rate == _ for _ in sample_rates) assert all(nsamples == len(_) for _ in acors) nsamples = nsamples * 2 - 1 snr_series = [] for acor, single in zip(acors, singles): series = lal.CreateCOMPLEX8TimeSeries('fake SNR', 0, 0, deltaT, lal.StrainUnit, nsamples) series.epoch = single.time - 0.5 * (nsamples - 1) * deltaT acor = np.concatenate((np.conj(acor[:0:-1]), acor)) series.data.data = single.snr * filter.exp_i(single.phase) * acor snr_series.append(series) # Ensure that all of the SNR time series have the same sample rate. # FIXME: for now, the Python wrapper expects all of the SNR time sries to # also be the same length. deltaT = snr_series[0].deltaT sample_rate = 1 / deltaT if any(deltaT != series.deltaT for series in snr_series): raise ValueError('BAYESTAR does not yet support SNR time series with ' 'mixed sample rates') # Ensure that all of the SNR time series have odd lengths. if any(len(series.data.data) % 2 == 0 for series in snr_series): raise ValueError('SNR time series must have odd lengths') # Trim time series to the desired length. max_abs_n = int(np.ceil(max_abs_t * sample_rate)) desired_length = 2 * max_abs_n - 1 for i, series in enumerate(snr_series): length = len(series.data.data) if length > desired_length: snr_series[i] = lal.CutCOMPLEX8TimeSeries( series, length // 2 + 1 - max_abs_n, desired_length) # FIXME: for now, the Python wrapper expects all of the SNR time sries to # also be the same length. nsamples = len(snr_series[0].data.data) if any(nsamples != len(series.data.data) for series in snr_series): raise ValueError('BAYESTAR does not yet support SNR time series of ' 'mixed lengths') # Perform sanity checks that the middle sample of the SNR time series match # the sngl_inspiral records. Relax valid interval slightly from # +/- 0.5 deltaT to +/- 0.6 deltaT for floating point roundoff error. for single, series in zip(singles, snr_series): if np.abs(0.5 * (nsamples - 1) * series.deltaT + float(series.epoch - single.time)) >= 0.6 * deltaT: raise ValueError('BAYESTAR expects the SNR time series to be ' 'centered on the single-detector trigger times') # Extract the TOAs in GPS nanoseconds from the SNR time series, assuming # that the trigger happened in the middle. toas_ns = [ series.epoch.ns() + 1e9 * 0.5 * (len(series.data.data) - 1) * series.deltaT for series in snr_series ] # Collect all of the SNR series in one array. snr_series = np.vstack([series.data.data for series in snr_series]) # Center times of arrival and compute GMST at mean arrival time. # Pre-center in integer nanoseconds to preserve precision of # initial datatype. epoch = sum(toas_ns) // len(toas_ns) toas = 1e-9 * (np.asarray(toas_ns) - epoch) # FIXME: np.average does not yet support masked arrays. # Replace with np.average when numpy 1.13.0 is available. mean_toa = np.sum(toas * weights) / np.sum(weights) toas -= mean_toa epoch += int(np.round(1e9 * mean_toa)) epoch = lal.LIGOTimeGPS(0, int(epoch)) gmst = lal.GreenwichMeanSiderealTime(epoch) # Translate SNR time series back to time of first sample. toas -= 0.5 * (nsamples - 1) * deltaT # If minimum distance is not specified, then default to 0 Mpc. if min_distance is None: min_distance = 0 # If maximum distance is not specified, then default to the SNR=4 # horizon distance of the most sensitive detector. if max_distance is None: max_distance = max(horizons) / 4 # If prior_distance_power is not specified, then default to 2 # (p(r) ~ r^2, uniform in volume). if prior_distance_power is None: prior_distance_power = 2 # Raise an exception if 0 Mpc is the minimum effective distance and the # prior is of the form r**k for k<0 if min_distance == 0 and prior_distance_power < 0: raise ValueError( ('Prior is a power law r^k with k={}, ' 'undefined at min_distance=0').format(prior_distance_power)) # Time and run sky localization. log.debug('starting computationally-intensive section') if method == 'toa_phoa_snr': skymap, log_bci, log_bsn = _sky_map.toa_phoa_snr( min_distance, max_distance, prior_distance_power, cosmology, gmst, sample_rate, toas, snr_series, responses, locations, horizons) skymap = Table(skymap) skymap.meta['log_bci'] = log_bci skymap.meta['log_bsn'] = log_bsn elif method == 'toa_phoa_snr_mcmc': skymap = localize_emcee( logl=_sky_map.log_likelihood_toa_phoa_snr, loglargs=(gmst, sample_rate, toas, snr_series, responses, locations, horizons), logp=toa_phoa_snr_log_prior, logpargs=(min_distance, max_distance, prior_distance_power, max_abs_t), xmin=[0, -1, min_distance, -1, 0, 0], xmax=[2 * np.pi, 1, max_distance, 1, 2 * np.pi, 2 * max_abs_t], nside=nside, chain_dump=chain_dump) else: raise ValueError('Unrecognized method: %s' % method) # Convert distance moments to parameters distmean = skymap.columns.pop('DISTMEAN') diststd = skymap.columns.pop('DISTSTD') skymap['DISTMU'], skymap['DISTSIGMA'], skymap['DISTNORM'] = \ distance.moments_to_parameters(distmean, diststd) # Add marginal distance moments good = np.isfinite(distmean) & np.isfinite(diststd) prob = (moc.uniq2pixarea(skymap['UNIQ']) * skymap['PROBDENSITY'])[good] distmean = distmean[good] diststd = diststd[good] rbar = (prob * distmean).sum() r2bar = (prob * (np.square(diststd) + np.square(distmean))).sum() skymap.meta['distmean'] = rbar skymap.meta['diststd'] = np.sqrt(r2bar - np.square(rbar)) log.debug('finished computationally-intensive section') end_time = lal.GPSTimeNow() # Fill in metadata and return. program, _ = os.path.splitext(os.path.basename(sys.argv[0])) skymap.meta['creator'] = 'BAYESTAR' skymap.meta['origin'] = 'LIGO/Virgo' skymap.meta['vcs_info'] = vcs_info skymap.meta['gps_time'] = float(epoch) skymap.meta['runtime'] = float(end_time - start_time) skymap.meta['instruments'] = {single.detector for single in singles} skymap.meta['gps_creation_time'] = end_time skymap.meta['history'] = [ '', 'Generated by calling the following Python function:', '{}.{}{}'.format(__name__, frame.f_code.co_name, argstr), '', 'This was the command line that started the program:', ' '.join([program] + sys.argv[1:]) ] return skymap
def text(eparams, context=5): """Return a plain text document describing a given traceback.""" import os import types import time import traceback import linecache import inspect import pydoc etype, evalue, etb = eparams if isinstance(etype, types.ClassType): etype = etype.__name__ pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable date = time.ctime(time.time()) head = "%s\n%s\n%s\n" % (str(etype), pyver, date) + ''' A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred. ''' frames = [] records = inspect.getinnerframes(etb, context) for frame, file, lnum, func, lines, index in records: file = file and os.path.abspath(file) or '?' args, varargs, varkw, locals = inspect.getargvalues(frame) call = '' if func != '?': call = 'in ' + func + \ inspect.formatargvalues(args, varargs, varkw, locals, formatvalue=lambda value: '=' + pydoc.text.repr(value)) highlight = {} def reader(lnum=[lnum]): highlight[lnum[0]] = 1 try: return linecache.getline(file, lnum[0]) finally: lnum[0] += 1 vars = scanvars(reader, frame, locals) rows = [' %s %s' % (file, call)] if index is not None: i = lnum - index for line in lines: num = '%5d ' % i rows.append(num + line.rstrip()) i += 1 done, dump = {}, [] for name, where, value in vars: if name in done: continue done[name] = 1 if value is not __UNDEF__: if where == 'global': name = 'global ' + name elif where == 'local': name = name else: name = where + name.split('.')[-1] dump.append('%s = %s' % (name, pydoc.text.repr(value))) else: dump.append(name + ' undefined') rows.append('\n'.join(dump)) frames.append('\n%s\n' % '\n'.join(rows)) exception = ['%s: %s' % (str(etype), str(evalue))] if isinstance(evalue, types.InstanceType): for name in dir(evalue): value = pydoc.text.repr(getattr(evalue, name)) exception.append('\n%s%s = %s' % (" " * 4, name, value)) import traceback return head + ''.join(frames) + ''.join(exception) + ''' The above is a description of an error in a Python program. Here is the original traceback: %s ''' % ''.join(traceback.format_exception(etype, evalue, etb))
def localize(event, waveform='o2-uberbank', f_low=30.0, min_inclination=0, max_inclination=np.pi / 2, min_distance=None, max_distance=None, prior_distance_power=None, cosmology=False, mcmc=False, chain_dump=None, enable_snr_series=True, f_high_truncate=0.95, rescale_loglikelihood=_RESCALE_LOGLIKELIHOOD): """Localize a compact binary signal using the BAYESTAR algorithm. Parameters ---------- event : `ligo.skymap.io.events.Event` The event candidate. waveform : str, optional The name of the waveform approximant. f_low : float, optional The low frequency cutoff. min_distance, max_distance : float, optional The limits of integration over luminosity distance, in Mpc (default: determine automatically from detector sensitivity). prior_distance_power : int, optional The power of distance that appears in the prior (default: 2, uniform in volume). cosmology: bool, optional Set to enable a uniform in comoving volume prior (default: false). mcmc : bool, optional Set to use MCMC sampling rather than more accurate Gaussian quadrature. chain_dump : str, optional Save posterior samples to this filename if `mcmc` is set. enable_snr_series : bool, optional Set to False to disable SNR time series. f_high_truncate : float, optional Truncate the noise power spectral densities at this factor times the highest sampled frequency to suppress artifacts caused by incorrect PSD conditioning by some matched filter pipelines. Returns ------- skymap : `astropy.table.Table` A 3D sky map in multi-order HEALPix format. """ # Hide event parameters, but show all other arguments def formatvalue(value): if isinstance(value, Event): return '=...' else: return '=' + repr(value) frame = inspect.currentframe() argstr = inspect.formatargvalues(*inspect.getargvalues(frame), formatvalue=formatvalue) stopwatch = Stopwatch() stopwatch.start() epoch, sample_rate, toas, snrs, responses, locations, horizons = \ condition(event, waveform=waveform, f_low=f_low, enable_snr_series=enable_snr_series, f_high_truncate=f_high_truncate) min_distance, max_distance, prior_distance_power, cosmology = \ condition_prior(horizons, min_distance, max_distance, prior_distance_power, cosmology) gmst = lal.GreenwichMeanSiderealTime(epoch) # Time and run sky localization. log.debug('starting computationally-intensive section') if mcmc: max_abs_t = 2 * snrs.data.shape[1] / sample_rate if min_inclination != 0 or max_inclination != np.pi / 2: log.warn('inclination limits are not supported for MCMC mode') args = (min_distance, max_distance, prior_distance_power, cosmology, gmst, sample_rate, toas, snrs, responses, locations, horizons, rescale_loglikelihood) skymap = localize_emcee( args=args, xmin=[0, -1, min_distance, -1, 0, 0], xmax=[2 * np.pi, 1, max_distance, 1, 2 * np.pi, 2 * max_abs_t], chain_dump=chain_dump) else: args = (min_inclination, max_inclination, min_distance, max_distance, prior_distance_power, cosmology, gmst, sample_rate, toas, snrs, responses, locations, horizons, rescale_loglikelihood) skymap, log_bci, log_bsn = core.toa_phoa_snr(*args) skymap = Table(skymap, copy=False) skymap.meta['log_bci'] = log_bci skymap.meta['log_bsn'] = log_bsn # Convert distance moments to parameters try: distmean = skymap.columns.pop('DISTMEAN') diststd = skymap.columns.pop('DISTSTD') except KeyError: distmean, diststd, _ = distance.parameters_to_moments( skymap['DISTMU'], skymap['DISTSIGMA']) else: skymap['DISTMU'], skymap['DISTSIGMA'], skymap['DISTNORM'] = \ distance.moments_to_parameters(distmean, diststd) # Add marginal distance moments good = np.isfinite(distmean) & np.isfinite(diststd) prob = (moc.uniq2pixarea(skymap['UNIQ']) * skymap['PROBDENSITY'])[good] distmean = distmean[good] diststd = diststd[good] rbar = (prob * distmean).sum() r2bar = (prob * (np.square(diststd) + np.square(distmean))).sum() skymap.meta['distmean'] = rbar skymap.meta['diststd'] = np.sqrt(r2bar - np.square(rbar)) stopwatch.stop() end_time = lal.GPSTimeNow() log.info('finished computationally-intensive section in %s', stopwatch) # Fill in metadata and return. program, _ = os.path.splitext(os.path.basename(sys.argv[0])) skymap.meta.update(metadata_for_version_module(version)) skymap.meta['creator'] = 'BAYESTAR' skymap.meta['origin'] = 'LIGO/Virgo' skymap.meta['gps_time'] = float(epoch) skymap.meta['runtime'] = stopwatch.real skymap.meta['instruments'] = {single.detector for single in event.singles} skymap.meta['gps_creation_time'] = end_time skymap.meta['history'] = [ '', 'Generated by calling the following Python function:', *wrap('{}.{}{}'.format(__name__, frame.f_code.co_name, argstr), 72), '', 'This was the command line that started the program:', *wrap(' '.join([program] + sys.argv[1:]), 72) ] return skymap
def snapshot(info=None, context=5, code=None, environment=None): """Return a dict describing a given traceback (based on cgitb.text).""" import os, types, time, traceback, linecache, inspect, pydoc, cgitb # if no exception info given, get current: etype, evalue, etb = info or sys.exc_info() if type(etype) is types.ClassType: etype = etype.__name__ # create a snapshot dict with some basic information s = {} s['pyver'] = 'Python ' + sys.version.split()[0] + ': ' + sys.executable s['date'] = time.ctime(time.time()) # start to process frames records = inspect.getinnerframes(etb, context) s['frames'] = [] for frame, file, lnum, func, lines, index in records: file = file and os.path.abspath(file) or '?' args, varargs, varkw, locals = inspect.getargvalues(frame) call = '' if func != '?': call = inspect.formatargvalues(args, varargs, varkw, locals, formatvalue=lambda value: '=' + pydoc.text.repr(value)) # basic frame information f = {'file': file, 'func': func, 'call': call, 'lines': {}, 'lnum': lnum} highlight = {} def reader(lnum=[lnum]): highlight[lnum[0]] = 1 try: return linecache.getline(file, lnum[0]) finally: lnum[0] += 1 vars = cgitb.scanvars(reader, frame, locals) # if it is a view, replace with generated code if file.endswith('html'): lmin = lnum>context and (lnum-context) or 0 lmax = lnum+context lines = code.split("\n")[lmin:lmax] index = min(context, lnum) - 1 if index is not None: i = lnum - index for line in lines: f['lines'][i] = line.rstrip() i += 1 # dump local variables (referenced in current line only) f['dump'] = {} for name, where, value in vars: if name in f['dump']: continue if value is not cgitb.__UNDEF__: if where == 'global': name = 'global ' + name elif where != 'local': name = where + name.split('.')[-1] f['dump'][name] = pydoc.text.repr(value) else: f['dump'][name] = 'undefined' s['frames'].append(f) # add exception type, value and attributes s['etype'] = str(etype) s['evalue'] = str(evalue) s['exception'] = {} if isinstance(evalue, BaseException): for name in dir(evalue): # prevent py26 DeprecatedWarning: if name!='message' or sys.version_info<(2.6): value = pydoc.text.repr(getattr(evalue, name)) s['exception'][name] = value # add all local values (of last frame) to the snapshot s['locals'] = {} for name, value in locals.items(): s['locals'][name] = pydoc.text.repr(value) # add web2py environment variables for k,v in environment.items(): if k in ('request', 'response', 'session'): s[k] = {} for name, value in v.items(): s[k][name] = pydoc.text.repr(value) return s
def analyse(exctyp, value, tb): import tokenize import keyword import platform from lib.meta import MYPAINT_VERSION trace = StringIO() nlines = 3 frecs = inspect.getinnerframes(tb, nlines) trace.write('Mypaint version: %s\n' % MYPAINT_VERSION) trace.write('System information: %s\n' % platform.platform()) trace.write('Traceback (most recent call last):\n') for frame, fname, lineno, funcname, context, cindex in frecs: trace.write(' File "%s", line %d, ' % (fname, lineno)) args, varargs, varkw, lcls = inspect.getargvalues(frame) def readline(lno=[lineno], *args): if args: print args try: return linecache.getline(fname, lno[0]) finally: lno[0] += 1 all, prev, name, scope = {}, None, '', None for ttype, tstr, stup, etup, line in tokenize.generate_tokens( readline): if ttype == tokenize.NAME and tstr not in keyword.kwlist: if name: if name[-1] == '.': try: val = getattr(prev, tstr) except AttributeError: # XXX skip the rest of this identifier only break name += tstr else: assert not name and not scope scope, val = lookup(tstr, frame, lcls) name = tstr if val is not None: prev = val elif tstr == '.': if prev: name += '.' else: if name: all[name] = (scope, prev) prev, name, scope = None, '', None if ttype == tokenize.NEWLINE: break try: details = inspect.formatargvalues( args, varargs, varkw, lcls, formatvalue=lambda v: '=' + pydoc.text.repr(v)) except: # seen that one on Windows (actual exception was KeyError: self) details = '(no details)' trace.write(funcname + details + '\n') if context is None: context = ['<source context missing>\n'] trace.write(''.join([ ' ' + x.replace('\t', ' ') for x in filter(lambda a: a.strip(), context) ])) if len(all): trace.write(' variables: %s\n' % str(all)) trace.write('%s: %s' % (exctyp.__name__, value)) return trace
def html(einfo, context=5): (etype, evalue, etb) = einfo if isinstance(etype, type): etype = etype.__name__ pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable date = time.ctime(time.time()) head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading( '<big><big>%s</big></big>' % strong(pydoc.html.escape(str(etype))), '#ffffff', '#6622aa', pyver + '<br>' + date ) + '\n<p>A problem occurred in a Python script. Here is the sequence of\nfunction calls leading up to the error, in the order they occurred.</p>' indent = '<tt>' + small(' ' * 5) + ' </tt>' frames = [] records = inspect.getinnerframes(etb, context) for (frame, file, lnum, func, lines, index) in records: if file: file = os.path.abspath(file) link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file)) else: file = link = '?' (args, varargs, varkw, locals) = inspect.getargvalues(frame) call = '' if func != '?': call = 'in ' + strong(func) + inspect.formatargvalues( args, varargs, varkw, locals, formatvalue=lambda value: '=' + pydoc.html.repr(value)) highlight = {} def reader(lnum=[lnum]): highlight[lnum[0]] = 1 try: return linecache.getline(file, lnum[0]) finally: lnum[0] += 1 vars = scanvars(reader, frame, locals) rows = [ '<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' % ('<big> </big>', link, call) ] if index is not None: i = lnum - index for line in lines: num = small(' ' * (5 - len(str(i))) + str(i)) + ' ' if i in highlight: line = '<tt>=>%s%s</tt>' % (num, pydoc.html.preformat(line)) rows.append('<tr><td bgcolor="#ffccee">%s</td></tr>' % line) else: line = '<tt> %s%s</tt>' % ( num, pydoc.html.preformat(line)) rows.append('<tr><td>%s</td></tr>' % grey(line)) i += 1 (done, dump) = ({}, []) for (name, where, value) in vars: if name in done: pass done[name] = 1 if value is not __UNDEF__: if where in ('global', 'builtin'): name = '<em>%s</em> ' % where + strong(name) elif where == 'local': name = strong(name) else: name = where + strong(name.split('.')[-1]) dump.append('%s = %s' % (name, pydoc.html.repr(value))) else: dump.append(name + ' <em>undefined</em>') rows.append('<tr><td>%s</td></tr>' % small(grey(', '.join(dump)))) frames.append( '\n<table width="100%%" cellspacing=0 cellpadding=0 border=0>\n%s</table>' % '\n'.join(rows)) exception = [ '<p>%s: %s' % (strong(pydoc.html.escape(str(etype))), pydoc.html.escape(str(evalue))) ] for name in dir(evalue): if name[:1] == '_': pass value = pydoc.html.repr(getattr(evalue, name)) exception.append('\n<br>%s%s =\n%s' % (indent, name, value)) return head + ''.join(frames) + ''.join( exception ) + "\n\n\n<!-- The above is a description of an error in a Python program, formatted\n for a Web browser because the 'cgitb' module was enabled. In case you\n are not reading this in a Web browser, here is the original traceback:\n\n%s\n-->\n" % pydoc.html.escape( ''.join(traceback.format_exception(etype, evalue, etb)))
def format_records(records): # , print_globals=False): # Loop over all records printing context and info frames = [] abspath = os.path.abspath for frame, file, lnum, func, lines, index in records: try: file = file and abspath(file) or '?' except OSError: # if file is '<console>' or something not in the filesystem, # the abspath call will throw an OSError. Just ignore it and # keep the original file string. pass link = file try: args, varargs, varkw, locals = inspect.getargvalues(frame) except: # This can happen due to a bug in python2.3. We should be # able to remove this try/except when 2.4 becomes a # requirement. Bug details at http://python.org/sf/1005466 print("\nJoblib's exception reporting continues...\n") if func == '?': call = '' else: # Decide whether to include variable details or not try: call = 'in %s%s' % ( func, inspect.formatargvalues( args, varargs, varkw, locals, formatvalue=eq_repr)) except KeyError: # Very odd crash from inspect.formatargvalues(). The # scenario under which it appeared was a call to # view(array,scale) in NumTut.view.view(), where scale had # been defined as a scalar (it should be a tuple). Somehow # inspect messes up resolving the argument list of view() # and barfs out. At some point I should dig into this one # and file a bug report about it. print("\nJoblib's exception reporting continues...\n") call = 'in %s(***failed resolving arguments***)' % func # Initialize a list of names on the current line, which the # tokenizer below will populate. names = [] def tokeneater(token_type, token, start, end, line): """Stateful tokeneater which builds dotted names. The list of names it appends to (from the enclosing scope) can contain repeated composite names. This is unavoidable, since there is no way to disambiguate partial dotted structures until the full list is known. The caller is responsible for pruning the final list of duplicates before using it.""" # build composite names if token == '.': try: names[-1] += '.' # store state so the next token is added for x.y.z names tokeneater.name_cont = True return except IndexError: pass if token_type == tokenize.NAME and token not in keyword.kwlist: if tokeneater.name_cont: # Dotted names names[-1] += token tokeneater.name_cont = False else: # Regular new names. We append everything, the caller # will be responsible for pruning the list later. It's # very tricky to try to prune as we go, b/c composite # names can fool us. The pruning at the end is easy # to do (or the caller can print a list with repeated # names if so desired. names.append(token) elif token_type == tokenize.NEWLINE: raise IndexError # we need to store a bit of state in the tokenizer to build # dotted names tokeneater.name_cont = False def linereader(file=file, lnum=[lnum], getline=linecache.getline): line = getline(file, lnum[0]) lnum[0] += 1 return line # Build the list of names on this line of code where the exception # occurred. try: # This builds the names list in-place by capturing it from the # enclosing scope. for token in generate_tokens(linereader): tokeneater(*token) except (IndexError, UnicodeDecodeError): # signals exit of tokenizer pass except tokenize.TokenError as msg: _m = ( "An unexpected error occurred while tokenizing input file %s\n" "The following traceback may be corrupted or invalid\n" "The error message is: %s\n" % (file, msg)) print(_m) # prune names list of duplicates, but keep the right order unique_names = uniq_stable(names) # Start loop over vars lvals = [] for name_full in unique_names: name_base = name_full.split('.', 1)[0] if name_base in frame.f_code.co_varnames: if name_base in list(locals.keys()): try: value = safe_repr(eval(name_full, locals)) except: value = "undefined" else: value = "undefined" name = name_full lvals.append('%s = %s' % (name, value)) #elif print_globals: # if frame.f_globals.has_key(name_base): # try: # value = safe_repr(eval(name_full,frame.f_globals)) # except: # value = "undefined" # else: # value = "undefined" # name = 'global %s' % name_full # lvals.append('%s = %s' % (name,value)) if lvals: lvals = '%s%s' % (INDENT, ('\n%s' % INDENT).join(lvals)) else: lvals = '' level = '%s\n%s %s\n' % (75 * '.', link, call) if index is None: frames.append(level) else: frames.append('%s%s' % (level, ''.join( _format_traceback_lines(lnum, index, lines, lvals)))) return frames
def _FFC(frame): a_v = inspect.getargvalues(frame) return frame.f_code.co_name + inspect.formatargvalues(*a_v)
indent = '<tt>' + small(' ' * 5) + ' </tt>' frames = [] records = inspect.getinnerframes(etb, context) for frame, file, lnum, func, lines, index in records: if file: file = os.path.abspath(file) link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file)) else: file = link = '?' args, varargs, varkw, locals = inspect.getargvalues(frame) call = '' if func != '?': call = 'in ' + strong(func) + \ inspect.formatargvalues(args, varargs, varkw, locals, formatvalue=lambda value: '=' + pydoc.html.repr(value)) highlight = {} def reader(lnum=[lnum]): highlight[lnum[0]] = 1 try: return linecache.getline(file, lnum[0]) finally: lnum[0] += 1 vars = scanvars(reader, frame, locals) rows = [ '<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' % ('<big> </big>', link, call)
def get_argspec(func, strip_self=True, doc=None, source=None): """Get argument specifications. :param strip_self: strip `self` from argspec :type strip_self: bool :param doc: doc string of func (optional) :type doc: str :param source: source code of func (optional) :type source: str :returns: argument specification :rtype: str >>> get_argspec(inspect.getclasstree) '(classes, unique=0)' >>> get_argspec(abs) '(number)' """ # get the function object of the class try: func = func.__func__ except AttributeError: try: # py 2.X func = func.im_func except AttributeError: pass # is callable? if not hasattr(func, '__call__'): return '' # func should have a name try: func_name = func.__name__ except AttributeError: return '' # from docstring if doc is None: doc = get_doc(func) match = re.search(DEF_DOC % func_name, doc, RE_FLAG) # from source code if not match: if source is None: try: source = inspect.getsource(func) except (TypeError, IOError): source = '' if source: match = re.search(DEF_SOURCE % func_name, source, RE_FLAG) if match: argspec = reduce_spaces(match.group(1)) else: # try with the inspect.getarg* functions try: argspec = inspect.formatargspec(*inspect.getfullargspec(func)) except: try: # py 2.X argspec = inspect.formatargspec(*inspect.getargspec(func)) except: try: argspec = inspect.formatargvalues( *inspect.getargvalues(func)) except: argspec = '' if strip_self: argspec = argspec.replace('self, ', '') return argspec
def getExceptionTraceback(code): """Convert traceback (from sys.exc_info()) to a dict that contains the raw data. The dict is intended to be coded as JSON and then sent to scriptmgr/twister/editor. *code* is the code that was executing, as a single string. """ exc_type, exc_value, exc_traceback = sys.exc_info() codelines = code.splitlines() stackdump = [ ] # outer level is the controller, # second level is the module call. # anything beyond is within a function. # Move the function call description up one level to the correct place for frame, file, linenumber, func, lines, index in inspect.getinnerframes(exc_traceback, context=1)[1:]: # skip outer frame stackentry = { "linenumber":linenumber, "file":file } if func != "<module>": try: args, varargs, varkw, locals = inspect.getargvalues(frame) funcargs = inspect.formatargvalues(args, varargs, varkw, locals, formatvalue=formatvalue) stackentry["furtherlinetext"] = "%s(%s)" % (func, funcargs) # double brackets to make it stand out except: # TODO: Do something useful here. pass if file == "<string>" and 0 <= linenumber - 1 < len(codelines): # have to do this as context=1 doesn't work (it doesn't give me anything in lines) stackentry["linetext"] = codelines[linenumber - 1] else: # XXX bit of a hack to show the line number in third party libraries stackentry["file"] += ":" + str(linenumber) if stackdump and stackdump[-1] == stackentry: duplicates += 1 else: if stackdump: stackdump[-1]["duplicates"] = duplicates stackdump.append(stackentry) duplicates = 0 if file.startswith("/usr/lib/python"): break if stackdump: stackdump[-1]["duplicates"] = duplicates if exc_type in [ SyntaxError, IndentationError ]: stackentry = {"linenumber":exc_value.lineno, "file":exc_value.filename, "offset":exc_value.offset, "duplicates":1} if (stackentry["file"] == "<string>" and 0 <= stackentry["linenumber"] - 1 < len(codelines)): # Can't seem to recover the text from the SyntaxError # object, though it is in its repr. stackentry["linetext"] = as_unicode(codelines[stackentry["linenumber"] - 1]) stackentry["furtherlinetext"] = exc_value.msg stackdump.append(stackentry) # truncate the list if len(stackdump) > 50: stackdump = stackdump[:20] + [{"furtherlinetext": "%d entries omitted" % (len(stackdump)-40) }] + stackdump[-20:] result = { "message_type": 'exception', "exceptiondescription": "%s: %s" % (exc_value.__class__.__name__, as_unicode(str(exc_value))), "stackdump": stackdump } #raise IOError('http error', 403, 'Scraperwiki blocked access to "http://t**s.ru/". Click <a href="/whitelist/?url=http%3A//t**s.ru/">here</a> for details.', <httplib.HTTPMessage instance at 0x84c318c>) if exc_type == HTTPError and exc_value.code == 403: mblockaccess = re.search('Scraperwiki blocked access to "(.*)"', exc_value.msg) if mblockaccess: result["blockedurl"] = urllib.unquote(mblockaccess.group(1)) result["blockedurlquoted"] = mblockaccess.group(1) return result