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