def add(self, ent): if self.disabled: return try: old = self.db.hget('shadowhist', ent, _sentinel) if old is not _sentinel: return newidx = self.inc_idx() #print "new",newidx # dbg self.db.hset('shadowhist',ent, newidx) except: ipapi.get().showtraceback() print "WARNING: disabling shadow history" self.disabled = True
def main(): global zope_debug ip = ipapi.get() o = ip.options # autocall to "full" mode (smart mode is default, I like full mode) SOFTWARE_HOME = os.environ.get( "SOFTWARE_HOME" ) sys.path.append( SOFTWARE_HOME ) print "SOFTWARE_HOME=%s\n" % SOFTWARE_HOME zope_debug = ZopeDebug() # <HACK ALERT> import pdb; pdb.set_trace = ipy_set_trace # </HACK ALERT> # I like my banner minimal. o.banner = "ZOPE Py %s IPy %s\n" % (sys.version.split('\n')[0],release.version) print textwrap.dedent("""\ ZOPE mode iPython shell. Bound names: app portal utils.{ %s } Uses the $SOFTWARE_HOME and $CONFIG_FILE environment variables. """ % ( ",".join([ x for x in dir(zope_debug.utils) if not x.startswith("_") ] ) ) ) ip.user_ns.update( zope_debug.namespace )
def __init__(self,color_scheme='NoColor',completekey=None, stdin=None, stdout=None): # Parent constructor: if has_pydb and completekey is None: OldPdb.__init__(self,stdin=stdin,stdout=io.stdout) else: OldPdb.__init__(self,completekey,stdin,stdout) self.prompt = prompt # The default prompt is '(Pdb)' # IPython changes... self.is_pydb = has_pydb self.shell = ipapi.get() if self.is_pydb: # interactiveshell.py's ipalias seems to want pdb's checkline # which located in pydb.fn import pydb.fns self.checkline = lambda filename, lineno: \ pydb.fns.checkline(self, filename, lineno) self.curframe = None self.do_restart = self.new_do_restart self.old_all_completions = self.shell.Completer.all_completions self.shell.Completer.all_completions=self.all_completions self.do_list = decorate_fn_with_doc(self.list_command_pydb, OldPdb.do_list) self.do_l = self.do_list self.do_frame = decorate_fn_with_doc(self.new_do_frame, OldPdb.do_frame) self.aliases = {} # Create color table: we copy the default one from the traceback # module and add a few attributes needed for debugging self.color_scheme_table = exception_colors() # shorthands C = coloransi.TermColors cst = self.color_scheme_table cst['NoColor'].colors.breakpoint_enabled = C.NoColor cst['NoColor'].colors.breakpoint_disabled = C.NoColor cst['Linux'].colors.breakpoint_enabled = C.LightRed cst['Linux'].colors.breakpoint_disabled = C.Red cst['LightBG'].colors.breakpoint_enabled = C.LightRed cst['LightBG'].colors.breakpoint_disabled = C.Red self.set_colors(color_scheme) # Add a python parser so we can syntax highlight source while # debugging. self.parser = PyColorize.Parser()
def main(): global zope_debug ip = ipapi.get() o = ip.options # autocall to "full" mode (smart mode is default, I like full mode) SOFTWARE_HOME = os.environ.get( "SOFTWARE_HOME" ) sys.path.append( SOFTWARE_HOME ) print "SOFTWARE_HOME=%s\n" % SOFTWARE_HOME zope_debug = ZopeDebug() # <HACK ALERT> import pdb; pdb.set_trace = ipy_set_trace # </HACK ALERT> # I like my banner minimal. o.banner = "ZOPE Py %s IPy %s\n" % (sys.version.split('\n')[0],release.version) print textwrap.dedent("""\ ZOPE mode iPython shell. Bound names: app portal utils.{ %s } Uses the $SOFTWARE_HOME and $CONFIG_FILE environment variables. """ % ( ",".join([ x for x in dir(zope_debug.utils) if not x.startswith("_") ] ) ) ) sys.stdin = sys_oldstdin ip.user_ns.update( zope_debug.namespace )
def optionLoad(self): try: ip = get() path = ip.ipython_dir opt = open(path + '/options.conf', 'r') lines = opt.readlines() opt.close() options_ipython_panel = self.ipython_panel.getOptions() options_history_panel = self.history_panel.getOptions() for line in lines: key = line.split('=')[0] value = line.split('=')[1].replace('\n', '').replace('\r', '') if key in options_ipython_panel.keys(): options_ipython_panel[key]['value'] = value elif key in options_history_panel.keys(): options_history_panel[key]['value'] = value else: print >> sys.__stdout__, "Warning: key ", key, "not found in widget options. Check Options.conf" self.ipython_panel.reloadOptions(options_ipython_panel) self.history_panel.reloadOptions(options_history_panel) except IOError: print >> sys.__stdout__, "Could not open Options.conf, defaulting to default values."
def optionLoad(self): try: ip = get() path = ip.ipython_dir opt = open(path + '/options.conf','r') lines = opt.readlines() opt.close() options_ipython_panel = self.ipython_panel.getOptions() options_history_panel = self.history_panel.getOptions() for line in lines: key = line.split('=')[0] value = line.split('=')[1].replace('\n','').replace('\r','') if key in list(options_ipython_panel.keys()): options_ipython_panel[key]['value'] = value elif key in list(options_history_panel.keys()): options_history_panel[key]['value'] = value else: print("Warning: key ",key,"not found in widget options. Check Options.conf", file=sys.__stdout__) self.ipython_panel.reloadOptions(options_ipython_panel) self.history_panel.reloadOptions(options_history_panel) except IOError: print("Could not open Options.conf, defaulting to default values.", file=sys.__stdout__)
def post_mortem(tb): ip = ipapi.get() def_colors = ip.colors p = Pdb(def_colors) p.reset() while tb.tb_next is not None: tb = tb.tb_next p.interaction(tb.tb_frame, tb)
def __init__(self, colors=None): """Create a local debugger instance. :Parameters: - `colors` (None): a string containing the name of the color scheme to use, it must be one of IPython's valid color schemes. If not given, the function will default to the current IPython scheme when running inside IPython, and to 'NoColor' otherwise. Usage example: from IPython.core.debugger import Tracer; debug_here = Tracer() ... later in your code debug_here() # -> will open up the debugger at that point. Once the debugger activates, you can use all of its regular commands to step through code, set breakpoints, etc. See the pdb documentation from the Python standard library for usage details. """ global __IPYTHON__ try: __IPYTHON__ except NameError: # Outside of ipython, we set our own exception hook manually __IPYTHON__ = ipapi.get() BdbQuit_excepthook.excepthook_ori = sys.excepthook sys.excepthook = BdbQuit_excepthook def_colors = 'NoColor' try: # Limited tab completion support import readline readline.parse_and_bind('tab: complete') except ImportError: pass else: # In ipython, we use its custom exception handler mechanism ip = ipapi.get() def_colors = ip.colors ip.set_custom_exc((bdb.BdbQuit, ), BdbQuit_IPython_excepthook) if colors is None: colors = def_colors self.debugger = Pdb(colors)
def __str__(self): ip = ipapi.get() for cmd in reversed(ip.input_hist_raw): parts = cmd.strip().split() if len(parts) < 2 or parts[-1] in ['$LA', 'LA()']: continue return parts[-1] return ""
def enable(): try: import IPython ip = IPython.get_ipython() prerun_hook_name = 'pre_run_code_hook' except: try: # IPython 0.11 import IPython.core.ipapi as ipapi prerun_hook_name = 'pre_run_code_hook' ip = ipapi.get() except: # IPython 0.10.1 import IPython.ipapi as ipapi prerun_hook_name = 'pre_runcode_hook' ip = ipapi.get() ip.set_hook(prerun_hook_name, ipython_autoreload_hook) print('autoreload enabled')
def enable(): try: import IPython ip = IPython.get_ipython() prerun_hook_name = 'pre_run_code_hook' except: try: # IPython 0.11 import IPython.core.ipapi as ipapi prerun_hook_name = 'pre_run_code_hook' ip = ipapi.get() except: # IPython 0.10.1 import IPython.ipapi as ipapi prerun_hook_name = 'pre_runcode_hook' ip = ipapi.get() ip.set_hook(prerun_hook_name, ipython_autoreload_hook) print 'autoreload enabled'
def pickinput(self, row): try: value = self.table.items[row] except Exception as exc: self.error_output(str(exc)) else: api = ipapi.get() api.set_next_input(str(value)) self.quit(value)
def nipype_mode_off(): """Turn traits completer off""" from IPython.strdispatch import StrDispatch ip = ipapi.get() sdisp = StrDispatch() ip.IP.strdispatchers['complete_command'] = sdisp ip.IP.Completer.custom_completers = sdisp print 'nipype_mode is OFF'
def main(): """When we use the nipype profile we turn nipype_mode on.""" ip = ipapi.get() mode = get_nipype_mode() # initialize nipype_mode in user_ns nipype_mode_on() # enable magic function ip.expose_magic('nipype_mode', toggle_nipype_mode)
def pickinputattr(self, row, col): try: attr = self.table._displayattrs[col] value = attr.value(self.table.items[row]) except Exception as exc: self.error_output(str(exc)) else: api = ipapi.get() api.set_next_input(str(value)) self.quit(value)
def ipdb(): """ Break execution and drop into an IPython debug shell at the point where this function is called. """ from IPython.core.debugger import Pdb from IPython.core import ipapi ip = ipapi.get() def_colors = ip.colors Pdb(def_colors).set_trace(sys._getframe().f_back)
def install(): global ip ip = ipapi.get() # needed to make startjob visible as _ip.startjob('blah') ip.startjob = startjob ip.set_hook('input_prefilter', jobctrl_prefilter_f) ip.set_hook('shell_hook', jobctrl_shellcmd) ip.define_magic('kill', magic_kill) ip.define_magic('tasks', magic_tasks) ip.define_magic('jobqueue', jobqueue_f) ip.set_hook('pre_prompt_hook', jobq_output_hook)
def install(): global ip ip = ipapi.get() # needed to make startjob visible as _ip.startjob('blah') ip.startjob = startjob ip.set_hook('input_prefilter', jobctrl_prefilter_f) ip.set_hook('shell_hook', jobctrl_shellcmd) ip.define_magic('kill',magic_kill) ip.define_magic('tasks',magic_tasks) ip.define_magic('jobqueue',jobqueue_f) ip.set_hook('pre_prompt_hook', jobq_output_hook)
def main(): ip = ipapi.get() try: ip.ex("import math,cmath") ip.ex("import numpy") ip.ex("import numpy as np") ip.ex("from numpy import *") except ImportError: print "Unable to start NumPy profile, is numpy installed?"
def nipype_mode_on(): """Turn on the traits completer. This will hide all the traits attributes from the view of the user when doing TAB completion on any object that is a descendant of HasTraits. """ from ipy_traits_completer import activate ip = ipapi.get() activate() print 'nipype_mode is ON'
def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None): numbers_width = INDENT_SIZE - 1 res = [] i = lnum - index # This lets us get fully syntax-highlighted tracebacks. if scheme is None: ipinst = ipapi.get() if ipinst is not None: scheme = ipinst.colors else: scheme = DEFAULT_SCHEME _line_format = _parser.format2 for line in lines: # FIXME: we need to ensure the source is a pure string at this point, # else the coloring code makes a royal mess. This is in need of a # serious refactoring, so that all of the ultratb and PyColorize code # is unicode-safe. So for now this is rather an ugly hack, but # necessary to at least have readable tracebacks. Improvements welcome! if type(line)==unicode: line = line.encode('utf-8', 'replace') new_line, err = _line_format(line, 'str', scheme) if not err: line = new_line 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 = marker + str(i) line = '%s%s%s %s%s' %(Colors.linenoEm, num, Colors.line, line, Colors.Normal) else: num = '%*s' % (numbers_width,i) line = '%s%s%s %s' %(Colors.lineno, num, Colors.Normal, line) res.append(line) if lvals and i == lnum: res.append(lvals + '\n') i = i + 1 return res
def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None): numbers_width = INDENT_SIZE - 1 res = [] i = lnum - index # This lets us get fully syntax-highlighted tracebacks. if scheme is None: ipinst = ipapi.get() if ipinst is not None: scheme = ipinst.colors else: scheme = DEFAULT_SCHEME _line_format = _parser.format2 for line in lines: # FIXME: we need to ensure the source is a pure string at this point, # else the coloring code makes a royal mess. This is in need of a # serious refactoring, so that all of the ultratb and PyColorize code # is unicode-safe. So for now this is rather an ugly hack, but # necessary to at least have readable tracebacks. Improvements welcome! line = py3compat.cast_bytes_py2(line, 'utf-8') new_line, err = _line_format(line, 'str', scheme) if not err: line = new_line 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 = marker + str(i) line = '%s%s%s %s%s' %(Colors.linenoEm, num, Colors.line, line, Colors.Normal) else: num = '%*s' % (numbers_width,i) line = '%s%s%s %s' %(Colors.lineno, num, Colors.Normal, line) res.append(line) if lvals and i == lnum: res.append(lvals + '\n') i = i + 1 return res
def optionSave(self, name, value): ip = get() path = ip.ipython_dir opt = open(path + '/options.conf','w') try: options_ipython_panel = self.ipython_panel.getOptions() options_history_panel = self.history_panel.getOptions() for key in list(options_ipython_panel.keys()): opt.write(key + '=' + options_ipython_panel[key]['value']+'\n') for key in list(options_history_panel.keys()): opt.write(key + '=' + options_history_panel[key]['value']+'\n') finally: opt.close()
def optionSave(self, name, value): ip = get() path = ip.ipython_dir opt = open(path + '/options.conf','w') try: options_ipython_panel = self.ipython_panel.getOptions() options_history_panel = self.history_panel.getOptions() for key in options_ipython_panel.keys(): opt.write(key + '=' + options_ipython_panel[key]['value']+'\n') for key in options_history_panel.keys(): opt.write(key + '=' + options_history_panel[key]['value']+'\n') finally: opt.close()
def main(): ip = ipapi.get() try: ip.ex("import math,cmath") ip.ex("import numpy") ip.ex("import scipy") ip.ex("import numpy as np") ip.ex("import scipy as sp") ip.ex("from numpy import *") ip.ex("from scipy import *") except ImportError: print("Unable to start scipy profile, are numpy and scipy installed?")
def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None): numbers_width = INDENT_SIZE - 1 res = [] i = lnum - index # This lets us get fully syntax-highlighted tracebacks. if scheme is None: ipinst = ipapi.get() if ipinst is not None: scheme = ipinst.colors else: scheme = DEFAULT_SCHEME _line_format = _parser.format2 for line in lines: line = py3compat.cast_unicode(line) new_line, err = _line_format(line, 'str', scheme) if not err: line = new_line 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 = marker + str(i) line = '%s%s%s %s%s' %(Colors.linenoEm, num, Colors.line, line, Colors.Normal) else: num = '%*s' % (numbers_width,i) line = '%s%s%s %s' %(Colors.lineno, num, Colors.Normal, line) res.append(line) if lvals and i == lnum: res.append(lvals + '\n') i = i + 1 return res
def __init__(self, colors=None): """Create a local debugger instance. :Parameters: - `colors` (None): a string containing the name of the color scheme to use, it must be one of IPython's valid color schemes. If not given, the function will default to the current IPython scheme when running inside IPython, and to 'NoColor' otherwise. Usage example: from IPython.core.debugger import Tracer; debug_here = Tracer() ... later in your code debug_here() # -> will open up the debugger at that point. Once the debugger activates, you can use all of its regular commands to step through code, set breakpoints, etc. See the pdb documentation from the Python standard library for usage details. """ try: ip = ipapi.get() except: # Outside of ipython, we set our own exception hook manually BdbQuit_excepthook.excepthook_ori = sys.excepthook sys.excepthook = BdbQuit_excepthook def_colors = "NoColor" try: # Limited tab completion support import readline readline.parse_and_bind("tab: complete") except ImportError: pass else: # In ipython, we use its custom exception handler mechanism def_colors = ip.colors ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook) if colors is None: colors = def_colors self.debugger = Pdb(colors)
def main(): ip = ipapi.get() o = ip.options # Set the prompts similar to the defaults o.prompt_in1 = '>>> ' o.prompt_in2 = '... ' o.prompt_out = '' # Add a blank line before each new set of inputs. This is needed by # doctest to distinguish each test from the next. o.separate_in = '\n' o.separate_out = '' o.separate_out2 = '' # Disable pprint, so that outputs are printed as similarly to standard # python as possible o.pprint = False # Use plain exceptions, to also resemble normal pyhton. o.xmode = 'plain' # Store the activity flag in the metadata bag from the running shell ip.meta.doctest_mode = True
""" Install various IPython completers IPython extension that installs completers related to core ipython behaviour. The actual implementations are in extensions/ipy_completers.py """ from IPython.core import ipapi ip = ipapi.get() from ipy_completers import * ip.set_hook('complete_command', module_completer, str_key = 'import') ip.set_hook('complete_command', module_completer, str_key = 'from') ip.set_hook('complete_command', runlistpy, str_key = '%run') ip.set_hook('complete_command', cd_completer, str_key = '%cd')
def import_all(module): ipapi.get().ex('from %s import *' % module)
def import_x(module, submodules): if not isinstance(submodules, str): submodules = (',').join(submodules) ipapi.get().ex('from %s import %s' % (module, submodules))
"""Doctest-related utilities for IPython. For most common uses, all you should need to run is:: from IPython.dtutils import idoctest See the idoctest docstring below for usage details. """ import doctest import sys from IPython.core import ipapi ip = ipapi.get() def rundoctest(text, ns=None, eraise=False): """Run a the input source as a doctest, in the caller's namespace. :Parameters: text : str Source to execute. :Keywords: ns : dict (None) Namespace where the code should be executed. If not given, the caller's locals and globals are used. eraise : bool (False) If true, immediately raise any exceptions instead of reporting them at the end. This allows you to then do interactive debugging via IPython's facilities (use %debug after the fact, or with %pdb for
def main(): ip = ipapi.get() o = ip.options # autocall to "full" mode (smart mode is default, I like full mode) o.autocall = 2 # Jason Orendorff's path class is handy to have in user namespace # if you are doing shell-like stuff try: ip.ex("from IPython.external.path import path" ) except ImportError: pass # beefed up %env is handy in shell mode import envpersist # To see where mycmd resides (in path/aliases), do %which mycmd import ipy_which # tab completers for hg, svn, ... import ipy_app_completers # To make executables foo and bar in mybin usable without PATH change, do: # %rehashdir c:/mybin # %store foo # %store bar import ipy_rehashdir # does not work without subprocess module! #import ipy_signals ip.ex('import os') ip.ex("def up(): os.chdir('..')") ip.user_ns['LA'] = LastArgFinder() # You can assign to _prompt_title variable # to provide some extra information for prompt # (e.g. the current mode, host/username...) ip.user_ns['_prompt_title'] = '' # Nice prompt o.prompt_in1= r'\C_Green${_prompt_title}\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Green|\#> ' o.prompt_in2= r'\C_Green|\C_LightGreen\D\C_Green> ' o.prompt_out= '<\#> ' from IPython.core import release import sys # Non-chatty banner o.banner = "IPython %s [on Py %s]\n" % (release.version,sys.version.split(None,1)[0]) ip.default_option('cd','-q') ip.default_option('macro', '-r') # If you only rarely want to execute the things you %edit... #ip.default_option('edit','-x') o.prompts_pad_left="1" # Remove all blank lines in between prompts, like a normal shell. o.separate_in="0" o.separate_out="0" o.separate_out2="0" # now alias all syscommands db = ip.db syscmds = db.get("syscmdlist",[] ) if not syscmds: print(textwrap.dedent(""" System command list not initialized, probably the first run... running %rehashx to refresh the command list. Run %rehashx again to refresh command list (after installing new software etc.) """)) ip.magic('rehashx') syscmds = db.get("syscmdlist") # lowcase aliases on win32 only if os.name == 'posix': mapper = lambda s:s else: def mapper(s): return s.lower() for cmd in syscmds: # print "sys",cmd #dbg noext, ext = os.path.splitext(cmd) if ext.lower() == '.exe': cmd = noext key = mapper(cmd) if key not in ip.alias_manager.alias_table: # Dots will be removed from alias names, since ipython # assumes names with dots to be python code ip.define_alias(key.replace('.',''), cmd) # mglob combines 'find', recursion, exclusion... '%mglob?' to learn more ip.load("IPython.external.mglob") # win32 is crippled w/o cygwin, try to help it a little bit if sys.platform == 'win32': if 'cygwin' in os.environ['PATH'].lower(): # use the colors of cygwin ls (recommended) ip.define_alias('d', 'ls -F --color=auto') else: # get icp, imv, imkdir, igrep, irm,... ip.load('ipy_fsops') # and the next best thing to real 'ls -F' ip.define_alias('d','dir /w /og /on') ip.set_hook('input_prefilter', slash_prefilter_f) extend_shell_behavior(ip)
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(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: 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 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 structured_traceback(self, etype, evalue, etb, tb_offset=None, context=5): """Return a nice text document describing the traceback.""" tb_offset = self.tb_offset if tb_offset is None else tb_offset # some locals try: etype = etype.__name__ except AttributeError: pass Colors = self.Colors # just a shorthand + quicker name lookup ColorsNormal = Colors.Normal # used a lot col_scheme = self.color_scheme_table.active_scheme_name indent = ' '*INDENT_SIZE em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) undefined = '%sundefined%s' % (Colors.em, ColorsNormal) exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal) # some internal-use functions def text_repr(value): """Hopefully pretty robust repr equivalent.""" # this is pretty horrible but should always return *something* try: return pydoc.text.repr(value) except KeyboardInterrupt: raise except: try: return repr(value) except KeyboardInterrupt: raise except: try: # all still in an except block so we catch # getattr raising name = getattr(value, '__name__', None) if name: # ick, recursion return text_repr(name) klass = getattr(value, '__class__', None) if klass: return '%s instance' % text_repr(klass) except KeyboardInterrupt: raise except: return 'UNRECOVERABLE REPR FAILURE' def eqrepr(value, repr=text_repr): return '=%s' % repr(value) def nullrepr(value, repr=text_repr): return '' # meat of the code begins try: etype = etype.__name__ except AttributeError: pass if self.long_header: # 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, '-'*75, ColorsNormal, exc, ' '*(75-len(str(etype))-len(pyver)), pyver, date.rjust(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, 'Traceback (most recent call last)'.\ rjust(75 - len(str(etype)) ) ) frames = [] # Flush cache before calling inspect. This helps alleviate some of the # problems with python 2.3's inspect.py. ##self.check_cache() # Drop topmost frames if requested try: # Try the default getinnerframes and Alex's: Alex's fixes some # problems, but it generates empty tracebacks for console errors # (5 blanks lines) where none should be returned. #records = inspect.getinnerframes(etb, context)[tb_offset:] #print 'python records:', records # dbg records = _fixed_getinnerframes(etb, context, tb_offset) #print 'alex records:', records # dbg 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=self.ostream) 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 if not file: file = '?' elif not(file.startswith(str("<")) and file.endswith(str(">"))): # Guess that filenames like <string> aren't real filenames, so # don't call abspath on them. try: file = abspath(file) except OSError: # Not sure if this can still happen: abspath now works with # file names like <string> pass file = py3compat.cast_unicode(file, util_path.fs_encoding) link = tpl_link % file args, varargs, varkw, locals = inspect.getargvalues(frame) 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: # This happens in situations like errors inside generator # expressions, where local variables are listed in the # line, but can't be extracted from the frame. I'm not # 100% sure this isn't actually a bug in inspect itself, # but since there's no info for us to compute with, the # best we can do is report the failure and move on. Here # we must *not* call any traceback construction again, # because that would mess up use of %debug later on. So we # simply report the failure and move on. The only # limitation will be that this frame won't have locals # listed in the call signature. Quite subtle problem... # I can't think of a good way to validate this in a unit # test, but running a script consisting of: # dict( (k,v.strip()) for (k,v) in range(10) ) # will illustrate the error, if this exception catch is # disabled. call = tpl_call_fail % func # Don't attempt to tokenize binary files. if file.endswith(('.so', '.pyd', '.dll')): frames.append('%s %s\n' % (link,call)) continue elif file.endswith(('.pyc','.pyo')): # Look up the corresponding source file. file = pyfile.source_from_cache(file) def linereader(file=file, lnum=[lnum], getline=ulinecache.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: names = [] name_cont = False for token_type, token, start, end, line in generate_tokens(linereader): # build composite names if token_type == tokenize.NAME and token not in keyword.kwlist: if name_cont: # Continuation of a dotted name try: names[-1].append(token) except IndexError: names.append([token]) 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 == '.': name_cont = True elif token_type == tokenize.NEWLINE: break except (IndexError, UnicodeDecodeError): # signals exit of tokenizer pass except tokenize.TokenError as 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) # Join composite names (e.g. "dict.fromkeys") names = ['.'.join(n) for n in names] # 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 name_base in locals: try: value = repr(eval(name_full,locals)) except: value = undefined else: value = undefined name = tpl_local_var % name_full else: if name_base in frame.f_globals: 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) if index is None: frames.append(level) else: frames.append('%s%s' % (level,''.join( _format_traceback_lines(lnum,index,lines,Colors,lvals, col_scheme)))) # Get (safely) a string form of the exception info try: etype_str,evalue_str = map(str,(etype,evalue)) except: # User exception is improperly defined. etype,evalue = str,sys.exc_info()[:2] etype_str,evalue_str = map(str,(etype,evalue)) # ... and format it exception = ['%s%s%s: %s' % (Colors.excName, etype_str, ColorsNormal, py3compat.cast_unicode(evalue_str))] if (not py3compat.PY3) and type(evalue) is types.InstanceType: try: names = [w for w in dir(evalue) if isinstance(w, basestring)] except: # Every now and then, an object with funny inernals blows up # when dir() is called on it. We do the best we can to report # the problem and continue _m = '%sException reporting error (object with broken dir())%s:' exception.append(_m % (Colors.excName,ColorsNormal)) etype_str,evalue_str = map(str,sys.exc_info()[:2]) exception.append('%s%s%s: %s' % (Colors.excName,etype_str, ColorsNormal, py3compat.cast_unicode(evalue_str))) names = [] for name in names: value = text_repr(getattr(evalue, name)) exception.append('\n%s%s = %s' % (indent, name, value)) # vds: >> if records: filepath, lnum = records[-1][1:3] #print "file:", str(file), "linenb", str(lnum) # dbg filepath = os.path.abspath(filepath) ipinst = ipapi.get() if ipinst is not None: ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) # vds: << # return all our info assembled as a single string # return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) return [head] + frames + [''.join(exception[0])]
def __call__(self, hist_idx = None): ip = ipapi.get() if hist_idx is None: return str(self) return ip.input_hist_raw[hist_idx].strip().split()[-1]
def structured_traceback(self, etype, evalue, etb, tb_offset=None, context=5): """Return a nice text document describing the traceback.""" tb_offset = self.tb_offset if tb_offset is None else tb_offset # some locals try: etype = etype.__name__ except AttributeError: pass Colors = self.Colors # just a shorthand + quicker name lookup ColorsNormal = Colors.Normal # used a lot col_scheme = self.color_scheme_table.active_scheme_name indent = ' '*INDENT_SIZE em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) undefined = '%sundefined%s' % (Colors.em, ColorsNormal) exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal) # some internal-use functions def text_repr(value): """Hopefully pretty robust repr equivalent.""" # this is pretty horrible but should always return *something* try: return pydoc.text.repr(value) except KeyboardInterrupt: raise except: try: return repr(value) except KeyboardInterrupt: raise except: try: # all still in an except block so we catch # getattr raising name = getattr(value, '__name__', None) if name: # ick, recursion return text_repr(name) klass = getattr(value, '__class__', None) if klass: return '%s instance' % text_repr(klass) except KeyboardInterrupt: raise except: return 'UNRECOVERABLE REPR FAILURE' def eqrepr(value, repr=text_repr): return '=%s' % repr(value) def nullrepr(value, repr=text_repr): return '' # meat of the code begins try: etype = etype.__name__ except AttributeError: pass if self.long_header: # 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, '-'*75, ColorsNormal, exc, ' '*(75-len(str(etype))-len(pyver)), pyver, date.rjust(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, 'Traceback (most recent call last)'.\ rjust(75 - len(str(etype)) ) ) frames = [] # Flush cache before calling inspect. This helps alleviate some of the # problems with python 2.3's inspect.py. ##self.check_cache() # Drop topmost frames if requested try: # Try the default getinnerframes and Alex's: Alex's fixes some # problems, but it generates empty tracebacks for console errors # (5 blanks lines) where none should be returned. #records = inspect.getinnerframes(etb, context)[tb_offset:] #print 'python records:', records # dbg records = _fixed_getinnerframes(etb, context, tb_offset) #print 'alex records:', records # dbg 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=self.ostream) 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 if not file: file = '?' elif not(file.startswith("<") and file.endswith(">")): # Guess that filenames like <string> aren't real filenames, so # don't call abspath on them. try: file = abspath(file) except OSError: # Not sure if this can still happen: abspath now works with # file names like <string> pass link = tpl_link % file args, varargs, varkw, locals = inspect.getargvalues(frame) 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: # This happens in situations like errors inside generator # expressions, where local variables are listed in the # line, but can't be extracted from the frame. I'm not # 100% sure this isn't actually a bug in inspect itself, # but since there's no info for us to compute with, the # best we can do is report the failure and move on. Here # we must *not* call any traceback construction again, # because that would mess up use of %debug later on. So we # simply report the failure and move on. The only # limitation will be that this frame won't have locals # listed in the call signature. Quite subtle problem... # I can't think of a good way to validate this in a unit # test, but running a script consisting of: # dict( (k,v.strip()) for (k,v) in range(10) ) # will illustrate the error, if this exception catch is # disabled. call = tpl_call_fail % func # Don't attempt to tokenize binary files. if file.endswith(('.so', '.pyd', '.dll')): frames.append('%s %s\n' % (link,call)) continue elif file.endswith(('.pyc','.pyo')): # Look up the corresponding source file. file = pyfile.source_from_cache(file) 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: names = [] name_cont = False for token_type, token, start, end, line in generate_tokens(linereader): # build composite names if token_type == tokenize.NAME and token not in keyword.kwlist: if name_cont: # Continuation of a dotted name try: names[-1].append(token) except IndexError: names.append([token]) 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 == '.': name_cont = True elif token_type == tokenize.NEWLINE: break except (IndexError, UnicodeDecodeError): # signals exit of tokenizer pass except tokenize.TokenError as 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) # Join composite names (e.g. "dict.fromkeys") names = ['.'.join(n) for n in names] # 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) if index is None: frames.append(level) else: frames.append('%s%s' % (level,''.join( _format_traceback_lines(lnum,index,lines,Colors,lvals, col_scheme)))) # Get (safely) a string form of the exception info try: etype_str,evalue_str = map(str,(etype,evalue)) except: # User exception is improperly defined. etype,evalue = str,sys.exc_info()[:2] etype_str,evalue_str = map(str,(etype,evalue)) # ... and format it exception = ['%s%s%s: %s' % (Colors.excName, etype_str, ColorsNormal, evalue_str)] if (not py3compat.PY3) and type(evalue) is types.InstanceType: try: names = [w for w in dir(evalue) if isinstance(w, basestring)] except: # Every now and then, an object with funny inernals blows up # when dir() is called on it. We do the best we can to report # the problem and continue _m = '%sException reporting error (object with broken dir())%s:' exception.append(_m % (Colors.excName,ColorsNormal)) etype_str,evalue_str = map(str,sys.exc_info()[:2]) exception.append('%s%s%s: %s' % (Colors.excName,etype_str, ColorsNormal, evalue_str)) names = [] for name in names: value = text_repr(getattr(evalue, name)) exception.append('\n%s%s = %s' % (indent, name, value)) # vds: >> if records: filepath, lnum = records[-1][1:3] #print "file:", str(file), "linenb", str(lnum) # dbg filepath = os.path.abspath(filepath) ipinst = ipapi.get() if ipinst is not None: ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) # vds: << # return all our info assembled as a single string # return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) return [head] + frames + [''.join(exception[0])]
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
try: from IPython.core import ipapi except ImportError: from IPython import ipapi def import_all(module): ipapi.get().ex("from %s import *" % module) def import_x(module, submodules): if not isinstance(submodules, str): submodules = (",").join(submodules) ipapi.get().ex("from %s import %s" % (module, submodules)) import_all("requests") # ipapi.get().ex('%load_ext autoreload') # ipapi.get().ex("%autoreload 2") import_x("db_logic", "DBConnection") ipapi.get().ex("conn = DBConnection()")
def import_x(module, submodules): if not isinstance(submodules, str): submodules = (",").join(submodules) ipapi.get().ex("from %s import %s" % (module, submodules))
def import_all(module): ipapi.get().ex("from %s import *" % module)
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 = [] try: stype = Colors.excName + etype.__name__ + Colors.Normal except AttributeError: stype = etype # String exceptions don't get special coloring if value is None: list.append( str(stype) + '\n') else: if etype is SyntaxError: try: msg, (filename, lineno, offset, line) = value except: have_filedata = False else: have_filedata = True #print 'filename is',filename # dbg if not filename: filename = "<string>" list.append('%s File %s"%s"%s, line %s%d%s\n' % \ (Colors.normalEm, Colors.filenameEm, filename, Colors.normalEm, Colors.linenoEm, lineno, Colors.Normal )) if line is not None: i = 0 while i < len(line) and line[i].isspace(): i = i+1 list.append('%s %s%s\n' % (Colors.line, line.strip(), Colors.Normal)) if offset is not None: s = ' ' for c in line[i:offset-1]: if c.isspace(): s = s + c else: s = s + ' ' list.append('%s%s^%s\n' % (Colors.caret, s, Colors.Normal) ) value = msg 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(filename, lineno, 0) return list
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(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: 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 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)
class VerboseTB(TBTools): """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead of HTML. Requires inspect and pydoc. Crazy, man. Modified version which optionally strips the topmost entries from the traceback, to be used with alternate interpreters (because their own code would appear in the traceback).""" def __init__(self,color_scheme = 'Linux', call_pdb=False, ostream=None, tb_offset=0, long_header=False, include_vars=True, check_cache=None): """Specify traceback offset, headers and color scheme. Define how many frames to drop from the tracebacks. Calling it with tb_offset=1 allows use of this handler in interpreters which will have their own code at the top of the traceback (VerboseTB will first remove that frame before printing the traceback info).""" TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, ostream=ostream) self.tb_offset = tb_offset self.long_header = long_header self.include_vars = include_vars # By default we use linecache.checkcache, but the user can provide a # different check_cache implementation. This is used by the IPython # kernel to provide tracebacks for interactive code that is cached, # by a compiler instance that flushes the linecache but preserves its # own code cache. if check_cache is None: check_cache = linecache.checkcache self.check_cache = check_cache def structured_traceback(self, etype, evalue, etb, tb_offset=None, context=5): """Return a nice text document describing the traceback.""" tb_offset = self.tb_offset if tb_offset is None else tb_offset # some locals try: etype = etype.__name__ except AttributeError: pass Colors = self.Colors # just a shorthand + quicker name lookup ColorsNormal = Colors.Normal # used a lot col_scheme = self.color_scheme_table.active_scheme_name indent = ' '*INDENT_SIZE em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) undefined = '%sundefined%s' % (Colors.em, ColorsNormal) exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal) # some internal-use functions def text_repr(value): """Hopefully pretty robust repr equivalent.""" # this is pretty horrible but should always return *something* try: return pydoc.text.repr(value) except KeyboardInterrupt: raise except: try: return repr(value) except KeyboardInterrupt: raise except: try: # all still in an except block so we catch # getattr raising name = getattr(value, '__name__', None) if name: # ick, recursion return text_repr(name) klass = getattr(value, '__class__', None) if klass: return '%s instance' % text_repr(klass) except KeyboardInterrupt: raise except: return 'UNRECOVERABLE REPR FAILURE' def eqrepr(value, repr=text_repr): return '=%s' % repr(value) def nullrepr(value, repr=text_repr): return '' # meat of the code begins try: etype = etype.__name__ except AttributeError: pass if self.long_header: # 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, '-'*75, ColorsNormal, exc, ' '*(75-len(str(etype))-len(pyver)), pyver, date.rjust(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, 'Traceback (most recent call last)'.\ rjust(75 - len(str(etype)) ) ) frames = [] # Flush cache before calling inspect. This helps alleviate some of the # problems with python 2.3's inspect.py. ##self.check_cache() # Drop topmost frames if requested try: # Try the default getinnerframes and Alex's: Alex's fixes some # problems, but it generates empty tracebacks for console errors # (5 blanks lines) where none should be returned. #records = inspect.getinnerframes(etb, context)[tb_offset:] #print 'python records:', records # dbg records = _fixed_getinnerframes(etb, context, tb_offset) #print 'alex records:', records # dbg 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=self.ostream) 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=self.ostream) 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: # This happens in situations like errors inside generator # expressions, where local variables are listed in the # line, but can't be extracted from the frame. I'm not # 100% sure this isn't actually a bug in inspect itself, # but since there's no info for us to compute with, the # best we can do is report the failure and move on. Here # we must *not* call any traceback construction again, # because that would mess up use of %debug later on. So we # simply report the failure and move on. The only # limitation will be that this frame won't have locals # listed in the call signature. Quite subtle problem... # I can't think of a good way to validate this in a unit # test, but running a script consisting of: # dict( (k,v.strip()) for (k,v) in range(10) ) # will illustrate the error, if this exception catch is # disabled. 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. for token in generate_tokens(linereader): tokeneater(*token) 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) if index is None: frames.append(level) else: frames.append('%s%s' % (level,''.join( _format_traceback_lines(lnum,index,lines,Colors,lvals, col_scheme)))) # Get (safely) a string form of the exception info try: etype_str,evalue_str = map(str,(etype,evalue)) except: # User exception is improperly defined. etype,evalue = str,sys.exc_info()[:2] etype_str,evalue_str = map(str,(etype,evalue)) # ... and format it exception = ['%s%s%s: %s' % (Colors.excName, etype_str, ColorsNormal, evalue_str)] if (not py3compat.PY3) and type(evalue) is types.InstanceType: try: names = [w for w in dir(evalue) if isinstance(w, basestring)] except: # Every now and then, an object with funny inernals blows up # when dir() is called on it. We do the best we can to report # the problem and continue _m = '%sException reporting error (object with broken dir())%s:' exception.append(_m % (Colors.excName,ColorsNormal)) etype_str,evalue_str = map(str,sys.exc_info()[:2]) exception.append('%s%s%s: %s' % (Colors.excName,etype_str, ColorsNormal, evalue_str)) names = [] for name in names: value = text_repr(getattr(evalue, name)) exception.append('\n%s%s = %s' % (indent, name, value)) # vds: >> if records: filepath, lnum = records[-1][1:3] #print "file:", str(file), "linenb", str(lnum) # dbg filepath = os.path.abspath(filepath) ipinst = ipapi.get() if ipinst is not None: ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) # vds: << # return all our info assembled as a single string # return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) return [head] + frames + [''.join(exception[0])]
def main(): ip = ipapi.get() o = ip.options # autocall to "full" mode (smart mode is default, I like full mode) o.autocall = 2 # Jason Orendorff's path class is handy to have in user namespace # if you are doing shell-like stuff try: ip.ex("from IPython.external.path import path") except ImportError: pass # beefed up %env is handy in shell mode import envpersist # To see where mycmd resides (in path/aliases), do %which mycmd import ipy_which # tab completers for hg, svn, ... import ipy_app_completers # To make executables foo and bar in mybin usable without PATH change, do: # %rehashdir c:/mybin # %store foo # %store bar import ipy_rehashdir # does not work without subprocess module! #import ipy_signals ip.ex('import os') ip.ex("def up(): os.chdir('..')") ip.user_ns['LA'] = LastArgFinder() # You can assign to _prompt_title variable # to provide some extra information for prompt # (e.g. the current mode, host/username...) ip.user_ns['_prompt_title'] = '' # Nice prompt o.prompt_in1 = r'\C_Green${_prompt_title}\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Green|\#> ' o.prompt_in2 = r'\C_Green|\C_LightGreen\D\C_Green> ' o.prompt_out = '<\#> ' from IPython.core import release import sys # Non-chatty banner o.banner = "IPython %s [on Py %s]\n" % (release.version, sys.version.split(None, 1)[0]) ip.default_option('cd', '-q') ip.default_option('macro', '-r') # If you only rarely want to execute the things you %edit... #ip.default_option('edit','-x') o.prompts_pad_left = "1" # Remove all blank lines in between prompts, like a normal shell. o.separate_in = "0" o.separate_out = "0" o.separate_out2 = "0" # now alias all syscommands db = ip.db syscmds = db.get("syscmdlist", []) if not syscmds: print textwrap.dedent(""" System command list not initialized, probably the first run... running %rehashx to refresh the command list. Run %rehashx again to refresh command list (after installing new software etc.) """) ip.magic('rehashx') syscmds = db.get("syscmdlist") # lowcase aliases on win32 only if os.name == 'posix': mapper = lambda s: s else: def mapper(s): return s.lower() for cmd in syscmds: # print "sys",cmd #dbg noext, ext = os.path.splitext(cmd) if ext.lower() == '.exe': cmd = noext key = mapper(cmd) if key not in ip.alias_manager.alias_table: # Dots will be removed from alias names, since ipython # assumes names with dots to be python code ip.define_alias(key.replace('.', ''), cmd) # win32 is crippled w/o cygwin, try to help it a little bit if sys.platform == 'win32': if 'cygwin' in os.environ['PATH'].lower(): # use the colors of cygwin ls (recommended) ip.define_alias('d', 'ls -F --color=auto') else: # get icp, imv, imkdir, igrep, irm,... ip.load('ipy_fsops') # and the next best thing to real 'ls -F' ip.define_alias('d', 'dir /w /og /on') ip.set_hook('input_prefilter', slash_prefilter_f) extend_shell_behavior(ip)
try: from IPython.core import ipapi except ImportError: from IPython import ipapi def import_all(module): ipapi.get().ex('from %s import *' % module) def import_x(module, submodules): if not isinstance(submodules, str): submodules = (',').join(submodules) ipapi.get().ex('from %s import %s' % (module, submodules)) import_all("requests") # ipapi.get().ex('%load_ext autoreload') # ipapi.get().ex("%autoreload 2") import_x("db_logic", "DBConnection") ipapi.get().ex('conn = DBConnection()')
def __call__(self, hist_idx=None): ip = ipapi.get() if hist_idx is None: return str(self) return ip.input_hist_raw[hist_idx].strip().split()[-1]
def set_trace(): ip = ipapi.get() def_colors = ip.colors Pdb(def_colors).set_trace(sys._getframe().f_back)
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