##// END OF EJS Templates
sys.displayhook is now managed dynamically by display_trap.
Brian Granger -
Show More
@@ -0,0 +1,62 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 A context manager for handling sys.displayhook.
5
6 Authors:
7
8 * Robert Kern
9 * Brian Granger
10 """
11
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
18
19 #-----------------------------------------------------------------------------
20 # Imports
21 #-----------------------------------------------------------------------------
22
23 import sys
24
25 from IPython.core.component import Component
26
27 #-----------------------------------------------------------------------------
28 # Classes and functions
29 #-----------------------------------------------------------------------------
30
31
32 class DisplayTrap(Component):
33 """Object to manage sys.displayhook.
34
35 This came from IPython.core.kernel.display_hook, but is simplified
36 (no callbacks or formatters) until more of the core is refactored.
37 """
38
39 def __init__(self, parent, hook):
40 super(DisplayTrap, self).__init__(parent, None, None)
41
42 self.hook = hook
43 self.old_hook = None
44
45 def __enter__(self):
46 self.set()
47 return self
48
49 def __exit__(self, type, value, traceback):
50 self.unset()
51 return True
52
53 def set(self):
54 """Set the hook."""
55 if sys.displayhook is not self.hook:
56 self.old_hook = sys.displayhook
57 sys.displayhook = self.hook
58
59 def unset(self):
60 """Unset the hook."""
61 sys.displayhook = self.old_hook
62
@@ -1,107 +1,107 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 A context manager for managing things injected into :mod:`__builtin__`.
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 """
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Copyright (C) 2008-2009 The IPython Development Team
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 22 import __builtin__
23 23
24 24 from IPython.core.component import Component
25 25 from IPython.core.quitter import Quitter
26 26
27 27 from IPython.utils.traitlets import Instance
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Classes and functions
31 31 #-----------------------------------------------------------------------------
32 32
33 33
34 34 class BuiltinUndefined(object): pass
35 35 BuiltinUndefined = BuiltinUndefined()
36 36
37 37
38 38 class BuiltinTrap(Component):
39 39 shell = Instance('IPython.core.iplib.InteractiveShell')
40 40
41 def __init__(self, parent, name=None, config=None):
42 super(BuiltinTrap, self).__init__(parent, name, config)
41 def __init__(self, parent):
42 super(BuiltinTrap, self).__init__(parent, None, None)
43 43 # Don't just grab parent!!!
44 44 self.shell = Component.get_instances(root=self.root,
45 45 klass='IPython.core.iplib.InteractiveShell')[0]
46 46 self._orig_builtins = {}
47 47
48 48 def __enter__(self):
49 49 self.set()
50 50 # I return self, so callers can use add_builtin in a with clause.
51 51 return self
52 52
53 53 def __exit__(self, type, value, traceback):
54 54 self.unset()
55 55 return True
56 56
57 57 def add_builtin(self, key, value):
58 58 """Add a builtin and save the original."""
59 59 orig = __builtin__.__dict__.get(key, BuiltinUndefined)
60 60 self._orig_builtins[key] = orig
61 61 __builtin__.__dict__[key] = value
62 62
63 63 def remove_builtin(self, key):
64 64 """Remove an added builtin and re-set the original."""
65 65 try:
66 66 orig = self._orig_builtins.pop(key)
67 67 except KeyError:
68 68 pass
69 69 else:
70 70 if orig is BuiltinUndefined:
71 71 del __builtin__.__dict__[key]
72 72 else:
73 73 __builtin__.__dict__[key] = orig
74 74
75 75 def set(self):
76 76 """Store ipython references in the __builtin__ namespace."""
77 77 self.add_builtin('exit', Quitter(self.shell, 'exit'))
78 78 self.add_builtin('quit', Quitter(self.shell, 'quit'))
79 79
80 80 # Recursive reload function
81 81 try:
82 82 from IPython.lib import deepreload
83 83 if self.shell.deep_reload:
84 84 self.add_builtin('reload', deepreload.reload)
85 85 else:
86 86 self.add_builtin('dreload', deepreload.reload)
87 87 del deepreload
88 88 except ImportError:
89 89 pass
90 90
91 91 # Keep in the builtins a flag for when IPython is active. We set it
92 92 # with setdefault so that multiple nested IPythons don't clobber one
93 93 # another. Each will increase its value by one upon being activated,
94 94 # which also gives us a way to determine the nesting level.
95 95 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
96 96
97 97 def unset(self):
98 98 """Remove any builtins which might have been added by add_builtins, or
99 99 restore overwritten ones to their previous values."""
100 100 for key in self._orig_builtins.keys():
101 101 self.remove_builtin(key)
102 102 self._orig_builtins.clear()
103 103 self._builtins_added = False
104 104 try:
105 105 del __builtin__.__dict__['__IPYTHON__active']
106 106 except KeyError:
107 107 pass No newline at end of file
@@ -1,282 +1,260 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 An embedded IPython shell.
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez
10 10
11 11 Notes
12 12 -----
13 13 """
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Copyright (C) 2008-2009 The IPython Development Team
17 17 #
18 18 # Distributed under the terms of the BSD License. The full license is in
19 19 # the file COPYING, distributed as part of this software.
20 20 #-----------------------------------------------------------------------------
21 21
22 22 #-----------------------------------------------------------------------------
23 23 # Imports
24 24 #-----------------------------------------------------------------------------
25 25
26 26 from __future__ import with_statement
27 27
28 28 import sys
29 from contextlib import nested
29 30
30 31 from IPython.core import ultratb
31 32 from IPython.core.iplib import InteractiveShell
32 33
33 34 from IPython.utils.traitlets import Bool, Str, CBool
34 35 from IPython.utils.genutils import ask_yes_no
35 36
36 37 #-----------------------------------------------------------------------------
37 38 # Classes and functions
38 39 #-----------------------------------------------------------------------------
39 40
40 41 # This is an additional magic that is exposed in embedded shells.
41 42 def kill_embedded(self,parameter_s=''):
42 43 """%kill_embedded : deactivate for good the current embedded IPython.
43 44
44 45 This function (after asking for confirmation) sets an internal flag so that
45 46 an embedded IPython will never activate again. This is useful to
46 47 permanently disable a shell that is being called inside a loop: once you've
47 48 figured out what you needed from it, you may then kill it and the program
48 49 will then continue to run without the interactive shell interfering again.
49 50 """
50 51
51 52 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
52 53 "(y/n)? [y/N] ",'n')
53 54 if kill:
54 55 self.embedded_active = False
55 56 print "This embedded IPython will not reactivate anymore once you exit."
56 57
57 58
58 59 class InteractiveShellEmbed(InteractiveShell):
59 60
60 61 dummy_mode = Bool(False)
61 62 exit_msg = Str('')
62 63 embedded = CBool(True)
63 64 embedded_active = CBool(True)
64 65
65 66 def __init__(self, parent=None, config=None, ipythondir=None, usage=None,
66 67 user_ns=None, user_global_ns=None,
67 68 banner1=None, banner2=None,
68 69 custom_exceptions=((),None), exit_msg=''):
69 70
70 # First we need to save the state of sys.displayhook and
71 # sys.ipcompleter so we can restore it when we are done.
72 self.save_sys_displayhook()
73 71 self.save_sys_ipcompleter()
74 72
75 73 super(InteractiveShellEmbed,self).__init__(
76 74 parent=parent, config=config, ipythondir=ipythondir, usage=usage,
77 75 user_ns=user_ns, user_global_ns=user_global_ns,
78 banner1=banner1, banner2=banner2,
76 banner1=banner1, banner2=banner2,
79 77 custom_exceptions=custom_exceptions)
80 78
81 self.save_sys_displayhook_embed()
82 79 self.exit_msg = exit_msg
83 80 self.define_magic("kill_embedded", kill_embedded)
84 81
85 82 # don't use the ipython crash handler so that user exceptions aren't
86 83 # trapped
87 84 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
88 85 mode=self.xmode,
89 86 call_pdb=self.pdb)
90 87
91 self.restore_sys_displayhook()
92 88 self.restore_sys_ipcompleter()
93 89
94 90 def init_sys_modules(self):
95 91 pass
96 92
97 def save_sys_displayhook(self):
98 # sys.displayhook is a global, we need to save the user's original
99 # Don't rely on __displayhook__, as the user may have changed that.
100 self.sys_displayhook_orig = sys.displayhook
101
102 93 def save_sys_ipcompleter(self):
103 94 """Save readline completer status."""
104 95 try:
105 96 #print 'Save completer',sys.ipcompleter # dbg
106 97 self.sys_ipcompleter_orig = sys.ipcompleter
107 98 except:
108 99 pass # not nested with IPython
109 100
110 def restore_sys_displayhook(self):
111 sys.displayhook = self.sys_displayhook_orig
112
113 101 def restore_sys_ipcompleter(self):
114 102 """Restores the readline completer which was in place.
115 103
116 104 This allows embedded IPython within IPython not to disrupt the
117 105 parent's completion.
118 106 """
119 107 try:
120 108 self.readline.set_completer(self.sys_ipcompleter_orig)
121 109 sys.ipcompleter = self.sys_ipcompleter_orig
122 110 except:
123 111 pass
124 112
125 def save_sys_displayhook_embed(self):
126 self.sys_displayhook_embed = sys.displayhook
127
128 def restore_sys_displayhook_embed(self):
129 sys.displayhook = self.sys_displayhook_embed
130
131 113 def __call__(self, header='', local_ns=None, global_ns=None, dummy=None,
132 114 stack_depth=1):
133 115 """Activate the interactive interpreter.
134 116
135 117 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
136 118 the interpreter shell with the given local and global namespaces, and
137 119 optionally print a header string at startup.
138 120
139 121 The shell can be globally activated/deactivated using the
140 122 set/get_dummy_mode methods. This allows you to turn off a shell used
141 123 for debugging globally.
142 124
143 125 However, *each* time you call the shell you can override the current
144 126 state of dummy_mode with the optional keyword parameter 'dummy'. For
145 127 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
146 128 can still have a specific call work by making it as IPShell(dummy=0).
147 129
148 130 The optional keyword parameter dummy controls whether the call
149 131 actually does anything.
150 132 """
151 133
152 134 # If the user has turned it off, go away
153 135 if not self.embedded_active:
154 136 return
155 137
156 138 # Normal exits from interactive mode set this flag, so the shell can't
157 139 # re-enter (it checks this variable at the start of interactive mode).
158 140 self.exit_now = False
159 141
160 142 # Allow the dummy parameter to override the global __dummy_mode
161 143 if dummy or (dummy != 0 and self.dummy_mode):
162 144 return
163 145
164 self.restore_sys_displayhook_embed()
165
166 146 if self.has_readline:
167 147 self.set_completer()
168 148
169 149 if self.banner and header:
170 150 format = '%s\n%s\n'
171 151 else:
172 152 format = '%s%s\n'
173 153 banner = format % (self.banner,header)
174 154
175 155 # Call the embedding code with a stack depth of 1 so it can skip over
176 156 # our call and get the original caller's namespaces.
177 self.mainloop(banner, local_ns, global_ns,
157 self.mainloop(banner, local_ns, global_ns,
178 158 stack_depth=stack_depth)
179 159
180 160 if self.exit_msg is not None:
181 161 print self.exit_msg
182
183 # Restore global systems (display, completion)
184 self.restore_sys_displayhook()
162
185 163 self.restore_sys_ipcompleter()
186 164
187 165 def mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
188 166 """Embeds IPython into a running python program.
189 167
190 168 Input:
191 169
192 170 - header: An optional header message can be specified.
193 171
194 172 - local_ns, global_ns: working namespaces. If given as None, the
195 173 IPython-initialized one is updated with __main__.__dict__, so that
196 174 program variables become visible but user-specific configuration
197 175 remains possible.
198 176
199 177 - stack_depth: specifies how many levels in the stack to go to
200 178 looking for namespaces (when local_ns and global_ns are None). This
201 179 allows an intermediate caller to make sure that this function gets
202 180 the namespace from the intended level in the stack. By default (0)
203 181 it will get its locals and globals from the immediate caller.
204 182
205 183 Warning: it's possible to use this in a program which is being run by
206 184 IPython itself (via %run), but some funny things will happen (a few
207 185 globals get overwritten). In the future this will be cleaned up, as
208 186 there is no fundamental reason why it can't work perfectly."""
209 187
210 188 # Get locals and globals from caller
211 189 if local_ns is None or global_ns is None:
212 190 call_frame = sys._getframe(stack_depth).f_back
213 191
214 192 if local_ns is None:
215 193 local_ns = call_frame.f_locals
216 194 if global_ns is None:
217 195 global_ns = call_frame.f_globals
218 196
219 197 # Update namespaces and fire up interpreter
220 198
221 199 # The global one is easy, we can just throw it in
222 200 self.user_global_ns = global_ns
223 201
224 202 # but the user/local one is tricky: ipython needs it to store internal
225 203 # data, but we also need the locals. We'll copy locals in the user
226 204 # one, but will track what got copied so we can delete them at exit.
227 205 # This is so that a later embedded call doesn't see locals from a
228 206 # previous call (which most likely existed in a separate scope).
229 207 local_varnames = local_ns.keys()
230 208 self.user_ns.update(local_ns)
231 209 #self.user_ns['local_ns'] = local_ns # dbg
232 210
233 211 # Patch for global embedding to make sure that things don't overwrite
234 212 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
235 213 # FIXME. Test this a bit more carefully (the if.. is new)
236 214 if local_ns is None and global_ns is None:
237 215 self.user_global_ns.update(__main__.__dict__)
238 216
239 217 # make sure the tab-completer has the correct frame information, so it
240 218 # actually completes using the frame's locals/globals
241 219 self.set_completer_frame()
242 220
243 with self.builtin_trap:
221 with nested(self.builtin_trap, self.display_trap):
244 222 self.interact(header)
245 223
246 224 # now, purge out the user namespace from anything we might have added
247 225 # from the caller's local namespace
248 226 delvar = self.user_ns.pop
249 227 for var in local_varnames:
250 228 delvar(var,None)
251 229
252 230
253 231 _embedded_shell = None
254 232
255 233
256 234 def embed(header='', config=None, usage=None, banner1=None, banner2=None,
257 235 exit_msg=''):
258 236 """Call this to embed IPython at the current point in your program.
259 237
260 238 The first invocation of this will create an :class:`InteractiveShellEmbed`
261 239 instance and then call it. Consecutive calls just call the already
262 240 created instance.
263 241
264 242 Here is a simple example::
265 243
266 244 from IPython import embed
267 245 a = 10
268 246 b = 20
269 247 embed('First time')
270 248 c = 30
271 249 d = 40
272 250 embed
273 251
274 252 Full customization can be done by passing a :class:`Struct` in as the
275 253 config argument.
276 254 """
277 255 global _embedded_shell
278 256 if _embedded_shell is None:
279 257 _embedded_shell = InteractiveShellEmbed(config=config,
280 258 usage=usage, banner1=banner1, banner2=banner2, exit_msg=exit_msg)
281 259 _embedded_shell(header=header, stack_depth=2)
282 260
@@ -1,2961 +1,2958 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Main IPython Component
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
8 8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
9 9 # Copyright (C) 2008-2009 The IPython Development Team
10 10 #
11 11 # Distributed under the terms of the BSD License. The full license is in
12 12 # the file COPYING, distributed as part of this software.
13 13 #-----------------------------------------------------------------------------
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Imports
17 17 #-----------------------------------------------------------------------------
18 18
19 19 from __future__ import with_statement
20 20
21 21 import __main__
22 22 import __builtin__
23 23 import StringIO
24 24 import bdb
25 25 import codeop
26 26 import exceptions
27 27 import glob
28 28 import keyword
29 29 import new
30 30 import os
31 31 import re
32 32 import shutil
33 33 import string
34 34 import sys
35 35 import tempfile
36 from contextlib import nested
36 37
37 38 from IPython.core import ultratb
38 39 from IPython.core import debugger, oinspect
39 40 from IPython.core import shadowns
40 41 from IPython.core import history as ipcorehist
41 42 from IPython.core import prefilter
42 43 from IPython.core.autocall import IPyAutocall
43 44 from IPython.core.builtin_trap import BuiltinTrap
45 from IPython.core.display_trap import DisplayTrap
44 46 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
45 47 from IPython.core.logger import Logger
46 48 from IPython.core.magic import Magic
47 49 from IPython.core.prompts import CachedOutput
48 50 from IPython.core.page import page
49 51 from IPython.core.component import Component
50 52 from IPython.core.oldusersetup import user_setup
51 53 from IPython.core.usage import interactive_usage, default_banner
52 54 from IPython.core.error import TryNext, UsageError
53 55
54 56 from IPython.extensions import pickleshare
55 57 from IPython.external.Itpl import ItplNS
56 58 from IPython.lib.backgroundjobs import BackgroundJobManager
57 59 from IPython.utils.ipstruct import Struct
58 60 from IPython.utils import PyColorize
59 61 from IPython.utils.genutils import *
60 62 from IPython.utils.strdispatch import StrDispatch
61 63 from IPython.utils.platutils import toggle_set_term_title, set_term_title
62 64
63 65 from IPython.utils.traitlets import (
64 66 Int, Float, Str, CBool, CaselessStrEnum, Enum, List, Unicode
65 67 )
66 68
67 69 #-----------------------------------------------------------------------------
68 70 # Globals
69 71 #-----------------------------------------------------------------------------
70 72
71 73
72 74 # store the builtin raw_input globally, and use this always, in case user code
73 75 # overwrites it (like wx.py.PyShell does)
74 76 raw_input_original = raw_input
75 77
76 78 # compiled regexps for autoindent management
77 79 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
78 80
79 81
80 82 #-----------------------------------------------------------------------------
81 83 # Utilities
82 84 #-----------------------------------------------------------------------------
83 85
84 86
85 87 ini_spaces_re = re.compile(r'^(\s+)')
86 88
87 89
88 90 def num_ini_spaces(strng):
89 91 """Return the number of initial spaces in a string"""
90 92
91 93 ini_spaces = ini_spaces_re.match(strng)
92 94 if ini_spaces:
93 95 return ini_spaces.end()
94 96 else:
95 97 return 0
96 98
97 99
98 100 def softspace(file, newvalue):
99 101 """Copied from code.py, to remove the dependency"""
100 102
101 103 oldvalue = 0
102 104 try:
103 105 oldvalue = file.softspace
104 106 except AttributeError:
105 107 pass
106 108 try:
107 109 file.softspace = newvalue
108 110 except (AttributeError, TypeError):
109 111 # "attribute-less object" or "read-only attributes"
110 112 pass
111 113 return oldvalue
112 114
113 115
114 116 class SpaceInInput(exceptions.Exception): pass
115 117
116 118 class Bunch: pass
117 119
118 120 class InputList(list):
119 121 """Class to store user input.
120 122
121 123 It's basically a list, but slices return a string instead of a list, thus
122 124 allowing things like (assuming 'In' is an instance):
123 125
124 126 exec In[4:7]
125 127
126 128 or
127 129
128 130 exec In[5:9] + In[14] + In[21:25]"""
129 131
130 132 def __getslice__(self,i,j):
131 133 return ''.join(list.__getslice__(self,i,j))
132 134
133 135
134 136 class SyntaxTB(ultratb.ListTB):
135 137 """Extension which holds some state: the last exception value"""
136 138
137 139 def __init__(self,color_scheme = 'NoColor'):
138 140 ultratb.ListTB.__init__(self,color_scheme)
139 141 self.last_syntax_error = None
140 142
141 143 def __call__(self, etype, value, elist):
142 144 self.last_syntax_error = value
143 145 ultratb.ListTB.__call__(self,etype,value,elist)
144 146
145 147 def clear_err_state(self):
146 148 """Return the current error state and clear it"""
147 149 e = self.last_syntax_error
148 150 self.last_syntax_error = None
149 151 return e
150 152
151 153
152 154 def get_default_editor():
153 155 try:
154 156 ed = os.environ['EDITOR']
155 157 except KeyError:
156 158 if os.name == 'posix':
157 159 ed = 'vi' # the only one guaranteed to be there!
158 160 else:
159 161 ed = 'notepad' # same in Windows!
160 162 return ed
161 163
162 164
163 165 class SeparateStr(Str):
164 166 """A Str subclass to validate separate_in, separate_out, etc.
165 167
166 168 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
167 169 """
168 170
169 171 def validate(self, obj, value):
170 172 if value == '0': value = ''
171 173 value = value.replace('\\n','\n')
172 174 return super(SeparateStr, self).validate(obj, value)
173 175
174 176
175 177 #-----------------------------------------------------------------------------
176 178 # Main IPython class
177 179 #-----------------------------------------------------------------------------
178 180
179 181
180 182 class InteractiveShell(Component, Magic):
181 183 """An enhanced, interactive shell for Python."""
182 184
183 185 autocall = Enum((0,1,2), config_key='AUTOCALL')
184 186 autoedit_syntax = CBool(False, config_key='AUTOEDIT_SYNTAX')
185 187 autoindent = CBool(True, config_key='AUTOINDENT')
186 188 automagic = CBool(True, config_key='AUTOMAGIC')
187 189 display_banner = CBool(True, config_key='DISPLAY_BANNER')
188 190 banner = Str('')
189 191 banner1 = Str(default_banner, config_key='BANNER1')
190 192 banner2 = Str('', config_key='BANNER2')
191 193 c = Str('', config_key='C')
192 194 cache_size = Int(1000, config_key='CACHE_SIZE')
193 195 classic = CBool(False, config_key='CLASSIC')
194 196 color_info = CBool(True, config_key='COLOR_INFO')
195 197 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
196 198 default_value='LightBG', config_key='COLORS')
197 199 confirm_exit = CBool(True, config_key='CONFIRM_EXIT')
198 200 debug = CBool(False, config_key='DEBUG')
199 201 deep_reload = CBool(False, config_key='DEEP_RELOAD')
200 202 embedded = CBool(False)
201 203 embedded_active = CBool(False)
202 204 editor = Str(get_default_editor(), config_key='EDITOR')
203 205 filename = Str("<ipython console>")
204 206 interactive = CBool(False, config_key='INTERACTIVE')
205 207 ipythondir= Unicode('', config_key='IPYTHONDIR') # Set to os.getcwd() in __init__
206 208 logstart = CBool(False, config_key='LOGSTART')
207 209 logfile = Str('', config_key='LOGFILE')
208 210 logplay = Str('', config_key='LOGPLAY')
209 211 multi_line_specials = CBool(True, config_key='MULTI_LINE_SPECIALS')
210 212 object_info_string_level = Enum((0,1,2), default_value=0,
211 213 config_keys='OBJECT_INFO_STRING_LEVEL')
212 214 pager = Str('less', config_key='PAGER')
213 215 pdb = CBool(False, config_key='PDB')
214 216 pprint = CBool(True, config_key='PPRINT')
215 217 profile = Str('', config_key='PROFILE')
216 218 prompt_in1 = Str('In [\\#]: ', config_key='PROMPT_IN1')
217 219 prompt_in2 = Str(' .\\D.: ', config_key='PROMPT_IN2')
218 220 prompt_out = Str('Out[\\#]: ', config_key='PROMPT_OUT1')
219 221 prompts_pad_left = CBool(True, config_key='PROMPTS_PAD_LEFT')
220 222 quiet = CBool(False, config_key='QUIET')
221 223
222 224 readline_use = CBool(True, config_key='READLINE_USE')
223 225 readline_merge_completions = CBool(True,
224 226 config_key='READLINE_MERGE_COMPLETIONS')
225 227 readline_omit__names = Enum((0,1,2), default_value=0,
226 228 config_key='READLINE_OMIT_NAMES')
227 229 readline_remove_delims = Str('-/~', config_key='READLINE_REMOVE_DELIMS')
228 230 readline_parse_and_bind = List([
229 231 'tab: complete',
230 232 '"\C-l": possible-completions',
231 233 'set show-all-if-ambiguous on',
232 234 '"\C-o": tab-insert',
233 235 '"\M-i": " "',
234 236 '"\M-o": "\d\d\d\d"',
235 237 '"\M-I": "\d\d\d\d"',
236 238 '"\C-r": reverse-search-history',
237 239 '"\C-s": forward-search-history',
238 240 '"\C-p": history-search-backward',
239 241 '"\C-n": history-search-forward',
240 242 '"\e[A": history-search-backward',
241 243 '"\e[B": history-search-forward',
242 244 '"\C-k": kill-line',
243 245 '"\C-u": unix-line-discard',
244 246 ], allow_none=False, config_key='READLINE_PARSE_AND_BIND'
245 247 )
246 248
247 249 screen_length = Int(0, config_key='SCREEN_LENGTH')
248 250
249 251 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
250 252 separate_in = SeparateStr('\n', config_key='SEPARATE_IN')
251 253 separate_out = SeparateStr('', config_key='SEPARATE_OUT')
252 254 separate_out2 = SeparateStr('', config_key='SEPARATE_OUT2')
253 255
254 256 system_header = Str('IPython system call: ', config_key='SYSTEM_HEADER')
255 257 system_verbose = CBool(False, config_key='SYSTEM_VERBOSE')
256 258 term_title = CBool(False, config_key='TERM_TITLE')
257 259 wildcards_case_sensitive = CBool(True, config_key='WILDCARDS_CASE_SENSITIVE')
258 260 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
259 261 default_value='Context', config_key='XMODE')
260 262
261 263 alias = List(allow_none=False, config_key='ALIAS')
262 264 autoexec = List(allow_none=False)
263 265
264 266 # class attribute to indicate whether the class supports threads or not.
265 267 # Subclasses with thread support should override this as needed.
266 268 isthreaded = False
267 269
268 270 def __init__(self, parent=None, config=None, ipythondir=None, usage=None,
269 271 user_ns=None, user_global_ns=None,
270 272 banner1=None, banner2=None,
271 273 custom_exceptions=((),None)):
272 274
273 275 # This is where traitlets with a config_key argument are updated
274 276 # from the values on config.
275 277 super(InteractiveShell, self).__init__(parent, config=config, name='__IP')
276 278
277 279 # These are relatively independent and stateless
278 280 self.init_ipythondir(ipythondir)
279 281 self.init_instance_attrs()
280 282 self.init_term_title()
281 283 self.init_usage(usage)
282 284 self.init_banner(banner1, banner2)
283 285
284 286 # Create namespaces (user_ns, user_global_ns, alias_table, etc.)
285 287 self.init_create_namespaces(user_ns, user_global_ns)
286 288 # This has to be done after init_create_namespaces because it uses
287 289 # something in self.user_ns, but before init_sys_modules, which
288 290 # is the first thing to modify sys.
289 291 self.save_sys_module_state()
290 292 self.init_sys_modules()
291 293
292 294 self.init_history()
293 295 self.init_encoding()
294 296 self.init_handlers()
295 297
296 298 Magic.__init__(self, self)
297 299
298 300 self.init_syntax_highlighting()
299 301 self.init_hooks()
300 302 self.init_pushd_popd_magic()
301 303 self.init_traceback_handlers(custom_exceptions)
302 304 self.init_user_ns()
303 305 self.init_logger()
304 306 self.init_aliases()
305 307 self.init_builtins()
306 308
307 309 # pre_config_initialization
308 310 self.init_shadow_hist()
309 311
310 312 # The next section should contain averything that was in ipmaker.
311 313 self.init_logstart()
312 314
313 315 # The following was in post_config_initialization
314 316 self.init_inspector()
315 317 self.init_readline()
316 318 self.init_prompts()
317 319 self.init_displayhook()
318 320 self.init_reload_doctest()
319 321 self.init_magics()
320 322 self.init_pdb()
321 323 self.hooks.late_startup_hook()
322 324
323 325 def cleanup(self):
324 326 self.restore_sys_module_state()
325 327
326 328 #-------------------------------------------------------------------------
327 329 # Traitlet changed handlers
328 330 #-------------------------------------------------------------------------
329 331
330 332 def _banner1_changed(self):
331 333 self.compute_banner()
332 334
333 335 def _banner2_changed(self):
334 336 self.compute_banner()
335 337
336 338 @property
337 339 def usable_screen_length(self):
338 340 if self.screen_length == 0:
339 341 return 0
340 342 else:
341 343 num_lines_bot = self.separate_in.count('\n')+1
342 344 return self.screen_length - num_lines_bot
343 345
344 346 def _term_title_changed(self, name, new_value):
345 347 self.init_term_title()
346 348
347 349 #-------------------------------------------------------------------------
348 350 # init_* methods called by __init__
349 351 #-------------------------------------------------------------------------
350 352
351 353 def init_ipythondir(self, ipythondir):
352 354 if ipythondir is not None:
353 355 self.ipythondir = ipythondir
354 356 self.config.IPYTHONDIR = self.ipythondir
355 357 return
356 358
357 359 if hasattr(self.config, 'IPYTHONDIR'):
358 360 self.ipythondir = self.config.IPYTHONDIR
359 361 if not hasattr(self.config, 'IPYTHONDIR'):
360 362 # cdw is always defined
361 363 self.ipythondir = os.getcwd()
362 364
363 365 # The caller must make sure that ipythondir exists. We should
364 366 # probably handle this using a Dir traitlet.
365 367 if not os.path.isdir(self.ipythondir):
366 368 raise IOError('IPython dir does not exist: %s' % self.ipythondir)
367 369
368 370 # All children can just read this
369 371 self.config.IPYTHONDIR = self.ipythondir
370 372
371 373 def init_instance_attrs(self):
372 374 self.jobs = BackgroundJobManager()
373 375 self.more = False
374 376
375 377 # command compiler
376 378 self.compile = codeop.CommandCompiler()
377 379
378 380 # User input buffer
379 381 self.buffer = []
380 382
381 383 # Make an empty namespace, which extension writers can rely on both
382 384 # existing and NEVER being used by ipython itself. This gives them a
383 385 # convenient location for storing additional information and state
384 386 # their extensions may require, without fear of collisions with other
385 387 # ipython names that may develop later.
386 388 self.meta = Struct()
387 389
388 390 # Object variable to store code object waiting execution. This is
389 391 # used mainly by the multithreaded shells, but it can come in handy in
390 392 # other situations. No need to use a Queue here, since it's a single
391 393 # item which gets cleared once run.
392 394 self.code_to_run = None
393 395
394 396 # Flag to mark unconditional exit
395 397 self.exit_now = False
396 398
397 399 # Temporary files used for various purposes. Deleted at exit.
398 400 self.tempfiles = []
399 401
400 402 # Keep track of readline usage (later set by init_readline)
401 403 self.has_readline = False
402 404
403 405 # keep track of where we started running (mainly for crash post-mortem)
404 406 # This is not being used anywhere currently.
405 407 self.starting_dir = os.getcwd()
406 408
407 409 # Indentation management
408 410 self.indent_current_nsp = 0
409 411
410 412 def init_term_title(self):
411 413 # Enable or disable the terminal title.
412 414 if self.term_title:
413 415 toggle_set_term_title(True)
414 416 set_term_title('IPython: ' + abbrev_cwd())
415 417 else:
416 418 toggle_set_term_title(False)
417 419
418 420 def init_usage(self, usage=None):
419 421 if usage is None:
420 422 self.usage = interactive_usage
421 423 else:
422 424 self.usage = usage
423 425
424 426 def init_banner(self, banner1, banner2):
425 427 if self.c: # regular python doesn't print the banner with -c
426 428 self.display_banner = False
427 429 if banner1 is not None:
428 430 self.banner1 = banner1
429 431 if banner2 is not None:
430 432 self.banner2 = banner2
431 433 self.compute_banner()
432 434
433 435 def compute_banner(self):
434 436 self.banner = self.banner1 + '\n'
435 437 if self.profile:
436 438 self.banner += '\nIPython profile: %s\n' % self.profile
437 439 if self.banner2:
438 440 self.banner += '\n' + self.banner2 + '\n'
439 441
440 442 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
441 443 # Create the namespace where the user will operate. user_ns is
442 444 # normally the only one used, and it is passed to the exec calls as
443 445 # the locals argument. But we do carry a user_global_ns namespace
444 446 # given as the exec 'globals' argument, This is useful in embedding
445 447 # situations where the ipython shell opens in a context where the
446 448 # distinction between locals and globals is meaningful. For
447 449 # non-embedded contexts, it is just the same object as the user_ns dict.
448 450
449 451 # FIXME. For some strange reason, __builtins__ is showing up at user
450 452 # level as a dict instead of a module. This is a manual fix, but I
451 453 # should really track down where the problem is coming from. Alex
452 454 # Schmolck reported this problem first.
453 455
454 456 # A useful post by Alex Martelli on this topic:
455 457 # Re: inconsistent value from __builtins__
456 458 # Von: Alex Martelli <aleaxit@yahoo.com>
457 459 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
458 460 # Gruppen: comp.lang.python
459 461
460 462 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
461 463 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
462 464 # > <type 'dict'>
463 465 # > >>> print type(__builtins__)
464 466 # > <type 'module'>
465 467 # > Is this difference in return value intentional?
466 468
467 469 # Well, it's documented that '__builtins__' can be either a dictionary
468 470 # or a module, and it's been that way for a long time. Whether it's
469 471 # intentional (or sensible), I don't know. In any case, the idea is
470 472 # that if you need to access the built-in namespace directly, you
471 473 # should start with "import __builtin__" (note, no 's') which will
472 474 # definitely give you a module. Yeah, it's somewhat confusing:-(.
473 475
474 476 # These routines return properly built dicts as needed by the rest of
475 477 # the code, and can also be used by extension writers to generate
476 478 # properly initialized namespaces.
477 479 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
478 480 user_global_ns)
479 481
480 482 # Assign namespaces
481 483 # This is the namespace where all normal user variables live
482 484 self.user_ns = user_ns
483 485 self.user_global_ns = user_global_ns
484 486
485 487 # An auxiliary namespace that checks what parts of the user_ns were
486 488 # loaded at startup, so we can list later only variables defined in
487 489 # actual interactive use. Since it is always a subset of user_ns, it
488 490 # doesn't need to be seaparately tracked in the ns_table
489 491 self.user_config_ns = {}
490 492
491 493 # A namespace to keep track of internal data structures to prevent
492 494 # them from cluttering user-visible stuff. Will be updated later
493 495 self.internal_ns = {}
494 496
495 497 # Namespace of system aliases. Each entry in the alias
496 498 # table must be a 2-tuple of the form (N,name), where N is the number
497 499 # of positional arguments of the alias.
498 500 self.alias_table = {}
499 501
500 502 # Now that FakeModule produces a real module, we've run into a nasty
501 503 # problem: after script execution (via %run), the module where the user
502 504 # code ran is deleted. Now that this object is a true module (needed
503 505 # so docetst and other tools work correctly), the Python module
504 506 # teardown mechanism runs over it, and sets to None every variable
505 507 # present in that module. Top-level references to objects from the
506 508 # script survive, because the user_ns is updated with them. However,
507 509 # calling functions defined in the script that use other things from
508 510 # the script will fail, because the function's closure had references
509 511 # to the original objects, which are now all None. So we must protect
510 512 # these modules from deletion by keeping a cache.
511 513 #
512 514 # To avoid keeping stale modules around (we only need the one from the
513 515 # last run), we use a dict keyed with the full path to the script, so
514 516 # only the last version of the module is held in the cache. Note,
515 517 # however, that we must cache the module *namespace contents* (their
516 518 # __dict__). Because if we try to cache the actual modules, old ones
517 519 # (uncached) could be destroyed while still holding references (such as
518 520 # those held by GUI objects that tend to be long-lived)>
519 521 #
520 522 # The %reset command will flush this cache. See the cache_main_mod()
521 523 # and clear_main_mod_cache() methods for details on use.
522 524
523 525 # This is the cache used for 'main' namespaces
524 526 self._main_ns_cache = {}
525 527 # And this is the single instance of FakeModule whose __dict__ we keep
526 528 # copying and clearing for reuse on each %run
527 529 self._user_main_module = FakeModule()
528 530
529 531 # A table holding all the namespaces IPython deals with, so that
530 532 # introspection facilities can search easily.
531 533 self.ns_table = {'user':user_ns,
532 534 'user_global':user_global_ns,
533 535 'alias':self.alias_table,
534 536 'internal':self.internal_ns,
535 537 'builtin':__builtin__.__dict__
536 538 }
537 539
538 540 # Similarly, track all namespaces where references can be held and that
539 541 # we can safely clear (so it can NOT include builtin). This one can be
540 542 # a simple list.
541 543 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
542 544 self.alias_table, self.internal_ns,
543 545 self._main_ns_cache ]
544 546
545 547 def init_sys_modules(self):
546 548 # We need to insert into sys.modules something that looks like a
547 549 # module but which accesses the IPython namespace, for shelve and
548 550 # pickle to work interactively. Normally they rely on getting
549 551 # everything out of __main__, but for embedding purposes each IPython
550 552 # instance has its own private namespace, so we can't go shoving
551 553 # everything into __main__.
552 554
553 555 # note, however, that we should only do this for non-embedded
554 556 # ipythons, which really mimic the __main__.__dict__ with their own
555 557 # namespace. Embedded instances, on the other hand, should not do
556 558 # this because they need to manage the user local/global namespaces
557 559 # only, but they live within a 'normal' __main__ (meaning, they
558 560 # shouldn't overtake the execution environment of the script they're
559 561 # embedded in).
560 562
561 563 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
562 564
563 565 try:
564 566 main_name = self.user_ns['__name__']
565 567 except KeyError:
566 568 raise KeyError('user_ns dictionary MUST have a "__name__" key')
567 569 else:
568 570 sys.modules[main_name] = FakeModule(self.user_ns)
569 571
570 572 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
571 573 """Return a valid local and global user interactive namespaces.
572 574
573 575 This builds a dict with the minimal information needed to operate as a
574 576 valid IPython user namespace, which you can pass to the various
575 577 embedding classes in ipython. The default implementation returns the
576 578 same dict for both the locals and the globals to allow functions to
577 579 refer to variables in the namespace. Customized implementations can
578 580 return different dicts. The locals dictionary can actually be anything
579 581 following the basic mapping protocol of a dict, but the globals dict
580 582 must be a true dict, not even a subclass. It is recommended that any
581 583 custom object for the locals namespace synchronize with the globals
582 584 dict somehow.
583 585
584 586 Raises TypeError if the provided globals namespace is not a true dict.
585 587
586 588 :Parameters:
587 589 user_ns : dict-like, optional
588 590 The current user namespace. The items in this namespace should
589 591 be included in the output. If None, an appropriate blank
590 592 namespace should be created.
591 593 user_global_ns : dict, optional
592 594 The current user global namespace. The items in this namespace
593 595 should be included in the output. If None, an appropriate
594 596 blank namespace should be created.
595 597
596 598 :Returns:
597 599 A tuple pair of dictionary-like object to be used as the local namespace
598 600 of the interpreter and a dict to be used as the global namespace.
599 601 """
600 602
601 603 if user_ns is None:
602 604 # Set __name__ to __main__ to better match the behavior of the
603 605 # normal interpreter.
604 606 user_ns = {'__name__' :'__main__',
605 607 '__builtins__' : __builtin__,
606 608 }
607 609 else:
608 610 user_ns.setdefault('__name__','__main__')
609 611 user_ns.setdefault('__builtins__',__builtin__)
610 612
611 613 if user_global_ns is None:
612 614 user_global_ns = user_ns
613 615 if type(user_global_ns) is not dict:
614 616 raise TypeError("user_global_ns must be a true dict; got %r"
615 617 % type(user_global_ns))
616 618
617 619 return user_ns, user_global_ns
618 620
619 621 def init_history(self):
620 622 # List of input with multi-line handling.
621 623 self.input_hist = InputList()
622 624 # This one will hold the 'raw' input history, without any
623 625 # pre-processing. This will allow users to retrieve the input just as
624 626 # it was exactly typed in by the user, with %hist -r.
625 627 self.input_hist_raw = InputList()
626 628
627 629 # list of visited directories
628 630 try:
629 631 self.dir_hist = [os.getcwd()]
630 632 except OSError:
631 633 self.dir_hist = []
632 634
633 635 # dict of output history
634 636 self.output_hist = {}
635 637
636 638 # Now the history file
637 639 try:
638 640 histfname = 'history-%s' % self.profile
639 641 except AttributeError:
640 642 histfname = 'history'
641 643 self.histfile = os.path.join(self.config.IPYTHONDIR, histfname)
642 644
643 645 # Fill the history zero entry, user counter starts at 1
644 646 self.input_hist.append('\n')
645 647 self.input_hist_raw.append('\n')
646 648
647 649 def init_encoding(self):
648 650 # Get system encoding at startup time. Certain terminals (like Emacs
649 651 # under Win32 have it set to None, and we need to have a known valid
650 652 # encoding to use in the raw_input() method
651 653 try:
652 654 self.stdin_encoding = sys.stdin.encoding or 'ascii'
653 655 except AttributeError:
654 656 self.stdin_encoding = 'ascii'
655 657
656 658 def init_handlers(self):
657 659 # escapes for automatic behavior on the command line
658 660 self.ESC_SHELL = '!'
659 661 self.ESC_SH_CAP = '!!'
660 662 self.ESC_HELP = '?'
661 663 self.ESC_MAGIC = '%'
662 664 self.ESC_QUOTE = ','
663 665 self.ESC_QUOTE2 = ';'
664 666 self.ESC_PAREN = '/'
665 667
666 668 # And their associated handlers
667 669 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
668 670 self.ESC_QUOTE : self.handle_auto,
669 671 self.ESC_QUOTE2 : self.handle_auto,
670 672 self.ESC_MAGIC : self.handle_magic,
671 673 self.ESC_HELP : self.handle_help,
672 674 self.ESC_SHELL : self.handle_shell_escape,
673 675 self.ESC_SH_CAP : self.handle_shell_escape,
674 676 }
675 677
676 678 def init_syntax_highlighting(self):
677 679 # Python source parser/formatter for syntax highlighting
678 680 pyformat = PyColorize.Parser().format
679 681 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
680 682
681 683 def init_hooks(self):
682 684 # hooks holds pointers used for user-side customizations
683 685 self.hooks = Struct()
684 686
685 687 self.strdispatchers = {}
686 688
687 689 # Set all default hooks, defined in the IPython.hooks module.
688 690 import IPython.core.hooks
689 691 hooks = IPython.core.hooks
690 692 for hook_name in hooks.__all__:
691 693 # default hooks have priority 100, i.e. low; user hooks should have
692 694 # 0-100 priority
693 695 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
694 696
695 697 def init_pushd_popd_magic(self):
696 698 # for pushd/popd management
697 699 try:
698 700 self.home_dir = get_home_dir()
699 701 except HomeDirError, msg:
700 702 fatal(msg)
701 703
702 704 self.dir_stack = []
703 705
704 706 def init_traceback_handlers(self, custom_exceptions):
705 707 # Syntax error handler.
706 708 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
707 709
708 710 # The interactive one is initialized with an offset, meaning we always
709 711 # want to remove the topmost item in the traceback, which is our own
710 712 # internal code. Valid modes: ['Plain','Context','Verbose']
711 713 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
712 714 color_scheme='NoColor',
713 715 tb_offset = 1)
714 716
715 717 # IPython itself shouldn't crash. This will produce a detailed
716 718 # post-mortem if it does. But we only install the crash handler for
717 719 # non-threaded shells, the threaded ones use a normal verbose reporter
718 720 # and lose the crash handler. This is because exceptions in the main
719 721 # thread (such as in GUI code) propagate directly to sys.excepthook,
720 722 # and there's no point in printing crash dumps for every user exception.
721 723 if self.isthreaded:
722 724 ipCrashHandler = ultratb.FormattedTB()
723 725 else:
724 726 from IPython.core import crashhandler
725 727 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
726 728 self.set_crash_handler(ipCrashHandler)
727 729
728 730 # and add any custom exception handlers the user may have specified
729 731 self.set_custom_exc(*custom_exceptions)
730 732
731 733 def init_logger(self):
732 734 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
733 735 # local shortcut, this is used a LOT
734 736 self.log = self.logger.log
735 737 # template for logfile headers. It gets resolved at runtime by the
736 738 # logstart method.
737 739 self.loghead_tpl = \
738 740 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
739 741 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
740 742 #log# opts = %s
741 743 #log# args = %s
742 744 #log# It is safe to make manual edits below here.
743 745 #log#-----------------------------------------------------------------------
744 746 """
745 747
746 748 def init_logstart(self):
747 749 if self.logplay:
748 750 self.magic_logstart(self.logplay + ' append')
749 751 elif self.logfile:
750 752 self.magic_logstart(self.logfile)
751 753 elif self.logstart:
752 754 self.magic_logstart()
753 755
754 756 def init_aliases(self):
755 757 # dict of things NOT to alias (keywords, builtins and some magics)
756 758 no_alias = {}
757 759 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
758 760 for key in keyword.kwlist + no_alias_magics:
759 761 no_alias[key] = 1
760 762 no_alias.update(__builtin__.__dict__)
761 763 self.no_alias = no_alias
762 764
763 765 # Make some aliases automatically
764 766 # Prepare list of shell aliases to auto-define
765 767 if os.name == 'posix':
766 768 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
767 769 'mv mv -i','rm rm -i','cp cp -i',
768 770 'cat cat','less less','clear clear',
769 771 # a better ls
770 772 'ls ls -F',
771 773 # long ls
772 774 'll ls -lF')
773 775 # Extra ls aliases with color, which need special treatment on BSD
774 776 # variants
775 777 ls_extra = ( # color ls
776 778 'lc ls -F -o --color',
777 779 # ls normal files only
778 780 'lf ls -F -o --color %l | grep ^-',
779 781 # ls symbolic links
780 782 'lk ls -F -o --color %l | grep ^l',
781 783 # directories or links to directories,
782 784 'ldir ls -F -o --color %l | grep /$',
783 785 # things which are executable
784 786 'lx ls -F -o --color %l | grep ^-..x',
785 787 )
786 788 # The BSDs don't ship GNU ls, so they don't understand the
787 789 # --color switch out of the box
788 790 if 'bsd' in sys.platform:
789 791 ls_extra = ( # ls normal files only
790 792 'lf ls -lF | grep ^-',
791 793 # ls symbolic links
792 794 'lk ls -lF | grep ^l',
793 795 # directories or links to directories,
794 796 'ldir ls -lF | grep /$',
795 797 # things which are executable
796 798 'lx ls -lF | grep ^-..x',
797 799 )
798 800 auto_alias = auto_alias + ls_extra
799 801 elif os.name in ['nt','dos']:
800 802 auto_alias = ('ls dir /on',
801 803 'ddir dir /ad /on', 'ldir dir /ad /on',
802 804 'mkdir mkdir','rmdir rmdir','echo echo',
803 805 'ren ren','cls cls','copy copy')
804 806 else:
805 807 auto_alias = ()
806 808 self.auto_alias = [s.split(None,1) for s in auto_alias]
807 809
808 810 # Load default aliases
809 811 for alias, cmd in self.auto_alias:
810 812 self.define_alias(alias,cmd)
811 813
812 814 # Load user aliases
813 815 for alias in self.alias:
814 816 self.magic_alias(alias)
815 817
816 818 def init_builtins(self):
817 819 self.builtin_trap = BuiltinTrap(self)
818 820
819 821 def init_shadow_hist(self):
820 822 try:
821 823 self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db")
822 824 except exceptions.UnicodeDecodeError:
823 825 print "Your ipythondir can't be decoded to unicode!"
824 826 print "Please set HOME environment variable to something that"
825 827 print r"only has ASCII characters, e.g. c:\home"
826 828 print "Now it is", self.config.IPYTHONDIR
827 829 sys.exit()
828 830 self.shadowhist = ipcorehist.ShadowHist(self.db)
829 831
830 832 def init_inspector(self):
831 833 # Object inspector
832 834 self.inspector = oinspect.Inspector(oinspect.InspectColors,
833 835 PyColorize.ANSICodeColors,
834 836 'NoColor',
835 837 self.object_info_string_level)
836 838
837 839 def init_readline(self):
838 840 """Command history completion/saving/reloading."""
839 841
840 842 self.rl_next_input = None
841 843 self.rl_do_indent = False
842 844
843 845 if not self.readline_use:
844 846 return
845 847
846 848 import IPython.utils.rlineimpl as readline
847 849
848 850 if not readline.have_readline:
849 851 self.has_readline = 0
850 852 self.readline = None
851 853 # no point in bugging windows users with this every time:
852 854 warn('Readline services not available on this platform.')
853 855 else:
854 856 sys.modules['readline'] = readline
855 857 import atexit
856 858 from IPython.core.completer import IPCompleter
857 859 self.Completer = IPCompleter(self,
858 860 self.user_ns,
859 861 self.user_global_ns,
860 862 self.readline_omit__names,
861 863 self.alias_table)
862 864 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
863 865 self.strdispatchers['complete_command'] = sdisp
864 866 self.Completer.custom_completers = sdisp
865 867 # Platform-specific configuration
866 868 if os.name == 'nt':
867 869 self.readline_startup_hook = readline.set_pre_input_hook
868 870 else:
869 871 self.readline_startup_hook = readline.set_startup_hook
870 872
871 873 # Load user's initrc file (readline config)
872 874 # Or if libedit is used, load editrc.
873 875 inputrc_name = os.environ.get('INPUTRC')
874 876 if inputrc_name is None:
875 877 home_dir = get_home_dir()
876 878 if home_dir is not None:
877 879 inputrc_name = '.inputrc'
878 880 if readline.uses_libedit:
879 881 inputrc_name = '.editrc'
880 882 inputrc_name = os.path.join(home_dir, inputrc_name)
881 883 if os.path.isfile(inputrc_name):
882 884 try:
883 885 readline.read_init_file(inputrc_name)
884 886 except:
885 887 warn('Problems reading readline initialization file <%s>'
886 888 % inputrc_name)
887 889
888 890 self.has_readline = 1
889 891 self.readline = readline
890 892 # save this in sys so embedded copies can restore it properly
891 893 sys.ipcompleter = self.Completer.complete
892 894 self.set_completer()
893 895
894 896 # Configure readline according to user's prefs
895 897 # This is only done if GNU readline is being used. If libedit
896 898 # is being used (as on Leopard) the readline config is
897 899 # not run as the syntax for libedit is different.
898 900 if not readline.uses_libedit:
899 901 for rlcommand in self.readline_parse_and_bind:
900 902 #print "loading rl:",rlcommand # dbg
901 903 readline.parse_and_bind(rlcommand)
902 904
903 905 # Remove some chars from the delimiters list. If we encounter
904 906 # unicode chars, discard them.
905 907 delims = readline.get_completer_delims().encode("ascii", "ignore")
906 908 delims = delims.translate(string._idmap,
907 909 self.readline_remove_delims)
908 910 readline.set_completer_delims(delims)
909 911 # otherwise we end up with a monster history after a while:
910 912 readline.set_history_length(1000)
911 913 try:
912 914 #print '*** Reading readline history' # dbg
913 915 readline.read_history_file(self.histfile)
914 916 except IOError:
915 917 pass # It doesn't exist yet.
916 918
917 919 atexit.register(self.atexit_operations)
918 920 del atexit
919 921
920 922 # Configure auto-indent for all platforms
921 923 self.set_autoindent(self.autoindent)
922 924
923 925 def init_prompts(self):
924 926 # Initialize cache, set in/out prompts and printing system
925 927 self.outputcache = CachedOutput(self,
926 928 self.cache_size,
927 929 self.pprint,
928 930 input_sep = self.separate_in,
929 931 output_sep = self.separate_out,
930 932 output_sep2 = self.separate_out2,
931 933 ps1 = self.prompt_in1,
932 934 ps2 = self.prompt_in2,
933 935 ps_out = self.prompt_out,
934 936 pad_left = self.prompts_pad_left)
935 937
936 938 # user may have over-ridden the default print hook:
937 939 try:
938 940 self.outputcache.__class__.display = self.hooks.display
939 941 except AttributeError:
940 942 pass
941 943
942 944 def init_displayhook(self):
943 # I don't like assigning globally to sys, because it means when
944 # embedding instances, each embedded instance overrides the previous
945 # choice. But sys.displayhook seems to be called internally by exec,
946 # so I don't see a way around it. We first save the original and then
947 # overwrite it.
948 self.sys_displayhook = sys.displayhook
949 sys.displayhook = self.outputcache
945 self.display_trap = DisplayTrap(self, self.outputcache)
950 946
951 947 def init_reload_doctest(self):
952 948 # Do a proper resetting of doctest, including the necessary displayhook
953 949 # monkeypatching
954 950 try:
955 951 doctest_reload()
956 952 except ImportError:
957 953 warn("doctest module does not exist.")
958 954
959 955 def init_magics(self):
960 956 # Set user colors (don't do it in the constructor above so that it
961 957 # doesn't crash if colors option is invalid)
962 958 self.magic_colors(self.colors)
963 959
964 960 def init_pdb(self):
965 961 # Set calling of pdb on exceptions
966 962 # self.call_pdb is a property
967 963 self.call_pdb = self.pdb
968 964
969 965 # def init_exec_commands(self):
970 966 # for cmd in self.config.EXECUTE:
971 967 # print "execute:", cmd
972 968 # self.api.runlines(cmd)
973 969 #
974 970 # batchrun = False
975 971 # if self.config.has_key('EXECFILE'):
976 972 # for batchfile in [path(arg) for arg in self.config.EXECFILE
977 973 # if arg.lower().endswith('.ipy')]:
978 974 # if not batchfile.isfile():
979 975 # print "No such batch file:", batchfile
980 976 # continue
981 977 # self.api.runlines(batchfile.text())
982 978 # batchrun = True
983 979 # # without -i option, exit after running the batch file
984 980 # if batchrun and not self.interactive:
985 981 # self.ask_exit()
986 982
987 983 # def load(self, mod):
988 984 # """ Load an extension.
989 985 #
990 986 # Some modules should (or must) be 'load()':ed, rather than just imported.
991 987 #
992 988 # Loading will do:
993 989 #
994 990 # - run init_ipython(ip)
995 991 # - run ipython_firstrun(ip)
996 992 # """
997 993 #
998 994 # if mod in self.extensions:
999 995 # # just to make sure we don't init it twice
1000 996 # # note that if you 'load' a module that has already been
1001 997 # # imported, init_ipython gets run anyway
1002 998 #
1003 999 # return self.extensions[mod]
1004 1000 # __import__(mod)
1005 1001 # m = sys.modules[mod]
1006 1002 # if hasattr(m,'init_ipython'):
1007 1003 # m.init_ipython(self)
1008 1004 #
1009 1005 # if hasattr(m,'ipython_firstrun'):
1010 1006 # already_loaded = self.db.get('firstrun_done', set())
1011 1007 # if mod not in already_loaded:
1012 1008 # m.ipython_firstrun(self)
1013 1009 # already_loaded.add(mod)
1014 1010 # self.db['firstrun_done'] = already_loaded
1015 1011 #
1016 1012 # self.extensions[mod] = m
1017 1013 # return m
1018 1014
1019 1015 def init_user_ns(self):
1020 1016 """Initialize all user-visible namespaces to their minimum defaults.
1021 1017
1022 1018 Certain history lists are also initialized here, as they effectively
1023 1019 act as user namespaces.
1024 1020
1025 1021 Notes
1026 1022 -----
1027 1023 All data structures here are only filled in, they are NOT reset by this
1028 1024 method. If they were not empty before, data will simply be added to
1029 1025 therm.
1030 1026 """
1031 1027 # The user namespace MUST have a pointer to the shell itself.
1032 1028 self.user_ns[self.name] = self
1033 1029
1034 1030 # Store myself as the public api!!!
1035 1031 self.user_ns['_ip'] = self
1036 1032
1037 1033 # make global variables for user access to the histories
1038 1034 self.user_ns['_ih'] = self.input_hist
1039 1035 self.user_ns['_oh'] = self.output_hist
1040 1036 self.user_ns['_dh'] = self.dir_hist
1041 1037
1042 1038 # user aliases to input and output histories
1043 1039 self.user_ns['In'] = self.input_hist
1044 1040 self.user_ns['Out'] = self.output_hist
1045 1041
1046 1042 self.user_ns['_sh'] = shadowns
1047 1043
1048 1044 # Put 'help' in the user namespace
1049 1045 try:
1050 1046 from site import _Helper
1051 1047 self.user_ns['help'] = _Helper()
1052 1048 except ImportError:
1053 1049 warn('help() not available - check site.py')
1054 1050
1055 1051 def save_sys_module_state(self):
1056 1052 """Save the state of hooks in the sys module.
1057 1053
1058 1054 This has to be called after self.user_ns is created.
1059 1055 """
1060 1056 self._orig_sys_module_state = {}
1061 1057 self._orig_sys_module_state['stdin'] = sys.stdin
1062 1058 self._orig_sys_module_state['stdout'] = sys.stdout
1063 1059 self._orig_sys_module_state['stderr'] = sys.stderr
1064 self._orig_sys_module_state['displayhook'] = sys.displayhook
1065 1060 self._orig_sys_module_state['excepthook'] = sys.excepthook
1066 1061 try:
1067 1062 self._orig_sys_modules_main_name = self.user_ns['__name__']
1068 1063 except KeyError:
1069 1064 pass
1070 1065
1071 1066 def restore_sys_module_state(self):
1072 1067 """Restore the state of the sys module."""
1073 1068 try:
1074 1069 for k, v in self._orig_sys_module_state.items():
1075 1070 setattr(sys, k, v)
1076 1071 except AttributeError:
1077 1072 pass
1078 1073 try:
1079 1074 delattr(sys, 'ipcompleter')
1080 1075 except AttributeError:
1081 1076 pass
1082 1077 # Reset what what done in self.init_sys_modules
1083 1078 try:
1084 1079 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
1085 1080 except (AttributeError, KeyError):
1086 1081 pass
1087 1082
1088 1083 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
1089 1084 """set_hook(name,hook) -> sets an internal IPython hook.
1090 1085
1091 1086 IPython exposes some of its internal API as user-modifiable hooks. By
1092 1087 adding your function to one of these hooks, you can modify IPython's
1093 1088 behavior to call at runtime your own routines."""
1094 1089
1095 1090 # At some point in the future, this should validate the hook before it
1096 1091 # accepts it. Probably at least check that the hook takes the number
1097 1092 # of args it's supposed to.
1098 1093
1099 1094 f = new.instancemethod(hook,self,self.__class__)
1100 1095
1101 1096 # check if the hook is for strdispatcher first
1102 1097 if str_key is not None:
1103 1098 sdp = self.strdispatchers.get(name, StrDispatch())
1104 1099 sdp.add_s(str_key, f, priority )
1105 1100 self.strdispatchers[name] = sdp
1106 1101 return
1107 1102 if re_key is not None:
1108 1103 sdp = self.strdispatchers.get(name, StrDispatch())
1109 1104 sdp.add_re(re.compile(re_key), f, priority )
1110 1105 self.strdispatchers[name] = sdp
1111 1106 return
1112 1107
1113 1108 dp = getattr(self.hooks, name, None)
1114 1109 if name not in IPython.core.hooks.__all__:
1115 1110 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
1116 1111 if not dp:
1117 1112 dp = IPython.core.hooks.CommandChainDispatcher()
1118 1113
1119 1114 try:
1120 1115 dp.add(f,priority)
1121 1116 except AttributeError:
1122 1117 # it was not commandchain, plain old func - replace
1123 1118 dp = f
1124 1119
1125 1120 setattr(self.hooks,name, dp)
1126 1121
1127 1122 def set_crash_handler(self, crashHandler):
1128 1123 """Set the IPython crash handler.
1129 1124
1130 1125 This must be a callable with a signature suitable for use as
1131 1126 sys.excepthook."""
1132 1127
1133 1128 # Install the given crash handler as the Python exception hook
1134 1129 sys.excepthook = crashHandler
1135 1130
1136 1131 # The instance will store a pointer to this, so that runtime code
1137 1132 # (such as magics) can access it. This is because during the
1138 1133 # read-eval loop, it gets temporarily overwritten (to deal with GUI
1139 1134 # frameworks).
1140 1135 self.sys_excepthook = sys.excepthook
1141 1136
1142 1137 def set_custom_exc(self,exc_tuple,handler):
1143 1138 """set_custom_exc(exc_tuple,handler)
1144 1139
1145 1140 Set a custom exception handler, which will be called if any of the
1146 1141 exceptions in exc_tuple occur in the mainloop (specifically, in the
1147 1142 runcode() method.
1148 1143
1149 1144 Inputs:
1150 1145
1151 1146 - exc_tuple: a *tuple* of valid exceptions to call the defined
1152 1147 handler for. It is very important that you use a tuple, and NOT A
1153 1148 LIST here, because of the way Python's except statement works. If
1154 1149 you only want to trap a single exception, use a singleton tuple:
1155 1150
1156 1151 exc_tuple == (MyCustomException,)
1157 1152
1158 1153 - handler: this must be defined as a function with the following
1159 1154 basic interface: def my_handler(self,etype,value,tb).
1160 1155
1161 1156 This will be made into an instance method (via new.instancemethod)
1162 1157 of IPython itself, and it will be called if any of the exceptions
1163 1158 listed in the exc_tuple are caught. If the handler is None, an
1164 1159 internal basic one is used, which just prints basic info.
1165 1160
1166 1161 WARNING: by putting in your own exception handler into IPython's main
1167 1162 execution loop, you run a very good chance of nasty crashes. This
1168 1163 facility should only be used if you really know what you are doing."""
1169 1164
1170 1165 assert type(exc_tuple)==type(()) , \
1171 1166 "The custom exceptions must be given AS A TUPLE."
1172 1167
1173 1168 def dummy_handler(self,etype,value,tb):
1174 1169 print '*** Simple custom exception handler ***'
1175 1170 print 'Exception type :',etype
1176 1171 print 'Exception value:',value
1177 1172 print 'Traceback :',tb
1178 1173 print 'Source code :','\n'.join(self.buffer)
1179 1174
1180 1175 if handler is None: handler = dummy_handler
1181 1176
1182 1177 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1183 1178 self.custom_exceptions = exc_tuple
1184 1179
1185 1180 def set_custom_completer(self,completer,pos=0):
1186 1181 """set_custom_completer(completer,pos=0)
1187 1182
1188 1183 Adds a new custom completer function.
1189 1184
1190 1185 The position argument (defaults to 0) is the index in the completers
1191 1186 list where you want the completer to be inserted."""
1192 1187
1193 1188 newcomp = new.instancemethod(completer,self.Completer,
1194 1189 self.Completer.__class__)
1195 1190 self.Completer.matchers.insert(pos,newcomp)
1196 1191
1197 1192 def set_completer(self):
1198 1193 """reset readline's completer to be our own."""
1199 1194 self.readline.set_completer(self.Completer.complete)
1200 1195
1201 1196 def _get_call_pdb(self):
1202 1197 return self._call_pdb
1203 1198
1204 1199 def _set_call_pdb(self,val):
1205 1200
1206 1201 if val not in (0,1,False,True):
1207 1202 raise ValueError,'new call_pdb value must be boolean'
1208 1203
1209 1204 # store value in instance
1210 1205 self._call_pdb = val
1211 1206
1212 1207 # notify the actual exception handlers
1213 1208 self.InteractiveTB.call_pdb = val
1214 1209 if self.isthreaded:
1215 1210 try:
1216 1211 self.sys_excepthook.call_pdb = val
1217 1212 except:
1218 1213 warn('Failed to activate pdb for threaded exception handler')
1219 1214
1220 1215 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
1221 1216 'Control auto-activation of pdb at exceptions')
1222 1217
1223 1218 def magic(self,arg_s):
1224 1219 """Call a magic function by name.
1225 1220
1226 1221 Input: a string containing the name of the magic function to call and any
1227 1222 additional arguments to be passed to the magic.
1228 1223
1229 1224 magic('name -opt foo bar') is equivalent to typing at the ipython
1230 1225 prompt:
1231 1226
1232 1227 In[1]: %name -opt foo bar
1233 1228
1234 1229 To call a magic without arguments, simply use magic('name').
1235 1230
1236 1231 This provides a proper Python function to call IPython's magics in any
1237 1232 valid Python code you can type at the interpreter, including loops and
1238 1233 compound statements.
1239 1234 """
1240 1235
1241 1236 args = arg_s.split(' ',1)
1242 1237 magic_name = args[0]
1243 1238 magic_name = magic_name.lstrip(self.ESC_MAGIC)
1244 1239
1245 1240 try:
1246 1241 magic_args = args[1]
1247 1242 except IndexError:
1248 1243 magic_args = ''
1249 1244 fn = getattr(self,'magic_'+magic_name,None)
1250 1245 if fn is None:
1251 1246 error("Magic function `%s` not found." % magic_name)
1252 1247 else:
1253 1248 magic_args = self.var_expand(magic_args,1)
1254 with self.builtin_trap:
1249 with nested(self.builtin_trap, self.display_trap):
1255 1250 return fn(magic_args)
1256 1251 # return result
1257 1252
1258 1253 def define_magic(self, magicname, func):
1259 1254 """Expose own function as magic function for ipython
1260 1255
1261 1256 def foo_impl(self,parameter_s=''):
1262 1257 'My very own magic!. (Use docstrings, IPython reads them).'
1263 1258 print 'Magic function. Passed parameter is between < >:'
1264 1259 print '<%s>' % parameter_s
1265 1260 print 'The self object is:',self
1266 1261
1267 1262 self.define_magic('foo',foo_impl)
1268 1263 """
1269 1264
1270 1265 import new
1271 1266 im = new.instancemethod(func,self, self.__class__)
1272 1267 old = getattr(self, "magic_" + magicname, None)
1273 1268 setattr(self, "magic_" + magicname, im)
1274 1269 return old
1275 1270
1276 1271 def define_macro(self, name, themacro):
1277 1272 """Define a new macro
1278 1273
1279 1274 Parameters
1280 1275 ----------
1281 1276 name : str
1282 1277 The name of the macro.
1283 1278 themacro : str or Macro
1284 1279 The action to do upon invoking the macro. If a string, a new
1285 1280 Macro object is created by passing the string to it.
1286 1281 """
1287 1282
1288 1283 from IPython.core import macro
1289 1284
1290 1285 if isinstance(themacro, basestring):
1291 1286 themacro = macro.Macro(themacro)
1292 1287 if not isinstance(themacro, macro.Macro):
1293 1288 raise ValueError('A macro must be a string or a Macro instance.')
1294 1289 self.user_ns[name] = themacro
1295 1290
1296 1291 def define_alias(self, name, cmd):
1297 1292 """ Define a new alias."""
1298 1293
1299 1294 if callable(cmd):
1300 1295 self.alias_table[name] = cmd
1301 1296 from IPython.core import shadowns
1302 1297 setattr(shadowns, name, cmd)
1303 1298 return
1304 1299
1305 1300 if isinstance(cmd, basestring):
1306 1301 nargs = cmd.count('%s')
1307 1302 if nargs>0 and cmd.find('%l')>=0:
1308 1303 raise Exception('The %s and %l specifiers are mutually '
1309 1304 'exclusive in alias definitions.')
1310 1305
1311 1306 self.alias_table[name] = (nargs,cmd)
1312 1307 return
1313 1308
1314 1309 self.alias_table[name] = cmd
1315 1310
1316 1311 def ipalias(self,arg_s):
1317 1312 """Call an alias by name.
1318 1313
1319 1314 Input: a string containing the name of the alias to call and any
1320 1315 additional arguments to be passed to the magic.
1321 1316
1322 1317 ipalias('name -opt foo bar') is equivalent to typing at the ipython
1323 1318 prompt:
1324 1319
1325 1320 In[1]: name -opt foo bar
1326 1321
1327 1322 To call an alias without arguments, simply use ipalias('name').
1328 1323
1329 1324 This provides a proper Python function to call IPython's aliases in any
1330 1325 valid Python code you can type at the interpreter, including loops and
1331 1326 compound statements. It is added by IPython to the Python builtin
1332 1327 namespace upon initialization."""
1333 1328
1334 1329 args = arg_s.split(' ',1)
1335 1330 alias_name = args[0]
1336 1331 try:
1337 1332 alias_args = args[1]
1338 1333 except IndexError:
1339 1334 alias_args = ''
1340 1335 if alias_name in self.alias_table:
1341 1336 self.call_alias(alias_name,alias_args)
1342 1337 else:
1343 1338 error("Alias `%s` not found." % alias_name)
1344 1339
1345 1340 def system(self, cmd):
1346 1341 """Make a system call, using IPython."""
1347 1342 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1348 1343
1349 1344 def ex(self, cmd):
1350 1345 """Execute a normal python statement in user namespace."""
1351 with self.builtin_trap:
1346 with nested(self.builtin_trap, self.display_trap):
1352 1347 exec cmd in self.user_global_ns, self.user_ns
1353 1348
1354 1349 def ev(self, expr):
1355 1350 """Evaluate python expression expr in user namespace.
1356 1351
1357 1352 Returns the result of evaluation
1358 1353 """
1359 with self.builtin_trap:
1354 with nested(self.builtin_trap, self.display_trap):
1360 1355 return eval(expr, self.user_global_ns, self.user_ns)
1361 1356
1362 1357 def getoutput(self, cmd):
1363 1358 return getoutput(self.var_expand(cmd,depth=2),
1364 1359 header=self.system_header,
1365 1360 verbose=self.system_verbose)
1366 1361
1367 1362 def getoutputerror(self, cmd):
1368 1363 return getoutputerror(self.var_expand(cmd,depth=2),
1369 1364 header=self.system_header,
1370 1365 verbose=self.system_verbose)
1371 1366
1372 1367 def complete(self, text):
1373 1368 """Return a sorted list of all possible completions on text.
1374 1369
1375 1370 Inputs:
1376 1371
1377 1372 - text: a string of text to be completed on.
1378 1373
1379 1374 This is a wrapper around the completion mechanism, similar to what
1380 1375 readline does at the command line when the TAB key is hit. By
1381 1376 exposing it as a method, it can be used by other non-readline
1382 1377 environments (such as GUIs) for text completion.
1383 1378
1384 1379 Simple usage example:
1385 1380
1386 1381 In [7]: x = 'hello'
1387 1382
1388 1383 In [8]: x
1389 1384 Out[8]: 'hello'
1390 1385
1391 1386 In [9]: print x
1392 1387 hello
1393 1388
1394 1389 In [10]: _ip.complete('x.l')
1395 1390 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1396 1391 """
1397 1392
1398 1393 # Inject names into __builtin__ so we can complete on the added names.
1399 1394 with self.builtin_trap:
1400 1395 complete = self.Completer.complete
1401 1396 state = 0
1402 1397 # use a dict so we get unique keys, since ipyhton's multiple
1403 1398 # completers can return duplicates. When we make 2.4 a requirement,
1404 1399 # start using sets instead, which are faster.
1405 1400 comps = {}
1406 1401 while True:
1407 1402 newcomp = complete(text,state,line_buffer=text)
1408 1403 if newcomp is None:
1409 1404 break
1410 1405 comps[newcomp] = 1
1411 1406 state += 1
1412 1407 outcomps = comps.keys()
1413 1408 outcomps.sort()
1414 1409 #print "T:",text,"OC:",outcomps # dbg
1415 1410 #print "vars:",self.user_ns.keys()
1416 1411 return outcomps
1417 1412
1418 1413 def set_completer_frame(self, frame=None):
1419 1414 if frame:
1420 1415 self.Completer.namespace = frame.f_locals
1421 1416 self.Completer.global_namespace = frame.f_globals
1422 1417 else:
1423 1418 self.Completer.namespace = self.user_ns
1424 1419 self.Completer.global_namespace = self.user_global_ns
1425 1420
1426 1421 def init_auto_alias(self):
1427 1422 """Define some aliases automatically.
1428 1423
1429 1424 These are ALL parameter-less aliases"""
1430 1425
1431 1426 for alias,cmd in self.auto_alias:
1432 1427 self.define_alias(alias,cmd)
1433 1428
1434 1429 def alias_table_validate(self,verbose=0):
1435 1430 """Update information about the alias table.
1436 1431
1437 1432 In particular, make sure no Python keywords/builtins are in it."""
1438 1433
1439 1434 no_alias = self.no_alias
1440 1435 for k in self.alias_table.keys():
1441 1436 if k in no_alias:
1442 1437 del self.alias_table[k]
1443 1438 if verbose:
1444 1439 print ("Deleting alias <%s>, it's a Python "
1445 1440 "keyword or builtin." % k)
1446 1441
1447 1442 def set_next_input(self, s):
1448 1443 """ Sets the 'default' input string for the next command line.
1449 1444
1450 1445 Requires readline.
1451 1446
1452 1447 Example:
1453 1448
1454 1449 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1455 1450 [D:\ipython]|2> Hello Word_ # cursor is here
1456 1451 """
1457 1452
1458 1453 self.rl_next_input = s
1459 1454
1460 1455 def set_autoindent(self,value=None):
1461 1456 """Set the autoindent flag, checking for readline support.
1462 1457
1463 1458 If called with no arguments, it acts as a toggle."""
1464 1459
1465 1460 if not self.has_readline:
1466 1461 if os.name == 'posix':
1467 1462 warn("The auto-indent feature requires the readline library")
1468 1463 self.autoindent = 0
1469 1464 return
1470 1465 if value is None:
1471 1466 self.autoindent = not self.autoindent
1472 1467 else:
1473 1468 self.autoindent = value
1474 1469
1475 1470 def atexit_operations(self):
1476 1471 """This will be executed at the time of exit.
1477 1472
1478 1473 Saving of persistent data should be performed here. """
1479 1474
1480 1475 #print '*** IPython exit cleanup ***' # dbg
1481 1476 # input history
1482 1477 self.savehist()
1483 1478
1484 1479 # Cleanup all tempfiles left around
1485 1480 for tfile in self.tempfiles:
1486 1481 try:
1487 1482 os.unlink(tfile)
1488 1483 except OSError:
1489 1484 pass
1490 1485
1491 1486 # Clear all user namespaces to release all references cleanly.
1492 1487 self.reset()
1493 1488
1494 1489 # Run user hooks
1495 1490 self.hooks.shutdown_hook()
1496 1491
1497 1492 def reset(self):
1498 1493 """Clear all internal namespaces.
1499 1494
1500 1495 Note that this is much more aggressive than %reset, since it clears
1501 1496 fully all namespaces, as well as all input/output lists.
1502 1497 """
1503 1498 for ns in self.ns_refs_table:
1504 1499 ns.clear()
1505 1500
1506 1501 # Clear input and output histories
1507 1502 self.input_hist[:] = []
1508 1503 self.input_hist_raw[:] = []
1509 1504 self.output_hist.clear()
1510 1505 # Restore the user namespaces to minimal usability
1511 1506 self.init_user_ns()
1512 1507
1513 1508 def savehist(self):
1514 1509 """Save input history to a file (via readline library)."""
1515 1510
1516 1511 if not self.has_readline:
1517 1512 return
1518 1513
1519 1514 try:
1520 1515 self.readline.write_history_file(self.histfile)
1521 1516 except:
1522 1517 print 'Unable to save IPython command history to file: ' + \
1523 1518 `self.histfile`
1524 1519
1525 1520 def reloadhist(self):
1526 1521 """Reload the input history from disk file."""
1527 1522
1528 1523 if self.has_readline:
1529 1524 try:
1530 1525 self.readline.clear_history()
1531 1526 self.readline.read_history_file(self.shell.histfile)
1532 1527 except AttributeError:
1533 1528 pass
1534 1529
1535 1530
1536 1531 def history_saving_wrapper(self, func):
1537 1532 """ Wrap func for readline history saving
1538 1533
1539 1534 Convert func into callable that saves & restores
1540 1535 history around the call """
1541 1536
1542 1537 if not self.has_readline:
1543 1538 return func
1544 1539
1545 1540 def wrapper():
1546 1541 self.savehist()
1547 1542 try:
1548 1543 func()
1549 1544 finally:
1550 1545 readline.read_history_file(self.histfile)
1551 1546 return wrapper
1552 1547
1553 1548 def pre_readline(self):
1554 1549 """readline hook to be used at the start of each line.
1555 1550
1556 1551 Currently it handles auto-indent only."""
1557 1552
1558 1553 #debugx('self.indent_current_nsp','pre_readline:')
1559 1554
1560 1555 if self.rl_do_indent:
1561 1556 self.readline.insert_text(self.indent_current_str())
1562 1557 if self.rl_next_input is not None:
1563 1558 self.readline.insert_text(self.rl_next_input)
1564 1559 self.rl_next_input = None
1565 1560
1566 1561 def ask_yes_no(self,prompt,default=True):
1567 1562 if self.quiet:
1568 1563 return True
1569 1564 return ask_yes_no(prompt,default)
1570 1565
1571 1566 def new_main_mod(self,ns=None):
1572 1567 """Return a new 'main' module object for user code execution.
1573 1568 """
1574 1569 main_mod = self._user_main_module
1575 1570 init_fakemod_dict(main_mod,ns)
1576 1571 return main_mod
1577 1572
1578 1573 def cache_main_mod(self,ns,fname):
1579 1574 """Cache a main module's namespace.
1580 1575
1581 1576 When scripts are executed via %run, we must keep a reference to the
1582 1577 namespace of their __main__ module (a FakeModule instance) around so
1583 1578 that Python doesn't clear it, rendering objects defined therein
1584 1579 useless.
1585 1580
1586 1581 This method keeps said reference in a private dict, keyed by the
1587 1582 absolute path of the module object (which corresponds to the script
1588 1583 path). This way, for multiple executions of the same script we only
1589 1584 keep one copy of the namespace (the last one), thus preventing memory
1590 1585 leaks from old references while allowing the objects from the last
1591 1586 execution to be accessible.
1592 1587
1593 1588 Note: we can not allow the actual FakeModule instances to be deleted,
1594 1589 because of how Python tears down modules (it hard-sets all their
1595 1590 references to None without regard for reference counts). This method
1596 1591 must therefore make a *copy* of the given namespace, to allow the
1597 1592 original module's __dict__ to be cleared and reused.
1598 1593
1599 1594
1600 1595 Parameters
1601 1596 ----------
1602 1597 ns : a namespace (a dict, typically)
1603 1598
1604 1599 fname : str
1605 1600 Filename associated with the namespace.
1606 1601
1607 1602 Examples
1608 1603 --------
1609 1604
1610 1605 In [10]: import IPython
1611 1606
1612 1607 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
1613 1608
1614 1609 In [12]: IPython.__file__ in _ip._main_ns_cache
1615 1610 Out[12]: True
1616 1611 """
1617 1612 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
1618 1613
1619 1614 def clear_main_mod_cache(self):
1620 1615 """Clear the cache of main modules.
1621 1616
1622 1617 Mainly for use by utilities like %reset.
1623 1618
1624 1619 Examples
1625 1620 --------
1626 1621
1627 1622 In [15]: import IPython
1628 1623
1629 1624 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
1630 1625
1631 1626 In [17]: len(_ip._main_ns_cache) > 0
1632 1627 Out[17]: True
1633 1628
1634 1629 In [18]: _ip.clear_main_mod_cache()
1635 1630
1636 1631 In [19]: len(_ip._main_ns_cache) == 0
1637 1632 Out[19]: True
1638 1633 """
1639 1634 self._main_ns_cache.clear()
1640 1635
1641 1636 def _should_recompile(self,e):
1642 1637 """Utility routine for edit_syntax_error"""
1643 1638
1644 1639 if e.filename in ('<ipython console>','<input>','<string>',
1645 1640 '<console>','<BackgroundJob compilation>',
1646 1641 None):
1647 1642
1648 1643 return False
1649 1644 try:
1650 1645 if (self.autoedit_syntax and
1651 1646 not self.ask_yes_no('Return to editor to correct syntax error? '
1652 1647 '[Y/n] ','y')):
1653 1648 return False
1654 1649 except EOFError:
1655 1650 return False
1656 1651
1657 1652 def int0(x):
1658 1653 try:
1659 1654 return int(x)
1660 1655 except TypeError:
1661 1656 return 0
1662 1657 # always pass integer line and offset values to editor hook
1663 1658 try:
1664 1659 self.hooks.fix_error_editor(e.filename,
1665 1660 int0(e.lineno),int0(e.offset),e.msg)
1666 1661 except TryNext:
1667 1662 warn('Could not open editor')
1668 1663 return False
1669 1664 return True
1670 1665
1671 1666 def edit_syntax_error(self):
1672 1667 """The bottom half of the syntax error handler called in the main loop.
1673 1668
1674 1669 Loop until syntax error is fixed or user cancels.
1675 1670 """
1676 1671
1677 1672 while self.SyntaxTB.last_syntax_error:
1678 1673 # copy and clear last_syntax_error
1679 1674 err = self.SyntaxTB.clear_err_state()
1680 1675 if not self._should_recompile(err):
1681 1676 return
1682 1677 try:
1683 1678 # may set last_syntax_error again if a SyntaxError is raised
1684 1679 self.safe_execfile(err.filename,self.user_ns)
1685 1680 except:
1686 1681 self.showtraceback()
1687 1682 else:
1688 1683 try:
1689 1684 f = file(err.filename)
1690 1685 try:
1686 # This should be inside a display_trap block and I
1687 # think it is.
1691 1688 sys.displayhook(f.read())
1692 1689 finally:
1693 1690 f.close()
1694 1691 except:
1695 1692 self.showtraceback()
1696 1693
1697 1694 def showsyntaxerror(self, filename=None):
1698 1695 """Display the syntax error that just occurred.
1699 1696
1700 1697 This doesn't display a stack trace because there isn't one.
1701 1698
1702 1699 If a filename is given, it is stuffed in the exception instead
1703 1700 of what was there before (because Python's parser always uses
1704 1701 "<string>" when reading from a string).
1705 1702 """
1706 1703 etype, value, last_traceback = sys.exc_info()
1707 1704
1708 1705 # See note about these variables in showtraceback() below
1709 1706 sys.last_type = etype
1710 1707 sys.last_value = value
1711 1708 sys.last_traceback = last_traceback
1712 1709
1713 1710 if filename and etype is SyntaxError:
1714 1711 # Work hard to stuff the correct filename in the exception
1715 1712 try:
1716 1713 msg, (dummy_filename, lineno, offset, line) = value
1717 1714 except:
1718 1715 # Not the format we expect; leave it alone
1719 1716 pass
1720 1717 else:
1721 1718 # Stuff in the right filename
1722 1719 try:
1723 1720 # Assume SyntaxError is a class exception
1724 1721 value = SyntaxError(msg, (filename, lineno, offset, line))
1725 1722 except:
1726 1723 # If that failed, assume SyntaxError is a string
1727 1724 value = msg, (filename, lineno, offset, line)
1728 1725 self.SyntaxTB(etype,value,[])
1729 1726
1730 1727 def debugger(self,force=False):
1731 1728 """Call the pydb/pdb debugger.
1732 1729
1733 1730 Keywords:
1734 1731
1735 1732 - force(False): by default, this routine checks the instance call_pdb
1736 1733 flag and does not actually invoke the debugger if the flag is false.
1737 1734 The 'force' option forces the debugger to activate even if the flag
1738 1735 is false.
1739 1736 """
1740 1737
1741 1738 if not (force or self.call_pdb):
1742 1739 return
1743 1740
1744 1741 if not hasattr(sys,'last_traceback'):
1745 1742 error('No traceback has been produced, nothing to debug.')
1746 1743 return
1747 1744
1748 1745 # use pydb if available
1749 1746 if debugger.has_pydb:
1750 1747 from pydb import pm
1751 1748 else:
1752 1749 # fallback to our internal debugger
1753 1750 pm = lambda : self.InteractiveTB.debugger(force=True)
1754 1751 self.history_saving_wrapper(pm)()
1755 1752
1756 1753 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1757 1754 """Display the exception that just occurred.
1758 1755
1759 1756 If nothing is known about the exception, this is the method which
1760 1757 should be used throughout the code for presenting user tracebacks,
1761 1758 rather than directly invoking the InteractiveTB object.
1762 1759
1763 1760 A specific showsyntaxerror() also exists, but this method can take
1764 1761 care of calling it if needed, so unless you are explicitly catching a
1765 1762 SyntaxError exception, don't try to analyze the stack manually and
1766 1763 simply call this method."""
1767 1764
1768 1765
1769 1766 # Though this won't be called by syntax errors in the input line,
1770 1767 # there may be SyntaxError cases whith imported code.
1771 1768
1772 1769 try:
1773 1770 if exc_tuple is None:
1774 1771 etype, value, tb = sys.exc_info()
1775 1772 else:
1776 1773 etype, value, tb = exc_tuple
1777 1774
1778 1775 if etype is SyntaxError:
1779 1776 self.showsyntaxerror(filename)
1780 1777 elif etype is UsageError:
1781 1778 print "UsageError:", value
1782 1779 else:
1783 1780 # WARNING: these variables are somewhat deprecated and not
1784 1781 # necessarily safe to use in a threaded environment, but tools
1785 1782 # like pdb depend on their existence, so let's set them. If we
1786 1783 # find problems in the field, we'll need to revisit their use.
1787 1784 sys.last_type = etype
1788 1785 sys.last_value = value
1789 1786 sys.last_traceback = tb
1790 1787
1791 1788 if etype in self.custom_exceptions:
1792 1789 self.CustomTB(etype,value,tb)
1793 1790 else:
1794 1791 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1795 1792 if self.InteractiveTB.call_pdb and self.has_readline:
1796 1793 # pdb mucks up readline, fix it back
1797 1794 self.set_completer()
1798 1795 except KeyboardInterrupt:
1799 1796 self.write("\nKeyboardInterrupt\n")
1800 1797
1801 1798 def mainloop(self, banner=None):
1802 1799 """Start the mainloop.
1803 1800
1804 1801 If an optional banner argument is given, it will override the
1805 1802 internally created default banner.
1806 1803 """
1807 1804
1808 with self.builtin_trap:
1805 with nested(self.builtin_trap, self.display_trap):
1809 1806 if self.c: # Emulate Python's -c option
1810 1807 self.exec_init_cmd()
1811 1808
1812 1809 if self.display_banner:
1813 1810 if banner is None:
1814 1811 banner = self.banner
1815 1812
1816 1813 # if you run stuff with -c <cmd>, raw hist is not updated
1817 1814 # ensure that it's in sync
1818 1815 if len(self.input_hist) != len (self.input_hist_raw):
1819 1816 self.input_hist_raw = InputList(self.input_hist)
1820 1817
1821 1818 while 1:
1822 1819 try:
1823 1820 self.interact()
1824 1821 #self.interact_with_readline()
1825 1822 # XXX for testing of a readline-decoupled repl loop, call
1826 1823 # interact_with_readline above
1827 1824 break
1828 1825 except KeyboardInterrupt:
1829 1826 # this should not be necessary, but KeyboardInterrupt
1830 1827 # handling seems rather unpredictable...
1831 1828 self.write("\nKeyboardInterrupt in interact()\n")
1832 1829
1833 1830 def exec_init_cmd(self):
1834 1831 """Execute a command given at the command line.
1835 1832
1836 1833 This emulates Python's -c option."""
1837 1834
1838 1835 #sys.argv = ['-c']
1839 1836 self.push_line(self.prefilter(self.c, False))
1840 1837 if not self.interactive:
1841 1838 self.ask_exit()
1842 1839
1843 1840 def interact_prompt(self):
1844 1841 """ Print the prompt (in read-eval-print loop)
1845 1842
1846 1843 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1847 1844 used in standard IPython flow.
1848 1845 """
1849 1846 if self.more:
1850 1847 try:
1851 1848 prompt = self.hooks.generate_prompt(True)
1852 1849 except:
1853 1850 self.showtraceback()
1854 1851 if self.autoindent:
1855 1852 self.rl_do_indent = True
1856 1853
1857 1854 else:
1858 1855 try:
1859 1856 prompt = self.hooks.generate_prompt(False)
1860 1857 except:
1861 1858 self.showtraceback()
1862 1859 self.write(prompt)
1863 1860
1864 1861 def interact_handle_input(self,line):
1865 1862 """ Handle the input line (in read-eval-print loop)
1866 1863
1867 1864 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1868 1865 used in standard IPython flow.
1869 1866 """
1870 1867 if line.lstrip() == line:
1871 1868 self.shadowhist.add(line.strip())
1872 1869 lineout = self.prefilter(line,self.more)
1873 1870
1874 1871 if line.strip():
1875 1872 if self.more:
1876 1873 self.input_hist_raw[-1] += '%s\n' % line
1877 1874 else:
1878 1875 self.input_hist_raw.append('%s\n' % line)
1879 1876
1880 1877
1881 1878 self.more = self.push_line(lineout)
1882 1879 if (self.SyntaxTB.last_syntax_error and
1883 1880 self.autoedit_syntax):
1884 1881 self.edit_syntax_error()
1885 1882
1886 1883 def interact_with_readline(self):
1887 1884 """ Demo of using interact_handle_input, interact_prompt
1888 1885
1889 1886 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1890 1887 it should work like this.
1891 1888 """
1892 1889 self.readline_startup_hook(self.pre_readline)
1893 1890 while not self.exit_now:
1894 1891 self.interact_prompt()
1895 1892 if self.more:
1896 1893 self.rl_do_indent = True
1897 1894 else:
1898 1895 self.rl_do_indent = False
1899 1896 line = raw_input_original().decode(self.stdin_encoding)
1900 1897 self.interact_handle_input(line)
1901 1898
1902 1899 def interact(self, banner=None):
1903 1900 """Closely emulate the interactive Python console."""
1904 1901
1905 1902 # batch run -> do not interact
1906 1903 if self.exit_now:
1907 1904 return
1908 1905
1909 1906 if self.display_banner:
1910 1907 if banner is None:
1911 1908 banner = self.banner
1912 1909 self.write(banner)
1913 1910
1914 1911 more = 0
1915 1912
1916 1913 # Mark activity in the builtins
1917 1914 __builtin__.__dict__['__IPYTHON__active'] += 1
1918 1915
1919 1916 if self.has_readline:
1920 1917 self.readline_startup_hook(self.pre_readline)
1921 1918 # exit_now is set by a call to %Exit or %Quit, through the
1922 1919 # ask_exit callback.
1923 1920
1924 1921 while not self.exit_now:
1925 1922 self.hooks.pre_prompt_hook()
1926 1923 if more:
1927 1924 try:
1928 1925 prompt = self.hooks.generate_prompt(True)
1929 1926 except:
1930 1927 self.showtraceback()
1931 1928 if self.autoindent:
1932 1929 self.rl_do_indent = True
1933 1930
1934 1931 else:
1935 1932 try:
1936 1933 prompt = self.hooks.generate_prompt(False)
1937 1934 except:
1938 1935 self.showtraceback()
1939 1936 try:
1940 1937 line = self.raw_input(prompt, more)
1941 1938 if self.exit_now:
1942 1939 # quick exit on sys.std[in|out] close
1943 1940 break
1944 1941 if self.autoindent:
1945 1942 self.rl_do_indent = False
1946 1943
1947 1944 except KeyboardInterrupt:
1948 1945 #double-guard against keyboardinterrupts during kbdint handling
1949 1946 try:
1950 1947 self.write('\nKeyboardInterrupt\n')
1951 1948 self.resetbuffer()
1952 1949 # keep cache in sync with the prompt counter:
1953 1950 self.outputcache.prompt_count -= 1
1954 1951
1955 1952 if self.autoindent:
1956 1953 self.indent_current_nsp = 0
1957 1954 more = 0
1958 1955 except KeyboardInterrupt:
1959 1956 pass
1960 1957 except EOFError:
1961 1958 if self.autoindent:
1962 1959 self.rl_do_indent = False
1963 1960 self.readline_startup_hook(None)
1964 1961 self.write('\n')
1965 1962 self.exit()
1966 1963 except bdb.BdbQuit:
1967 1964 warn('The Python debugger has exited with a BdbQuit exception.\n'
1968 1965 'Because of how pdb handles the stack, it is impossible\n'
1969 1966 'for IPython to properly format this particular exception.\n'
1970 1967 'IPython will resume normal operation.')
1971 1968 except:
1972 1969 # exceptions here are VERY RARE, but they can be triggered
1973 1970 # asynchronously by signal handlers, for example.
1974 1971 self.showtraceback()
1975 1972 else:
1976 1973 more = self.push_line(line)
1977 1974 if (self.SyntaxTB.last_syntax_error and
1978 1975 self.autoedit_syntax):
1979 1976 self.edit_syntax_error()
1980 1977
1981 1978 # We are off again...
1982 1979 __builtin__.__dict__['__IPYTHON__active'] -= 1
1983 1980
1984 1981 def excepthook(self, etype, value, tb):
1985 1982 """One more defense for GUI apps that call sys.excepthook.
1986 1983
1987 1984 GUI frameworks like wxPython trap exceptions and call
1988 1985 sys.excepthook themselves. I guess this is a feature that
1989 1986 enables them to keep running after exceptions that would
1990 1987 otherwise kill their mainloop. This is a bother for IPython
1991 1988 which excepts to catch all of the program exceptions with a try:
1992 1989 except: statement.
1993 1990
1994 1991 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1995 1992 any app directly invokes sys.excepthook, it will look to the user like
1996 1993 IPython crashed. In order to work around this, we can disable the
1997 1994 CrashHandler and replace it with this excepthook instead, which prints a
1998 1995 regular traceback using our InteractiveTB. In this fashion, apps which
1999 1996 call sys.excepthook will generate a regular-looking exception from
2000 1997 IPython, and the CrashHandler will only be triggered by real IPython
2001 1998 crashes.
2002 1999
2003 2000 This hook should be used sparingly, only in places which are not likely
2004 2001 to be true IPython errors.
2005 2002 """
2006 2003 self.showtraceback((etype,value,tb),tb_offset=0)
2007 2004
2008 2005 def expand_alias(self, line):
2009 2006 """ Expand an alias in the command line
2010 2007
2011 2008 Returns the provided command line, possibly with the first word
2012 2009 (command) translated according to alias expansion rules.
2013 2010
2014 2011 [ipython]|16> _ip.expand_aliases("np myfile.txt")
2015 2012 <16> 'q:/opt/np/notepad++.exe myfile.txt'
2016 2013 """
2017 2014
2018 2015 pre,fn,rest = self.split_user_input(line)
2019 2016 res = pre + self.expand_aliases(fn, rest)
2020 2017 return res
2021 2018
2022 2019 def expand_aliases(self, fn, rest):
2023 2020 """Expand multiple levels of aliases:
2024 2021
2025 2022 if:
2026 2023
2027 2024 alias foo bar /tmp
2028 2025 alias baz foo
2029 2026
2030 2027 then:
2031 2028
2032 2029 baz huhhahhei -> bar /tmp huhhahhei
2033 2030
2034 2031 """
2035 2032 line = fn + " " + rest
2036 2033
2037 2034 done = set()
2038 2035 while 1:
2039 2036 pre,fn,rest = prefilter.splitUserInput(line,
2040 2037 prefilter.shell_line_split)
2041 2038 if fn in self.alias_table:
2042 2039 if fn in done:
2043 2040 warn("Cyclic alias definition, repeated '%s'" % fn)
2044 2041 return ""
2045 2042 done.add(fn)
2046 2043
2047 2044 l2 = self.transform_alias(fn,rest)
2048 2045 # dir -> dir
2049 2046 # print "alias",line, "->",l2 #dbg
2050 2047 if l2 == line:
2051 2048 break
2052 2049 # ls -> ls -F should not recurse forever
2053 2050 if l2.split(None,1)[0] == line.split(None,1)[0]:
2054 2051 line = l2
2055 2052 break
2056 2053
2057 2054 line=l2
2058 2055
2059 2056
2060 2057 # print "al expand to",line #dbg
2061 2058 else:
2062 2059 break
2063 2060
2064 2061 return line
2065 2062
2066 2063 def transform_alias(self, alias,rest=''):
2067 2064 """ Transform alias to system command string.
2068 2065 """
2069 2066 trg = self.alias_table[alias]
2070 2067
2071 2068 nargs,cmd = trg
2072 2069 # print trg #dbg
2073 2070 if ' ' in cmd and os.path.isfile(cmd):
2074 2071 cmd = '"%s"' % cmd
2075 2072
2076 2073 # Expand the %l special to be the user's input line
2077 2074 if cmd.find('%l') >= 0:
2078 2075 cmd = cmd.replace('%l',rest)
2079 2076 rest = ''
2080 2077 if nargs==0:
2081 2078 # Simple, argument-less aliases
2082 2079 cmd = '%s %s' % (cmd,rest)
2083 2080 else:
2084 2081 # Handle aliases with positional arguments
2085 2082 args = rest.split(None,nargs)
2086 2083 if len(args)< nargs:
2087 2084 error('Alias <%s> requires %s arguments, %s given.' %
2088 2085 (alias,nargs,len(args)))
2089 2086 return None
2090 2087 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
2091 2088 # Now call the macro, evaluating in the user's namespace
2092 2089 #print 'new command: <%r>' % cmd # dbg
2093 2090 return cmd
2094 2091
2095 2092 def call_alias(self,alias,rest=''):
2096 2093 """Call an alias given its name and the rest of the line.
2097 2094
2098 2095 This is only used to provide backwards compatibility for users of
2099 2096 ipalias(), use of which is not recommended for anymore."""
2100 2097
2101 2098 # Now call the macro, evaluating in the user's namespace
2102 2099 cmd = self.transform_alias(alias, rest)
2103 2100 try:
2104 2101 self.system(cmd)
2105 2102 except:
2106 2103 self.showtraceback()
2107 2104
2108 2105 def indent_current_str(self):
2109 2106 """return the current level of indentation as a string"""
2110 2107 return self.indent_current_nsp * ' '
2111 2108
2112 2109 def autoindent_update(self,line):
2113 2110 """Keep track of the indent level."""
2114 2111
2115 2112 #debugx('line')
2116 2113 #debugx('self.indent_current_nsp')
2117 2114 if self.autoindent:
2118 2115 if line:
2119 2116 inisp = num_ini_spaces(line)
2120 2117 if inisp < self.indent_current_nsp:
2121 2118 self.indent_current_nsp = inisp
2122 2119
2123 2120 if line[-1] == ':':
2124 2121 self.indent_current_nsp += 4
2125 2122 elif dedent_re.match(line):
2126 2123 self.indent_current_nsp -= 4
2127 2124 else:
2128 2125 self.indent_current_nsp = 0
2129 2126
2130 2127 def push(self, variables, interactive=True):
2131 2128 """Inject a group of variables into the IPython user namespace.
2132 2129
2133 2130 Parameters
2134 2131 ----------
2135 2132 variables : dict, str or list/tuple of str
2136 2133 The variables to inject into the user's namespace. If a dict,
2137 2134 a simple update is done. If a str, the string is assumed to
2138 2135 have variable names separated by spaces. A list/tuple of str
2139 2136 can also be used to give the variable names. If just the variable
2140 2137 names are give (list/tuple/str) then the variable values looked
2141 2138 up in the callers frame.
2142 2139 interactive : bool
2143 2140 If True (default), the variables will be listed with the ``who``
2144 2141 magic.
2145 2142 """
2146 2143 vdict = None
2147 2144
2148 2145 # We need a dict of name/value pairs to do namespace updates.
2149 2146 if isinstance(variables, dict):
2150 2147 vdict = variables
2151 2148 elif isinstance(variables, (basestring, list, tuple)):
2152 2149 if isinstance(variables, basestring):
2153 2150 vlist = variables.split()
2154 2151 else:
2155 2152 vlist = variables
2156 2153 vdict = {}
2157 2154 cf = sys._getframe(1)
2158 2155 for name in vlist:
2159 2156 try:
2160 2157 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
2161 2158 except:
2162 2159 print ('Could not get variable %s from %s' %
2163 2160 (name,cf.f_code.co_name))
2164 2161 else:
2165 2162 raise ValueError('variables must be a dict/str/list/tuple')
2166 2163
2167 2164 # Propagate variables to user namespace
2168 2165 self.user_ns.update(vdict)
2169 2166
2170 2167 # And configure interactive visibility
2171 2168 config_ns = self.user_config_ns
2172 2169 if interactive:
2173 2170 for name, val in vdict.iteritems():
2174 2171 config_ns.pop(name, None)
2175 2172 else:
2176 2173 for name,val in vdict.iteritems():
2177 2174 config_ns[name] = val
2178 2175
2179 2176 def cleanup_ipy_script(self, script):
2180 2177 """Make a script safe for self.runlines()
2181 2178
2182 2179 Notes
2183 2180 -----
2184 2181 This was copied over from the old ipapi and probably can be done
2185 2182 away with once we move to block based interpreter.
2186 2183
2187 2184 - Removes empty lines Suffixes all indented blocks that end with
2188 2185 - unindented lines with empty lines
2189 2186 """
2190 2187
2191 2188 res = []
2192 2189 lines = script.splitlines()
2193 2190
2194 2191 level = 0
2195 2192 for l in lines:
2196 2193 lstripped = l.lstrip()
2197 2194 stripped = l.strip()
2198 2195 if not stripped:
2199 2196 continue
2200 2197 newlevel = len(l) - len(lstripped)
2201 2198 def is_secondary_block_start(s):
2202 2199 if not s.endswith(':'):
2203 2200 return False
2204 2201 if (s.startswith('elif') or
2205 2202 s.startswith('else') or
2206 2203 s.startswith('except') or
2207 2204 s.startswith('finally')):
2208 2205 return True
2209 2206
2210 2207 if level > 0 and newlevel == 0 and \
2211 2208 not is_secondary_block_start(stripped):
2212 2209 # add empty line
2213 2210 res.append('')
2214 2211
2215 2212 res.append(l)
2216 2213 level = newlevel
2217 2214 return '\n'.join(res) + '\n'
2218 2215
2219 2216 def runlines(self, lines, clean=False):
2220 2217 """Run a string of one or more lines of source.
2221 2218
2222 2219 This method is capable of running a string containing multiple source
2223 2220 lines, as if they had been entered at the IPython prompt. Since it
2224 2221 exposes IPython's processing machinery, the given strings can contain
2225 2222 magic calls (%magic), special shell access (!cmd), etc.
2226 2223 """
2227 2224
2228 2225 if isinstance(lines, (list, tuple)):
2229 2226 lines = '\n'.join(lines)
2230 2227
2231 2228 if clean:
2232 2229 lines = self.cleanup_ipy_script(lines)
2233 2230
2234 2231 # We must start with a clean buffer, in case this is run from an
2235 2232 # interactive IPython session (via a magic, for example).
2236 2233 self.resetbuffer()
2237 2234 lines = lines.splitlines()
2238 2235 more = 0
2239 2236
2240 with self.builtin_trap:
2237 with nested(self.builtin_trap, self.display_trap):
2241 2238 for line in lines:
2242 2239 # skip blank lines so we don't mess up the prompt counter, but do
2243 2240 # NOT skip even a blank line if we are in a code block (more is
2244 2241 # true)
2245 2242
2246 2243 if line or more:
2247 2244 # push to raw history, so hist line numbers stay in sync
2248 2245 self.input_hist_raw.append("# " + line + "\n")
2249 2246 more = self.push_line(self.prefilter(line,more))
2250 2247 # IPython's runsource returns None if there was an error
2251 2248 # compiling the code. This allows us to stop processing right
2252 2249 # away, so the user gets the error message at the right place.
2253 2250 if more is None:
2254 2251 break
2255 2252 else:
2256 2253 self.input_hist_raw.append("\n")
2257 2254 # final newline in case the input didn't have it, so that the code
2258 2255 # actually does get executed
2259 2256 if more:
2260 2257 self.push_line('\n')
2261 2258
2262 2259 def runsource(self, source, filename='<input>', symbol='single'):
2263 2260 """Compile and run some source in the interpreter.
2264 2261
2265 2262 Arguments are as for compile_command().
2266 2263
2267 2264 One several things can happen:
2268 2265
2269 2266 1) The input is incorrect; compile_command() raised an
2270 2267 exception (SyntaxError or OverflowError). A syntax traceback
2271 2268 will be printed by calling the showsyntaxerror() method.
2272 2269
2273 2270 2) The input is incomplete, and more input is required;
2274 2271 compile_command() returned None. Nothing happens.
2275 2272
2276 2273 3) The input is complete; compile_command() returned a code
2277 2274 object. The code is executed by calling self.runcode() (which
2278 2275 also handles run-time exceptions, except for SystemExit).
2279 2276
2280 2277 The return value is:
2281 2278
2282 2279 - True in case 2
2283 2280
2284 2281 - False in the other cases, unless an exception is raised, where
2285 2282 None is returned instead. This can be used by external callers to
2286 2283 know whether to continue feeding input or not.
2287 2284
2288 2285 The return value can be used to decide whether to use sys.ps1 or
2289 2286 sys.ps2 to prompt the next line."""
2290 2287
2291 2288 # if the source code has leading blanks, add 'if 1:\n' to it
2292 2289 # this allows execution of indented pasted code. It is tempting
2293 2290 # to add '\n' at the end of source to run commands like ' a=1'
2294 2291 # directly, but this fails for more complicated scenarios
2295 2292 source=source.encode(self.stdin_encoding)
2296 2293 if source[:1] in [' ', '\t']:
2297 2294 source = 'if 1:\n%s' % source
2298 2295
2299 2296 try:
2300 2297 code = self.compile(source,filename,symbol)
2301 2298 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2302 2299 # Case 1
2303 2300 self.showsyntaxerror(filename)
2304 2301 return None
2305 2302
2306 2303 if code is None:
2307 2304 # Case 2
2308 2305 return True
2309 2306
2310 2307 # Case 3
2311 2308 # We store the code object so that threaded shells and
2312 2309 # custom exception handlers can access all this info if needed.
2313 2310 # The source corresponding to this can be obtained from the
2314 2311 # buffer attribute as '\n'.join(self.buffer).
2315 2312 self.code_to_run = code
2316 2313 # now actually execute the code object
2317 2314 if self.runcode(code) == 0:
2318 2315 return False
2319 2316 else:
2320 2317 return None
2321 2318
2322 2319 def runcode(self,code_obj):
2323 2320 """Execute a code object.
2324 2321
2325 2322 When an exception occurs, self.showtraceback() is called to display a
2326 2323 traceback.
2327 2324
2328 2325 Return value: a flag indicating whether the code to be run completed
2329 2326 successfully:
2330 2327
2331 2328 - 0: successful execution.
2332 2329 - 1: an error occurred.
2333 2330 """
2334 2331
2335 2332 # Set our own excepthook in case the user code tries to call it
2336 2333 # directly, so that the IPython crash handler doesn't get triggered
2337 2334 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2338 2335
2339 2336 # we save the original sys.excepthook in the instance, in case config
2340 2337 # code (such as magics) needs access to it.
2341 2338 self.sys_excepthook = old_excepthook
2342 2339 outflag = 1 # happens in more places, so it's easier as default
2343 2340 try:
2344 2341 try:
2345 2342 self.hooks.pre_runcode_hook()
2346 2343 exec code_obj in self.user_global_ns, self.user_ns
2347 2344 finally:
2348 2345 # Reset our crash handler in place
2349 2346 sys.excepthook = old_excepthook
2350 2347 except SystemExit:
2351 2348 self.resetbuffer()
2352 2349 self.showtraceback()
2353 2350 warn("Type %exit or %quit to exit IPython "
2354 2351 "(%Exit or %Quit do so unconditionally).",level=1)
2355 2352 except self.custom_exceptions:
2356 2353 etype,value,tb = sys.exc_info()
2357 2354 self.CustomTB(etype,value,tb)
2358 2355 except:
2359 2356 self.showtraceback()
2360 2357 else:
2361 2358 outflag = 0
2362 2359 if softspace(sys.stdout, 0):
2363 2360 print
2364 2361 # Flush out code object which has been run (and source)
2365 2362 self.code_to_run = None
2366 2363 return outflag
2367 2364
2368 2365 def push_line(self, line):
2369 2366 """Push a line to the interpreter.
2370 2367
2371 2368 The line should not have a trailing newline; it may have
2372 2369 internal newlines. The line is appended to a buffer and the
2373 2370 interpreter's runsource() method is called with the
2374 2371 concatenated contents of the buffer as source. If this
2375 2372 indicates that the command was executed or invalid, the buffer
2376 2373 is reset; otherwise, the command is incomplete, and the buffer
2377 2374 is left as it was after the line was appended. The return
2378 2375 value is 1 if more input is required, 0 if the line was dealt
2379 2376 with in some way (this is the same as runsource()).
2380 2377 """
2381 2378
2382 2379 # autoindent management should be done here, and not in the
2383 2380 # interactive loop, since that one is only seen by keyboard input. We
2384 2381 # need this done correctly even for code run via runlines (which uses
2385 2382 # push).
2386 2383
2387 2384 #print 'push line: <%s>' % line # dbg
2388 2385 for subline in line.splitlines():
2389 2386 self.autoindent_update(subline)
2390 2387 self.buffer.append(line)
2391 2388 more = self.runsource('\n'.join(self.buffer), self.filename)
2392 2389 if not more:
2393 2390 self.resetbuffer()
2394 2391 return more
2395 2392
2396 2393 def split_user_input(self, line):
2397 2394 # This is really a hold-over to support ipapi and some extensions
2398 2395 return prefilter.splitUserInput(line)
2399 2396
2400 2397 def resetbuffer(self):
2401 2398 """Reset the input buffer."""
2402 2399 self.buffer[:] = []
2403 2400
2404 2401 def raw_input(self,prompt='',continue_prompt=False):
2405 2402 """Write a prompt and read a line.
2406 2403
2407 2404 The returned line does not include the trailing newline.
2408 2405 When the user enters the EOF key sequence, EOFError is raised.
2409 2406
2410 2407 Optional inputs:
2411 2408
2412 2409 - prompt(''): a string to be printed to prompt the user.
2413 2410
2414 2411 - continue_prompt(False): whether this line is the first one or a
2415 2412 continuation in a sequence of inputs.
2416 2413 """
2417 2414
2418 2415 # Code run by the user may have modified the readline completer state.
2419 2416 # We must ensure that our completer is back in place.
2420 2417 if self.has_readline:
2421 2418 self.set_completer()
2422 2419
2423 2420 try:
2424 2421 line = raw_input_original(prompt).decode(self.stdin_encoding)
2425 2422 except ValueError:
2426 2423 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2427 2424 " or sys.stdout.close()!\nExiting IPython!")
2428 2425 self.ask_exit()
2429 2426 return ""
2430 2427
2431 2428 # Try to be reasonably smart about not re-indenting pasted input more
2432 2429 # than necessary. We do this by trimming out the auto-indent initial
2433 2430 # spaces, if the user's actual input started itself with whitespace.
2434 2431 #debugx('self.buffer[-1]')
2435 2432
2436 2433 if self.autoindent:
2437 2434 if num_ini_spaces(line) > self.indent_current_nsp:
2438 2435 line = line[self.indent_current_nsp:]
2439 2436 self.indent_current_nsp = 0
2440 2437
2441 2438 # store the unfiltered input before the user has any chance to modify
2442 2439 # it.
2443 2440 if line.strip():
2444 2441 if continue_prompt:
2445 2442 self.input_hist_raw[-1] += '%s\n' % line
2446 2443 if self.has_readline: # and some config option is set?
2447 2444 try:
2448 2445 histlen = self.readline.get_current_history_length()
2449 2446 if histlen > 1:
2450 2447 newhist = self.input_hist_raw[-1].rstrip()
2451 2448 self.readline.remove_history_item(histlen-1)
2452 2449 self.readline.replace_history_item(histlen-2,
2453 2450 newhist.encode(self.stdin_encoding))
2454 2451 except AttributeError:
2455 2452 pass # re{move,place}_history_item are new in 2.4.
2456 2453 else:
2457 2454 self.input_hist_raw.append('%s\n' % line)
2458 2455 # only entries starting at first column go to shadow history
2459 2456 if line.lstrip() == line:
2460 2457 self.shadowhist.add(line.strip())
2461 2458 elif not continue_prompt:
2462 2459 self.input_hist_raw.append('\n')
2463 2460 try:
2464 2461 lineout = self.prefilter(line,continue_prompt)
2465 2462 except:
2466 2463 # blanket except, in case a user-defined prefilter crashes, so it
2467 2464 # can't take all of ipython with it.
2468 2465 self.showtraceback()
2469 2466 return ''
2470 2467 else:
2471 2468 return lineout
2472 2469
2473 2470 def _prefilter(self, line, continue_prompt):
2474 2471 """Calls different preprocessors, depending on the form of line."""
2475 2472
2476 2473 # All handlers *must* return a value, even if it's blank ('').
2477 2474
2478 2475 # Lines are NOT logged here. Handlers should process the line as
2479 2476 # needed, update the cache AND log it (so that the input cache array
2480 2477 # stays synced).
2481 2478
2482 2479 #.....................................................................
2483 2480 # Code begins
2484 2481
2485 2482 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
2486 2483
2487 2484 # save the line away in case we crash, so the post-mortem handler can
2488 2485 # record it
2489 2486 self._last_input_line = line
2490 2487
2491 2488 #print '***line: <%s>' % line # dbg
2492 2489
2493 2490 if not line:
2494 2491 # Return immediately on purely empty lines, so that if the user
2495 2492 # previously typed some whitespace that started a continuation
2496 2493 # prompt, he can break out of that loop with just an empty line.
2497 2494 # This is how the default python prompt works.
2498 2495
2499 2496 # Only return if the accumulated input buffer was just whitespace!
2500 2497 if ''.join(self.buffer).isspace():
2501 2498 self.buffer[:] = []
2502 2499 return ''
2503 2500
2504 2501 line_info = prefilter.LineInfo(line, continue_prompt)
2505 2502
2506 2503 # the input history needs to track even empty lines
2507 2504 stripped = line.strip()
2508 2505
2509 2506 if not stripped:
2510 2507 if not continue_prompt:
2511 2508 self.outputcache.prompt_count -= 1
2512 2509 return self.handle_normal(line_info)
2513 2510
2514 2511 # print '***cont',continue_prompt # dbg
2515 2512 # special handlers are only allowed for single line statements
2516 2513 if continue_prompt and not self.multi_line_specials:
2517 2514 return self.handle_normal(line_info)
2518 2515
2519 2516
2520 2517 # See whether any pre-existing handler can take care of it
2521 2518 rewritten = self.hooks.input_prefilter(stripped)
2522 2519 if rewritten != stripped: # ok, some prefilter did something
2523 2520 rewritten = line_info.pre + rewritten # add indentation
2524 2521 return self.handle_normal(prefilter.LineInfo(rewritten,
2525 2522 continue_prompt))
2526 2523
2527 2524 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2528 2525
2529 2526 return prefilter.prefilter(line_info, self)
2530 2527
2531 2528
2532 2529 def _prefilter_dumb(self, line, continue_prompt):
2533 2530 """simple prefilter function, for debugging"""
2534 2531 return self.handle_normal(line,continue_prompt)
2535 2532
2536 2533
2537 2534 def multiline_prefilter(self, line, continue_prompt):
2538 2535 """ Run _prefilter for each line of input
2539 2536
2540 2537 Covers cases where there are multiple lines in the user entry,
2541 2538 which is the case when the user goes back to a multiline history
2542 2539 entry and presses enter.
2543 2540
2544 2541 """
2545 2542 out = []
2546 2543 for l in line.rstrip('\n').split('\n'):
2547 2544 out.append(self._prefilter(l, continue_prompt))
2548 2545 return '\n'.join(out)
2549 2546
2550 2547 # Set the default prefilter() function (this can be user-overridden)
2551 2548 prefilter = multiline_prefilter
2552 2549
2553 2550 def handle_normal(self,line_info):
2554 2551 """Handle normal input lines. Use as a template for handlers."""
2555 2552
2556 2553 # With autoindent on, we need some way to exit the input loop, and I
2557 2554 # don't want to force the user to have to backspace all the way to
2558 2555 # clear the line. The rule will be in this case, that either two
2559 2556 # lines of pure whitespace in a row, or a line of pure whitespace but
2560 2557 # of a size different to the indent level, will exit the input loop.
2561 2558 line = line_info.line
2562 2559 continue_prompt = line_info.continue_prompt
2563 2560
2564 2561 if (continue_prompt and self.autoindent and line.isspace() and
2565 2562 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
2566 2563 (self.buffer[-1]).isspace() )):
2567 2564 line = ''
2568 2565
2569 2566 self.log(line,line,continue_prompt)
2570 2567 return line
2571 2568
2572 2569 def handle_alias(self,line_info):
2573 2570 """Handle alias input lines. """
2574 2571 tgt = self.alias_table[line_info.iFun]
2575 2572 # print "=>",tgt #dbg
2576 2573 if callable(tgt):
2577 2574 if '$' in line_info.line:
2578 2575 call_meth = '(_ip, _ip.var_expand(%s))'
2579 2576 else:
2580 2577 call_meth = '(_ip,%s)'
2581 2578 line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace,
2582 2579 line_info.iFun,
2583 2580 make_quoted_expr(line_info.line))
2584 2581 else:
2585 2582 transformed = self.expand_aliases(line_info.iFun,line_info.theRest)
2586 2583
2587 2584 # pre is needed, because it carries the leading whitespace. Otherwise
2588 2585 # aliases won't work in indented sections.
2589 2586 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2590 2587 make_quoted_expr( transformed ))
2591 2588
2592 2589 self.log(line_info.line,line_out,line_info.continue_prompt)
2593 2590 #print 'line out:',line_out # dbg
2594 2591 return line_out
2595 2592
2596 2593 def handle_shell_escape(self, line_info):
2597 2594 """Execute the line in a shell, empty return value"""
2598 2595 #print 'line in :', `line` # dbg
2599 2596 line = line_info.line
2600 2597 if line.lstrip().startswith('!!'):
2601 2598 # rewrite LineInfo's line, iFun and theRest to properly hold the
2602 2599 # call to %sx and the actual command to be executed, so
2603 2600 # handle_magic can work correctly. Note that this works even if
2604 2601 # the line is indented, so it handles multi_line_specials
2605 2602 # properly.
2606 2603 new_rest = line.lstrip()[2:]
2607 2604 line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest)
2608 2605 line_info.iFun = 'sx'
2609 2606 line_info.theRest = new_rest
2610 2607 return self.handle_magic(line_info)
2611 2608 else:
2612 2609 cmd = line.lstrip().lstrip('!')
2613 2610 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2614 2611 make_quoted_expr(cmd))
2615 2612 # update cache/log and return
2616 2613 self.log(line,line_out,line_info.continue_prompt)
2617 2614 return line_out
2618 2615
2619 2616 def handle_magic(self, line_info):
2620 2617 """Execute magic functions."""
2621 2618 iFun = line_info.iFun
2622 2619 theRest = line_info.theRest
2623 2620 cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace,
2624 2621 make_quoted_expr(iFun + " " + theRest))
2625 2622 self.log(line_info.line,cmd,line_info.continue_prompt)
2626 2623 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2627 2624 return cmd
2628 2625
2629 2626 def handle_auto(self, line_info):
2630 2627 """Hande lines which can be auto-executed, quoting if requested."""
2631 2628
2632 2629 line = line_info.line
2633 2630 iFun = line_info.iFun
2634 2631 theRest = line_info.theRest
2635 2632 pre = line_info.pre
2636 2633 continue_prompt = line_info.continue_prompt
2637 2634 obj = line_info.ofind(self)['obj']
2638 2635
2639 2636 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2640 2637
2641 2638 # This should only be active for single-line input!
2642 2639 if continue_prompt:
2643 2640 self.log(line,line,continue_prompt)
2644 2641 return line
2645 2642
2646 2643 force_auto = isinstance(obj, IPyAutocall)
2647 2644 auto_rewrite = True
2648 2645
2649 2646 if pre == self.ESC_QUOTE:
2650 2647 # Auto-quote splitting on whitespace
2651 2648 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2652 2649 elif pre == self.ESC_QUOTE2:
2653 2650 # Auto-quote whole string
2654 2651 newcmd = '%s("%s")' % (iFun,theRest)
2655 2652 elif pre == self.ESC_PAREN:
2656 2653 newcmd = '%s(%s)' % (iFun,",".join(theRest.split()))
2657 2654 else:
2658 2655 # Auto-paren.
2659 2656 # We only apply it to argument-less calls if the autocall
2660 2657 # parameter is set to 2. We only need to check that autocall is <
2661 2658 # 2, since this function isn't called unless it's at least 1.
2662 2659 if not theRest and (self.autocall < 2) and not force_auto:
2663 2660 newcmd = '%s %s' % (iFun,theRest)
2664 2661 auto_rewrite = False
2665 2662 else:
2666 2663 if not force_auto and theRest.startswith('['):
2667 2664 if hasattr(obj,'__getitem__'):
2668 2665 # Don't autocall in this case: item access for an object
2669 2666 # which is BOTH callable and implements __getitem__.
2670 2667 newcmd = '%s %s' % (iFun,theRest)
2671 2668 auto_rewrite = False
2672 2669 else:
2673 2670 # if the object doesn't support [] access, go ahead and
2674 2671 # autocall
2675 2672 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2676 2673 elif theRest.endswith(';'):
2677 2674 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2678 2675 else:
2679 2676 newcmd = '%s(%s)' % (iFun.rstrip(), theRest)
2680 2677
2681 2678 if auto_rewrite:
2682 2679 rw = self.outputcache.prompt1.auto_rewrite() + newcmd
2683 2680
2684 2681 try:
2685 2682 # plain ascii works better w/ pyreadline, on some machines, so
2686 2683 # we use it and only print uncolored rewrite if we have unicode
2687 2684 rw = str(rw)
2688 2685 print >>Term.cout, rw
2689 2686 except UnicodeEncodeError:
2690 2687 print "-------------->" + newcmd
2691 2688
2692 2689 # log what is now valid Python, not the actual user input (without the
2693 2690 # final newline)
2694 2691 self.log(line,newcmd,continue_prompt)
2695 2692 return newcmd
2696 2693
2697 2694 def handle_help(self, line_info):
2698 2695 """Try to get some help for the object.
2699 2696
2700 2697 obj? or ?obj -> basic information.
2701 2698 obj?? or ??obj -> more details.
2702 2699 """
2703 2700
2704 2701 line = line_info.line
2705 2702 # We need to make sure that we don't process lines which would be
2706 2703 # otherwise valid python, such as "x=1 # what?"
2707 2704 try:
2708 2705 codeop.compile_command(line)
2709 2706 except SyntaxError:
2710 2707 # We should only handle as help stuff which is NOT valid syntax
2711 2708 if line[0]==self.ESC_HELP:
2712 2709 line = line[1:]
2713 2710 elif line[-1]==self.ESC_HELP:
2714 2711 line = line[:-1]
2715 2712 self.log(line,'#?'+line,line_info.continue_prompt)
2716 2713 if line:
2717 2714 #print 'line:<%r>' % line # dbg
2718 2715 self.magic_pinfo(line)
2719 2716 else:
2720 2717 page(self.usage,screen_lines=self.usable_screen_length)
2721 2718 return '' # Empty string is needed here!
2722 2719 except:
2723 2720 # Pass any other exceptions through to the normal handler
2724 2721 return self.handle_normal(line_info)
2725 2722 else:
2726 2723 # If the code compiles ok, we should handle it normally
2727 2724 return self.handle_normal(line_info)
2728 2725
2729 2726 def handle_emacs(self, line_info):
2730 2727 """Handle input lines marked by python-mode."""
2731 2728
2732 2729 # Currently, nothing is done. Later more functionality can be added
2733 2730 # here if needed.
2734 2731
2735 2732 # The input cache shouldn't be updated
2736 2733 return line_info.line
2737 2734
2738 2735 def var_expand(self,cmd,depth=0):
2739 2736 """Expand python variables in a string.
2740 2737
2741 2738 The depth argument indicates how many frames above the caller should
2742 2739 be walked to look for the local namespace where to expand variables.
2743 2740
2744 2741 The global namespace for expansion is always the user's interactive
2745 2742 namespace.
2746 2743 """
2747 2744
2748 2745 return str(ItplNS(cmd,
2749 2746 self.user_ns, # globals
2750 2747 # Skip our own frame in searching for locals:
2751 2748 sys._getframe(depth+1).f_locals # locals
2752 2749 ))
2753 2750
2754 2751 def mktempfile(self,data=None):
2755 2752 """Make a new tempfile and return its filename.
2756 2753
2757 2754 This makes a call to tempfile.mktemp, but it registers the created
2758 2755 filename internally so ipython cleans it up at exit time.
2759 2756
2760 2757 Optional inputs:
2761 2758
2762 2759 - data(None): if data is given, it gets written out to the temp file
2763 2760 immediately, and the file is closed again."""
2764 2761
2765 2762 filename = tempfile.mktemp('.py','ipython_edit_')
2766 2763 self.tempfiles.append(filename)
2767 2764
2768 2765 if data:
2769 2766 tmp_file = open(filename,'w')
2770 2767 tmp_file.write(data)
2771 2768 tmp_file.close()
2772 2769 return filename
2773 2770
2774 2771 def write(self,data):
2775 2772 """Write a string to the default output"""
2776 2773 Term.cout.write(data)
2777 2774
2778 2775 def write_err(self,data):
2779 2776 """Write a string to the default error output"""
2780 2777 Term.cerr.write(data)
2781 2778
2782 2779 def ask_exit(self):
2783 2780 """ Call for exiting. Can be overiden and used as a callback. """
2784 2781 self.exit_now = True
2785 2782
2786 2783 def exit(self):
2787 2784 """Handle interactive exit.
2788 2785
2789 2786 This method calls the ask_exit callback."""
2790 2787 if self.confirm_exit:
2791 2788 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2792 2789 self.ask_exit()
2793 2790 else:
2794 2791 self.ask_exit()
2795 2792
2796 2793 def safe_execfile(self,fname,*where,**kw):
2797 2794 """A safe version of the builtin execfile().
2798 2795
2799 2796 This version will never throw an exception, and knows how to handle
2800 2797 ipython logs as well.
2801 2798
2802 2799 :Parameters:
2803 2800 fname : string
2804 2801 Name of the file to be executed.
2805 2802
2806 2803 where : tuple
2807 2804 One or two namespaces, passed to execfile() as (globals,locals).
2808 2805 If only one is given, it is passed as both.
2809 2806
2810 2807 :Keywords:
2811 2808 islog : boolean (False)
2812 2809
2813 2810 quiet : boolean (True)
2814 2811
2815 2812 exit_ignore : boolean (False)
2816 2813 """
2817 2814
2818 2815 def syspath_cleanup():
2819 2816 """Internal cleanup routine for sys.path."""
2820 2817 if add_dname:
2821 2818 try:
2822 2819 sys.path.remove(dname)
2823 2820 except ValueError:
2824 2821 # For some reason the user has already removed it, ignore.
2825 2822 pass
2826 2823
2827 2824 fname = os.path.expanduser(fname)
2828 2825
2829 2826 # Find things also in current directory. This is needed to mimic the
2830 2827 # behavior of running a script from the system command line, where
2831 2828 # Python inserts the script's directory into sys.path
2832 2829 dname = os.path.dirname(os.path.abspath(fname))
2833 2830 add_dname = False
2834 2831 if dname not in sys.path:
2835 2832 sys.path.insert(0,dname)
2836 2833 add_dname = True
2837 2834
2838 2835 try:
2839 2836 xfile = open(fname)
2840 2837 except:
2841 2838 print >> Term.cerr, \
2842 2839 'Could not open file <%s> for safe execution.' % fname
2843 2840 syspath_cleanup()
2844 2841 return None
2845 2842
2846 2843 kw.setdefault('islog',0)
2847 2844 kw.setdefault('quiet',1)
2848 2845 kw.setdefault('exit_ignore',0)
2849 2846
2850 2847 first = xfile.readline()
2851 2848 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2852 2849 xfile.close()
2853 2850 # line by line execution
2854 2851 if first.startswith(loghead) or kw['islog']:
2855 2852 print 'Loading log file <%s> one line at a time...' % fname
2856 2853 if kw['quiet']:
2857 2854 stdout_save = sys.stdout
2858 2855 sys.stdout = StringIO.StringIO()
2859 2856 try:
2860 2857 globs,locs = where[0:2]
2861 2858 except:
2862 2859 try:
2863 2860 globs = locs = where[0]
2864 2861 except:
2865 2862 globs = locs = globals()
2866 2863 badblocks = []
2867 2864
2868 2865 # we also need to identify indented blocks of code when replaying
2869 2866 # logs and put them together before passing them to an exec
2870 2867 # statement. This takes a bit of regexp and look-ahead work in the
2871 2868 # file. It's easiest if we swallow the whole thing in memory
2872 2869 # first, and manually walk through the lines list moving the
2873 2870 # counter ourselves.
2874 2871 indent_re = re.compile('\s+\S')
2875 2872 xfile = open(fname)
2876 2873 filelines = xfile.readlines()
2877 2874 xfile.close()
2878 2875 nlines = len(filelines)
2879 2876 lnum = 0
2880 2877 while lnum < nlines:
2881 2878 line = filelines[lnum]
2882 2879 lnum += 1
2883 2880 # don't re-insert logger status info into cache
2884 2881 if line.startswith('#log#'):
2885 2882 continue
2886 2883 else:
2887 2884 # build a block of code (maybe a single line) for execution
2888 2885 block = line
2889 2886 try:
2890 2887 next = filelines[lnum] # lnum has already incremented
2891 2888 except:
2892 2889 next = None
2893 2890 while next and indent_re.match(next):
2894 2891 block += next
2895 2892 lnum += 1
2896 2893 try:
2897 2894 next = filelines[lnum]
2898 2895 except:
2899 2896 next = None
2900 2897 # now execute the block of one or more lines
2901 2898 try:
2902 2899 exec block in globs,locs
2903 2900 except SystemExit:
2904 2901 pass
2905 2902 except:
2906 2903 badblocks.append(block.rstrip())
2907 2904 if kw['quiet']: # restore stdout
2908 2905 sys.stdout.close()
2909 2906 sys.stdout = stdout_save
2910 2907 print 'Finished replaying log file <%s>' % fname
2911 2908 if badblocks:
2912 2909 print >> sys.stderr, ('\nThe following lines/blocks in file '
2913 2910 '<%s> reported errors:' % fname)
2914 2911
2915 2912 for badline in badblocks:
2916 2913 print >> sys.stderr, badline
2917 2914 else: # regular file execution
2918 2915 try:
2919 2916 if sys.platform == 'win32' and sys.version_info < (2,5,1):
2920 2917 # Work around a bug in Python for Windows. The bug was
2921 2918 # fixed in in Python 2.5 r54159 and 54158, but that's still
2922 2919 # SVN Python as of March/07. For details, see:
2923 2920 # http://projects.scipy.org/ipython/ipython/ticket/123
2924 2921 try:
2925 2922 globs,locs = where[0:2]
2926 2923 except:
2927 2924 try:
2928 2925 globs = locs = where[0]
2929 2926 except:
2930 2927 globs = locs = globals()
2931 2928 exec file(fname) in globs,locs
2932 2929 else:
2933 2930 execfile(fname,*where)
2934 2931 except SyntaxError:
2935 2932 self.showsyntaxerror()
2936 2933 warn('Failure executing file: <%s>' % fname)
2937 2934 except SystemExit,status:
2938 2935 # Code that correctly sets the exit status flag to success (0)
2939 2936 # shouldn't be bothered with a traceback. Note that a plain
2940 2937 # sys.exit() does NOT set the message to 0 (it's empty) so that
2941 2938 # will still get a traceback. Note that the structure of the
2942 2939 # SystemExit exception changed between Python 2.4 and 2.5, so
2943 2940 # the checks must be done in a version-dependent way.
2944 2941 show = False
2945 2942
2946 2943 if sys.version_info[:2] > (2,5):
2947 2944 if status.message!=0 and not kw['exit_ignore']:
2948 2945 show = True
2949 2946 else:
2950 2947 if status.code and not kw['exit_ignore']:
2951 2948 show = True
2952 2949 if show:
2953 2950 self.showtraceback()
2954 2951 warn('Failure executing file: <%s>' % fname)
2955 2952 except:
2956 2953 self.showtraceback()
2957 2954 warn('Failure executing file: <%s>' % fname)
2958 2955
2959 2956 syspath_cleanup()
2960 2957
2961 2958 #************************* end of file <iplib.py> *****************************
@@ -1,1063 +1,1062 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 ultratb.py -- Spice up your tracebacks!
4 4
5 5 * ColorTB
6 6 I've always found it a bit hard to visually parse tracebacks in Python. The
7 7 ColorTB class is a solution to that problem. It colors the different parts of a
8 8 traceback in a manner similar to what you would expect from a syntax-highlighting
9 9 text editor.
10 10
11 11 Installation instructions for ColorTB:
12 12 import sys,ultratb
13 13 sys.excepthook = ultratb.ColorTB()
14 14
15 15 * VerboseTB
16 16 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
17 17 of useful info when a traceback occurs. Ping originally had it spit out HTML
18 18 and intended it for CGI programmers, but why should they have all the fun? I
19 19 altered it to spit out colored text to the terminal. It's a bit overwhelming,
20 20 but kind of neat, and maybe useful for long-running programs that you believe
21 21 are bug-free. If a crash *does* occur in that type of program you want details.
22 22 Give it a shot--you'll love it or you'll hate it.
23 23
24 24 Note:
25 25
26 26 The Verbose mode prints the variables currently visible where the exception
27 27 happened (shortening their strings if too long). This can potentially be
28 28 very slow, if you happen to have a huge data structure whose string
29 29 representation is complex to compute. Your computer may appear to freeze for
30 30 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
31 31 with Ctrl-C (maybe hitting it more than once).
32 32
33 33 If you encounter this kind of situation often, you may want to use the
34 34 Verbose_novars mode instead of the regular Verbose, which avoids formatting
35 35 variables (but otherwise includes the information and context given by
36 36 Verbose).
37 37
38 38
39 39 Installation instructions for ColorTB:
40 40 import sys,ultratb
41 41 sys.excepthook = ultratb.VerboseTB()
42 42
43 43 Note: Much of the code in this module was lifted verbatim from the standard
44 44 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
45 45
46 46 * Color schemes
47 47 The colors are defined in the class TBTools through the use of the
48 48 ColorSchemeTable class. Currently the following exist:
49 49
50 50 - NoColor: allows all of this module to be used in any terminal (the color
51 51 escapes are just dummy blank strings).
52 52
53 53 - Linux: is meant to look good in a terminal like the Linux console (black
54 54 or very dark background).
55 55
56 56 - LightBG: similar to Linux but swaps dark/light colors to be more readable
57 57 in light background terminals.
58 58
59 59 You can implement other color schemes easily, the syntax is fairly
60 60 self-explanatory. Please send back new schemes you develop to the author for
61 61 possible inclusion in future releases.
62 62 """
63 63
64 64 #*****************************************************************************
65 65 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
66 66 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
67 67 #
68 68 # Distributed under the terms of the BSD License. The full license is in
69 69 # the file COPYING, distributed as part of this software.
70 70 #*****************************************************************************
71 71
72 # Required modules
72 from __future__ import with_statement
73
73 74 import inspect
74 75 import keyword
75 76 import linecache
76 77 import os
77 78 import pydoc
78 79 import re
79 80 import string
80 81 import sys
81 82 import time
82 83 import tokenize
83 84 import traceback
84 85 import types
85 86
86 87 # For purposes of monkeypatching inspect to fix a bug in it.
87 88 from inspect import getsourcefile, getfile, getmodule,\
88 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
89 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
89 90
90 91
91 92 # IPython's own modules
92 93 # Modified pdb which doesn't damage IPython's readline handling
93 94 from IPython.utils import PyColorize
94 95 from IPython.core import debugger, ipapi
96 from IPython.core.display_trap import DisplayTrap
95 97 from IPython.utils.ipstruct import Struct
96 98 from IPython.core.excolors import exception_colors
97 from IPython.utils.genutils import Term,uniq_stable,error,info
99 from IPython.utils.genutils import Term, uniq_stable, error, info
98 100
99 101 # Globals
100 102 # amount of space to put line numbers before verbose tracebacks
101 103 INDENT_SIZE = 8
102 104
103 105 # Default color scheme. This is used, for example, by the traceback
104 106 # formatter. When running in an actual IPython instance, the user's rc.colors
105 107 # value is used, but havinga module global makes this functionality available
106 108 # to users of ultratb who are NOT running inside ipython.
107 109 DEFAULT_SCHEME = 'NoColor'
108 110
109 111 #---------------------------------------------------------------------------
110 112 # Code begins
111 113
112 114 # Utility functions
113 115 def inspect_error():
114 116 """Print a message about internal inspect errors.
115 117
116 118 These are unfortunately quite common."""
117 119
118 120 error('Internal Python error in the inspect module.\n'
119 121 'Below is the traceback from this internal error.\n')
120 122
121 123
122 124 def findsource(object):
123 125 """Return the entire source file and starting line number for an object.
124 126
125 127 The argument may be a module, class, method, function, traceback, frame,
126 128 or code object. The source code is returned as a list of all the lines
127 129 in the file and the line number indexes a line in that list. An IOError
128 130 is raised if the source code cannot be retrieved.
129 131
130 132 FIXED version with which we monkeypatch the stdlib to work around a bug."""
131 133
132 134 file = getsourcefile(object) or getfile(object)
133 135 # If the object is a frame, then trying to get the globals dict from its
134 136 # module won't work. Instead, the frame object itself has the globals
135 137 # dictionary.
136 138 globals_dict = None
137 139 if inspect.isframe(object):
138 140 # XXX: can this ever be false?
139 141 globals_dict = object.f_globals
140 142 else:
141 143 module = getmodule(object, file)
142 144 if module:
143 145 globals_dict = module.__dict__
144 146 lines = linecache.getlines(file, globals_dict)
145 147 if not lines:
146 148 raise IOError('could not get source code')
147 149
148 150 if ismodule(object):
149 151 return lines, 0
150 152
151 153 if isclass(object):
152 154 name = object.__name__
153 155 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
154 156 # make some effort to find the best matching class definition:
155 157 # use the one with the least indentation, which is the one
156 158 # that's most probably not inside a function definition.
157 159 candidates = []
158 160 for i in range(len(lines)):
159 161 match = pat.match(lines[i])
160 162 if match:
161 163 # if it's at toplevel, it's already the best one
162 164 if lines[i][0] == 'c':
163 165 return lines, i
164 166 # else add whitespace to candidate list
165 167 candidates.append((match.group(1), i))
166 168 if candidates:
167 169 # this will sort by whitespace, and by line number,
168 170 # less whitespace first
169 171 candidates.sort()
170 172 return lines, candidates[0][1]
171 173 else:
172 174 raise IOError('could not find class definition')
173 175
174 176 if ismethod(object):
175 177 object = object.im_func
176 178 if isfunction(object):
177 179 object = object.func_code
178 180 if istraceback(object):
179 181 object = object.tb_frame
180 182 if isframe(object):
181 183 object = object.f_code
182 184 if iscode(object):
183 185 if not hasattr(object, 'co_firstlineno'):
184 186 raise IOError('could not find function definition')
185 187 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
186 188 pmatch = pat.match
187 189 # fperez - fix: sometimes, co_firstlineno can give a number larger than
188 190 # the length of lines, which causes an error. Safeguard against that.
189 191 lnum = min(object.co_firstlineno,len(lines))-1
190 192 while lnum > 0:
191 193 if pmatch(lines[lnum]): break
192 194 lnum -= 1
193 195
194 196 return lines, lnum
195 197 raise IOError('could not find code object')
196 198
197 199 # Monkeypatch inspect to apply our bugfix. This code only works with py25
198 200 if sys.version_info[:2] >= (2,5):
199 201 inspect.findsource = findsource
200 202
201 203 def fix_frame_records_filenames(records):
202 204 """Try to fix the filenames in each record from inspect.getinnerframes().
203 205
204 206 Particularly, modules loaded from within zip files have useless filenames
205 207 attached to their code object, and inspect.getinnerframes() just uses it.
206 208 """
207 209 fixed_records = []
208 210 for frame, filename, line_no, func_name, lines, index in records:
209 211 # Look inside the frame's globals dictionary for __file__, which should
210 212 # be better.
211 213 better_fn = frame.f_globals.get('__file__', None)
212 214 if isinstance(better_fn, str):
213 215 # Check the type just in case someone did something weird with
214 216 # __file__. It might also be None if the error occurred during
215 217 # import.
216 218 filename = better_fn
217 219 fixed_records.append((frame, filename, line_no, func_name, lines, index))
218 220 return fixed_records
219 221
220 222
221 223 def _fixed_getinnerframes(etb, context=1,tb_offset=0):
222 224 import linecache
223 225 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
224 226
225 227 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
226 228
227 229 # If the error is at the console, don't build any context, since it would
228 230 # otherwise produce 5 blank lines printed out (there is no file at the
229 231 # console)
230 232 rec_check = records[tb_offset:]
231 233 try:
232 234 rname = rec_check[0][1]
233 235 if rname == '<ipython console>' or rname.endswith('<string>'):
234 236 return rec_check
235 237 except IndexError:
236 238 pass
237 239
238 240 aux = traceback.extract_tb(etb)
239 241 assert len(records) == len(aux)
240 242 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
241 243 maybeStart = lnum-1 - context//2
242 244 start = max(maybeStart, 0)
243 245 end = start + context
244 246 lines = linecache.getlines(file)[start:end]
245 247 # pad with empty lines if necessary
246 248 if maybeStart < 0:
247 249 lines = (['\n'] * -maybeStart) + lines
248 250 if len(lines) < context:
249 251 lines += ['\n'] * (context - len(lines))
250 252 buf = list(records[i])
251 253 buf[LNUM_POS] = lnum
252 254 buf[INDEX_POS] = lnum - 1 - start
253 255 buf[LINES_POS] = lines
254 256 records[i] = tuple(buf)
255 257 return records[tb_offset:]
256 258
257 259 # Helper function -- largely belongs to VerboseTB, but we need the same
258 260 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
259 261 # can be recognized properly by ipython.el's py-traceback-line-re
260 262 # (SyntaxErrors have to be treated specially because they have no traceback)
261 263
262 264 _parser = PyColorize.Parser()
263 265
264 266 def _formatTracebackLines(lnum, index, lines, Colors, lvals=None,scheme=None):
265 267 numbers_width = INDENT_SIZE - 1
266 268 res = []
267 269 i = lnum - index
268 270
269 271 # This lets us get fully syntax-highlighted tracebacks.
270 272 if scheme is None:
271 273 ipinst = ipapi.get()
272 274 if ipinst is not None:
273 275 scheme = ipinst.colors
274 276 else:
275 277 scheme = DEFAULT_SCHEME
276 278
277 279 _line_format = _parser.format2
278 280
279 281 for line in lines:
280 282 new_line, err = _line_format(line,'str',scheme)
281 283 if not err: line = new_line
282 284
283 285 if i == lnum:
284 286 # This is the line with the error
285 287 pad = numbers_width - len(str(i))
286 288 if pad >= 3:
287 289 marker = '-'*(pad-3) + '-> '
288 290 elif pad == 2:
289 291 marker = '> '
290 292 elif pad == 1:
291 293 marker = '>'
292 294 else:
293 295 marker = ''
294 296 num = marker + str(i)
295 297 line = '%s%s%s %s%s' %(Colors.linenoEm, num,
296 298 Colors.line, line, Colors.Normal)
297 299 else:
298 300 num = '%*s' % (numbers_width,i)
299 301 line = '%s%s%s %s' %(Colors.lineno, num,
300 302 Colors.Normal, line)
301 303
302 304 res.append(line)
303 305 if lvals and i == lnum:
304 306 res.append(lvals + '\n')
305 307 i = i + 1
306 308 return res
307 309
308 310
309 311 #---------------------------------------------------------------------------
310 312 # Module classes
311 313 class TBTools:
312 314 """Basic tools used by all traceback printer classes."""
313 315
314 316 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
315 317 # Whether to call the interactive pdb debugger after printing
316 318 # tracebacks or not
317 319 self.call_pdb = call_pdb
318 320
319 321 # Create color table
320 322 self.color_scheme_table = exception_colors()
321 323
322 324 self.set_colors(color_scheme)
323 325 self.old_scheme = color_scheme # save initial value for toggles
324 326
325 327 if call_pdb:
326 328 self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name)
327 329 else:
328 330 self.pdb = None
329 331
330 332 def set_colors(self,*args,**kw):
331 333 """Shorthand access to the color table scheme selector method."""
332 334
333 335 # Set own color table
334 336 self.color_scheme_table.set_active_scheme(*args,**kw)
335 337 # for convenience, set Colors to the active scheme
336 338 self.Colors = self.color_scheme_table.active_colors
337 339 # Also set colors of debugger
338 340 if hasattr(self,'pdb') and self.pdb is not None:
339 341 self.pdb.set_colors(*args,**kw)
340 342
341 343 def color_toggle(self):
342 344 """Toggle between the currently active color scheme and NoColor."""
343 345
344 346 if self.color_scheme_table.active_scheme_name == 'NoColor':
345 347 self.color_scheme_table.set_active_scheme(self.old_scheme)
346 348 self.Colors = self.color_scheme_table.active_colors
347 349 else:
348 350 self.old_scheme = self.color_scheme_table.active_scheme_name
349 351 self.color_scheme_table.set_active_scheme('NoColor')
350 352 self.Colors = self.color_scheme_table.active_colors
351 353
352 354 #---------------------------------------------------------------------------
353 355 class ListTB(TBTools):
354 356 """Print traceback information from a traceback list, with optional color.
355 357
356 358 Calling: requires 3 arguments:
357 359 (etype, evalue, elist)
358 360 as would be obtained by:
359 361 etype, evalue, tb = sys.exc_info()
360 362 if tb:
361 363 elist = traceback.extract_tb(tb)
362 364 else:
363 365 elist = None
364 366
365 367 It can thus be used by programs which need to process the traceback before
366 368 printing (such as console replacements based on the code module from the
367 369 standard library).
368 370
369 371 Because they are meant to be called without a full traceback (only a
370 372 list), instances of this class can't call the interactive pdb debugger."""
371 373
372 374 def __init__(self,color_scheme = 'NoColor'):
373 375 TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0)
374 376
375 377 def __call__(self, etype, value, elist):
376 378 Term.cout.flush()
377 379 print >> Term.cerr, self.text(etype,value,elist)
378 380 Term.cerr.flush()
379 381
380 382 def text(self,etype, value, elist,context=5):
381 383 """Return a color formatted string with the traceback info."""
382 384
383 385 Colors = self.Colors
384 386 out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]
385 387 if elist:
386 388 out_string.append('Traceback %s(most recent call last)%s:' % \
387 389 (Colors.normalEm, Colors.Normal) + '\n')
388 390 out_string.extend(self._format_list(elist))
389 391 lines = self._format_exception_only(etype, value)
390 392 for line in lines[:-1]:
391 393 out_string.append(" "+line)
392 394 out_string.append(lines[-1])
393 395 return ''.join(out_string)
394 396
395 397 def _format_list(self, extracted_list):
396 398 """Format a list of traceback entry tuples for printing.
397 399
398 400 Given a list of tuples as returned by extract_tb() or
399 401 extract_stack(), return a list of strings ready for printing.
400 402 Each string in the resulting list corresponds to the item with the
401 403 same index in the argument list. Each string ends in a newline;
402 404 the strings may contain internal newlines as well, for those items
403 405 whose source text line is not None.
404 406
405 407 Lifted almost verbatim from traceback.py
406 408 """
407 409
408 410 Colors = self.Colors
409 411 list = []
410 412 for filename, lineno, name, line in extracted_list[:-1]:
411 413 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
412 414 (Colors.filename, filename, Colors.Normal,
413 415 Colors.lineno, lineno, Colors.Normal,
414 416 Colors.name, name, Colors.Normal)
415 417 if line:
416 418 item = item + ' %s\n' % line.strip()
417 419 list.append(item)
418 420 # Emphasize the last entry
419 421 filename, lineno, name, line = extracted_list[-1]
420 422 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
421 423 (Colors.normalEm,
422 424 Colors.filenameEm, filename, Colors.normalEm,
423 425 Colors.linenoEm, lineno, Colors.normalEm,
424 426 Colors.nameEm, name, Colors.normalEm,
425 427 Colors.Normal)
426 428 if line:
427 429 item = item + '%s %s%s\n' % (Colors.line, line.strip(),
428 430 Colors.Normal)
429 431 list.append(item)
430 432 return list
431 433
432 434 def _format_exception_only(self, etype, value):
433 435 """Format the exception part of a traceback.
434 436
435 437 The arguments are the exception type and value such as given by
436 438 sys.exc_info()[:2]. The return value is a list of strings, each ending
437 439 in a newline. Normally, the list contains a single string; however,
438 440 for SyntaxError exceptions, it contains several lines that (when
439 441 printed) display detailed information about where the syntax error
440 442 occurred. The message indicating which exception occurred is the
441 443 always last string in the list.
442 444
443 445 Also lifted nearly verbatim from traceback.py
444 446 """
445 447
446 448 have_filedata = False
447 449 Colors = self.Colors
448 450 list = []
449 451 try:
450 452 stype = Colors.excName + etype.__name__ + Colors.Normal
451 453 except AttributeError:
452 454 stype = etype # String exceptions don't get special coloring
453 455 if value is None:
454 456 list.append( str(stype) + '\n')
455 457 else:
456 458 if etype is SyntaxError:
457 459 try:
458 460 msg, (filename, lineno, offset, line) = value
459 461 except:
460 462 have_filedata = False
461 463 else:
462 464 have_filedata = True
463 465 #print 'filename is',filename # dbg
464 466 if not filename: filename = "<string>"
465 467 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
466 468 (Colors.normalEm,
467 469 Colors.filenameEm, filename, Colors.normalEm,
468 470 Colors.linenoEm, lineno, Colors.Normal ))
469 471 if line is not None:
470 472 i = 0
471 473 while i < len(line) and line[i].isspace():
472 474 i = i+1
473 475 list.append('%s %s%s\n' % (Colors.line,
474 476 line.strip(),
475 477 Colors.Normal))
476 478 if offset is not None:
477 479 s = ' '
478 480 for c in line[i:offset-1]:
479 481 if c.isspace():
480 482 s = s + c
481 483 else:
482 484 s = s + ' '
483 485 list.append('%s%s^%s\n' % (Colors.caret, s,
484 486 Colors.Normal) )
485 487 value = msg
486 488 s = self._some_str(value)
487 489 if s:
488 490 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
489 491 Colors.Normal, s))
490 492 else:
491 493 list.append('%s\n' % str(stype))
492 494
493 495 # vds:>>
494 496 if have_filedata:
495 497 ipinst = ipapi.get()
496 498 if ipinst is not None:
497 499 ipinst.hooks.synchronize_with_editor(filename, lineno, 0)
498 500 # vds:<<
499 501
500 502 return list
501 503
502 504 def _some_str(self, value):
503 505 # Lifted from traceback.py
504 506 try:
505 507 return str(value)
506 508 except:
507 509 return '<unprintable %s object>' % type(value).__name__
508 510
509 511 #----------------------------------------------------------------------------
510 512 class VerboseTB(TBTools):
511 513 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
512 514 of HTML. Requires inspect and pydoc. Crazy, man.
513 515
514 516 Modified version which optionally strips the topmost entries from the
515 517 traceback, to be used with alternate interpreters (because their own code
516 518 would appear in the traceback)."""
517 519
518 520 def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0,
519 521 call_pdb = 0, include_vars=1):
520 522 """Specify traceback offset, headers and color scheme.
521 523
522 524 Define how many frames to drop from the tracebacks. Calling it with
523 525 tb_offset=1 allows use of this handler in interpreters which will have
524 526 their own code at the top of the traceback (VerboseTB will first
525 527 remove that frame before printing the traceback info)."""
526 528 TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb)
527 529 self.tb_offset = tb_offset
528 530 self.long_header = long_header
529 531 self.include_vars = include_vars
530 532
531 533 def text(self, etype, evalue, etb, context=5):
532 534 """Return a nice text document describing the traceback."""
533 535
534 536 # some locals
535 537 try:
536 538 etype = etype.__name__
537 539 except AttributeError:
538 540 pass
539 541 Colors = self.Colors # just a shorthand + quicker name lookup
540 542 ColorsNormal = Colors.Normal # used a lot
541 543 col_scheme = self.color_scheme_table.active_scheme_name
542 544 indent = ' '*INDENT_SIZE
543 545 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
544 546 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
545 547 exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal)
546 548
547 549 # some internal-use functions
548 550 def text_repr(value):
549 551 """Hopefully pretty robust repr equivalent."""
550 552 # this is pretty horrible but should always return *something*
551 553 try:
552 554 return pydoc.text.repr(value)
553 555 except KeyboardInterrupt:
554 556 raise
555 557 except:
556 558 try:
557 559 return repr(value)
558 560 except KeyboardInterrupt:
559 561 raise
560 562 except:
561 563 try:
562 564 # all still in an except block so we catch
563 565 # getattr raising
564 566 name = getattr(value, '__name__', None)
565 567 if name:
566 568 # ick, recursion
567 569 return text_repr(name)
568 570 klass = getattr(value, '__class__', None)
569 571 if klass:
570 572 return '%s instance' % text_repr(klass)
571 573 except KeyboardInterrupt:
572 574 raise
573 575 except:
574 576 return 'UNRECOVERABLE REPR FAILURE'
575 577 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
576 578 def nullrepr(value, repr=text_repr): return ''
577 579
578 580 # meat of the code begins
579 581 try:
580 582 etype = etype.__name__
581 583 except AttributeError:
582 584 pass
583 585
584 586 if self.long_header:
585 587 # Header with the exception type, python version, and date
586 588 pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable
587 589 date = time.ctime(time.time())
588 590
589 591 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
590 592 exc, ' '*(75-len(str(etype))-len(pyver)),
591 593 pyver, string.rjust(date, 75) )
592 594 head += "\nA problem occured executing Python code. Here is the sequence of function"\
593 595 "\ncalls leading up to the error, with the most recent (innermost) call last."
594 596 else:
595 597 # Simplified header
596 598 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
597 599 string.rjust('Traceback (most recent call last)',
598 600 75 - len(str(etype)) ) )
599 601 frames = []
600 602 # Flush cache before calling inspect. This helps alleviate some of the
601 603 # problems with python 2.3's inspect.py.
602 604 linecache.checkcache()
603 605 # Drop topmost frames if requested
604 606 try:
605 607 # Try the default getinnerframes and Alex's: Alex's fixes some
606 608 # problems, but it generates empty tracebacks for console errors
607 609 # (5 blanks lines) where none should be returned.
608 610 #records = inspect.getinnerframes(etb, context)[self.tb_offset:]
609 611 #print 'python records:', records # dbg
610 612 records = _fixed_getinnerframes(etb, context,self.tb_offset)
611 613 #print 'alex records:', records # dbg
612 614 except:
613 615
614 616 # FIXME: I've been getting many crash reports from python 2.3
615 617 # users, traceable to inspect.py. If I can find a small test-case
616 618 # to reproduce this, I should either write a better workaround or
617 619 # file a bug report against inspect (if that's the real problem).
618 620 # So far, I haven't been able to find an isolated example to
619 621 # reproduce the problem.
620 622 inspect_error()
621 623 traceback.print_exc(file=Term.cerr)
622 624 info('\nUnfortunately, your original traceback can not be constructed.\n')
623 625 return ''
624 626
625 627 # build some color string templates outside these nested loops
626 628 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
627 629 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
628 630 ColorsNormal)
629 631 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
630 632 (Colors.vName, Colors.valEm, ColorsNormal)
631 633 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
632 634 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
633 635 Colors.vName, ColorsNormal)
634 636 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
635 637 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
636 638 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
637 639 ColorsNormal)
638 640
639 641 # now, loop over all records printing context and info
640 642 abspath = os.path.abspath
641 643 for frame, file, lnum, func, lines, index in records:
642 644 #print '*** record:',file,lnum,func,lines,index # dbg
643 645 try:
644 646 file = file and abspath(file) or '?'
645 647 except OSError:
646 648 # if file is '<console>' or something not in the filesystem,
647 649 # the abspath call will throw an OSError. Just ignore it and
648 650 # keep the original file string.
649 651 pass
650 652 link = tpl_link % file
651 653 try:
652 654 args, varargs, varkw, locals = inspect.getargvalues(frame)
653 655 except:
654 656 # This can happen due to a bug in python2.3. We should be
655 657 # able to remove this try/except when 2.4 becomes a
656 658 # requirement. Bug details at http://python.org/sf/1005466
657 659 inspect_error()
658 660 traceback.print_exc(file=Term.cerr)
659 661 info("\nIPython's exception reporting continues...\n")
660 662
661 663 if func == '?':
662 664 call = ''
663 665 else:
664 666 # Decide whether to include variable details or not
665 667 var_repr = self.include_vars and eqrepr or nullrepr
666 668 try:
667 669 call = tpl_call % (func,inspect.formatargvalues(args,
668 670 varargs, varkw,
669 671 locals,formatvalue=var_repr))
670 672 except KeyError:
671 673 # Very odd crash from inspect.formatargvalues(). The
672 674 # scenario under which it appeared was a call to
673 675 # view(array,scale) in NumTut.view.view(), where scale had
674 676 # been defined as a scalar (it should be a tuple). Somehow
675 677 # inspect messes up resolving the argument list of view()
676 678 # and barfs out. At some point I should dig into this one
677 679 # and file a bug report about it.
678 680 inspect_error()
679 681 traceback.print_exc(file=Term.cerr)
680 682 info("\nIPython's exception reporting continues...\n")
681 683 call = tpl_call_fail % func
682 684
683 685 # Initialize a list of names on the current line, which the
684 686 # tokenizer below will populate.
685 687 names = []
686 688
687 689 def tokeneater(token_type, token, start, end, line):
688 690 """Stateful tokeneater which builds dotted names.
689 691
690 692 The list of names it appends to (from the enclosing scope) can
691 693 contain repeated composite names. This is unavoidable, since
692 694 there is no way to disambguate partial dotted structures until
693 695 the full list is known. The caller is responsible for pruning
694 696 the final list of duplicates before using it."""
695 697
696 698 # build composite names
697 699 if token == '.':
698 700 try:
699 701 names[-1] += '.'
700 702 # store state so the next token is added for x.y.z names
701 703 tokeneater.name_cont = True
702 704 return
703 705 except IndexError:
704 706 pass
705 707 if token_type == tokenize.NAME and token not in keyword.kwlist:
706 708 if tokeneater.name_cont:
707 709 # Dotted names
708 710 names[-1] += token
709 711 tokeneater.name_cont = False
710 712 else:
711 713 # Regular new names. We append everything, the caller
712 714 # will be responsible for pruning the list later. It's
713 715 # very tricky to try to prune as we go, b/c composite
714 716 # names can fool us. The pruning at the end is easy
715 717 # to do (or the caller can print a list with repeated
716 718 # names if so desired.
717 719 names.append(token)
718 720 elif token_type == tokenize.NEWLINE:
719 721 raise IndexError
720 722 # we need to store a bit of state in the tokenizer to build
721 723 # dotted names
722 724 tokeneater.name_cont = False
723 725
724 726 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
725 727 line = getline(file, lnum[0])
726 728 lnum[0] += 1
727 729 return line
728 730
729 731 # Build the list of names on this line of code where the exception
730 732 # occurred.
731 733 try:
732 734 # This builds the names list in-place by capturing it from the
733 735 # enclosing scope.
734 736 tokenize.tokenize(linereader, tokeneater)
735 737 except IndexError:
736 738 # signals exit of tokenizer
737 739 pass
738 740 except tokenize.TokenError,msg:
739 741 _m = ("An unexpected error occurred while tokenizing input\n"
740 742 "The following traceback may be corrupted or invalid\n"
741 743 "The error message is: %s\n" % msg)
742 744 error(_m)
743 745
744 746 # prune names list of duplicates, but keep the right order
745 747 unique_names = uniq_stable(names)
746 748
747 749 # Start loop over vars
748 750 lvals = []
749 751 if self.include_vars:
750 752 for name_full in unique_names:
751 753 name_base = name_full.split('.',1)[0]
752 754 if name_base in frame.f_code.co_varnames:
753 755 if locals.has_key(name_base):
754 756 try:
755 757 value = repr(eval(name_full,locals))
756 758 except:
757 759 value = undefined
758 760 else:
759 761 value = undefined
760 762 name = tpl_local_var % name_full
761 763 else:
762 764 if frame.f_globals.has_key(name_base):
763 765 try:
764 766 value = repr(eval(name_full,frame.f_globals))
765 767 except:
766 768 value = undefined
767 769 else:
768 770 value = undefined
769 771 name = tpl_global_var % name_full
770 772 lvals.append(tpl_name_val % (name,value))
771 773 if lvals:
772 774 lvals = '%s%s' % (indent,em_normal.join(lvals))
773 775 else:
774 776 lvals = ''
775 777
776 778 level = '%s %s\n' % (link,call)
777 779
778 780 if index is None:
779 781 frames.append(level)
780 782 else:
781 783 frames.append('%s%s' % (level,''.join(
782 784 _formatTracebackLines(lnum,index,lines,Colors,lvals,
783 785 col_scheme))))
784 786
785 787 # Get (safely) a string form of the exception info
786 788 try:
787 789 etype_str,evalue_str = map(str,(etype,evalue))
788 790 except:
789 791 # User exception is improperly defined.
790 792 etype,evalue = str,sys.exc_info()[:2]
791 793 etype_str,evalue_str = map(str,(etype,evalue))
792 794 # ... and format it
793 795 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
794 796 ColorsNormal, evalue_str)]
795 797 if type(evalue) is types.InstanceType:
796 798 try:
797 799 names = [w for w in dir(evalue) if isinstance(w, basestring)]
798 800 except:
799 801 # Every now and then, an object with funny inernals blows up
800 802 # when dir() is called on it. We do the best we can to report
801 803 # the problem and continue
802 804 _m = '%sException reporting error (object with broken dir())%s:'
803 805 exception.append(_m % (Colors.excName,ColorsNormal))
804 806 etype_str,evalue_str = map(str,sys.exc_info()[:2])
805 807 exception.append('%s%s%s: %s' % (Colors.excName,etype_str,
806 808 ColorsNormal, evalue_str))
807 809 names = []
808 810 for name in names:
809 811 value = text_repr(getattr(evalue, name))
810 812 exception.append('\n%s%s = %s' % (indent, name, value))
811 813
812 814 # vds: >>
813 815 if records:
814 816 filepath, lnum = records[-1][1:3]
815 817 #print "file:", str(file), "linenb", str(lnum) # dbg
816 818 filepath = os.path.abspath(filepath)
817 819 ipinst = ipapi.get()
818 820 if ipinst is not None:
819 821 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
820 822 # vds: <<
821 823
822 824 # return all our info assembled as a single string
823 825 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
824 826
825 827 def debugger(self,force=False):
826 828 """Call up the pdb debugger if desired, always clean up the tb
827 829 reference.
828 830
829 831 Keywords:
830 832
831 833 - force(False): by default, this routine checks the instance call_pdb
832 834 flag and does not actually invoke the debugger if the flag is false.
833 835 The 'force' option forces the debugger to activate even if the flag
834 836 is false.
835 837
836 838 If the call_pdb flag is set, the pdb interactive debugger is
837 839 invoked. In all cases, the self.tb reference to the current traceback
838 840 is deleted to prevent lingering references which hamper memory
839 841 management.
840 842
841 843 Note that each call to pdb() does an 'import readline', so if your app
842 844 requires a special setup for the readline completers, you'll have to
843 845 fix that by hand after invoking the exception handler."""
844 846
845 847 if force or self.call_pdb:
846 848 if self.pdb is None:
847 849 self.pdb = debugger.Pdb(
848 850 self.color_scheme_table.active_scheme_name)
849 851 # the system displayhook may have changed, restore the original
850 852 # for pdb
851 dhook = sys.displayhook
852 sys.displayhook = sys.__displayhook__
853 self.pdb.reset()
854 # Find the right frame so we don't pop up inside ipython itself
855 if hasattr(self,'tb'):
856 etb = self.tb
857 else:
858 etb = self.tb = sys.last_traceback
859 while self.tb.tb_next is not None:
860 self.tb = self.tb.tb_next
861 try:
853 display_trap = DisplayTrap(None, sys.__displayhook__)
854 with display_trap:
855 self.pdb.reset()
856 # Find the right frame so we don't pop up inside ipython itself
857 if hasattr(self,'tb'):
858 etb = self.tb
859 else:
860 etb = self.tb = sys.last_traceback
861 while self.tb.tb_next is not None:
862 self.tb = self.tb.tb_next
862 863 if etb and etb.tb_next:
863 864 etb = etb.tb_next
864 865 self.pdb.botframe = etb.tb_frame
865 866 self.pdb.interaction(self.tb.tb_frame, self.tb)
866 finally:
867 sys.displayhook = dhook
868
867
869 868 if hasattr(self,'tb'):
870 869 del self.tb
871 870
872 871 def handler(self, info=None):
873 872 (etype, evalue, etb) = info or sys.exc_info()
874 873 self.tb = etb
875 874 Term.cout.flush()
876 875 print >> Term.cerr, self.text(etype, evalue, etb)
877 876 Term.cerr.flush()
878 877
879 878 # Changed so an instance can just be called as VerboseTB_inst() and print
880 879 # out the right info on its own.
881 880 def __call__(self, etype=None, evalue=None, etb=None):
882 881 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
883 882 if etb is None:
884 883 self.handler()
885 884 else:
886 885 self.handler((etype, evalue, etb))
887 886 try:
888 887 self.debugger()
889 888 except KeyboardInterrupt:
890 889 print "\nKeyboardInterrupt"
891 890
892 891 #----------------------------------------------------------------------------
893 892 class FormattedTB(VerboseTB,ListTB):
894 893 """Subclass ListTB but allow calling with a traceback.
895 894
896 895 It can thus be used as a sys.excepthook for Python > 2.1.
897 896
898 897 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
899 898
900 899 Allows a tb_offset to be specified. This is useful for situations where
901 900 one needs to remove a number of topmost frames from the traceback (such as
902 901 occurs with python programs that themselves execute other python code,
903 902 like Python shells). """
904 903
905 904 def __init__(self, mode = 'Plain', color_scheme='Linux',
906 905 tb_offset = 0,long_header=0,call_pdb=0,include_vars=0):
907 906
908 907 # NEVER change the order of this list. Put new modes at the end:
909 908 self.valid_modes = ['Plain','Context','Verbose']
910 909 self.verbose_modes = self.valid_modes[1:3]
911 910
912 911 VerboseTB.__init__(self,color_scheme,tb_offset,long_header,
913 912 call_pdb=call_pdb,include_vars=include_vars)
914 913 self.set_mode(mode)
915 914
916 915 def _extract_tb(self,tb):
917 916 if tb:
918 917 return traceback.extract_tb(tb)
919 918 else:
920 919 return None
921 920
922 921 def text(self, etype, value, tb,context=5,mode=None):
923 922 """Return formatted traceback.
924 923
925 924 If the optional mode parameter is given, it overrides the current
926 925 mode."""
927 926
928 927 if mode is None:
929 928 mode = self.mode
930 929 if mode in self.verbose_modes:
931 930 # verbose modes need a full traceback
932 931 return VerboseTB.text(self,etype, value, tb,context=5)
933 932 else:
934 933 # We must check the source cache because otherwise we can print
935 934 # out-of-date source code.
936 935 linecache.checkcache()
937 936 # Now we can extract and format the exception
938 937 elist = self._extract_tb(tb)
939 938 if len(elist) > self.tb_offset:
940 939 del elist[:self.tb_offset]
941 940 return ListTB.text(self,etype,value,elist)
942 941
943 942 def set_mode(self,mode=None):
944 943 """Switch to the desired mode.
945 944
946 945 If mode is not specified, cycles through the available modes."""
947 946
948 947 if not mode:
949 948 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
950 949 len(self.valid_modes)
951 950 self.mode = self.valid_modes[new_idx]
952 951 elif mode not in self.valid_modes:
953 952 raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
954 953 'Valid modes: '+str(self.valid_modes)
955 954 else:
956 955 self.mode = mode
957 956 # include variable details only in 'Verbose' mode
958 957 self.include_vars = (self.mode == self.valid_modes[2])
959 958
960 959 # some convenient shorcuts
961 960 def plain(self):
962 961 self.set_mode(self.valid_modes[0])
963 962
964 963 def context(self):
965 964 self.set_mode(self.valid_modes[1])
966 965
967 966 def verbose(self):
968 967 self.set_mode(self.valid_modes[2])
969 968
970 969 #----------------------------------------------------------------------------
971 970 class AutoFormattedTB(FormattedTB):
972 971 """A traceback printer which can be called on the fly.
973 972
974 973 It will find out about exceptions by itself.
975 974
976 975 A brief example:
977 976
978 977 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
979 978 try:
980 979 ...
981 980 except:
982 981 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
983 982 """
984 983 def __call__(self,etype=None,evalue=None,etb=None,
985 984 out=None,tb_offset=None):
986 985 """Print out a formatted exception traceback.
987 986
988 987 Optional arguments:
989 988 - out: an open file-like object to direct output to.
990 989
991 990 - tb_offset: the number of frames to skip over in the stack, on a
992 991 per-call basis (this overrides temporarily the instance's tb_offset
993 992 given at initialization time. """
994 993
995 994 if out is None:
996 995 out = Term.cerr
997 996 Term.cout.flush()
998 997 if tb_offset is not None:
999 998 tb_offset, self.tb_offset = self.tb_offset, tb_offset
1000 999 print >> out, self.text(etype, evalue, etb)
1001 1000 self.tb_offset = tb_offset
1002 1001 else:
1003 1002 print >> out, self.text(etype, evalue, etb)
1004 1003 out.flush()
1005 1004 try:
1006 1005 self.debugger()
1007 1006 except KeyboardInterrupt:
1008 1007 print "\nKeyboardInterrupt"
1009 1008
1010 1009 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
1011 1010 if etype is None:
1012 1011 etype,value,tb = sys.exc_info()
1013 1012 self.tb = tb
1014 1013 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
1015 1014
1016 1015 #---------------------------------------------------------------------------
1017 1016 # A simple class to preserve Nathan's original functionality.
1018 1017 class ColorTB(FormattedTB):
1019 1018 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1020 1019 def __init__(self,color_scheme='Linux',call_pdb=0):
1021 1020 FormattedTB.__init__(self,color_scheme=color_scheme,
1022 1021 call_pdb=call_pdb)
1023 1022
1024 1023 #----------------------------------------------------------------------------
1025 1024 # module testing (minimal)
1026 1025 if __name__ == "__main__":
1027 1026 def spam(c, (d, e)):
1028 1027 x = c + d
1029 1028 y = c * d
1030 1029 foo(x, y)
1031 1030
1032 1031 def foo(a, b, bar=1):
1033 1032 eggs(a, b + bar)
1034 1033
1035 1034 def eggs(f, g, z=globals()):
1036 1035 h = f + g
1037 1036 i = f - g
1038 1037 return h / i
1039 1038
1040 1039 print ''
1041 1040 print '*** Before ***'
1042 1041 try:
1043 1042 print spam(1, (2, 3))
1044 1043 except:
1045 1044 traceback.print_exc()
1046 1045 print ''
1047 1046
1048 1047 handler = ColorTB()
1049 1048 print '*** ColorTB ***'
1050 1049 try:
1051 1050 print spam(1, (2, 3))
1052 1051 except:
1053 1052 apply(handler, sys.exc_info() )
1054 1053 print ''
1055 1054
1056 1055 handler = VerboseTB()
1057 1056 print '*** VerboseTB ***'
1058 1057 try:
1059 1058 print spam(1, (2, 3))
1060 1059 except:
1061 1060 apply(handler, sys.exc_info() )
1062 1061 print ''
1063 1062
General Comments 0
You need to be logged in to leave comments. Login now