def structured_traceback( self, etype: type, evalue: Optional[BaseException], etb: TracebackType, tb_offset: Optional[int] = None, number_of_lines_of_context: int = 5, ): """Return a nice text document describing the traceback.""" assert etb is not None formatted_exception = self.format_exception_as_a_whole( etype, evalue, etb, number_of_lines_of_context, tb_offset) colors = self.Colors # just a shorthand + quicker name lookup colorsnormal = colors.Normal # used a lot head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) structured_traceback_parts = [head] chained_exceptions_tb_offset = 0 lines_of_context = 3 formatted_exceptions = formatted_exception exception = self.get_parts_of_chained_exception(evalue) if exception: assert evalue is not None formatted_exceptions += self.prepare_chained_exception_message( evalue.__cause__) etype, evalue, etb = exception else: evalue = None chained_exc_ids = set() while evalue: formatted_exceptions += self.format_exception_as_a_whole( etype, evalue, etb, lines_of_context, chained_exceptions_tb_offset) exception = self.get_parts_of_chained_exception(evalue) if exception and not id(exception[1]) in chained_exc_ids: chained_exc_ids.add( id(exception[1] )) # trace exception to avoid infinite 'cause' loop formatted_exceptions += self.prepare_chained_exception_message( evalue.__cause__) etype, evalue, etb = exception else: evalue = None # we want to see exceptions in a reversed order: # the first exception should be on top for formatted_exception in reversed(formatted_exceptions): structured_traceback_parts += formatted_exception return structured_traceback_parts
def prepare_header(self, etype, long_version=False): colors = self.Colors # just a shorthand + quicker name lookup colorsnormal = colors.Normal # used a lot exc = '%s%s%s' % (colors.excName, etype, colorsnormal) width = min(75, get_terminal_size()[0]) if long_version: # Header with the exception type, python version, and date pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable date = time.ctime(time.time()) head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, exc, ' ' * (width - len(str(etype)) - len(pyver)), pyver, date.rjust(width) ) head += "\nA problem occurred 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' % (exc, 'Traceback (most recent call last)'. \ rjust(width - len(str(etype))) ) return head
def pager_page(strng, start=0, screen_lines=0, pager_cmd=None): """Display a string, piping through a pager after a certain length. strng can be a mime-bundle dict, supplying multiple representations, keyed by mime-type. The screen_lines parameter specifies the number of *usable* lines of your terminal screen (total lines minus lines you need to reserve to show other information). If you set screen_lines to a number <=0, page() will try to auto-determine your screen size and will only use up to (screen_size+screen_lines) for printing, paging after that. That is, if you want auto-detection but need to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for auto-detection without any lines reserved simply use screen_lines = 0. If a string won't fit in the allowed lines, it is sent through the specified pager command. If none given, look for PAGER in the environment, and ultimately default to less. If no system pager works, the string is sent through a 'dumb pager' written in python, very simplistic. """ # for compatibility with mime-bundle form: if isinstance(strng, dict): strng = strng['text/plain'] # Ugly kludge, but calling curses.initscr() flat out crashes in emacs TERM = os.environ.get('TERM', 'dumb') if TERM in ['dumb', 'emacs'] and os.name != 'nt': print(strng) return # chop off the topmost part of the string we don't want to see str_lines = strng.splitlines()[start:] str_toprint = os.linesep.join(str_lines) num_newlines = len(str_lines) len_str = len(str_toprint) # Dumb heuristics to guesstimate number of on-screen lines the string # takes. Very basic, but good enough for docstrings in reasonable # terminals. If someone later feels like refining it, it's not hard. numlines = max(num_newlines, int(len_str / 80) + 1) screen_lines_def = get_terminal_size()[1] # auto-determine screen size if screen_lines <= 0: try: screen_lines += _detect_screen_size(screen_lines_def) except (TypeError, UnsupportedOperation): print(str_toprint, file=io.stdout) return #print 'numlines',numlines,'screenlines',screen_lines # dbg if numlines <= screen_lines: #print '*** normal print' # dbg print(str_toprint, file=io.stdout) else: # Try to open pager and default to internal one if that fails. # All failure modes are tagged as 'retval=1', to match the return # value of a failed system command. If any intermediate attempt # sets retval to 1, at the end we resort to our own page_dumb() pager. pager_cmd = get_pager_cmd(pager_cmd) pager_cmd += ' ' + get_pager_start(pager_cmd, start) if os.name == 'nt': if pager_cmd.startswith('type'): # The default WinXP 'type' command is failing on complex strings. retval = 1 else: fd, tmpname = tempfile.mkstemp('.txt') try: os.close(fd) with open(tmpname, 'wt') as tmpfile: tmpfile.write(strng) cmd = "%s < %s" % (pager_cmd, tmpname) # tmpfile needs to be closed for windows if os.system(cmd): retval = 1 else: retval = None finally: os.remove(tmpname) else: try: retval = None # if I use popen4, things hang. No idea why. #pager,shell_out = os.popen4(pager_cmd) pager = os.popen(pager_cmd, 'w') try: pager_encoding = pager.encoding or sys.stdout.encoding pager.write( py3compat.cast_bytes_py2(strng, encoding=pager_encoding)) finally: retval = pager.close() except IOError as msg: # broken pipe when user quits if msg.args == (32, 'Broken pipe'): retval = None else: retval = 1 except OSError: # Other strange problems, sometimes seen in Win2k/cygwin retval = 1 if retval is not None: page_dumb(strng, screen_lines=screen_lines)
def pager_page(strng, start=0, screen_lines=0, pager_cmd=None): """Display a string, piping through a pager after a certain length. strng can be a mime-bundle dict, supplying multiple representations, keyed by mime-type. The screen_lines parameter specifies the number of *usable* lines of your terminal screen (total lines minus lines you need to reserve to show other information). If you set screen_lines to a number <=0, page() will try to auto-determine your screen size and will only use up to (screen_size+screen_lines) for printing, paging after that. That is, if you want auto-detection but need to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for auto-detection without any lines reserved simply use screen_lines = 0. If a string won't fit in the allowed lines, it is sent through the specified pager command. If none given, look for PAGER in the environment, and ultimately default to less. If no system pager works, the string is sent through a 'dumb pager' written in python, very simplistic. """ # for compatibility with mime-bundle form: if isinstance(strng, dict): strng = strng['text/plain'] # Ugly kludge, but calling curses.initscr() flat out crashes in emacs TERM = os.environ.get('TERM','dumb') if TERM in ['dumb','emacs'] and os.name != 'nt': print(strng) return # chop off the topmost part of the string we don't want to see str_lines = strng.splitlines()[start:] str_toprint = os.linesep.join(str_lines) num_newlines = len(str_lines) len_str = len(str_toprint) # Dumb heuristics to guesstimate number of on-screen lines the string # takes. Very basic, but good enough for docstrings in reasonable # terminals. If someone later feels like refining it, it's not hard. numlines = max(num_newlines,int(len_str/80)+1) screen_lines_def = get_terminal_size()[1] # auto-determine screen size if screen_lines <= 0: try: screen_lines += _detect_screen_size(screen_lines_def) except (TypeError, UnsupportedOperation): print(str_toprint, file=io.stdout) return #print 'numlines',numlines,'screenlines',screen_lines # dbg if numlines <= screen_lines : #print '*** normal print' # dbg print(str_toprint, file=io.stdout) else: # Try to open pager and default to internal one if that fails. # All failure modes are tagged as 'retval=1', to match the return # value of a failed system command. If any intermediate attempt # sets retval to 1, at the end we resort to our own page_dumb() pager. pager_cmd = get_pager_cmd(pager_cmd) pager_cmd += ' ' + get_pager_start(pager_cmd,start) if os.name == 'nt': if pager_cmd.startswith('type'): # The default WinXP 'type' command is failing on complex strings. retval = 1 else: fd, tmpname = tempfile.mkstemp('.txt') try: os.close(fd) with open(tmpname, 'wt') as tmpfile: tmpfile.write(strng) cmd = "%s < %s" % (pager_cmd, tmpname) # tmpfile needs to be closed for windows if os.system(cmd): retval = 1 else: retval = None finally: os.remove(tmpname) else: try: retval = None # if I use popen4, things hang. No idea why. #pager,shell_out = os.popen4(pager_cmd) pager = os.popen(pager_cmd, 'w') try: pager_encoding = pager.encoding or sys.stdout.encoding pager.write(py3compat.cast_bytes_py2( strng, encoding=pager_encoding)) finally: retval = pager.close() except IOError as msg: # broken pipe when user quits if msg.args == (32, 'Broken pipe'): retval = None else: retval = 1 except OSError: # Other strange problems, sometimes seen in Win2k/cygwin retval = 1 if retval is not None: page_dumb(strng,screen_lines=screen_lines)
def page(strng, start=0, screen_lines=0, pager_cmd=None): """Print a string, piping through a pager after a certain length. The screen_lines parameter specifies the number of *usable* lines of your terminal screen (total lines minus lines you need to reserve to show other information). If you set screen_lines to a number <=0, page() will try to auto-determine your screen size and will only use up to (screen_size+screen_lines) for printing, paging after that. That is, if you want auto-detection but need to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for auto-detection without any lines reserved simply use screen_lines = 0. If a string won't fit in the allowed lines, it is sent through the specified pager command. If none given, look for PAGER in the environment, and ultimately default to less. If no system pager works, the string is sent through a 'dumb pager' written in python, very simplistic. """ # Some routines may auto-compute start offsets incorrectly and pass a # negative value. Offset to 0 for robustness. start = max(0, start) # first, try the hook ip = ipapi.get() if ip: try: ip.hooks.show_in_pager(strng) return except TryNext: pass # Ugly kludge, but calling curses.initscr() flat out crashes in emacs TERM = os.environ.get('TERM', 'dumb') if TERM in ['dumb', 'emacs'] and os.name != 'nt': print strng return # chop off the topmost part of the string we don't want to see str_lines = strng.splitlines()[start:] str_toprint = os.linesep.join(str_lines) num_newlines = len(str_lines) len_str = len(str_toprint) # Dumb heuristics to guesstimate number of on-screen lines the string # takes. Very basic, but good enough for docstrings in reasonable # terminals. If someone later feels like refining it, it's not hard. numlines = max(num_newlines, int(len_str / 80) + 1) screen_lines_def = get_terminal_size()[1] # auto-determine screen size if screen_lines <= 0: if (TERM == 'xterm' or TERM == 'xterm-color') and sys.platform != 'sunos5': local_use_curses = use_curses else: # curses causes problems on many terminals other than xterm, and # some termios calls lock up on Sun OS5. local_use_curses = False if local_use_curses: import termios import curses # There is a bug in curses, where *sometimes* it fails to properly # initialize, and then after the endwin() call is made, the # terminal is left in an unusable state. Rather than trying to # check everytime for this (by requesting and comparing termios # flags each time), we just save the initial terminal state and # unconditionally reset it every time. It's cheaper than making # the checks. term_flags = termios.tcgetattr(sys.stdout) # Curses modifies the stdout buffer size by default, which messes # up Python's normal stdout buffering. This would manifest itself # to IPython users as delayed printing on stdout after having used # the pager. # # We can prevent this by manually setting the NCURSES_NO_SETBUF # environment variable. For more details, see: # http://bugs.python.org/issue10144 NCURSES_NO_SETBUF = os.environ.get('NCURSES_NO_SETBUF', None) os.environ['NCURSES_NO_SETBUF'] = '' # Proceed with curses initialization scr = curses.initscr() screen_lines_real, screen_cols = scr.getmaxyx() curses.endwin() # Restore environment if NCURSES_NO_SETBUF is None: del os.environ['NCURSES_NO_SETBUF'] else: os.environ['NCURSES_NO_SETBUF'] = NCURSES_NO_SETBUF # Restore terminal state in case endwin() didn't. termios.tcsetattr(sys.stdout, termios.TCSANOW, term_flags) # Now we have what we needed: the screen size in rows/columns screen_lines += screen_lines_real #print '***Screen size:',screen_lines_real,'lines x',\ #screen_cols,'columns.' # dbg else: screen_lines += screen_lines_def #print 'numlines',numlines,'screenlines',screen_lines # dbg if numlines <= screen_lines: #print '*** normal print' # dbg print >> IPython.utils.io.Term.cout, str_toprint else: # Try to open pager and default to internal one if that fails. # All failure modes are tagged as 'retval=1', to match the return # value of a failed system command. If any intermediate attempt # sets retval to 1, at the end we resort to our own page_dumb() pager. pager_cmd = get_pager_cmd(pager_cmd) pager_cmd += ' ' + get_pager_start(pager_cmd, start) if os.name == 'nt': if pager_cmd.startswith('type'): # The default WinXP 'type' command is failing on complex strings. retval = 1 else: tmpname = tempfile.mktemp('.txt') tmpfile = file(tmpname, 'wt') tmpfile.write(strng) tmpfile.close() cmd = "%s < %s" % (pager_cmd, tmpname) if os.system(cmd): retval = 1 else: retval = None os.remove(tmpname) else: try: retval = None # if I use popen4, things hang. No idea why. #pager,shell_out = os.popen4(pager_cmd) pager = os.popen(pager_cmd, 'w') pager.write(strng) pager.close() retval = pager.close() # success returns None except IOError, msg: # broken pipe when user quits if msg.args == (32, 'Broken pipe'): retval = None else: retval = 1 except OSError: # Other strange problems, sometimes seen in Win2k/cygwin retval = 1
def page(strng, start=0, screen_lines=0, pager_cmd=None): """Print a string, piping through a pager after a certain length. The screen_lines parameter specifies the number of *usable* lines of your terminal screen (total lines minus lines you need to reserve to show other information). If you set screen_lines to a number <=0, page() will try to auto-determine your screen size and will only use up to (screen_size+screen_lines) for printing, paging after that. That is, if you want auto-detection but need to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for auto-detection without any lines reserved simply use screen_lines = 0. If a string won't fit in the allowed lines, it is sent through the specified pager command. If none given, look for PAGER in the environment, and ultimately default to less. If no system pager works, the string is sent through a 'dumb pager' written in python, very simplistic. """ # Some routines may auto-compute start offsets incorrectly and pass a # negative value. Offset to 0 for robustness. start = max(0, start) # first, try the hook ip = ipapi.get() if ip: try: ip.hooks.show_in_pager(strng) return except TryNext: pass # Ugly kludge, but calling curses.initscr() flat out crashes in emacs TERM = os.environ.get('TERM','dumb') if TERM in ['dumb','emacs'] and os.name != 'nt': print strng return # chop off the topmost part of the string we don't want to see str_lines = strng.split(os.linesep)[start:] str_toprint = os.linesep.join(str_lines) num_newlines = len(str_lines) len_str = len(str_toprint) # Dumb heuristics to guesstimate number of on-screen lines the string # takes. Very basic, but good enough for docstrings in reasonable # terminals. If someone later feels like refining it, it's not hard. numlines = max(num_newlines,int(len_str/80)+1) screen_lines_def = get_terminal_size()[1] # auto-determine screen size if screen_lines <= 0: if (TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5': local_use_curses = use_curses else: # curses causes problems on many terminals other than xterm, and # some termios calls lock up on Sun OS5. local_use_curses = False if local_use_curses: import termios import curses # There is a bug in curses, where *sometimes* it fails to properly # initialize, and then after the endwin() call is made, the # terminal is left in an unusable state. Rather than trying to # check everytime for this (by requesting and comparing termios # flags each time), we just save the initial terminal state and # unconditionally reset it every time. It's cheaper than making # the checks. term_flags = termios.tcgetattr(sys.stdout) scr = curses.initscr() screen_lines_real,screen_cols = scr.getmaxyx() curses.endwin() # Restore terminal state in case endwin() didn't. termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) # Now we have what we needed: the screen size in rows/columns screen_lines += screen_lines_real #print '***Screen size:',screen_lines_real,'lines x',\ #screen_cols,'columns.' # dbg else: screen_lines += screen_lines_def #print 'numlines',numlines,'screenlines',screen_lines # dbg if numlines <= screen_lines : #print '*** normal print' # dbg print >>IPython.utils.io.Term.cout, str_toprint else: # Try to open pager and default to internal one if that fails. # All failure modes are tagged as 'retval=1', to match the return # value of a failed system command. If any intermediate attempt # sets retval to 1, at the end we resort to our own page_dumb() pager. pager_cmd = get_pager_cmd(pager_cmd) pager_cmd += ' ' + get_pager_start(pager_cmd,start) if os.name == 'nt': if pager_cmd.startswith('type'): # The default WinXP 'type' command is failing on complex strings. retval = 1 else: tmpname = tempfile.mktemp('.txt') tmpfile = file(tmpname,'wt') tmpfile.write(strng) tmpfile.close() cmd = "%s < %s" % (pager_cmd,tmpname) if os.system(cmd): retval = 1 else: retval = None os.remove(tmpname) else: try: retval = None # if I use popen4, things hang. No idea why. #pager,shell_out = os.popen4(pager_cmd) pager = os.popen(pager_cmd,'w') pager.write(strng) pager.close() retval = pager.close() # success returns None except IOError,msg: # broken pipe when user quits if msg.args == (32,'Broken pipe'): retval = None else: retval = 1 except OSError: # Other strange problems, sometimes seen in Win2k/cygwin retval = 1
def page(strng, start=0, screen_lines=0, pager_cmd=None): """Print a string, piping through a pager after a certain length. The screen_lines parameter specifies the number of *usable* lines of your terminal screen (total lines minus lines you need to reserve to show other information). If you set screen_lines to a number <=0, page() will try to auto-determine your screen size and will only use up to (screen_size+screen_lines) for printing, paging after that. That is, if you want auto-detection but need to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for auto-detection without any lines reserved simply use screen_lines = 0. If a string won't fit in the allowed lines, it is sent through the specified pager command. If none given, look for PAGER in the environment, and ultimately default to less. If no system pager works, the string is sent through a 'dumb pager' written in python, very simplistic. """ # Some routines may auto-compute start offsets incorrectly and pass a # negative value. Offset to 0 for robustness. start = max(0, start) # first, try the hook ip = ipapi.get() if ip: try: ip.hooks.show_in_pager(strng) return except TryNext: pass # Ugly kludge, but calling curses.initscr() flat out crashes in emacs TERM = os.environ.get('TERM','dumb') if TERM in ['dumb','emacs'] and os.name != 'nt': print strng return # chop off the topmost part of the string we don't want to see str_lines = strng.splitlines()[start:] str_toprint = os.linesep.join(str_lines) num_newlines = len(str_lines) len_str = len(str_toprint) # Dumb heuristics to guesstimate number of on-screen lines the string # takes. Very basic, but good enough for docstrings in reasonable # terminals. If someone later feels like refining it, it's not hard. numlines = max(num_newlines,int(len_str/80)+1) screen_lines_def = get_terminal_size()[1] # auto-determine screen size if screen_lines <= 0: try: screen_lines += _detect_screen_size(use_curses, screen_lines_def) except (TypeError, UnsupportedOperation): print >>io.stdout, str_toprint return #print 'numlines',numlines,'screenlines',screen_lines # dbg if numlines <= screen_lines : #print '*** normal print' # dbg print >>io.stdout, str_toprint else: # Try to open pager and default to internal one if that fails. # All failure modes are tagged as 'retval=1', to match the return # value of a failed system command. If any intermediate attempt # sets retval to 1, at the end we resort to our own page_dumb() pager. pager_cmd = get_pager_cmd(pager_cmd) pager_cmd += ' ' + get_pager_start(pager_cmd,start) if os.name == 'nt': if pager_cmd.startswith('type'): # The default WinXP 'type' command is failing on complex strings. retval = 1 else: tmpname = tempfile.mktemp('.txt') tmpfile = open(tmpname,'wt') tmpfile.write(strng) tmpfile.close() cmd = "%s < %s" % (pager_cmd,tmpname) if os.system(cmd): retval = 1 else: retval = None os.remove(tmpname) else: try: retval = None # if I use popen4, things hang. No idea why. #pager,shell_out = os.popen4(pager_cmd) pager = os.popen(pager_cmd,'w') pager.write(strng) pager.close() retval = pager.close() # success returns None except IOError,msg: # broken pipe when user quits if msg.args == (32,'Broken pipe'): retval = None else: retval = 1 except OSError: # Other strange problems, sometimes seen in Win2k/cygwin retval = 1
def page(strng, start=0, screen_lines=0, pager_cmd=None): """Print a string, piping through a pager after a certain length. The screen_lines parameter specifies the number of *usable* lines of your terminal screen (total lines minus lines you need to reserve to show other information). If you set screen_lines to a number <=0, page() will try to auto-determine your screen size and will only use up to (screen_size+screen_lines) for printing, paging after that. That is, if you want auto-detection but need to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for auto-detection without any lines reserved simply use screen_lines = 0. If a string won't fit in the allowed lines, it is sent through the specified pager command. If none given, look for PAGER in the environment, and ultimately default to less. If no system pager works, the string is sent through a 'dumb pager' written in python, very simplistic. """ # Some routines may auto-compute start offsets incorrectly and pass a # negative value. Offset to 0 for robustness. start = max(0, start) # first, try the hook ip = ipapi.get() if ip: try: ip.hooks.show_in_pager(strng) return except TryNext: pass # Ugly kludge, but calling curses.initscr() flat out crashes in emacs TERM = os.environ.get('TERM', 'dumb') if TERM in ['dumb', 'emacs'] and os.name != 'nt': print strng return # chop off the topmost part of the string we don't want to see str_lines = strng.splitlines()[start:] str_toprint = os.linesep.join(str_lines) num_newlines = len(str_lines) len_str = len(str_toprint) # Dumb heuristics to guesstimate number of on-screen lines the string # takes. Very basic, but good enough for docstrings in reasonable # terminals. If someone later feels like refining it, it's not hard. numlines = max(num_newlines, int(len_str / 80) + 1) screen_lines_def = get_terminal_size()[1] # auto-determine screen size if screen_lines <= 0: try: screen_lines += _detect_screen_size(use_curses, screen_lines_def) except (TypeError, UnsupportedOperation): print >> io.stdout, str_toprint return #print 'numlines',numlines,'screenlines',screen_lines # dbg if numlines <= screen_lines: #print '*** normal print' # dbg print >> io.stdout, str_toprint else: # Try to open pager and default to internal one if that fails. # All failure modes are tagged as 'retval=1', to match the return # value of a failed system command. If any intermediate attempt # sets retval to 1, at the end we resort to our own page_dumb() pager. pager_cmd = get_pager_cmd(pager_cmd) pager_cmd += ' ' + get_pager_start(pager_cmd, start) if os.name == 'nt': if pager_cmd.startswith('type'): # The default WinXP 'type' command is failing on complex strings. retval = 1 else: tmpname = tempfile.mktemp('.txt') tmpfile = file(tmpname, 'wt') tmpfile.write(strng) tmpfile.close() cmd = "%s < %s" % (pager_cmd, tmpname) if os.system(cmd): retval = 1 else: retval = None os.remove(tmpname) else: try: retval = None # if I use popen4, things hang. No idea why. #pager,shell_out = os.popen4(pager_cmd) pager = os.popen(pager_cmd, 'w') pager.write(strng) pager.close() retval = pager.close() # success returns None except IOError, msg: # broken pipe when user quits if msg.args == (32, 'Broken pipe'): retval = None else: retval = 1 except OSError: # Other strange problems, sometimes seen in Win2k/cygwin retval = 1
def get_terminal_width(): from IPython.utils.terminal import get_terminal_size return get_terminal_size()[0]