def print_list_lines(self, filename, first, last): """The printing (as opposed to the parsing part of a 'list' command.""" try: Colors = self.color_scheme_table.active_colors ColorsNormal = Colors.Normal tpl_line = "%%s%s%%s %s%%s" % (Colors.lineno, ColorsNormal) tpl_line_em = "%%s%s%%s %s%%s%s" % (Colors.linenoEm, Colors.line, ColorsNormal) src = [] if filename == "<string>" and hasattr(self, "_exec_filename"): filename = self._exec_filename for lineno in range(first, last + 1): line = ulinecache.getline(filename, lineno) if not line: break if lineno == self.curframe.f_lineno: line = self.__format_line(tpl_line_em, filename, lineno, line, arrow=True) else: line = self.__format_line(tpl_line, filename, lineno, line, arrow=False) src.append(line) self.lineno = lineno print("".join(src), file=io.stdout) except KeyboardInterrupt: pass
def print_list_lines(self, filename, first, last): """The printing (as opposed to the parsing part of a 'list' command.""" try: Colors = self.color_scheme_table.active_colors ColorsNormal = Colors.Normal tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal) src = [] if filename == "<string>" and hasattr(self, "_exec_filename"): filename = self._exec_filename for lineno in range(first, last+1): line = ulinecache.getline(filename, lineno) if not line: break if lineno == self.curframe.f_lineno: line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True) else: line = self.__format_line(tpl_line, filename, lineno, line, arrow = False) src.append(line) self.lineno = lineno print(''.join(src), file=io.stdout) except KeyboardInterrupt: pass
def structured_traceback(self, etype, value, elist, tb_offset=None, context=5): # If the source file has been edited, the line in the syntax error can # be wrong (retrieved from an outdated cache). This replaces it with # the current value. if isinstance(value.filename, py3compat.string_types) \ and isinstance(value.lineno, int): linecache.checkcache(value.filename) newtext = ulinecache.getline(value.filename, value.lineno) if newtext: value.text = newtext return super(SyntaxTB, self).structured_traceback(etype, value, elist, tb_offset=tb_offset, context=context)
def generate_lprofile(self, fun): """ Generate div containing profiled source code with timings of each line, taken from iline_profiler. """ self.value_lprofile = "" try: filename = fun.co_filename firstlineno = fun.co_firstlineno name = fun.co_name except AttributeError: return ltimings_key = (filename, firstlineno, name) try: ltimings = self.lprofile.timings[ltimings_key] except KeyError: return # Currently the correct filename is stored at the end of ltimings. # This is a work-around to fix cProfiler giving useless filenames for # zipped packages. filename = ltimings[-1] if filename.endswith(('.pyc', '.pyo')): filename = openpy.source_from_cache(filename) if ".egg/" in filename: add_zipped_file_to_linecache(filename) raw_code = "" linenos = range(firstlineno, ltimings[-2][0] + 1) for lineno in linenos: raw_code += ulinecache.getline(filename, lineno) formatter = LProfileFormatter(firstlineno, ltimings, noclasses=True) self.value_lprofile = highlight(raw_code, PythonLexer(), formatter)
def _format_exception_only(self, etype, value): """Format the exception part of a traceback. The arguments are the exception type and value such as given by sys.exc_info()[:2]. The return value is a list of strings, each ending in a newline. Normally, the list contains a single string; however, for SyntaxError exceptions, it contains several lines that (when printed) display detailed information about where the syntax error occurred. The message indicating which exception occurred is the always last string in the list. Also lifted nearly verbatim from traceback.py """ have_filedata = False Colors = self.Colors list = [] stype = Colors.excName + etype.__name__ + Colors.Normal if value is None: # Not sure if this can still happen in Python 2.6 and above list.append( py3compat.cast_unicode(stype) + '\n') else: if issubclass(etype, SyntaxError): have_filedata = True #print 'filename is',filename # dbg if not value.filename: value.filename = "<string>" if value.lineno: lineno = value.lineno textline = ulinecache.getline(value.filename, value.lineno) else: lineno = 'unknown' textline = '' list.append('%s File %s"%s"%s, line %s%s%s\n' % \ (Colors.normalEm, Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm, Colors.linenoEm, lineno, Colors.Normal )) if textline == '': textline = py3compat.cast_unicode(value.text, "utf-8") if textline is not None: i = 0 while i < len(textline) and textline[i].isspace(): i += 1 list.append('%s %s%s\n' % (Colors.line, textline.strip(), Colors.Normal)) if value.offset is not None: s = ' ' for c in textline[i:value.offset-1]: if c.isspace(): s += c else: s += ' ' list.append('%s%s^%s\n' % (Colors.caret, s, Colors.Normal) ) try: s = value.msg except Exception: s = self._some_str(value) if s: list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, Colors.Normal, s)) else: list.append('%s\n' % str(stype)) # sync with user hooks if have_filedata: ipinst = get_ipython() if ipinst is not None: ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) return list
def _format_exception_only(self, etype, value): """Format the exception part of a traceback. The arguments are the exception type and value such as given by sys.exc_info()[:2]. The return value is a list of strings, each ending in a newline. Normally, the list contains a single string; however, for SyntaxError exceptions, it contains several lines that (when printed) display detailed information about where the syntax error occurred. The message indicating which exception occurred is the always last string in the list. Also lifted nearly verbatim from traceback.py """ have_filedata = False Colors = self.Colors list = [] stype = Colors.excName + etype.__name__ + Colors.Normal if value is None: # Not sure if this can still happen in Python 2.6 and above list.append( py3compat.cast_unicode(stype) + '\n') else: if etype is SyntaxError: have_filedata = True #print 'filename is',filename # dbg if not value.filename: value.filename = "<string>" list.append('%s File %s"%s"%s, line %s%d%s\n' % \ (Colors.normalEm, Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm, Colors.linenoEm, value.lineno, Colors.Normal )) textline = ulinecache.getline(value.filename, value.lineno) if textline == '': textline = py3compat.cast_unicode(value.text, "utf-8") if textline is not None: i = 0 while i < len(textline) and textline[i].isspace(): i += 1 list.append('%s %s%s\n' % (Colors.line, textline.strip(), Colors.Normal)) if value.offset is not None: s = ' ' for c in textline[i:value.offset-1]: if c.isspace(): s += c else: s += ' ' list.append('%s%s^%s\n' % (Colors.caret, s, Colors.Normal) ) try: s = value.msg except Exception: s = self._some_str(value) if s: list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, Colors.Normal, s)) else: list.append('%s\n' % str(stype)) # sync with user hooks if have_filedata: ipinst = ipapi.get() if ipinst is not None: ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) return list