Show More
@@ -1,2527 +1,2529 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.3 or newer. |
|
6 | 6 | |
|
7 | 7 | This file contains all the classes and helper functions specific to IPython. |
|
8 | 8 | |
|
9 |
$Id: iplib.py 269 |
|
|
9 | $Id: iplib.py 2696 2007-08-30 17:17:15Z fperez $ | |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
14 | 14 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
15 | 15 | # |
|
16 | 16 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | 17 | # the file COPYING, distributed as part of this software. |
|
18 | 18 | # |
|
19 | 19 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
20 | 20 | # Python standard library. Over time, all of that class has been copied |
|
21 | 21 | # verbatim here for modifications which could not be accomplished by |
|
22 | 22 | # subclassing. At this point, there are no dependencies at all on the code |
|
23 | 23 | # module anymore (it is not even imported). The Python License (sec. 2) |
|
24 | 24 | # allows for this, but it's always nice to acknowledge credit where credit is |
|
25 | 25 | # due. |
|
26 | 26 | #***************************************************************************** |
|
27 | 27 | |
|
28 | 28 | #**************************************************************************** |
|
29 | 29 | # Modules and globals |
|
30 | 30 | |
|
31 | 31 | from IPython import Release |
|
32 | 32 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
33 | 33 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
34 | 34 | __license__ = Release.license |
|
35 | 35 | __version__ = Release.version |
|
36 | 36 | |
|
37 | 37 | # Python standard modules |
|
38 | 38 | import __main__ |
|
39 | 39 | import __builtin__ |
|
40 | 40 | import StringIO |
|
41 | 41 | import bdb |
|
42 | 42 | import cPickle as pickle |
|
43 | 43 | import codeop |
|
44 | 44 | import doctest |
|
45 | 45 | import exceptions |
|
46 | 46 | import glob |
|
47 | 47 | import inspect |
|
48 | 48 | import keyword |
|
49 | 49 | import new |
|
50 | 50 | import os |
|
51 | 51 | import pydoc |
|
52 | 52 | import re |
|
53 | 53 | import shutil |
|
54 | 54 | import string |
|
55 | 55 | import sys |
|
56 | 56 | import tempfile |
|
57 | 57 | import traceback |
|
58 | 58 | import types |
|
59 | 59 | import pickleshare |
|
60 | 60 | from sets import Set |
|
61 | 61 | from pprint import pprint, pformat |
|
62 | 62 | |
|
63 | 63 | # IPython's own modules |
|
64 | 64 | #import IPython |
|
65 | 65 | from IPython import Debugger,OInspect,PyColorize,ultraTB |
|
66 | 66 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names |
|
67 | 67 | from IPython.FakeModule import FakeModule |
|
68 | 68 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
69 | 69 | from IPython.Logger import Logger |
|
70 | 70 | from IPython.Magic import Magic |
|
71 | 71 | from IPython.Prompts import CachedOutput |
|
72 | 72 | from IPython.ipstruct import Struct |
|
73 | 73 | from IPython.background_jobs import BackgroundJobManager |
|
74 | 74 | from IPython.usage import cmd_line_usage,interactive_usage |
|
75 | 75 | from IPython.genutils import * |
|
76 | 76 | from IPython.strdispatch import StrDispatch |
|
77 | 77 | import IPython.ipapi |
|
78 | 78 | import IPython.history |
|
79 | 79 | import IPython.prefilter as prefilter |
|
80 | 80 | import IPython.shadowns |
|
81 | 81 | # Globals |
|
82 | 82 | |
|
83 | 83 | # store the builtin raw_input globally, and use this always, in case user code |
|
84 | 84 | # overwrites it (like wx.py.PyShell does) |
|
85 | 85 | raw_input_original = raw_input |
|
86 | 86 | |
|
87 | 87 | # compiled regexps for autoindent management |
|
88 | 88 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | #**************************************************************************** |
|
92 | 92 | # Some utility function definitions |
|
93 | 93 | |
|
94 | 94 | ini_spaces_re = re.compile(r'^(\s+)') |
|
95 | 95 | |
|
96 | 96 | def num_ini_spaces(strng): |
|
97 | 97 | """Return the number of initial spaces in a string""" |
|
98 | 98 | |
|
99 | 99 | ini_spaces = ini_spaces_re.match(strng) |
|
100 | 100 | if ini_spaces: |
|
101 | 101 | return ini_spaces.end() |
|
102 | 102 | else: |
|
103 | 103 | return 0 |
|
104 | 104 | |
|
105 | 105 | def softspace(file, newvalue): |
|
106 | 106 | """Copied from code.py, to remove the dependency""" |
|
107 | 107 | |
|
108 | 108 | oldvalue = 0 |
|
109 | 109 | try: |
|
110 | 110 | oldvalue = file.softspace |
|
111 | 111 | except AttributeError: |
|
112 | 112 | pass |
|
113 | 113 | try: |
|
114 | 114 | file.softspace = newvalue |
|
115 | 115 | except (AttributeError, TypeError): |
|
116 | 116 | # "attribute-less object" or "read-only attributes" |
|
117 | 117 | pass |
|
118 | 118 | return oldvalue |
|
119 | 119 | |
|
120 | 120 | |
|
121 | 121 | #**************************************************************************** |
|
122 | 122 | # Local use exceptions |
|
123 | 123 | class SpaceInInput(exceptions.Exception): pass |
|
124 | 124 | |
|
125 | 125 | |
|
126 | 126 | #**************************************************************************** |
|
127 | 127 | # Local use classes |
|
128 | 128 | class Bunch: pass |
|
129 | 129 | |
|
130 | 130 | class Undefined: pass |
|
131 | 131 | |
|
132 | 132 | class Quitter(object): |
|
133 | 133 | """Simple class to handle exit, similar to Python 2.5's. |
|
134 | 134 | |
|
135 | 135 | It handles exiting in an ipython-safe manner, which the one in Python 2.5 |
|
136 | 136 | doesn't do (obviously, since it doesn't know about ipython).""" |
|
137 | 137 | |
|
138 | 138 | def __init__(self,shell,name): |
|
139 | 139 | self.shell = shell |
|
140 | 140 | self.name = name |
|
141 | 141 | |
|
142 | 142 | def __repr__(self): |
|
143 | 143 | return 'Type %s() to exit.' % self.name |
|
144 | 144 | __str__ = __repr__ |
|
145 | 145 | |
|
146 | 146 | def __call__(self): |
|
147 | 147 | self.shell.exit() |
|
148 | 148 | |
|
149 | 149 | class InputList(list): |
|
150 | 150 | """Class to store user input. |
|
151 | 151 | |
|
152 | 152 | It's basically a list, but slices return a string instead of a list, thus |
|
153 | 153 | allowing things like (assuming 'In' is an instance): |
|
154 | 154 | |
|
155 | 155 | exec In[4:7] |
|
156 | 156 | |
|
157 | 157 | or |
|
158 | 158 | |
|
159 | 159 | exec In[5:9] + In[14] + In[21:25]""" |
|
160 | 160 | |
|
161 | 161 | def __getslice__(self,i,j): |
|
162 | 162 | return ''.join(list.__getslice__(self,i,j)) |
|
163 | 163 | |
|
164 | 164 | class SyntaxTB(ultraTB.ListTB): |
|
165 | 165 | """Extension which holds some state: the last exception value""" |
|
166 | 166 | |
|
167 | 167 | def __init__(self,color_scheme = 'NoColor'): |
|
168 | 168 | ultraTB.ListTB.__init__(self,color_scheme) |
|
169 | 169 | self.last_syntax_error = None |
|
170 | 170 | |
|
171 | 171 | def __call__(self, etype, value, elist): |
|
172 | 172 | self.last_syntax_error = value |
|
173 | 173 | ultraTB.ListTB.__call__(self,etype,value,elist) |
|
174 | 174 | |
|
175 | 175 | def clear_err_state(self): |
|
176 | 176 | """Return the current error state and clear it""" |
|
177 | 177 | e = self.last_syntax_error |
|
178 | 178 | self.last_syntax_error = None |
|
179 | 179 | return e |
|
180 | 180 | |
|
181 | 181 | #**************************************************************************** |
|
182 | 182 | # Main IPython class |
|
183 | 183 | |
|
184 | 184 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so |
|
185 | 185 | # until a full rewrite is made. I've cleaned all cross-class uses of |
|
186 | 186 | # attributes and methods, but too much user code out there relies on the |
|
187 | 187 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. |
|
188 | 188 | # |
|
189 | 189 | # But at least now, all the pieces have been separated and we could, in |
|
190 | 190 | # principle, stop using the mixin. This will ease the transition to the |
|
191 | 191 | # chainsaw branch. |
|
192 | 192 | |
|
193 | 193 | # For reference, the following is the list of 'self.foo' uses in the Magic |
|
194 | 194 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython |
|
195 | 195 | # class, to prevent clashes. |
|
196 | 196 | |
|
197 | 197 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', |
|
198 | 198 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', |
|
199 | 199 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', |
|
200 | 200 | # 'self.value'] |
|
201 | 201 | |
|
202 | 202 | class InteractiveShell(object,Magic): |
|
203 | 203 | """An enhanced console for Python.""" |
|
204 | 204 | |
|
205 | 205 | # class attribute to indicate whether the class supports threads or not. |
|
206 | 206 | # Subclasses with thread support should override this as needed. |
|
207 | 207 | isthreaded = False |
|
208 | 208 | |
|
209 | 209 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
210 | 210 | user_ns = None,user_global_ns=None,banner2='', |
|
211 | 211 | custom_exceptions=((),None),embedded=False): |
|
212 | 212 | |
|
213 | 213 | # log system |
|
214 | 214 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') |
|
215 | 215 | |
|
216 | 216 | # some minimal strict typechecks. For some core data structures, I |
|
217 | 217 | # want actual basic python types, not just anything that looks like |
|
218 | 218 | # one. This is especially true for namespaces. |
|
219 | 219 | for ns in (user_ns,user_global_ns): |
|
220 | 220 | if ns is not None and type(ns) != types.DictType: |
|
221 | 221 | raise TypeError,'namespace must be a dictionary' |
|
222 | 222 | |
|
223 | 223 | # Job manager (for jobs run as background threads) |
|
224 | 224 | self.jobs = BackgroundJobManager() |
|
225 | 225 | |
|
226 | 226 | # Store the actual shell's name |
|
227 | 227 | self.name = name |
|
228 | 228 | |
|
229 | 229 | # We need to know whether the instance is meant for embedding, since |
|
230 | 230 | # global/local namespaces need to be handled differently in that case |
|
231 | 231 | self.embedded = embedded |
|
232 | 232 | if embedded: |
|
233 | 233 | # Control variable so users can, from within the embedded instance, |
|
234 | 234 | # permanently deactivate it. |
|
235 | 235 | self.embedded_active = True |
|
236 | 236 | |
|
237 | 237 | # command compiler |
|
238 | 238 | self.compile = codeop.CommandCompiler() |
|
239 | 239 | |
|
240 | 240 | # User input buffer |
|
241 | 241 | self.buffer = [] |
|
242 | 242 | |
|
243 | 243 | # Default name given in compilation of code |
|
244 | 244 | self.filename = '<ipython console>' |
|
245 | 245 | |
|
246 | 246 | # Install our own quitter instead of the builtins. For python2.3-2.4, |
|
247 | 247 | # this brings in behavior like 2.5, and for 2.5 it's identical. |
|
248 | 248 | __builtin__.exit = Quitter(self,'exit') |
|
249 | 249 | __builtin__.quit = Quitter(self,'quit') |
|
250 | 250 | |
|
251 | 251 | # Make an empty namespace, which extension writers can rely on both |
|
252 | 252 | # existing and NEVER being used by ipython itself. This gives them a |
|
253 | 253 | # convenient location for storing additional information and state |
|
254 | 254 | # their extensions may require, without fear of collisions with other |
|
255 | 255 | # ipython names that may develop later. |
|
256 | 256 | self.meta = Struct() |
|
257 | 257 | |
|
258 | 258 | # Create the namespace where the user will operate. user_ns is |
|
259 | 259 | # normally the only one used, and it is passed to the exec calls as |
|
260 | 260 | # the locals argument. But we do carry a user_global_ns namespace |
|
261 | 261 | # given as the exec 'globals' argument, This is useful in embedding |
|
262 | 262 | # situations where the ipython shell opens in a context where the |
|
263 | 263 | # distinction between locals and globals is meaningful. |
|
264 | 264 | |
|
265 | 265 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
266 | 266 | # level as a dict instead of a module. This is a manual fix, but I |
|
267 | 267 | # should really track down where the problem is coming from. Alex |
|
268 | 268 | # Schmolck reported this problem first. |
|
269 | 269 | |
|
270 | 270 | # A useful post by Alex Martelli on this topic: |
|
271 | 271 | # Re: inconsistent value from __builtins__ |
|
272 | 272 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
273 | 273 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
274 | 274 | # Gruppen: comp.lang.python |
|
275 | 275 | |
|
276 | 276 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
277 | 277 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
278 | 278 | # > <type 'dict'> |
|
279 | 279 | # > >>> print type(__builtins__) |
|
280 | 280 | # > <type 'module'> |
|
281 | 281 | # > Is this difference in return value intentional? |
|
282 | 282 | |
|
283 | 283 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
284 | 284 | # or a module, and it's been that way for a long time. Whether it's |
|
285 | 285 | # intentional (or sensible), I don't know. In any case, the idea is |
|
286 | 286 | # that if you need to access the built-in namespace directly, you |
|
287 | 287 | # should start with "import __builtin__" (note, no 's') which will |
|
288 | 288 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
289 | 289 | |
|
290 | 290 | # These routines return properly built dicts as needed by the rest of |
|
291 | 291 | # the code, and can also be used by extension writers to generate |
|
292 | 292 | # properly initialized namespaces. |
|
293 | 293 | user_ns = IPython.ipapi.make_user_ns(user_ns) |
|
294 | 294 | user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns) |
|
295 | 295 | |
|
296 | 296 | # Assign namespaces |
|
297 | 297 | # This is the namespace where all normal user variables live |
|
298 | 298 | self.user_ns = user_ns |
|
299 | 299 | # Embedded instances require a separate namespace for globals. |
|
300 | 300 | # Normally this one is unused by non-embedded instances. |
|
301 | 301 | self.user_global_ns = user_global_ns |
|
302 | 302 | # A namespace to keep track of internal data structures to prevent |
|
303 | 303 | # them from cluttering user-visible stuff. Will be updated later |
|
304 | 304 | self.internal_ns = {} |
|
305 | 305 | |
|
306 | 306 | # Namespace of system aliases. Each entry in the alias |
|
307 | 307 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
308 | 308 | # of positional arguments of the alias. |
|
309 | 309 | self.alias_table = {} |
|
310 | 310 | |
|
311 | 311 | # A table holding all the namespaces IPython deals with, so that |
|
312 | 312 | # introspection facilities can search easily. |
|
313 | 313 | self.ns_table = {'user':user_ns, |
|
314 | 314 | 'user_global':user_global_ns, |
|
315 | 315 | 'alias':self.alias_table, |
|
316 | 316 | 'internal':self.internal_ns, |
|
317 | 317 | 'builtin':__builtin__.__dict__ |
|
318 | 318 | } |
|
319 | 319 | |
|
320 | 320 | # The user namespace MUST have a pointer to the shell itself. |
|
321 | 321 | self.user_ns[name] = self |
|
322 | 322 | |
|
323 | 323 | # We need to insert into sys.modules something that looks like a |
|
324 | 324 | # module but which accesses the IPython namespace, for shelve and |
|
325 | 325 | # pickle to work interactively. Normally they rely on getting |
|
326 | 326 | # everything out of __main__, but for embedding purposes each IPython |
|
327 | 327 | # instance has its own private namespace, so we can't go shoving |
|
328 | 328 | # everything into __main__. |
|
329 | 329 | |
|
330 | 330 | # note, however, that we should only do this for non-embedded |
|
331 | 331 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
332 | 332 | # namespace. Embedded instances, on the other hand, should not do |
|
333 | 333 | # this because they need to manage the user local/global namespaces |
|
334 | 334 | # only, but they live within a 'normal' __main__ (meaning, they |
|
335 | 335 | # shouldn't overtake the execution environment of the script they're |
|
336 | 336 | # embedded in). |
|
337 | 337 | |
|
338 | 338 | if not embedded: |
|
339 | 339 | try: |
|
340 | 340 | main_name = self.user_ns['__name__'] |
|
341 | 341 | except KeyError: |
|
342 | 342 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
343 | 343 | else: |
|
344 | 344 | #print "pickle hack in place" # dbg |
|
345 | 345 | #print 'main_name:',main_name # dbg |
|
346 | 346 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
347 | 347 | |
|
348 | 348 | # List of input with multi-line handling. |
|
349 | 349 | # Fill its zero entry, user counter starts at 1 |
|
350 | 350 | self.input_hist = InputList(['\n']) |
|
351 | 351 | # This one will hold the 'raw' input history, without any |
|
352 | 352 | # pre-processing. This will allow users to retrieve the input just as |
|
353 | 353 | # it was exactly typed in by the user, with %hist -r. |
|
354 | 354 | self.input_hist_raw = InputList(['\n']) |
|
355 | 355 | |
|
356 | 356 | # list of visited directories |
|
357 | 357 | try: |
|
358 | 358 | self.dir_hist = [os.getcwd()] |
|
359 | 359 | except OSError: |
|
360 | 360 | self.dir_hist = [] |
|
361 | 361 | |
|
362 | 362 | # dict of output history |
|
363 | 363 | self.output_hist = {} |
|
364 | 364 | |
|
365 | 365 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
366 | 366 | # under Win32 have it set to None, and we need to have a known valid |
|
367 | 367 | # encoding to use in the raw_input() method |
|
368 | 368 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
369 | 369 | |
|
370 | 370 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
371 | 371 | no_alias = {} |
|
372 | 372 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
373 | 373 | for key in keyword.kwlist + no_alias_magics: |
|
374 | 374 | no_alias[key] = 1 |
|
375 | 375 | no_alias.update(__builtin__.__dict__) |
|
376 | 376 | self.no_alias = no_alias |
|
377 | 377 | |
|
378 | 378 | # make global variables for user access to these |
|
379 | 379 | self.user_ns['_ih'] = self.input_hist |
|
380 | 380 | self.user_ns['_oh'] = self.output_hist |
|
381 | 381 | self.user_ns['_dh'] = self.dir_hist |
|
382 | 382 | |
|
383 | 383 | # user aliases to input and output histories |
|
384 | 384 | self.user_ns['In'] = self.input_hist |
|
385 | 385 | self.user_ns['Out'] = self.output_hist |
|
386 | 386 | |
|
387 | 387 | self.user_ns['_sh'] = IPython.shadowns |
|
388 | 388 | # Object variable to store code object waiting execution. This is |
|
389 | 389 | # used mainly by the multithreaded shells, but it can come in handy in |
|
390 | 390 | # other situations. No need to use a Queue here, since it's a single |
|
391 | 391 | # item which gets cleared once run. |
|
392 | 392 | self.code_to_run = None |
|
393 | 393 | |
|
394 | 394 | # escapes for automatic behavior on the command line |
|
395 | 395 | self.ESC_SHELL = '!' |
|
396 | 396 | self.ESC_SH_CAP = '!!' |
|
397 | 397 | self.ESC_HELP = '?' |
|
398 | 398 | self.ESC_MAGIC = '%' |
|
399 | 399 | self.ESC_QUOTE = ',' |
|
400 | 400 | self.ESC_QUOTE2 = ';' |
|
401 | 401 | self.ESC_PAREN = '/' |
|
402 | 402 | |
|
403 | 403 | # And their associated handlers |
|
404 | 404 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
405 | 405 | self.ESC_QUOTE : self.handle_auto, |
|
406 | 406 | self.ESC_QUOTE2 : self.handle_auto, |
|
407 | 407 | self.ESC_MAGIC : self.handle_magic, |
|
408 | 408 | self.ESC_HELP : self.handle_help, |
|
409 | 409 | self.ESC_SHELL : self.handle_shell_escape, |
|
410 | 410 | self.ESC_SH_CAP : self.handle_shell_escape, |
|
411 | 411 | } |
|
412 | 412 | |
|
413 | 413 | # class initializations |
|
414 | 414 | Magic.__init__(self,self) |
|
415 | 415 | |
|
416 | 416 | # Python source parser/formatter for syntax highlighting |
|
417 | 417 | pyformat = PyColorize.Parser().format |
|
418 | 418 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
419 | 419 | |
|
420 | 420 | # hooks holds pointers used for user-side customizations |
|
421 | 421 | self.hooks = Struct() |
|
422 | 422 | |
|
423 | 423 | self.strdispatchers = {} |
|
424 | 424 | |
|
425 | 425 | # Set all default hooks, defined in the IPython.hooks module. |
|
426 | 426 | hooks = IPython.hooks |
|
427 | 427 | for hook_name in hooks.__all__: |
|
428 | 428 | # default hooks have priority 100, i.e. low; user hooks should have |
|
429 | 429 | # 0-100 priority |
|
430 | 430 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
431 | 431 | #print "bound hook",hook_name |
|
432 | 432 | |
|
433 | 433 | # Flag to mark unconditional exit |
|
434 | 434 | self.exit_now = False |
|
435 | 435 | |
|
436 | 436 | self.usage_min = """\ |
|
437 | 437 | An enhanced console for Python. |
|
438 | 438 | Some of its features are: |
|
439 | 439 | - Readline support if the readline library is present. |
|
440 | 440 | - Tab completion in the local namespace. |
|
441 | 441 | - Logging of input, see command-line options. |
|
442 | 442 | - System shell escape via ! , eg !ls. |
|
443 | 443 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
444 | 444 | - Keeps track of locally defined variables via %who, %whos. |
|
445 | 445 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
446 | 446 | """ |
|
447 | 447 | if usage: self.usage = usage |
|
448 | 448 | else: self.usage = self.usage_min |
|
449 | 449 | |
|
450 | 450 | # Storage |
|
451 | 451 | self.rc = rc # This will hold all configuration information |
|
452 | 452 | self.pager = 'less' |
|
453 | 453 | # temporary files used for various purposes. Deleted at exit. |
|
454 | 454 | self.tempfiles = [] |
|
455 | 455 | |
|
456 | 456 | # Keep track of readline usage (later set by init_readline) |
|
457 | 457 | self.has_readline = False |
|
458 | 458 | |
|
459 | 459 | # template for logfile headers. It gets resolved at runtime by the |
|
460 | 460 | # logstart method. |
|
461 | 461 | self.loghead_tpl = \ |
|
462 | 462 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
463 | 463 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
464 | 464 | #log# opts = %s |
|
465 | 465 | #log# args = %s |
|
466 | 466 | #log# It is safe to make manual edits below here. |
|
467 | 467 | #log#----------------------------------------------------------------------- |
|
468 | 468 | """ |
|
469 | 469 | # for pushd/popd management |
|
470 | 470 | try: |
|
471 | 471 | self.home_dir = get_home_dir() |
|
472 | 472 | except HomeDirError,msg: |
|
473 | 473 | fatal(msg) |
|
474 | 474 | |
|
475 | 475 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] |
|
476 | 476 | |
|
477 | 477 | # Functions to call the underlying shell. |
|
478 | 478 | |
|
479 | 479 | # The first is similar to os.system, but it doesn't return a value, |
|
480 | 480 | # and it allows interpolation of variables in the user's namespace. |
|
481 | 481 | self.system = lambda cmd: \ |
|
482 | 482 | shell(self.var_expand(cmd,depth=2), |
|
483 | 483 | header=self.rc.system_header, |
|
484 | 484 | verbose=self.rc.system_verbose) |
|
485 | 485 | |
|
486 | 486 | # These are for getoutput and getoutputerror: |
|
487 | 487 | self.getoutput = lambda cmd: \ |
|
488 | 488 | getoutput(self.var_expand(cmd,depth=2), |
|
489 | 489 | header=self.rc.system_header, |
|
490 | 490 | verbose=self.rc.system_verbose) |
|
491 | 491 | |
|
492 | 492 | self.getoutputerror = lambda cmd: \ |
|
493 | 493 | getoutputerror(self.var_expand(cmd,depth=2), |
|
494 | 494 | header=self.rc.system_header, |
|
495 | 495 | verbose=self.rc.system_verbose) |
|
496 | 496 | |
|
497 | 497 | |
|
498 | 498 | # keep track of where we started running (mainly for crash post-mortem) |
|
499 | 499 | self.starting_dir = os.getcwd() |
|
500 | 500 | |
|
501 | 501 | # Various switches which can be set |
|
502 | 502 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
503 | 503 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
504 | 504 | self.banner2 = banner2 |
|
505 | 505 | |
|
506 | 506 | # TraceBack handlers: |
|
507 | 507 | |
|
508 | 508 | # Syntax error handler. |
|
509 | 509 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
510 | 510 | |
|
511 | 511 | # The interactive one is initialized with an offset, meaning we always |
|
512 | 512 | # want to remove the topmost item in the traceback, which is our own |
|
513 | 513 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
514 | 514 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
515 | 515 | color_scheme='NoColor', |
|
516 | 516 | tb_offset = 1) |
|
517 | 517 | |
|
518 | 518 | # IPython itself shouldn't crash. This will produce a detailed |
|
519 | 519 | # post-mortem if it does. But we only install the crash handler for |
|
520 | 520 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
521 | 521 | # and lose the crash handler. This is because exceptions in the main |
|
522 | 522 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
523 | 523 | # and there's no point in printing crash dumps for every user exception. |
|
524 | 524 | if self.isthreaded: |
|
525 | 525 | ipCrashHandler = ultraTB.FormattedTB() |
|
526 | 526 | else: |
|
527 | 527 | from IPython import CrashHandler |
|
528 | 528 | ipCrashHandler = CrashHandler.IPythonCrashHandler(self) |
|
529 | 529 | self.set_crash_handler(ipCrashHandler) |
|
530 | 530 | |
|
531 | 531 | # and add any custom exception handlers the user may have specified |
|
532 | 532 | self.set_custom_exc(*custom_exceptions) |
|
533 | 533 | |
|
534 | 534 | # indentation management |
|
535 | 535 | self.autoindent = False |
|
536 | 536 | self.indent_current_nsp = 0 |
|
537 | 537 | |
|
538 | 538 | # Make some aliases automatically |
|
539 | 539 | # Prepare list of shell aliases to auto-define |
|
540 | 540 | if os.name == 'posix': |
|
541 | 541 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
542 | 542 | 'mv mv -i','rm rm -i','cp cp -i', |
|
543 | 543 | 'cat cat','less less','clear clear', |
|
544 | 544 | # a better ls |
|
545 | 545 | 'ls ls -F', |
|
546 | 546 | # long ls |
|
547 | 547 | 'll ls -lF') |
|
548 | 548 | # Extra ls aliases with color, which need special treatment on BSD |
|
549 | 549 | # variants |
|
550 | 550 | ls_extra = ( # color ls |
|
551 | 551 | 'lc ls -F -o --color', |
|
552 | 552 | # ls normal files only |
|
553 | 553 | 'lf ls -F -o --color %l | grep ^-', |
|
554 | 554 | # ls symbolic links |
|
555 | 555 | 'lk ls -F -o --color %l | grep ^l', |
|
556 | 556 | # directories or links to directories, |
|
557 | 557 | 'ldir ls -F -o --color %l | grep /$', |
|
558 | 558 | # things which are executable |
|
559 | 559 | 'lx ls -F -o --color %l | grep ^-..x', |
|
560 | 560 | ) |
|
561 | 561 | # The BSDs don't ship GNU ls, so they don't understand the |
|
562 | 562 | # --color switch out of the box |
|
563 | 563 | if 'bsd' in sys.platform: |
|
564 | 564 | ls_extra = ( # ls normal files only |
|
565 | 565 | 'lf ls -lF | grep ^-', |
|
566 | 566 | # ls symbolic links |
|
567 | 567 | 'lk ls -lF | grep ^l', |
|
568 | 568 | # directories or links to directories, |
|
569 | 569 | 'ldir ls -lF | grep /$', |
|
570 | 570 | # things which are executable |
|
571 | 571 | 'lx ls -lF | grep ^-..x', |
|
572 | 572 | ) |
|
573 | 573 | auto_alias = auto_alias + ls_extra |
|
574 | 574 | elif os.name in ['nt','dos']: |
|
575 | 575 | auto_alias = ('dir dir /on', 'ls dir /on', |
|
576 | 576 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
577 | 577 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
578 | 578 | 'ren ren','cls cls','copy copy') |
|
579 | 579 | else: |
|
580 | 580 | auto_alias = () |
|
581 | 581 | self.auto_alias = [s.split(None,1) for s in auto_alias] |
|
582 | 582 | |
|
583 | 583 | # Produce a public API instance |
|
584 | 584 | self.api = IPython.ipapi.IPApi(self) |
|
585 | 585 | |
|
586 | 586 | # Call the actual (public) initializer |
|
587 | 587 | self.init_auto_alias() |
|
588 | 588 | |
|
589 | 589 | # track which builtins we add, so we can clean up later |
|
590 | 590 | self.builtins_added = {} |
|
591 | 591 | # This method will add the necessary builtins for operation, but |
|
592 | 592 | # tracking what it did via the builtins_added dict. |
|
593 | 593 | self.add_builtins() |
|
594 | 594 | |
|
595 | 595 | # end __init__ |
|
596 | 596 | |
|
597 | 597 | def var_expand(self,cmd,depth=0): |
|
598 | 598 | """Expand python variables in a string. |
|
599 | 599 | |
|
600 | 600 | The depth argument indicates how many frames above the caller should |
|
601 | 601 | be walked to look for the local namespace where to expand variables. |
|
602 | 602 | |
|
603 | 603 | The global namespace for expansion is always the user's interactive |
|
604 | 604 | namespace. |
|
605 | 605 | """ |
|
606 | 606 | |
|
607 | 607 | return str(ItplNS(cmd.replace('#','\#'), |
|
608 | 608 | self.user_ns, # globals |
|
609 | 609 | # Skip our own frame in searching for locals: |
|
610 | 610 | sys._getframe(depth+1).f_locals # locals |
|
611 | 611 | )) |
|
612 | 612 | |
|
613 | 613 | def pre_config_initialization(self): |
|
614 | 614 | """Pre-configuration init method |
|
615 | 615 | |
|
616 | 616 | This is called before the configuration files are processed to |
|
617 | 617 | prepare the services the config files might need. |
|
618 | 618 | |
|
619 | 619 | self.rc already has reasonable default values at this point. |
|
620 | 620 | """ |
|
621 | 621 | rc = self.rc |
|
622 | 622 | try: |
|
623 | 623 | self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") |
|
624 | 624 | except exceptions.UnicodeDecodeError: |
|
625 | 625 | print "Your ipythondir can't be decoded to unicode!" |
|
626 | 626 | print "Please set HOME environment variable to something that" |
|
627 | 627 | print r"only has ASCII characters, e.g. c:\home" |
|
628 | 628 | print "Now it is",rc.ipythondir |
|
629 | 629 | sys.exit() |
|
630 | 630 | self.shadowhist = IPython.history.ShadowHist(self.db) |
|
631 | 631 | |
|
632 | 632 | |
|
633 | 633 | def post_config_initialization(self): |
|
634 | 634 | """Post configuration init method |
|
635 | 635 | |
|
636 | 636 | This is called after the configuration files have been processed to |
|
637 | 637 | 'finalize' the initialization.""" |
|
638 | 638 | |
|
639 | 639 | rc = self.rc |
|
640 | 640 | |
|
641 | 641 | # Object inspector |
|
642 | 642 | self.inspector = OInspect.Inspector(OInspect.InspectColors, |
|
643 | 643 | PyColorize.ANSICodeColors, |
|
644 | 644 | 'NoColor', |
|
645 | 645 | rc.object_info_string_level) |
|
646 | 646 | |
|
647 | 647 | self.rl_next_input = None |
|
648 | 648 | self.rl_do_indent = False |
|
649 | 649 | # Load readline proper |
|
650 | 650 | if rc.readline: |
|
651 | 651 | self.init_readline() |
|
652 | 652 | |
|
653 | 653 | |
|
654 | 654 | # local shortcut, this is used a LOT |
|
655 | 655 | self.log = self.logger.log |
|
656 | 656 | |
|
657 | 657 | # Initialize cache, set in/out prompts and printing system |
|
658 | 658 | self.outputcache = CachedOutput(self, |
|
659 | 659 | rc.cache_size, |
|
660 | 660 | rc.pprint, |
|
661 | 661 | input_sep = rc.separate_in, |
|
662 | 662 | output_sep = rc.separate_out, |
|
663 | 663 | output_sep2 = rc.separate_out2, |
|
664 | 664 | ps1 = rc.prompt_in1, |
|
665 | 665 | ps2 = rc.prompt_in2, |
|
666 | 666 | ps_out = rc.prompt_out, |
|
667 | 667 | pad_left = rc.prompts_pad_left) |
|
668 | 668 | |
|
669 | 669 | # user may have over-ridden the default print hook: |
|
670 | 670 | try: |
|
671 | 671 | self.outputcache.__class__.display = self.hooks.display |
|
672 | 672 | except AttributeError: |
|
673 | 673 | pass |
|
674 | 674 | |
|
675 | 675 | # I don't like assigning globally to sys, because it means when |
|
676 | 676 | # embedding instances, each embedded instance overrides the previous |
|
677 | 677 | # choice. But sys.displayhook seems to be called internally by exec, |
|
678 | 678 | # so I don't see a way around it. We first save the original and then |
|
679 | 679 | # overwrite it. |
|
680 | 680 | self.sys_displayhook = sys.displayhook |
|
681 | 681 | sys.displayhook = self.outputcache |
|
682 | 682 | |
|
683 | 683 | # Monkeypatch doctest so that its core test runner method is protected |
|
684 | 684 | # from IPython's modified displayhook. Doctest expects the default |
|
685 | 685 | # displayhook behavior deep down, so our modification breaks it |
|
686 | 686 | # completely. For this reason, a hard monkeypatch seems like a |
|
687 | 687 | # reasonable solution rather than asking users to manually use a |
|
688 | 688 | # different doctest runner when under IPython. |
|
689 | 689 | try: |
|
690 | 690 | doctest.DocTestRunner |
|
691 | 691 | except AttributeError: |
|
692 |
# This is only for python 2.3 compatibility, remove once we move to |
|
|
692 | # This is only for python 2.3 compatibility, remove once we move to | |
|
693 | # 2.4 only. | |
|
694 | pass | |
|
693 | 695 | else: |
|
694 | 696 | doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run) |
|
695 | 697 | |
|
696 | 698 | # Set user colors (don't do it in the constructor above so that it |
|
697 | 699 | # doesn't crash if colors option is invalid) |
|
698 | 700 | self.magic_colors(rc.colors) |
|
699 | 701 | |
|
700 | 702 | # Set calling of pdb on exceptions |
|
701 | 703 | self.call_pdb = rc.pdb |
|
702 | 704 | |
|
703 | 705 | # Load user aliases |
|
704 | 706 | for alias in rc.alias: |
|
705 | 707 | self.magic_alias(alias) |
|
706 | 708 | |
|
707 | 709 | self.hooks.late_startup_hook() |
|
708 | 710 | |
|
709 | 711 | batchrun = False |
|
710 | 712 | for batchfile in [path(arg) for arg in self.rc.args |
|
711 | 713 | if arg.lower().endswith('.ipy')]: |
|
712 | 714 | if not batchfile.isfile(): |
|
713 | 715 | print "No such batch file:", batchfile |
|
714 | 716 | continue |
|
715 | 717 | self.api.runlines(batchfile.text()) |
|
716 | 718 | batchrun = True |
|
717 | 719 | # without -i option, exit after running the batch file |
|
718 | 720 | if batchrun and not self.rc.interact: |
|
719 | 721 | self.exit_now = True |
|
720 | 722 | |
|
721 | 723 | def add_builtins(self): |
|
722 | 724 | """Store ipython references into the builtin namespace. |
|
723 | 725 | |
|
724 | 726 | Some parts of ipython operate via builtins injected here, which hold a |
|
725 | 727 | reference to IPython itself.""" |
|
726 | 728 | |
|
727 | 729 | # TODO: deprecate all except _ip; 'jobs' should be installed |
|
728 | 730 | # by an extension and the rest are under _ip, ipalias is redundant |
|
729 | 731 | builtins_new = dict(__IPYTHON__ = self, |
|
730 | 732 | ip_set_hook = self.set_hook, |
|
731 | 733 | jobs = self.jobs, |
|
732 | 734 | ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'), |
|
733 | 735 | ipalias = wrap_deprecated(self.ipalias), |
|
734 | 736 | ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'), |
|
735 | 737 | _ip = self.api |
|
736 | 738 | ) |
|
737 | 739 | for biname,bival in builtins_new.items(): |
|
738 | 740 | try: |
|
739 | 741 | # store the orignal value so we can restore it |
|
740 | 742 | self.builtins_added[biname] = __builtin__.__dict__[biname] |
|
741 | 743 | except KeyError: |
|
742 | 744 | # or mark that it wasn't defined, and we'll just delete it at |
|
743 | 745 | # cleanup |
|
744 | 746 | self.builtins_added[biname] = Undefined |
|
745 | 747 | __builtin__.__dict__[biname] = bival |
|
746 | 748 | |
|
747 | 749 | # Keep in the builtins a flag for when IPython is active. We set it |
|
748 | 750 | # with setdefault so that multiple nested IPythons don't clobber one |
|
749 | 751 | # another. Each will increase its value by one upon being activated, |
|
750 | 752 | # which also gives us a way to determine the nesting level. |
|
751 | 753 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
752 | 754 | |
|
753 | 755 | def clean_builtins(self): |
|
754 | 756 | """Remove any builtins which might have been added by add_builtins, or |
|
755 | 757 | restore overwritten ones to their previous values.""" |
|
756 | 758 | for biname,bival in self.builtins_added.items(): |
|
757 | 759 | if bival is Undefined: |
|
758 | 760 | del __builtin__.__dict__[biname] |
|
759 | 761 | else: |
|
760 | 762 | __builtin__.__dict__[biname] = bival |
|
761 | 763 | self.builtins_added.clear() |
|
762 | 764 | |
|
763 | 765 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
764 | 766 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
765 | 767 | |
|
766 | 768 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
767 | 769 | adding your function to one of these hooks, you can modify IPython's |
|
768 | 770 | behavior to call at runtime your own routines.""" |
|
769 | 771 | |
|
770 | 772 | # At some point in the future, this should validate the hook before it |
|
771 | 773 | # accepts it. Probably at least check that the hook takes the number |
|
772 | 774 | # of args it's supposed to. |
|
773 | 775 | |
|
774 | 776 | f = new.instancemethod(hook,self,self.__class__) |
|
775 | 777 | |
|
776 | 778 | # check if the hook is for strdispatcher first |
|
777 | 779 | if str_key is not None: |
|
778 | 780 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
779 | 781 | sdp.add_s(str_key, f, priority ) |
|
780 | 782 | self.strdispatchers[name] = sdp |
|
781 | 783 | return |
|
782 | 784 | if re_key is not None: |
|
783 | 785 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
784 | 786 | sdp.add_re(re.compile(re_key), f, priority ) |
|
785 | 787 | self.strdispatchers[name] = sdp |
|
786 | 788 | return |
|
787 | 789 | |
|
788 | 790 | dp = getattr(self.hooks, name, None) |
|
789 | 791 | if name not in IPython.hooks.__all__: |
|
790 | 792 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ ) |
|
791 | 793 | if not dp: |
|
792 | 794 | dp = IPython.hooks.CommandChainDispatcher() |
|
793 | 795 | |
|
794 | 796 | try: |
|
795 | 797 | dp.add(f,priority) |
|
796 | 798 | except AttributeError: |
|
797 | 799 | # it was not commandchain, plain old func - replace |
|
798 | 800 | dp = f |
|
799 | 801 | |
|
800 | 802 | setattr(self.hooks,name, dp) |
|
801 | 803 | |
|
802 | 804 | |
|
803 | 805 | #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
804 | 806 | |
|
805 | 807 | def set_crash_handler(self,crashHandler): |
|
806 | 808 | """Set the IPython crash handler. |
|
807 | 809 | |
|
808 | 810 | This must be a callable with a signature suitable for use as |
|
809 | 811 | sys.excepthook.""" |
|
810 | 812 | |
|
811 | 813 | # Install the given crash handler as the Python exception hook |
|
812 | 814 | sys.excepthook = crashHandler |
|
813 | 815 | |
|
814 | 816 | # The instance will store a pointer to this, so that runtime code |
|
815 | 817 | # (such as magics) can access it. This is because during the |
|
816 | 818 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
817 | 819 | # frameworks). |
|
818 | 820 | self.sys_excepthook = sys.excepthook |
|
819 | 821 | |
|
820 | 822 | |
|
821 | 823 | def set_custom_exc(self,exc_tuple,handler): |
|
822 | 824 | """set_custom_exc(exc_tuple,handler) |
|
823 | 825 | |
|
824 | 826 | Set a custom exception handler, which will be called if any of the |
|
825 | 827 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
826 | 828 | runcode() method. |
|
827 | 829 | |
|
828 | 830 | Inputs: |
|
829 | 831 | |
|
830 | 832 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
831 | 833 | handler for. It is very important that you use a tuple, and NOT A |
|
832 | 834 | LIST here, because of the way Python's except statement works. If |
|
833 | 835 | you only want to trap a single exception, use a singleton tuple: |
|
834 | 836 | |
|
835 | 837 | exc_tuple == (MyCustomException,) |
|
836 | 838 | |
|
837 | 839 | - handler: this must be defined as a function with the following |
|
838 | 840 | basic interface: def my_handler(self,etype,value,tb). |
|
839 | 841 | |
|
840 | 842 | This will be made into an instance method (via new.instancemethod) |
|
841 | 843 | of IPython itself, and it will be called if any of the exceptions |
|
842 | 844 | listed in the exc_tuple are caught. If the handler is None, an |
|
843 | 845 | internal basic one is used, which just prints basic info. |
|
844 | 846 | |
|
845 | 847 | WARNING: by putting in your own exception handler into IPython's main |
|
846 | 848 | execution loop, you run a very good chance of nasty crashes. This |
|
847 | 849 | facility should only be used if you really know what you are doing.""" |
|
848 | 850 | |
|
849 | 851 | assert type(exc_tuple)==type(()) , \ |
|
850 | 852 | "The custom exceptions must be given AS A TUPLE." |
|
851 | 853 | |
|
852 | 854 | def dummy_handler(self,etype,value,tb): |
|
853 | 855 | print '*** Simple custom exception handler ***' |
|
854 | 856 | print 'Exception type :',etype |
|
855 | 857 | print 'Exception value:',value |
|
856 | 858 | print 'Traceback :',tb |
|
857 | 859 | print 'Source code :','\n'.join(self.buffer) |
|
858 | 860 | |
|
859 | 861 | if handler is None: handler = dummy_handler |
|
860 | 862 | |
|
861 | 863 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
862 | 864 | self.custom_exceptions = exc_tuple |
|
863 | 865 | |
|
864 | 866 | def set_custom_completer(self,completer,pos=0): |
|
865 | 867 | """set_custom_completer(completer,pos=0) |
|
866 | 868 | |
|
867 | 869 | Adds a new custom completer function. |
|
868 | 870 | |
|
869 | 871 | The position argument (defaults to 0) is the index in the completers |
|
870 | 872 | list where you want the completer to be inserted.""" |
|
871 | 873 | |
|
872 | 874 | newcomp = new.instancemethod(completer,self.Completer, |
|
873 | 875 | self.Completer.__class__) |
|
874 | 876 | self.Completer.matchers.insert(pos,newcomp) |
|
875 | 877 | |
|
876 | 878 | def set_completer(self): |
|
877 | 879 | """reset readline's completer to be our own.""" |
|
878 | 880 | self.readline.set_completer(self.Completer.complete) |
|
879 | 881 | |
|
880 | 882 | def _get_call_pdb(self): |
|
881 | 883 | return self._call_pdb |
|
882 | 884 | |
|
883 | 885 | def _set_call_pdb(self,val): |
|
884 | 886 | |
|
885 | 887 | if val not in (0,1,False,True): |
|
886 | 888 | raise ValueError,'new call_pdb value must be boolean' |
|
887 | 889 | |
|
888 | 890 | # store value in instance |
|
889 | 891 | self._call_pdb = val |
|
890 | 892 | |
|
891 | 893 | # notify the actual exception handlers |
|
892 | 894 | self.InteractiveTB.call_pdb = val |
|
893 | 895 | if self.isthreaded: |
|
894 | 896 | try: |
|
895 | 897 | self.sys_excepthook.call_pdb = val |
|
896 | 898 | except: |
|
897 | 899 | warn('Failed to activate pdb for threaded exception handler') |
|
898 | 900 | |
|
899 | 901 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
900 | 902 | 'Control auto-activation of pdb at exceptions') |
|
901 | 903 | |
|
902 | 904 | |
|
903 | 905 | # These special functions get installed in the builtin namespace, to |
|
904 | 906 | # provide programmatic (pure python) access to magics, aliases and system |
|
905 | 907 | # calls. This is important for logging, user scripting, and more. |
|
906 | 908 | |
|
907 | 909 | # We are basically exposing, via normal python functions, the three |
|
908 | 910 | # mechanisms in which ipython offers special call modes (magics for |
|
909 | 911 | # internal control, aliases for direct system access via pre-selected |
|
910 | 912 | # names, and !cmd for calling arbitrary system commands). |
|
911 | 913 | |
|
912 | 914 | def ipmagic(self,arg_s): |
|
913 | 915 | """Call a magic function by name. |
|
914 | 916 | |
|
915 | 917 | Input: a string containing the name of the magic function to call and any |
|
916 | 918 | additional arguments to be passed to the magic. |
|
917 | 919 | |
|
918 | 920 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
919 | 921 | prompt: |
|
920 | 922 | |
|
921 | 923 | In[1]: %name -opt foo bar |
|
922 | 924 | |
|
923 | 925 | To call a magic without arguments, simply use ipmagic('name'). |
|
924 | 926 | |
|
925 | 927 | This provides a proper Python function to call IPython's magics in any |
|
926 | 928 | valid Python code you can type at the interpreter, including loops and |
|
927 | 929 | compound statements. It is added by IPython to the Python builtin |
|
928 | 930 | namespace upon initialization.""" |
|
929 | 931 | |
|
930 | 932 | args = arg_s.split(' ',1) |
|
931 | 933 | magic_name = args[0] |
|
932 | 934 | magic_name = magic_name.lstrip(self.ESC_MAGIC) |
|
933 | 935 | |
|
934 | 936 | try: |
|
935 | 937 | magic_args = args[1] |
|
936 | 938 | except IndexError: |
|
937 | 939 | magic_args = '' |
|
938 | 940 | fn = getattr(self,'magic_'+magic_name,None) |
|
939 | 941 | if fn is None: |
|
940 | 942 | error("Magic function `%s` not found." % magic_name) |
|
941 | 943 | else: |
|
942 | 944 | magic_args = self.var_expand(magic_args,1) |
|
943 | 945 | return fn(magic_args) |
|
944 | 946 | |
|
945 | 947 | def ipalias(self,arg_s): |
|
946 | 948 | """Call an alias by name. |
|
947 | 949 | |
|
948 | 950 | Input: a string containing the name of the alias to call and any |
|
949 | 951 | additional arguments to be passed to the magic. |
|
950 | 952 | |
|
951 | 953 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
952 | 954 | prompt: |
|
953 | 955 | |
|
954 | 956 | In[1]: name -opt foo bar |
|
955 | 957 | |
|
956 | 958 | To call an alias without arguments, simply use ipalias('name'). |
|
957 | 959 | |
|
958 | 960 | This provides a proper Python function to call IPython's aliases in any |
|
959 | 961 | valid Python code you can type at the interpreter, including loops and |
|
960 | 962 | compound statements. It is added by IPython to the Python builtin |
|
961 | 963 | namespace upon initialization.""" |
|
962 | 964 | |
|
963 | 965 | args = arg_s.split(' ',1) |
|
964 | 966 | alias_name = args[0] |
|
965 | 967 | try: |
|
966 | 968 | alias_args = args[1] |
|
967 | 969 | except IndexError: |
|
968 | 970 | alias_args = '' |
|
969 | 971 | if alias_name in self.alias_table: |
|
970 | 972 | self.call_alias(alias_name,alias_args) |
|
971 | 973 | else: |
|
972 | 974 | error("Alias `%s` not found." % alias_name) |
|
973 | 975 | |
|
974 | 976 | def ipsystem(self,arg_s): |
|
975 | 977 | """Make a system call, using IPython.""" |
|
976 | 978 | |
|
977 | 979 | self.system(arg_s) |
|
978 | 980 | |
|
979 | 981 | def complete(self,text): |
|
980 | 982 | """Return a sorted list of all possible completions on text. |
|
981 | 983 | |
|
982 | 984 | Inputs: |
|
983 | 985 | |
|
984 | 986 | - text: a string of text to be completed on. |
|
985 | 987 | |
|
986 | 988 | This is a wrapper around the completion mechanism, similar to what |
|
987 | 989 | readline does at the command line when the TAB key is hit. By |
|
988 | 990 | exposing it as a method, it can be used by other non-readline |
|
989 | 991 | environments (such as GUIs) for text completion. |
|
990 | 992 | |
|
991 | 993 | Simple usage example: |
|
992 | 994 | |
|
993 | 995 | In [1]: x = 'hello' |
|
994 | 996 | |
|
995 | 997 | In [2]: __IP.complete('x.l') |
|
996 | 998 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" |
|
997 | 999 | |
|
998 | 1000 | complete = self.Completer.complete |
|
999 | 1001 | state = 0 |
|
1000 | 1002 | # use a dict so we get unique keys, since ipyhton's multiple |
|
1001 | 1003 | # completers can return duplicates. When we make 2.4 a requirement, |
|
1002 | 1004 | # start using sets instead, which are faster. |
|
1003 | 1005 | comps = {} |
|
1004 | 1006 | while True: |
|
1005 | 1007 | newcomp = complete(text,state,line_buffer=text) |
|
1006 | 1008 | if newcomp is None: |
|
1007 | 1009 | break |
|
1008 | 1010 | comps[newcomp] = 1 |
|
1009 | 1011 | state += 1 |
|
1010 | 1012 | outcomps = comps.keys() |
|
1011 | 1013 | outcomps.sort() |
|
1012 | 1014 | return outcomps |
|
1013 | 1015 | |
|
1014 | 1016 | def set_completer_frame(self, frame=None): |
|
1015 | 1017 | if frame: |
|
1016 | 1018 | self.Completer.namespace = frame.f_locals |
|
1017 | 1019 | self.Completer.global_namespace = frame.f_globals |
|
1018 | 1020 | else: |
|
1019 | 1021 | self.Completer.namespace = self.user_ns |
|
1020 | 1022 | self.Completer.global_namespace = self.user_global_ns |
|
1021 | 1023 | |
|
1022 | 1024 | def init_auto_alias(self): |
|
1023 | 1025 | """Define some aliases automatically. |
|
1024 | 1026 | |
|
1025 | 1027 | These are ALL parameter-less aliases""" |
|
1026 | 1028 | |
|
1027 | 1029 | for alias,cmd in self.auto_alias: |
|
1028 | 1030 | self.getapi().defalias(alias,cmd) |
|
1029 | 1031 | |
|
1030 | 1032 | |
|
1031 | 1033 | def alias_table_validate(self,verbose=0): |
|
1032 | 1034 | """Update information about the alias table. |
|
1033 | 1035 | |
|
1034 | 1036 | In particular, make sure no Python keywords/builtins are in it.""" |
|
1035 | 1037 | |
|
1036 | 1038 | no_alias = self.no_alias |
|
1037 | 1039 | for k in self.alias_table.keys(): |
|
1038 | 1040 | if k in no_alias: |
|
1039 | 1041 | del self.alias_table[k] |
|
1040 | 1042 | if verbose: |
|
1041 | 1043 | print ("Deleting alias <%s>, it's a Python " |
|
1042 | 1044 | "keyword or builtin." % k) |
|
1043 | 1045 | |
|
1044 | 1046 | def set_autoindent(self,value=None): |
|
1045 | 1047 | """Set the autoindent flag, checking for readline support. |
|
1046 | 1048 | |
|
1047 | 1049 | If called with no arguments, it acts as a toggle.""" |
|
1048 | 1050 | |
|
1049 | 1051 | if not self.has_readline: |
|
1050 | 1052 | if os.name == 'posix': |
|
1051 | 1053 | warn("The auto-indent feature requires the readline library") |
|
1052 | 1054 | self.autoindent = 0 |
|
1053 | 1055 | return |
|
1054 | 1056 | if value is None: |
|
1055 | 1057 | self.autoindent = not self.autoindent |
|
1056 | 1058 | else: |
|
1057 | 1059 | self.autoindent = value |
|
1058 | 1060 | |
|
1059 | 1061 | def rc_set_toggle(self,rc_field,value=None): |
|
1060 | 1062 | """Set or toggle a field in IPython's rc config. structure. |
|
1061 | 1063 | |
|
1062 | 1064 | If called with no arguments, it acts as a toggle. |
|
1063 | 1065 | |
|
1064 | 1066 | If called with a non-existent field, the resulting AttributeError |
|
1065 | 1067 | exception will propagate out.""" |
|
1066 | 1068 | |
|
1067 | 1069 | rc_val = getattr(self.rc,rc_field) |
|
1068 | 1070 | if value is None: |
|
1069 | 1071 | value = not rc_val |
|
1070 | 1072 | setattr(self.rc,rc_field,value) |
|
1071 | 1073 | |
|
1072 | 1074 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
1073 | 1075 | """Install the user configuration directory. |
|
1074 | 1076 | |
|
1075 | 1077 | Can be called when running for the first time or to upgrade the user's |
|
1076 | 1078 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
1077 | 1079 | and 'upgrade'.""" |
|
1078 | 1080 | |
|
1079 | 1081 | def wait(): |
|
1080 | 1082 | try: |
|
1081 | 1083 | raw_input("Please press <RETURN> to start IPython.") |
|
1082 | 1084 | except EOFError: |
|
1083 | 1085 | print >> Term.cout |
|
1084 | 1086 | print '*'*70 |
|
1085 | 1087 | |
|
1086 | 1088 | cwd = os.getcwd() # remember where we started |
|
1087 | 1089 | glb = glob.glob |
|
1088 | 1090 | print '*'*70 |
|
1089 | 1091 | if mode == 'install': |
|
1090 | 1092 | print \ |
|
1091 | 1093 | """Welcome to IPython. I will try to create a personal configuration directory |
|
1092 | 1094 | where you can customize many aspects of IPython's functionality in:\n""" |
|
1093 | 1095 | else: |
|
1094 | 1096 | print 'I am going to upgrade your configuration in:' |
|
1095 | 1097 | |
|
1096 | 1098 | print ipythondir |
|
1097 | 1099 | |
|
1098 | 1100 | rcdirend = os.path.join('IPython','UserConfig') |
|
1099 | 1101 | cfg = lambda d: os.path.join(d,rcdirend) |
|
1100 | 1102 | try: |
|
1101 | 1103 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
1102 | 1104 | except IOError: |
|
1103 | 1105 | warning = """ |
|
1104 | 1106 | Installation error. IPython's directory was not found. |
|
1105 | 1107 | |
|
1106 | 1108 | Check the following: |
|
1107 | 1109 | |
|
1108 | 1110 | The ipython/IPython directory should be in a directory belonging to your |
|
1109 | 1111 | PYTHONPATH environment variable (that is, it should be in a directory |
|
1110 | 1112 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
1111 | 1113 | |
|
1112 | 1114 | IPython will proceed with builtin defaults. |
|
1113 | 1115 | """ |
|
1114 | 1116 | warn(warning) |
|
1115 | 1117 | wait() |
|
1116 | 1118 | return |
|
1117 | 1119 | |
|
1118 | 1120 | if mode == 'install': |
|
1119 | 1121 | try: |
|
1120 | 1122 | shutil.copytree(rcdir,ipythondir) |
|
1121 | 1123 | os.chdir(ipythondir) |
|
1122 | 1124 | rc_files = glb("ipythonrc*") |
|
1123 | 1125 | for rc_file in rc_files: |
|
1124 | 1126 | os.rename(rc_file,rc_file+rc_suffix) |
|
1125 | 1127 | except: |
|
1126 | 1128 | warning = """ |
|
1127 | 1129 | |
|
1128 | 1130 | There was a problem with the installation: |
|
1129 | 1131 | %s |
|
1130 | 1132 | Try to correct it or contact the developers if you think it's a bug. |
|
1131 | 1133 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
1132 | 1134 | warn(warning) |
|
1133 | 1135 | wait() |
|
1134 | 1136 | return |
|
1135 | 1137 | |
|
1136 | 1138 | elif mode == 'upgrade': |
|
1137 | 1139 | try: |
|
1138 | 1140 | os.chdir(ipythondir) |
|
1139 | 1141 | except: |
|
1140 | 1142 | print """ |
|
1141 | 1143 | Can not upgrade: changing to directory %s failed. Details: |
|
1142 | 1144 | %s |
|
1143 | 1145 | """ % (ipythondir,sys.exc_info()[1]) |
|
1144 | 1146 | wait() |
|
1145 | 1147 | return |
|
1146 | 1148 | else: |
|
1147 | 1149 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
1148 | 1150 | for new_full_path in sources: |
|
1149 | 1151 | new_filename = os.path.basename(new_full_path) |
|
1150 | 1152 | if new_filename.startswith('ipythonrc'): |
|
1151 | 1153 | new_filename = new_filename + rc_suffix |
|
1152 | 1154 | # The config directory should only contain files, skip any |
|
1153 | 1155 | # directories which may be there (like CVS) |
|
1154 | 1156 | if os.path.isdir(new_full_path): |
|
1155 | 1157 | continue |
|
1156 | 1158 | if os.path.exists(new_filename): |
|
1157 | 1159 | old_file = new_filename+'.old' |
|
1158 | 1160 | if os.path.exists(old_file): |
|
1159 | 1161 | os.remove(old_file) |
|
1160 | 1162 | os.rename(new_filename,old_file) |
|
1161 | 1163 | shutil.copy(new_full_path,new_filename) |
|
1162 | 1164 | else: |
|
1163 | 1165 | raise ValueError,'unrecognized mode for install:',`mode` |
|
1164 | 1166 | |
|
1165 | 1167 | # Fix line-endings to those native to each platform in the config |
|
1166 | 1168 | # directory. |
|
1167 | 1169 | try: |
|
1168 | 1170 | os.chdir(ipythondir) |
|
1169 | 1171 | except: |
|
1170 | 1172 | print """ |
|
1171 | 1173 | Problem: changing to directory %s failed. |
|
1172 | 1174 | Details: |
|
1173 | 1175 | %s |
|
1174 | 1176 | |
|
1175 | 1177 | Some configuration files may have incorrect line endings. This should not |
|
1176 | 1178 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1177 | 1179 | wait() |
|
1178 | 1180 | else: |
|
1179 | 1181 | for fname in glb('ipythonrc*'): |
|
1180 | 1182 | try: |
|
1181 | 1183 | native_line_ends(fname,backup=0) |
|
1182 | 1184 | except IOError: |
|
1183 | 1185 | pass |
|
1184 | 1186 | |
|
1185 | 1187 | if mode == 'install': |
|
1186 | 1188 | print """ |
|
1187 | 1189 | Successful installation! |
|
1188 | 1190 | |
|
1189 | 1191 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1190 | 1192 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1191 | 1193 | distribution) to make sure that your system environment is properly configured |
|
1192 | 1194 | to take advantage of IPython's features. |
|
1193 | 1195 | |
|
1194 | 1196 | Important note: the configuration system has changed! The old system is |
|
1195 | 1197 | still in place, but its setting may be partly overridden by the settings in |
|
1196 | 1198 | "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file |
|
1197 | 1199 | if some of the new settings bother you. |
|
1198 | 1200 | |
|
1199 | 1201 | """ |
|
1200 | 1202 | else: |
|
1201 | 1203 | print """ |
|
1202 | 1204 | Successful upgrade! |
|
1203 | 1205 | |
|
1204 | 1206 | All files in your directory: |
|
1205 | 1207 | %(ipythondir)s |
|
1206 | 1208 | which would have been overwritten by the upgrade were backed up with a .old |
|
1207 | 1209 | extension. If you had made particular customizations in those files you may |
|
1208 | 1210 | want to merge them back into the new files.""" % locals() |
|
1209 | 1211 | wait() |
|
1210 | 1212 | os.chdir(cwd) |
|
1211 | 1213 | # end user_setup() |
|
1212 | 1214 | |
|
1213 | 1215 | def atexit_operations(self): |
|
1214 | 1216 | """This will be executed at the time of exit. |
|
1215 | 1217 | |
|
1216 | 1218 | Saving of persistent data should be performed here. """ |
|
1217 | 1219 | |
|
1218 | 1220 | #print '*** IPython exit cleanup ***' # dbg |
|
1219 | 1221 | # input history |
|
1220 | 1222 | self.savehist() |
|
1221 | 1223 | |
|
1222 | 1224 | # Cleanup all tempfiles left around |
|
1223 | 1225 | for tfile in self.tempfiles: |
|
1224 | 1226 | try: |
|
1225 | 1227 | os.unlink(tfile) |
|
1226 | 1228 | except OSError: |
|
1227 | 1229 | pass |
|
1228 | 1230 | |
|
1229 | 1231 | self.hooks.shutdown_hook() |
|
1230 | 1232 | |
|
1231 | 1233 | def savehist(self): |
|
1232 | 1234 | """Save input history to a file (via readline library).""" |
|
1233 | 1235 | try: |
|
1234 | 1236 | self.readline.write_history_file(self.histfile) |
|
1235 | 1237 | except: |
|
1236 | 1238 | print 'Unable to save IPython command history to file: ' + \ |
|
1237 | 1239 | `self.histfile` |
|
1238 | 1240 | |
|
1239 | 1241 | def reloadhist(self): |
|
1240 | 1242 | """Reload the input history from disk file.""" |
|
1241 | 1243 | |
|
1242 | 1244 | if self.has_readline: |
|
1243 | 1245 | self.readline.clear_history() |
|
1244 | 1246 | self.readline.read_history_file(self.shell.histfile) |
|
1245 | 1247 | |
|
1246 | 1248 | def history_saving_wrapper(self, func): |
|
1247 | 1249 | """ Wrap func for readline history saving |
|
1248 | 1250 | |
|
1249 | 1251 | Convert func into callable that saves & restores |
|
1250 | 1252 | history around the call """ |
|
1251 | 1253 | |
|
1252 | 1254 | if not self.has_readline: |
|
1253 | 1255 | return func |
|
1254 | 1256 | |
|
1255 | 1257 | def wrapper(): |
|
1256 | 1258 | self.savehist() |
|
1257 | 1259 | try: |
|
1258 | 1260 | func() |
|
1259 | 1261 | finally: |
|
1260 | 1262 | readline.read_history_file(self.histfile) |
|
1261 | 1263 | return wrapper |
|
1262 | 1264 | |
|
1263 | 1265 | |
|
1264 | 1266 | def pre_readline(self): |
|
1265 | 1267 | """readline hook to be used at the start of each line. |
|
1266 | 1268 | |
|
1267 | 1269 | Currently it handles auto-indent only.""" |
|
1268 | 1270 | |
|
1269 | 1271 | #debugx('self.indent_current_nsp','pre_readline:') |
|
1270 | 1272 | |
|
1271 | 1273 | if self.rl_do_indent: |
|
1272 | 1274 | self.readline.insert_text(self.indent_current_str()) |
|
1273 | 1275 | if self.rl_next_input is not None: |
|
1274 | 1276 | self.readline.insert_text(self.rl_next_input) |
|
1275 | 1277 | self.rl_next_input = None |
|
1276 | 1278 | |
|
1277 | 1279 | def init_readline(self): |
|
1278 | 1280 | """Command history completion/saving/reloading.""" |
|
1279 | 1281 | |
|
1280 | 1282 | |
|
1281 | 1283 | import IPython.rlineimpl as readline |
|
1282 | 1284 | |
|
1283 | 1285 | if not readline.have_readline: |
|
1284 | 1286 | self.has_readline = 0 |
|
1285 | 1287 | self.readline = None |
|
1286 | 1288 | # no point in bugging windows users with this every time: |
|
1287 | 1289 | warn('Readline services not available on this platform.') |
|
1288 | 1290 | else: |
|
1289 | 1291 | sys.modules['readline'] = readline |
|
1290 | 1292 | import atexit |
|
1291 | 1293 | from IPython.completer import IPCompleter |
|
1292 | 1294 | self.Completer = IPCompleter(self, |
|
1293 | 1295 | self.user_ns, |
|
1294 | 1296 | self.user_global_ns, |
|
1295 | 1297 | self.rc.readline_omit__names, |
|
1296 | 1298 | self.alias_table) |
|
1297 | 1299 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1298 | 1300 | self.strdispatchers['complete_command'] = sdisp |
|
1299 | 1301 | self.Completer.custom_completers = sdisp |
|
1300 | 1302 | # Platform-specific configuration |
|
1301 | 1303 | if os.name == 'nt': |
|
1302 | 1304 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1303 | 1305 | else: |
|
1304 | 1306 | self.readline_startup_hook = readline.set_startup_hook |
|
1305 | 1307 | |
|
1306 | 1308 | # Load user's initrc file (readline config) |
|
1307 | 1309 | inputrc_name = os.environ.get('INPUTRC') |
|
1308 | 1310 | if inputrc_name is None: |
|
1309 | 1311 | home_dir = get_home_dir() |
|
1310 | 1312 | if home_dir is not None: |
|
1311 | 1313 | inputrc_name = os.path.join(home_dir,'.inputrc') |
|
1312 | 1314 | if os.path.isfile(inputrc_name): |
|
1313 | 1315 | try: |
|
1314 | 1316 | readline.read_init_file(inputrc_name) |
|
1315 | 1317 | except: |
|
1316 | 1318 | warn('Problems reading readline initialization file <%s>' |
|
1317 | 1319 | % inputrc_name) |
|
1318 | 1320 | |
|
1319 | 1321 | self.has_readline = 1 |
|
1320 | 1322 | self.readline = readline |
|
1321 | 1323 | # save this in sys so embedded copies can restore it properly |
|
1322 | 1324 | sys.ipcompleter = self.Completer.complete |
|
1323 | 1325 | self.set_completer() |
|
1324 | 1326 | |
|
1325 | 1327 | # Configure readline according to user's prefs |
|
1326 | 1328 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1327 | 1329 | readline.parse_and_bind(rlcommand) |
|
1328 | 1330 | |
|
1329 | 1331 | # remove some chars from the delimiters list |
|
1330 | 1332 | delims = readline.get_completer_delims() |
|
1331 | 1333 | delims = delims.translate(string._idmap, |
|
1332 | 1334 | self.rc.readline_remove_delims) |
|
1333 | 1335 | readline.set_completer_delims(delims) |
|
1334 | 1336 | # otherwise we end up with a monster history after a while: |
|
1335 | 1337 | readline.set_history_length(1000) |
|
1336 | 1338 | try: |
|
1337 | 1339 | #print '*** Reading readline history' # dbg |
|
1338 | 1340 | readline.read_history_file(self.histfile) |
|
1339 | 1341 | except IOError: |
|
1340 | 1342 | pass # It doesn't exist yet. |
|
1341 | 1343 | |
|
1342 | 1344 | atexit.register(self.atexit_operations) |
|
1343 | 1345 | del atexit |
|
1344 | 1346 | |
|
1345 | 1347 | # Configure auto-indent for all platforms |
|
1346 | 1348 | self.set_autoindent(self.rc.autoindent) |
|
1347 | 1349 | |
|
1348 | 1350 | def ask_yes_no(self,prompt,default=True): |
|
1349 | 1351 | if self.rc.quiet: |
|
1350 | 1352 | return True |
|
1351 | 1353 | return ask_yes_no(prompt,default) |
|
1352 | 1354 | |
|
1353 | 1355 | def _should_recompile(self,e): |
|
1354 | 1356 | """Utility routine for edit_syntax_error""" |
|
1355 | 1357 | |
|
1356 | 1358 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1357 | 1359 | '<console>','<BackgroundJob compilation>', |
|
1358 | 1360 | None): |
|
1359 | 1361 | |
|
1360 | 1362 | return False |
|
1361 | 1363 | try: |
|
1362 | 1364 | if (self.rc.autoedit_syntax and |
|
1363 | 1365 | not self.ask_yes_no('Return to editor to correct syntax error? ' |
|
1364 | 1366 | '[Y/n] ','y')): |
|
1365 | 1367 | return False |
|
1366 | 1368 | except EOFError: |
|
1367 | 1369 | return False |
|
1368 | 1370 | |
|
1369 | 1371 | def int0(x): |
|
1370 | 1372 | try: |
|
1371 | 1373 | return int(x) |
|
1372 | 1374 | except TypeError: |
|
1373 | 1375 | return 0 |
|
1374 | 1376 | # always pass integer line and offset values to editor hook |
|
1375 | 1377 | self.hooks.fix_error_editor(e.filename, |
|
1376 | 1378 | int0(e.lineno),int0(e.offset),e.msg) |
|
1377 | 1379 | return True |
|
1378 | 1380 | |
|
1379 | 1381 | def edit_syntax_error(self): |
|
1380 | 1382 | """The bottom half of the syntax error handler called in the main loop. |
|
1381 | 1383 | |
|
1382 | 1384 | Loop until syntax error is fixed or user cancels. |
|
1383 | 1385 | """ |
|
1384 | 1386 | |
|
1385 | 1387 | while self.SyntaxTB.last_syntax_error: |
|
1386 | 1388 | # copy and clear last_syntax_error |
|
1387 | 1389 | err = self.SyntaxTB.clear_err_state() |
|
1388 | 1390 | if not self._should_recompile(err): |
|
1389 | 1391 | return |
|
1390 | 1392 | try: |
|
1391 | 1393 | # may set last_syntax_error again if a SyntaxError is raised |
|
1392 | 1394 | self.safe_execfile(err.filename,self.user_ns) |
|
1393 | 1395 | except: |
|
1394 | 1396 | self.showtraceback() |
|
1395 | 1397 | else: |
|
1396 | 1398 | try: |
|
1397 | 1399 | f = file(err.filename) |
|
1398 | 1400 | try: |
|
1399 | 1401 | sys.displayhook(f.read()) |
|
1400 | 1402 | finally: |
|
1401 | 1403 | f.close() |
|
1402 | 1404 | except: |
|
1403 | 1405 | self.showtraceback() |
|
1404 | 1406 | |
|
1405 | 1407 | def showsyntaxerror(self, filename=None): |
|
1406 | 1408 | """Display the syntax error that just occurred. |
|
1407 | 1409 | |
|
1408 | 1410 | This doesn't display a stack trace because there isn't one. |
|
1409 | 1411 | |
|
1410 | 1412 | If a filename is given, it is stuffed in the exception instead |
|
1411 | 1413 | of what was there before (because Python's parser always uses |
|
1412 | 1414 | "<string>" when reading from a string). |
|
1413 | 1415 | """ |
|
1414 | 1416 | etype, value, last_traceback = sys.exc_info() |
|
1415 | 1417 | |
|
1416 | 1418 | # See note about these variables in showtraceback() below |
|
1417 | 1419 | sys.last_type = etype |
|
1418 | 1420 | sys.last_value = value |
|
1419 | 1421 | sys.last_traceback = last_traceback |
|
1420 | 1422 | |
|
1421 | 1423 | if filename and etype is SyntaxError: |
|
1422 | 1424 | # Work hard to stuff the correct filename in the exception |
|
1423 | 1425 | try: |
|
1424 | 1426 | msg, (dummy_filename, lineno, offset, line) = value |
|
1425 | 1427 | except: |
|
1426 | 1428 | # Not the format we expect; leave it alone |
|
1427 | 1429 | pass |
|
1428 | 1430 | else: |
|
1429 | 1431 | # Stuff in the right filename |
|
1430 | 1432 | try: |
|
1431 | 1433 | # Assume SyntaxError is a class exception |
|
1432 | 1434 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1433 | 1435 | except: |
|
1434 | 1436 | # If that failed, assume SyntaxError is a string |
|
1435 | 1437 | value = msg, (filename, lineno, offset, line) |
|
1436 | 1438 | self.SyntaxTB(etype,value,[]) |
|
1437 | 1439 | |
|
1438 | 1440 | def debugger(self,force=False): |
|
1439 | 1441 | """Call the pydb/pdb debugger. |
|
1440 | 1442 | |
|
1441 | 1443 | Keywords: |
|
1442 | 1444 | |
|
1443 | 1445 | - force(False): by default, this routine checks the instance call_pdb |
|
1444 | 1446 | flag and does not actually invoke the debugger if the flag is false. |
|
1445 | 1447 | The 'force' option forces the debugger to activate even if the flag |
|
1446 | 1448 | is false. |
|
1447 | 1449 | """ |
|
1448 | 1450 | |
|
1449 | 1451 | if not (force or self.call_pdb): |
|
1450 | 1452 | return |
|
1451 | 1453 | |
|
1452 | 1454 | if not hasattr(sys,'last_traceback'): |
|
1453 | 1455 | error('No traceback has been produced, nothing to debug.') |
|
1454 | 1456 | return |
|
1455 | 1457 | |
|
1456 | 1458 | # use pydb if available |
|
1457 | 1459 | if Debugger.has_pydb: |
|
1458 | 1460 | from pydb import pm |
|
1459 | 1461 | else: |
|
1460 | 1462 | # fallback to our internal debugger |
|
1461 | 1463 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
1462 | 1464 | self.history_saving_wrapper(pm)() |
|
1463 | 1465 | |
|
1464 | 1466 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): |
|
1465 | 1467 | """Display the exception that just occurred. |
|
1466 | 1468 | |
|
1467 | 1469 | If nothing is known about the exception, this is the method which |
|
1468 | 1470 | should be used throughout the code for presenting user tracebacks, |
|
1469 | 1471 | rather than directly invoking the InteractiveTB object. |
|
1470 | 1472 | |
|
1471 | 1473 | A specific showsyntaxerror() also exists, but this method can take |
|
1472 | 1474 | care of calling it if needed, so unless you are explicitly catching a |
|
1473 | 1475 | SyntaxError exception, don't try to analyze the stack manually and |
|
1474 | 1476 | simply call this method.""" |
|
1475 | 1477 | |
|
1476 | 1478 | |
|
1477 | 1479 | # Though this won't be called by syntax errors in the input line, |
|
1478 | 1480 | # there may be SyntaxError cases whith imported code. |
|
1479 | 1481 | |
|
1480 | 1482 | |
|
1481 | 1483 | if exc_tuple is None: |
|
1482 | 1484 | etype, value, tb = sys.exc_info() |
|
1483 | 1485 | else: |
|
1484 | 1486 | etype, value, tb = exc_tuple |
|
1485 | 1487 | |
|
1486 | 1488 | if etype is SyntaxError: |
|
1487 | 1489 | self.showsyntaxerror(filename) |
|
1488 | 1490 | else: |
|
1489 | 1491 | # WARNING: these variables are somewhat deprecated and not |
|
1490 | 1492 | # necessarily safe to use in a threaded environment, but tools |
|
1491 | 1493 | # like pdb depend on their existence, so let's set them. If we |
|
1492 | 1494 | # find problems in the field, we'll need to revisit their use. |
|
1493 | 1495 | sys.last_type = etype |
|
1494 | 1496 | sys.last_value = value |
|
1495 | 1497 | sys.last_traceback = tb |
|
1496 | 1498 | |
|
1497 | 1499 | if etype in self.custom_exceptions: |
|
1498 | 1500 | self.CustomTB(etype,value,tb) |
|
1499 | 1501 | else: |
|
1500 | 1502 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1501 | 1503 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1502 | 1504 | # pdb mucks up readline, fix it back |
|
1503 | 1505 | self.set_completer() |
|
1504 | 1506 | |
|
1505 | 1507 | |
|
1506 | 1508 | def mainloop(self,banner=None): |
|
1507 | 1509 | """Creates the local namespace and starts the mainloop. |
|
1508 | 1510 | |
|
1509 | 1511 | If an optional banner argument is given, it will override the |
|
1510 | 1512 | internally created default banner.""" |
|
1511 | 1513 | |
|
1512 | 1514 | if self.rc.c: # Emulate Python's -c option |
|
1513 | 1515 | self.exec_init_cmd() |
|
1514 | 1516 | if banner is None: |
|
1515 | 1517 | if not self.rc.banner: |
|
1516 | 1518 | banner = '' |
|
1517 | 1519 | # banner is string? Use it directly! |
|
1518 | 1520 | elif isinstance(self.rc.banner,basestring): |
|
1519 | 1521 | banner = self.rc.banner |
|
1520 | 1522 | else: |
|
1521 | 1523 | banner = self.BANNER+self.banner2 |
|
1522 | 1524 | |
|
1523 | 1525 | self.interact(banner) |
|
1524 | 1526 | |
|
1525 | 1527 | def exec_init_cmd(self): |
|
1526 | 1528 | """Execute a command given at the command line. |
|
1527 | 1529 | |
|
1528 | 1530 | This emulates Python's -c option.""" |
|
1529 | 1531 | |
|
1530 | 1532 | #sys.argv = ['-c'] |
|
1531 | 1533 | self.push(self.prefilter(self.rc.c, False)) |
|
1532 | 1534 | if not self.rc.interact: |
|
1533 | 1535 | self.exit_now = True |
|
1534 | 1536 | |
|
1535 | 1537 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1536 | 1538 | """Embeds IPython into a running python program. |
|
1537 | 1539 | |
|
1538 | 1540 | Input: |
|
1539 | 1541 | |
|
1540 | 1542 | - header: An optional header message can be specified. |
|
1541 | 1543 | |
|
1542 | 1544 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1543 | 1545 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1544 | 1546 | program variables become visible but user-specific configuration |
|
1545 | 1547 | remains possible. |
|
1546 | 1548 | |
|
1547 | 1549 | - stack_depth: specifies how many levels in the stack to go to |
|
1548 | 1550 | looking for namespaces (when local_ns and global_ns are None). This |
|
1549 | 1551 | allows an intermediate caller to make sure that this function gets |
|
1550 | 1552 | the namespace from the intended level in the stack. By default (0) |
|
1551 | 1553 | it will get its locals and globals from the immediate caller. |
|
1552 | 1554 | |
|
1553 | 1555 | Warning: it's possible to use this in a program which is being run by |
|
1554 | 1556 | IPython itself (via %run), but some funny things will happen (a few |
|
1555 | 1557 | globals get overwritten). In the future this will be cleaned up, as |
|
1556 | 1558 | there is no fundamental reason why it can't work perfectly.""" |
|
1557 | 1559 | |
|
1558 | 1560 | # Get locals and globals from caller |
|
1559 | 1561 | if local_ns is None or global_ns is None: |
|
1560 | 1562 | call_frame = sys._getframe(stack_depth).f_back |
|
1561 | 1563 | |
|
1562 | 1564 | if local_ns is None: |
|
1563 | 1565 | local_ns = call_frame.f_locals |
|
1564 | 1566 | if global_ns is None: |
|
1565 | 1567 | global_ns = call_frame.f_globals |
|
1566 | 1568 | |
|
1567 | 1569 | # Update namespaces and fire up interpreter |
|
1568 | 1570 | |
|
1569 | 1571 | # The global one is easy, we can just throw it in |
|
1570 | 1572 | self.user_global_ns = global_ns |
|
1571 | 1573 | |
|
1572 | 1574 | # but the user/local one is tricky: ipython needs it to store internal |
|
1573 | 1575 | # data, but we also need the locals. We'll copy locals in the user |
|
1574 | 1576 | # one, but will track what got copied so we can delete them at exit. |
|
1575 | 1577 | # This is so that a later embedded call doesn't see locals from a |
|
1576 | 1578 | # previous call (which most likely existed in a separate scope). |
|
1577 | 1579 | local_varnames = local_ns.keys() |
|
1578 | 1580 | self.user_ns.update(local_ns) |
|
1579 | 1581 | |
|
1580 | 1582 | # Patch for global embedding to make sure that things don't overwrite |
|
1581 | 1583 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1582 | 1584 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1583 | 1585 | if local_ns is None and global_ns is None: |
|
1584 | 1586 | self.user_global_ns.update(__main__.__dict__) |
|
1585 | 1587 | |
|
1586 | 1588 | # make sure the tab-completer has the correct frame information, so it |
|
1587 | 1589 | # actually completes using the frame's locals/globals |
|
1588 | 1590 | self.set_completer_frame() |
|
1589 | 1591 | |
|
1590 | 1592 | # before activating the interactive mode, we need to make sure that |
|
1591 | 1593 | # all names in the builtin namespace needed by ipython point to |
|
1592 | 1594 | # ourselves, and not to other instances. |
|
1593 | 1595 | self.add_builtins() |
|
1594 | 1596 | |
|
1595 | 1597 | self.interact(header) |
|
1596 | 1598 | |
|
1597 | 1599 | # now, purge out the user namespace from anything we might have added |
|
1598 | 1600 | # from the caller's local namespace |
|
1599 | 1601 | delvar = self.user_ns.pop |
|
1600 | 1602 | for var in local_varnames: |
|
1601 | 1603 | delvar(var,None) |
|
1602 | 1604 | # and clean builtins we may have overridden |
|
1603 | 1605 | self.clean_builtins() |
|
1604 | 1606 | |
|
1605 | 1607 | def interact(self, banner=None): |
|
1606 | 1608 | """Closely emulate the interactive Python console. |
|
1607 | 1609 | |
|
1608 | 1610 | The optional banner argument specify the banner to print |
|
1609 | 1611 | before the first interaction; by default it prints a banner |
|
1610 | 1612 | similar to the one printed by the real Python interpreter, |
|
1611 | 1613 | followed by the current class name in parentheses (so as not |
|
1612 | 1614 | to confuse this with the real interpreter -- since it's so |
|
1613 | 1615 | close!). |
|
1614 | 1616 | |
|
1615 | 1617 | """ |
|
1616 | 1618 | |
|
1617 | 1619 | if self.exit_now: |
|
1618 | 1620 | # batch run -> do not interact |
|
1619 | 1621 | return |
|
1620 | 1622 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1621 | 1623 | if banner is None: |
|
1622 | 1624 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1623 | 1625 | (sys.version, sys.platform, cprt, |
|
1624 | 1626 | self.__class__.__name__)) |
|
1625 | 1627 | else: |
|
1626 | 1628 | self.write(banner) |
|
1627 | 1629 | |
|
1628 | 1630 | more = 0 |
|
1629 | 1631 | |
|
1630 | 1632 | # Mark activity in the builtins |
|
1631 | 1633 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1632 | 1634 | |
|
1633 | 1635 | if self.has_readline: |
|
1634 | 1636 | self.readline_startup_hook(self.pre_readline) |
|
1635 | 1637 | # exit_now is set by a call to %Exit or %Quit |
|
1636 | 1638 | |
|
1637 | 1639 | while not self.exit_now: |
|
1638 | 1640 | if more: |
|
1639 | 1641 | prompt = self.hooks.generate_prompt(True) |
|
1640 | 1642 | if self.autoindent: |
|
1641 | 1643 | self.rl_do_indent = True |
|
1642 | 1644 | |
|
1643 | 1645 | else: |
|
1644 | 1646 | prompt = self.hooks.generate_prompt(False) |
|
1645 | 1647 | try: |
|
1646 | 1648 | line = self.raw_input(prompt,more) |
|
1647 | 1649 | if self.exit_now: |
|
1648 | 1650 | # quick exit on sys.std[in|out] close |
|
1649 | 1651 | break |
|
1650 | 1652 | if self.autoindent: |
|
1651 | 1653 | self.rl_do_indent = False |
|
1652 | 1654 | |
|
1653 | 1655 | except KeyboardInterrupt: |
|
1654 | 1656 | self.write('\nKeyboardInterrupt\n') |
|
1655 | 1657 | self.resetbuffer() |
|
1656 | 1658 | # keep cache in sync with the prompt counter: |
|
1657 | 1659 | self.outputcache.prompt_count -= 1 |
|
1658 | 1660 | |
|
1659 | 1661 | if self.autoindent: |
|
1660 | 1662 | self.indent_current_nsp = 0 |
|
1661 | 1663 | more = 0 |
|
1662 | 1664 | except EOFError: |
|
1663 | 1665 | if self.autoindent: |
|
1664 | 1666 | self.rl_do_indent = False |
|
1665 | 1667 | self.readline_startup_hook(None) |
|
1666 | 1668 | self.write('\n') |
|
1667 | 1669 | self.exit() |
|
1668 | 1670 | except bdb.BdbQuit: |
|
1669 | 1671 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
1670 | 1672 | 'Because of how pdb handles the stack, it is impossible\n' |
|
1671 | 1673 | 'for IPython to properly format this particular exception.\n' |
|
1672 | 1674 | 'IPython will resume normal operation.') |
|
1673 | 1675 | except: |
|
1674 | 1676 | # exceptions here are VERY RARE, but they can be triggered |
|
1675 | 1677 | # asynchronously by signal handlers, for example. |
|
1676 | 1678 | self.showtraceback() |
|
1677 | 1679 | else: |
|
1678 | 1680 | more = self.push(line) |
|
1679 | 1681 | if (self.SyntaxTB.last_syntax_error and |
|
1680 | 1682 | self.rc.autoedit_syntax): |
|
1681 | 1683 | self.edit_syntax_error() |
|
1682 | 1684 | |
|
1683 | 1685 | # We are off again... |
|
1684 | 1686 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1685 | 1687 | |
|
1686 | 1688 | def excepthook(self, etype, value, tb): |
|
1687 | 1689 | """One more defense for GUI apps that call sys.excepthook. |
|
1688 | 1690 | |
|
1689 | 1691 | GUI frameworks like wxPython trap exceptions and call |
|
1690 | 1692 | sys.excepthook themselves. I guess this is a feature that |
|
1691 | 1693 | enables them to keep running after exceptions that would |
|
1692 | 1694 | otherwise kill their mainloop. This is a bother for IPython |
|
1693 | 1695 | which excepts to catch all of the program exceptions with a try: |
|
1694 | 1696 | except: statement. |
|
1695 | 1697 | |
|
1696 | 1698 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1697 | 1699 | any app directly invokes sys.excepthook, it will look to the user like |
|
1698 | 1700 | IPython crashed. In order to work around this, we can disable the |
|
1699 | 1701 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1700 | 1702 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1701 | 1703 | call sys.excepthook will generate a regular-looking exception from |
|
1702 | 1704 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1703 | 1705 | crashes. |
|
1704 | 1706 | |
|
1705 | 1707 | This hook should be used sparingly, only in places which are not likely |
|
1706 | 1708 | to be true IPython errors. |
|
1707 | 1709 | """ |
|
1708 | 1710 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1709 | 1711 | |
|
1710 | 1712 | def expand_aliases(self,fn,rest): |
|
1711 | 1713 | """ Expand multiple levels of aliases: |
|
1712 | 1714 | |
|
1713 | 1715 | if: |
|
1714 | 1716 | |
|
1715 | 1717 | alias foo bar /tmp |
|
1716 | 1718 | alias baz foo |
|
1717 | 1719 | |
|
1718 | 1720 | then: |
|
1719 | 1721 | |
|
1720 | 1722 | baz huhhahhei -> bar /tmp huhhahhei |
|
1721 | 1723 | |
|
1722 | 1724 | """ |
|
1723 | 1725 | line = fn + " " + rest |
|
1724 | 1726 | |
|
1725 | 1727 | done = Set() |
|
1726 | 1728 | while 1: |
|
1727 | 1729 | pre,fn,rest = prefilter.splitUserInput(line, |
|
1728 | 1730 | prefilter.shell_line_split) |
|
1729 | 1731 | if fn in self.alias_table: |
|
1730 | 1732 | if fn in done: |
|
1731 | 1733 | warn("Cyclic alias definition, repeated '%s'" % fn) |
|
1732 | 1734 | return "" |
|
1733 | 1735 | done.add(fn) |
|
1734 | 1736 | |
|
1735 | 1737 | l2 = self.transform_alias(fn,rest) |
|
1736 | 1738 | # dir -> dir |
|
1737 | 1739 | # print "alias",line, "->",l2 #dbg |
|
1738 | 1740 | if l2 == line: |
|
1739 | 1741 | break |
|
1740 | 1742 | # ls -> ls -F should not recurse forever |
|
1741 | 1743 | if l2.split(None,1)[0] == line.split(None,1)[0]: |
|
1742 | 1744 | line = l2 |
|
1743 | 1745 | break |
|
1744 | 1746 | |
|
1745 | 1747 | line=l2 |
|
1746 | 1748 | |
|
1747 | 1749 | |
|
1748 | 1750 | # print "al expand to",line #dbg |
|
1749 | 1751 | else: |
|
1750 | 1752 | break |
|
1751 | 1753 | |
|
1752 | 1754 | return line |
|
1753 | 1755 | |
|
1754 | 1756 | def transform_alias(self, alias,rest=''): |
|
1755 | 1757 | """ Transform alias to system command string. |
|
1756 | 1758 | """ |
|
1757 | 1759 | trg = self.alias_table[alias] |
|
1758 | 1760 | |
|
1759 | 1761 | nargs,cmd = trg |
|
1760 | 1762 | # print trg #dbg |
|
1761 | 1763 | if ' ' in cmd and os.path.isfile(cmd): |
|
1762 | 1764 | cmd = '"%s"' % cmd |
|
1763 | 1765 | |
|
1764 | 1766 | # Expand the %l special to be the user's input line |
|
1765 | 1767 | if cmd.find('%l') >= 0: |
|
1766 | 1768 | cmd = cmd.replace('%l',rest) |
|
1767 | 1769 | rest = '' |
|
1768 | 1770 | if nargs==0: |
|
1769 | 1771 | # Simple, argument-less aliases |
|
1770 | 1772 | cmd = '%s %s' % (cmd,rest) |
|
1771 | 1773 | else: |
|
1772 | 1774 | # Handle aliases with positional arguments |
|
1773 | 1775 | args = rest.split(None,nargs) |
|
1774 | 1776 | if len(args)< nargs: |
|
1775 | 1777 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1776 | 1778 | (alias,nargs,len(args))) |
|
1777 | 1779 | return None |
|
1778 | 1780 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1779 | 1781 | # Now call the macro, evaluating in the user's namespace |
|
1780 | 1782 | #print 'new command: <%r>' % cmd # dbg |
|
1781 | 1783 | return cmd |
|
1782 | 1784 | |
|
1783 | 1785 | def call_alias(self,alias,rest=''): |
|
1784 | 1786 | """Call an alias given its name and the rest of the line. |
|
1785 | 1787 | |
|
1786 | 1788 | This is only used to provide backwards compatibility for users of |
|
1787 | 1789 | ipalias(), use of which is not recommended for anymore.""" |
|
1788 | 1790 | |
|
1789 | 1791 | # Now call the macro, evaluating in the user's namespace |
|
1790 | 1792 | cmd = self.transform_alias(alias, rest) |
|
1791 | 1793 | try: |
|
1792 | 1794 | self.system(cmd) |
|
1793 | 1795 | except: |
|
1794 | 1796 | self.showtraceback() |
|
1795 | 1797 | |
|
1796 | 1798 | def indent_current_str(self): |
|
1797 | 1799 | """return the current level of indentation as a string""" |
|
1798 | 1800 | return self.indent_current_nsp * ' ' |
|
1799 | 1801 | |
|
1800 | 1802 | def autoindent_update(self,line): |
|
1801 | 1803 | """Keep track of the indent level.""" |
|
1802 | 1804 | |
|
1803 | 1805 | #debugx('line') |
|
1804 | 1806 | #debugx('self.indent_current_nsp') |
|
1805 | 1807 | if self.autoindent: |
|
1806 | 1808 | if line: |
|
1807 | 1809 | inisp = num_ini_spaces(line) |
|
1808 | 1810 | if inisp < self.indent_current_nsp: |
|
1809 | 1811 | self.indent_current_nsp = inisp |
|
1810 | 1812 | |
|
1811 | 1813 | if line[-1] == ':': |
|
1812 | 1814 | self.indent_current_nsp += 4 |
|
1813 | 1815 | elif dedent_re.match(line): |
|
1814 | 1816 | self.indent_current_nsp -= 4 |
|
1815 | 1817 | else: |
|
1816 | 1818 | self.indent_current_nsp = 0 |
|
1817 | 1819 | def runlines(self,lines): |
|
1818 | 1820 | """Run a string of one or more lines of source. |
|
1819 | 1821 | |
|
1820 | 1822 | This method is capable of running a string containing multiple source |
|
1821 | 1823 | lines, as if they had been entered at the IPython prompt. Since it |
|
1822 | 1824 | exposes IPython's processing machinery, the given strings can contain |
|
1823 | 1825 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1824 | 1826 | |
|
1825 | 1827 | # We must start with a clean buffer, in case this is run from an |
|
1826 | 1828 | # interactive IPython session (via a magic, for example). |
|
1827 | 1829 | self.resetbuffer() |
|
1828 | 1830 | lines = lines.split('\n') |
|
1829 | 1831 | more = 0 |
|
1830 | 1832 | |
|
1831 | 1833 | for line in lines: |
|
1832 | 1834 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1833 | 1835 | # NOT skip even a blank line if we are in a code block (more is |
|
1834 | 1836 | # true) |
|
1835 | 1837 | |
|
1836 | 1838 | |
|
1837 | 1839 | if line or more: |
|
1838 | 1840 | # push to raw history, so hist line numbers stay in sync |
|
1839 | 1841 | self.input_hist_raw.append("# " + line + "\n") |
|
1840 | 1842 | more = self.push(self.prefilter(line,more)) |
|
1841 | 1843 | # IPython's runsource returns None if there was an error |
|
1842 | 1844 | # compiling the code. This allows us to stop processing right |
|
1843 | 1845 | # away, so the user gets the error message at the right place. |
|
1844 | 1846 | if more is None: |
|
1845 | 1847 | break |
|
1846 | 1848 | else: |
|
1847 | 1849 | self.input_hist_raw.append("\n") |
|
1848 | 1850 | # final newline in case the input didn't have it, so that the code |
|
1849 | 1851 | # actually does get executed |
|
1850 | 1852 | if more: |
|
1851 | 1853 | self.push('\n') |
|
1852 | 1854 | |
|
1853 | 1855 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1854 | 1856 | """Compile and run some source in the interpreter. |
|
1855 | 1857 | |
|
1856 | 1858 | Arguments are as for compile_command(). |
|
1857 | 1859 | |
|
1858 | 1860 | One several things can happen: |
|
1859 | 1861 | |
|
1860 | 1862 | 1) The input is incorrect; compile_command() raised an |
|
1861 | 1863 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1862 | 1864 | will be printed by calling the showsyntaxerror() method. |
|
1863 | 1865 | |
|
1864 | 1866 | 2) The input is incomplete, and more input is required; |
|
1865 | 1867 | compile_command() returned None. Nothing happens. |
|
1866 | 1868 | |
|
1867 | 1869 | 3) The input is complete; compile_command() returned a code |
|
1868 | 1870 | object. The code is executed by calling self.runcode() (which |
|
1869 | 1871 | also handles run-time exceptions, except for SystemExit). |
|
1870 | 1872 | |
|
1871 | 1873 | The return value is: |
|
1872 | 1874 | |
|
1873 | 1875 | - True in case 2 |
|
1874 | 1876 | |
|
1875 | 1877 | - False in the other cases, unless an exception is raised, where |
|
1876 | 1878 | None is returned instead. This can be used by external callers to |
|
1877 | 1879 | know whether to continue feeding input or not. |
|
1878 | 1880 | |
|
1879 | 1881 | The return value can be used to decide whether to use sys.ps1 or |
|
1880 | 1882 | sys.ps2 to prompt the next line.""" |
|
1881 | 1883 | |
|
1882 | 1884 | # if the source code has leading blanks, add 'if 1:\n' to it |
|
1883 | 1885 | # this allows execution of indented pasted code. It is tempting |
|
1884 | 1886 | # to add '\n' at the end of source to run commands like ' a=1' |
|
1885 | 1887 | # directly, but this fails for more complicated scenarios |
|
1886 | 1888 | if source[:1] in [' ', '\t']: |
|
1887 | 1889 | source = 'if 1:\n%s' % source |
|
1888 | 1890 | |
|
1889 | 1891 | try: |
|
1890 | 1892 | code = self.compile(source,filename,symbol) |
|
1891 | 1893 | except (OverflowError, SyntaxError, ValueError): |
|
1892 | 1894 | # Case 1 |
|
1893 | 1895 | self.showsyntaxerror(filename) |
|
1894 | 1896 | return None |
|
1895 | 1897 | |
|
1896 | 1898 | if code is None: |
|
1897 | 1899 | # Case 2 |
|
1898 | 1900 | return True |
|
1899 | 1901 | |
|
1900 | 1902 | # Case 3 |
|
1901 | 1903 | # We store the code object so that threaded shells and |
|
1902 | 1904 | # custom exception handlers can access all this info if needed. |
|
1903 | 1905 | # The source corresponding to this can be obtained from the |
|
1904 | 1906 | # buffer attribute as '\n'.join(self.buffer). |
|
1905 | 1907 | self.code_to_run = code |
|
1906 | 1908 | # now actually execute the code object |
|
1907 | 1909 | if self.runcode(code) == 0: |
|
1908 | 1910 | return False |
|
1909 | 1911 | else: |
|
1910 | 1912 | return None |
|
1911 | 1913 | |
|
1912 | 1914 | def runcode(self,code_obj): |
|
1913 | 1915 | """Execute a code object. |
|
1914 | 1916 | |
|
1915 | 1917 | When an exception occurs, self.showtraceback() is called to display a |
|
1916 | 1918 | traceback. |
|
1917 | 1919 | |
|
1918 | 1920 | Return value: a flag indicating whether the code to be run completed |
|
1919 | 1921 | successfully: |
|
1920 | 1922 | |
|
1921 | 1923 | - 0: successful execution. |
|
1922 | 1924 | - 1: an error occurred. |
|
1923 | 1925 | """ |
|
1924 | 1926 | |
|
1925 | 1927 | # Set our own excepthook in case the user code tries to call it |
|
1926 | 1928 | # directly, so that the IPython crash handler doesn't get triggered |
|
1927 | 1929 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1928 | 1930 | |
|
1929 | 1931 | # we save the original sys.excepthook in the instance, in case config |
|
1930 | 1932 | # code (such as magics) needs access to it. |
|
1931 | 1933 | self.sys_excepthook = old_excepthook |
|
1932 | 1934 | outflag = 1 # happens in more places, so it's easier as default |
|
1933 | 1935 | try: |
|
1934 | 1936 | try: |
|
1935 | 1937 | # Embedded instances require separate global/local namespaces |
|
1936 | 1938 | # so they can see both the surrounding (local) namespace and |
|
1937 | 1939 | # the module-level globals when called inside another function. |
|
1938 | 1940 | if self.embedded: |
|
1939 | 1941 | exec code_obj in self.user_global_ns, self.user_ns |
|
1940 | 1942 | # Normal (non-embedded) instances should only have a single |
|
1941 | 1943 | # namespace for user code execution, otherwise functions won't |
|
1942 | 1944 | # see interactive top-level globals. |
|
1943 | 1945 | else: |
|
1944 | 1946 | exec code_obj in self.user_ns |
|
1945 | 1947 | finally: |
|
1946 | 1948 | # Reset our crash handler in place |
|
1947 | 1949 | sys.excepthook = old_excepthook |
|
1948 | 1950 | except SystemExit: |
|
1949 | 1951 | self.resetbuffer() |
|
1950 | 1952 | self.showtraceback() |
|
1951 | 1953 | warn("Type %exit or %quit to exit IPython " |
|
1952 | 1954 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
1953 | 1955 | except self.custom_exceptions: |
|
1954 | 1956 | etype,value,tb = sys.exc_info() |
|
1955 | 1957 | self.CustomTB(etype,value,tb) |
|
1956 | 1958 | except: |
|
1957 | 1959 | self.showtraceback() |
|
1958 | 1960 | else: |
|
1959 | 1961 | outflag = 0 |
|
1960 | 1962 | if softspace(sys.stdout, 0): |
|
1961 | 1963 | |
|
1962 | 1964 | # Flush out code object which has been run (and source) |
|
1963 | 1965 | self.code_to_run = None |
|
1964 | 1966 | return outflag |
|
1965 | 1967 | |
|
1966 | 1968 | def push(self, line): |
|
1967 | 1969 | """Push a line to the interpreter. |
|
1968 | 1970 | |
|
1969 | 1971 | The line should not have a trailing newline; it may have |
|
1970 | 1972 | internal newlines. The line is appended to a buffer and the |
|
1971 | 1973 | interpreter's runsource() method is called with the |
|
1972 | 1974 | concatenated contents of the buffer as source. If this |
|
1973 | 1975 | indicates that the command was executed or invalid, the buffer |
|
1974 | 1976 | is reset; otherwise, the command is incomplete, and the buffer |
|
1975 | 1977 | is left as it was after the line was appended. The return |
|
1976 | 1978 | value is 1 if more input is required, 0 if the line was dealt |
|
1977 | 1979 | with in some way (this is the same as runsource()). |
|
1978 | 1980 | """ |
|
1979 | 1981 | |
|
1980 | 1982 | # autoindent management should be done here, and not in the |
|
1981 | 1983 | # interactive loop, since that one is only seen by keyboard input. We |
|
1982 | 1984 | # need this done correctly even for code run via runlines (which uses |
|
1983 | 1985 | # push). |
|
1984 | 1986 | |
|
1985 | 1987 | #print 'push line: <%s>' % line # dbg |
|
1986 | 1988 | for subline in line.splitlines(): |
|
1987 | 1989 | self.autoindent_update(subline) |
|
1988 | 1990 | self.buffer.append(line) |
|
1989 | 1991 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
1990 | 1992 | if not more: |
|
1991 | 1993 | self.resetbuffer() |
|
1992 | 1994 | return more |
|
1993 | 1995 | |
|
1994 | 1996 | def split_user_input(self, line): |
|
1995 | 1997 | # This is really a hold-over to support ipapi and some extensions |
|
1996 | 1998 | return prefilter.splitUserInput(line) |
|
1997 | 1999 | |
|
1998 | 2000 | def resetbuffer(self): |
|
1999 | 2001 | """Reset the input buffer.""" |
|
2000 | 2002 | self.buffer[:] = [] |
|
2001 | 2003 | |
|
2002 | 2004 | def raw_input(self,prompt='',continue_prompt=False): |
|
2003 | 2005 | """Write a prompt and read a line. |
|
2004 | 2006 | |
|
2005 | 2007 | The returned line does not include the trailing newline. |
|
2006 | 2008 | When the user enters the EOF key sequence, EOFError is raised. |
|
2007 | 2009 | |
|
2008 | 2010 | Optional inputs: |
|
2009 | 2011 | |
|
2010 | 2012 | - prompt(''): a string to be printed to prompt the user. |
|
2011 | 2013 | |
|
2012 | 2014 | - continue_prompt(False): whether this line is the first one or a |
|
2013 | 2015 | continuation in a sequence of inputs. |
|
2014 | 2016 | """ |
|
2015 | 2017 | |
|
2016 | 2018 | # Code run by the user may have modified the readline completer state. |
|
2017 | 2019 | # We must ensure that our completer is back in place. |
|
2018 | 2020 | if self.has_readline: |
|
2019 | 2021 | self.set_completer() |
|
2020 | 2022 | |
|
2021 | 2023 | try: |
|
2022 | 2024 | line = raw_input_original(prompt).decode(self.stdin_encoding) |
|
2023 | 2025 | except ValueError: |
|
2024 | 2026 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" |
|
2025 | 2027 | " or sys.stdout.close()!\nExiting IPython!") |
|
2026 | 2028 | self.exit_now = True |
|
2027 | 2029 | return "" |
|
2028 | 2030 | |
|
2029 | 2031 | # Try to be reasonably smart about not re-indenting pasted input more |
|
2030 | 2032 | # than necessary. We do this by trimming out the auto-indent initial |
|
2031 | 2033 | # spaces, if the user's actual input started itself with whitespace. |
|
2032 | 2034 | #debugx('self.buffer[-1]') |
|
2033 | 2035 | |
|
2034 | 2036 | if self.autoindent: |
|
2035 | 2037 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
2036 | 2038 | line = line[self.indent_current_nsp:] |
|
2037 | 2039 | self.indent_current_nsp = 0 |
|
2038 | 2040 | |
|
2039 | 2041 | # store the unfiltered input before the user has any chance to modify |
|
2040 | 2042 | # it. |
|
2041 | 2043 | if line.strip(): |
|
2042 | 2044 | if continue_prompt: |
|
2043 | 2045 | self.input_hist_raw[-1] += '%s\n' % line |
|
2044 | 2046 | if self.has_readline: # and some config option is set? |
|
2045 | 2047 | try: |
|
2046 | 2048 | histlen = self.readline.get_current_history_length() |
|
2047 | 2049 | newhist = self.input_hist_raw[-1].rstrip() |
|
2048 | 2050 | self.readline.remove_history_item(histlen-1) |
|
2049 | 2051 | self.readline.replace_history_item(histlen-2,newhist) |
|
2050 | 2052 | except AttributeError: |
|
2051 | 2053 | pass # re{move,place}_history_item are new in 2.4. |
|
2052 | 2054 | else: |
|
2053 | 2055 | self.input_hist_raw.append('%s\n' % line) |
|
2054 | 2056 | # only entries starting at first column go to shadow history |
|
2055 | 2057 | if line.lstrip() == line: |
|
2056 | 2058 | self.shadowhist.add(line.strip()) |
|
2057 | 2059 | elif not continue_prompt: |
|
2058 | 2060 | self.input_hist_raw.append('\n') |
|
2059 | 2061 | try: |
|
2060 | 2062 | lineout = self.prefilter(line,continue_prompt) |
|
2061 | 2063 | except: |
|
2062 | 2064 | # blanket except, in case a user-defined prefilter crashes, so it |
|
2063 | 2065 | # can't take all of ipython with it. |
|
2064 | 2066 | self.showtraceback() |
|
2065 | 2067 | return '' |
|
2066 | 2068 | else: |
|
2067 | 2069 | return lineout |
|
2068 | 2070 | |
|
2069 | 2071 | def _prefilter(self, line, continue_prompt): |
|
2070 | 2072 | """Calls different preprocessors, depending on the form of line.""" |
|
2071 | 2073 | |
|
2072 | 2074 | # All handlers *must* return a value, even if it's blank (''). |
|
2073 | 2075 | |
|
2074 | 2076 | # Lines are NOT logged here. Handlers should process the line as |
|
2075 | 2077 | # needed, update the cache AND log it (so that the input cache array |
|
2076 | 2078 | # stays synced). |
|
2077 | 2079 | |
|
2078 | 2080 | #..................................................................... |
|
2079 | 2081 | # Code begins |
|
2080 | 2082 | |
|
2081 | 2083 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
2082 | 2084 | |
|
2083 | 2085 | # save the line away in case we crash, so the post-mortem handler can |
|
2084 | 2086 | # record it |
|
2085 | 2087 | self._last_input_line = line |
|
2086 | 2088 | |
|
2087 | 2089 | #print '***line: <%s>' % line # dbg |
|
2088 | 2090 | |
|
2089 | 2091 | if not line: |
|
2090 | 2092 | # Return immediately on purely empty lines, so that if the user |
|
2091 | 2093 | # previously typed some whitespace that started a continuation |
|
2092 | 2094 | # prompt, he can break out of that loop with just an empty line. |
|
2093 | 2095 | # This is how the default python prompt works. |
|
2094 | 2096 | |
|
2095 | 2097 | # Only return if the accumulated input buffer was just whitespace! |
|
2096 | 2098 | if ''.join(self.buffer).isspace(): |
|
2097 | 2099 | self.buffer[:] = [] |
|
2098 | 2100 | return '' |
|
2099 | 2101 | |
|
2100 | 2102 | line_info = prefilter.LineInfo(line, continue_prompt) |
|
2101 | 2103 | |
|
2102 | 2104 | # the input history needs to track even empty lines |
|
2103 | 2105 | stripped = line.strip() |
|
2104 | 2106 | |
|
2105 | 2107 | if not stripped: |
|
2106 | 2108 | if not continue_prompt: |
|
2107 | 2109 | self.outputcache.prompt_count -= 1 |
|
2108 | 2110 | return self.handle_normal(line_info) |
|
2109 | 2111 | |
|
2110 | 2112 | # print '***cont',continue_prompt # dbg |
|
2111 | 2113 | # special handlers are only allowed for single line statements |
|
2112 | 2114 | if continue_prompt and not self.rc.multi_line_specials: |
|
2113 | 2115 | return self.handle_normal(line_info) |
|
2114 | 2116 | |
|
2115 | 2117 | |
|
2116 | 2118 | # See whether any pre-existing handler can take care of it |
|
2117 | 2119 | rewritten = self.hooks.input_prefilter(stripped) |
|
2118 | 2120 | if rewritten != stripped: # ok, some prefilter did something |
|
2119 | 2121 | rewritten = line_info.pre + rewritten # add indentation |
|
2120 | 2122 | return self.handle_normal(prefilter.LineInfo(rewritten, |
|
2121 | 2123 | continue_prompt)) |
|
2122 | 2124 | |
|
2123 | 2125 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2124 | 2126 | |
|
2125 | 2127 | return prefilter.prefilter(line_info, self) |
|
2126 | 2128 | |
|
2127 | 2129 | |
|
2128 | 2130 | def _prefilter_dumb(self, line, continue_prompt): |
|
2129 | 2131 | """simple prefilter function, for debugging""" |
|
2130 | 2132 | return self.handle_normal(line,continue_prompt) |
|
2131 | 2133 | |
|
2132 | 2134 | |
|
2133 | 2135 | def multiline_prefilter(self, line, continue_prompt): |
|
2134 | 2136 | """ Run _prefilter for each line of input |
|
2135 | 2137 | |
|
2136 | 2138 | Covers cases where there are multiple lines in the user entry, |
|
2137 | 2139 | which is the case when the user goes back to a multiline history |
|
2138 | 2140 | entry and presses enter. |
|
2139 | 2141 | |
|
2140 | 2142 | """ |
|
2141 | 2143 | out = [] |
|
2142 | 2144 | for l in line.rstrip('\n').split('\n'): |
|
2143 | 2145 | out.append(self._prefilter(l, continue_prompt)) |
|
2144 | 2146 | return '\n'.join(out) |
|
2145 | 2147 | |
|
2146 | 2148 | # Set the default prefilter() function (this can be user-overridden) |
|
2147 | 2149 | prefilter = multiline_prefilter |
|
2148 | 2150 | |
|
2149 | 2151 | def handle_normal(self,line_info): |
|
2150 | 2152 | """Handle normal input lines. Use as a template for handlers.""" |
|
2151 | 2153 | |
|
2152 | 2154 | # With autoindent on, we need some way to exit the input loop, and I |
|
2153 | 2155 | # don't want to force the user to have to backspace all the way to |
|
2154 | 2156 | # clear the line. The rule will be in this case, that either two |
|
2155 | 2157 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
2156 | 2158 | # of a size different to the indent level, will exit the input loop. |
|
2157 | 2159 | line = line_info.line |
|
2158 | 2160 | continue_prompt = line_info.continue_prompt |
|
2159 | 2161 | |
|
2160 | 2162 | if (continue_prompt and self.autoindent and line.isspace() and |
|
2161 | 2163 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or |
|
2162 | 2164 | (self.buffer[-1]).isspace() )): |
|
2163 | 2165 | line = '' |
|
2164 | 2166 | |
|
2165 | 2167 | self.log(line,line,continue_prompt) |
|
2166 | 2168 | return line |
|
2167 | 2169 | |
|
2168 | 2170 | def handle_alias(self,line_info): |
|
2169 | 2171 | """Handle alias input lines. """ |
|
2170 | 2172 | tgt = self.alias_table[line_info.iFun] |
|
2171 | 2173 | # print "=>",tgt #dbg |
|
2172 | 2174 | if callable(tgt): |
|
2173 | 2175 | line_out = "_sh." + line_info.iFun + '(r"""' + line_info.line + '""")' |
|
2174 | 2176 | else: |
|
2175 | 2177 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) |
|
2176 | 2178 | |
|
2177 | 2179 | # pre is needed, because it carries the leading whitespace. Otherwise |
|
2178 | 2180 | # aliases won't work in indented sections. |
|
2179 | 2181 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
2180 | 2182 | make_quoted_expr( transformed )) |
|
2181 | 2183 | |
|
2182 | 2184 | self.log(line_info.line,line_out,line_info.continue_prompt) |
|
2183 | 2185 | #print 'line out:',line_out # dbg |
|
2184 | 2186 | return line_out |
|
2185 | 2187 | |
|
2186 | 2188 | def handle_shell_escape(self, line_info): |
|
2187 | 2189 | """Execute the line in a shell, empty return value""" |
|
2188 | 2190 | #print 'line in :', `line` # dbg |
|
2189 | 2191 | line = line_info.line |
|
2190 | 2192 | if line.lstrip().startswith('!!'): |
|
2191 | 2193 | # rewrite LineInfo's line, iFun and theRest to properly hold the |
|
2192 | 2194 | # call to %sx and the actual command to be executed, so |
|
2193 | 2195 | # handle_magic can work correctly. Note that this works even if |
|
2194 | 2196 | # the line is indented, so it handles multi_line_specials |
|
2195 | 2197 | # properly. |
|
2196 | 2198 | new_rest = line.lstrip()[2:] |
|
2197 | 2199 | line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest) |
|
2198 | 2200 | line_info.iFun = 'sx' |
|
2199 | 2201 | line_info.theRest = new_rest |
|
2200 | 2202 | return self.handle_magic(line_info) |
|
2201 | 2203 | else: |
|
2202 | 2204 | cmd = line.lstrip().lstrip('!') |
|
2203 | 2205 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
2204 | 2206 | make_quoted_expr(cmd)) |
|
2205 | 2207 | # update cache/log and return |
|
2206 | 2208 | self.log(line,line_out,line_info.continue_prompt) |
|
2207 | 2209 | return line_out |
|
2208 | 2210 | |
|
2209 | 2211 | def handle_magic(self, line_info): |
|
2210 | 2212 | """Execute magic functions.""" |
|
2211 | 2213 | iFun = line_info.iFun |
|
2212 | 2214 | theRest = line_info.theRest |
|
2213 | 2215 | cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace, |
|
2214 | 2216 | make_quoted_expr(iFun + " " + theRest)) |
|
2215 | 2217 | self.log(line_info.line,cmd,line_info.continue_prompt) |
|
2216 | 2218 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
2217 | 2219 | return cmd |
|
2218 | 2220 | |
|
2219 | 2221 | def handle_auto(self, line_info): |
|
2220 | 2222 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
2221 | 2223 | |
|
2222 | 2224 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2223 | 2225 | line = line_info.line |
|
2224 | 2226 | iFun = line_info.iFun |
|
2225 | 2227 | theRest = line_info.theRest |
|
2226 | 2228 | pre = line_info.pre |
|
2227 | 2229 | continue_prompt = line_info.continue_prompt |
|
2228 | 2230 | obj = line_info.ofind(self)['obj'] |
|
2229 | 2231 | |
|
2230 | 2232 | # This should only be active for single-line input! |
|
2231 | 2233 | if continue_prompt: |
|
2232 | 2234 | self.log(line,line,continue_prompt) |
|
2233 | 2235 | return line |
|
2234 | 2236 | |
|
2235 | 2237 | force_auto = isinstance(obj, IPython.ipapi.IPyAutocall) |
|
2236 | 2238 | auto_rewrite = True |
|
2237 | 2239 | |
|
2238 | 2240 | if pre == self.ESC_QUOTE: |
|
2239 | 2241 | # Auto-quote splitting on whitespace |
|
2240 | 2242 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
2241 | 2243 | elif pre == self.ESC_QUOTE2: |
|
2242 | 2244 | # Auto-quote whole string |
|
2243 | 2245 | newcmd = '%s("%s")' % (iFun,theRest) |
|
2244 | 2246 | elif pre == self.ESC_PAREN: |
|
2245 | 2247 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) |
|
2246 | 2248 | else: |
|
2247 | 2249 | # Auto-paren. |
|
2248 | 2250 | # We only apply it to argument-less calls if the autocall |
|
2249 | 2251 | # parameter is set to 2. We only need to check that autocall is < |
|
2250 | 2252 | # 2, since this function isn't called unless it's at least 1. |
|
2251 | 2253 | if not theRest and (self.rc.autocall < 2) and not force_auto: |
|
2252 | 2254 | newcmd = '%s %s' % (iFun,theRest) |
|
2253 | 2255 | auto_rewrite = False |
|
2254 | 2256 | else: |
|
2255 | 2257 | if not force_auto and theRest.startswith('['): |
|
2256 | 2258 | if hasattr(obj,'__getitem__'): |
|
2257 | 2259 | # Don't autocall in this case: item access for an object |
|
2258 | 2260 | # which is BOTH callable and implements __getitem__. |
|
2259 | 2261 | newcmd = '%s %s' % (iFun,theRest) |
|
2260 | 2262 | auto_rewrite = False |
|
2261 | 2263 | else: |
|
2262 | 2264 | # if the object doesn't support [] access, go ahead and |
|
2263 | 2265 | # autocall |
|
2264 | 2266 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
2265 | 2267 | elif theRest.endswith(';'): |
|
2266 | 2268 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
2267 | 2269 | else: |
|
2268 | 2270 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) |
|
2269 | 2271 | |
|
2270 | 2272 | if auto_rewrite: |
|
2271 | 2273 | rw = self.outputcache.prompt1.auto_rewrite() + newcmd |
|
2272 | 2274 | |
|
2273 | 2275 | try: |
|
2274 | 2276 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2275 | 2277 | # we use it and only print uncolored rewrite if we have unicode |
|
2276 | 2278 | rw = str(rw) |
|
2277 | 2279 | print >>Term.cout, rw |
|
2278 | 2280 | except UnicodeEncodeError: |
|
2279 | 2281 | print "-------------->" + newcmd |
|
2280 | 2282 | |
|
2281 | 2283 | # log what is now valid Python, not the actual user input (without the |
|
2282 | 2284 | # final newline) |
|
2283 | 2285 | self.log(line,newcmd,continue_prompt) |
|
2284 | 2286 | return newcmd |
|
2285 | 2287 | |
|
2286 | 2288 | def handle_help(self, line_info): |
|
2287 | 2289 | """Try to get some help for the object. |
|
2288 | 2290 | |
|
2289 | 2291 | obj? or ?obj -> basic information. |
|
2290 | 2292 | obj?? or ??obj -> more details. |
|
2291 | 2293 | """ |
|
2292 | 2294 | |
|
2293 | 2295 | line = line_info.line |
|
2294 | 2296 | # We need to make sure that we don't process lines which would be |
|
2295 | 2297 | # otherwise valid python, such as "x=1 # what?" |
|
2296 | 2298 | try: |
|
2297 | 2299 | codeop.compile_command(line) |
|
2298 | 2300 | except SyntaxError: |
|
2299 | 2301 | # We should only handle as help stuff which is NOT valid syntax |
|
2300 | 2302 | if line[0]==self.ESC_HELP: |
|
2301 | 2303 | line = line[1:] |
|
2302 | 2304 | elif line[-1]==self.ESC_HELP: |
|
2303 | 2305 | line = line[:-1] |
|
2304 | 2306 | self.log(line,'#?'+line,line_info.continue_prompt) |
|
2305 | 2307 | if line: |
|
2306 | 2308 | #print 'line:<%r>' % line # dbg |
|
2307 | 2309 | self.magic_pinfo(line) |
|
2308 | 2310 | else: |
|
2309 | 2311 | page(self.usage,screen_lines=self.rc.screen_length) |
|
2310 | 2312 | return '' # Empty string is needed here! |
|
2311 | 2313 | except: |
|
2312 | 2314 | # Pass any other exceptions through to the normal handler |
|
2313 | 2315 | return self.handle_normal(line_info) |
|
2314 | 2316 | else: |
|
2315 | 2317 | # If the code compiles ok, we should handle it normally |
|
2316 | 2318 | return self.handle_normal(line_info) |
|
2317 | 2319 | |
|
2318 | 2320 | def getapi(self): |
|
2319 | 2321 | """ Get an IPApi object for this shell instance |
|
2320 | 2322 | |
|
2321 | 2323 | Getting an IPApi object is always preferable to accessing the shell |
|
2322 | 2324 | directly, but this holds true especially for extensions. |
|
2323 | 2325 | |
|
2324 | 2326 | It should always be possible to implement an extension with IPApi |
|
2325 | 2327 | alone. If not, contact maintainer to request an addition. |
|
2326 | 2328 | |
|
2327 | 2329 | """ |
|
2328 | 2330 | return self.api |
|
2329 | 2331 | |
|
2330 | 2332 | def handle_emacs(self, line_info): |
|
2331 | 2333 | """Handle input lines marked by python-mode.""" |
|
2332 | 2334 | |
|
2333 | 2335 | # Currently, nothing is done. Later more functionality can be added |
|
2334 | 2336 | # here if needed. |
|
2335 | 2337 | |
|
2336 | 2338 | # The input cache shouldn't be updated |
|
2337 | 2339 | return line_info.line |
|
2338 | 2340 | |
|
2339 | 2341 | |
|
2340 | 2342 | def mktempfile(self,data=None): |
|
2341 | 2343 | """Make a new tempfile and return its filename. |
|
2342 | 2344 | |
|
2343 | 2345 | This makes a call to tempfile.mktemp, but it registers the created |
|
2344 | 2346 | filename internally so ipython cleans it up at exit time. |
|
2345 | 2347 | |
|
2346 | 2348 | Optional inputs: |
|
2347 | 2349 | |
|
2348 | 2350 | - data(None): if data is given, it gets written out to the temp file |
|
2349 | 2351 | immediately, and the file is closed again.""" |
|
2350 | 2352 | |
|
2351 | 2353 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2352 | 2354 | self.tempfiles.append(filename) |
|
2353 | 2355 | |
|
2354 | 2356 | if data: |
|
2355 | 2357 | tmp_file = open(filename,'w') |
|
2356 | 2358 | tmp_file.write(data) |
|
2357 | 2359 | tmp_file.close() |
|
2358 | 2360 | return filename |
|
2359 | 2361 | |
|
2360 | 2362 | def write(self,data): |
|
2361 | 2363 | """Write a string to the default output""" |
|
2362 | 2364 | Term.cout.write(data) |
|
2363 | 2365 | |
|
2364 | 2366 | def write_err(self,data): |
|
2365 | 2367 | """Write a string to the default error output""" |
|
2366 | 2368 | Term.cerr.write(data) |
|
2367 | 2369 | |
|
2368 | 2370 | def exit(self): |
|
2369 | 2371 | """Handle interactive exit. |
|
2370 | 2372 | |
|
2371 | 2373 | This method sets the exit_now attribute.""" |
|
2372 | 2374 | |
|
2373 | 2375 | if self.rc.confirm_exit: |
|
2374 | 2376 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
2375 | 2377 | self.exit_now = True |
|
2376 | 2378 | else: |
|
2377 | 2379 | self.exit_now = True |
|
2378 | 2380 | |
|
2379 | 2381 | def safe_execfile(self,fname,*where,**kw): |
|
2380 | 2382 | """A safe version of the builtin execfile(). |
|
2381 | 2383 | |
|
2382 | 2384 | This version will never throw an exception, and knows how to handle |
|
2383 | 2385 | ipython logs as well.""" |
|
2384 | 2386 | |
|
2385 | 2387 | def syspath_cleanup(): |
|
2386 | 2388 | """Internal cleanup routine for sys.path.""" |
|
2387 | 2389 | if add_dname: |
|
2388 | 2390 | try: |
|
2389 | 2391 | sys.path.remove(dname) |
|
2390 | 2392 | except ValueError: |
|
2391 | 2393 | # For some reason the user has already removed it, ignore. |
|
2392 | 2394 | pass |
|
2393 | 2395 | |
|
2394 | 2396 | fname = os.path.expanduser(fname) |
|
2395 | 2397 | |
|
2396 | 2398 | # Find things also in current directory. This is needed to mimic the |
|
2397 | 2399 | # behavior of running a script from the system command line, where |
|
2398 | 2400 | # Python inserts the script's directory into sys.path |
|
2399 | 2401 | dname = os.path.dirname(os.path.abspath(fname)) |
|
2400 | 2402 | add_dname = False |
|
2401 | 2403 | if dname not in sys.path: |
|
2402 | 2404 | sys.path.insert(0,dname) |
|
2403 | 2405 | add_dname = True |
|
2404 | 2406 | |
|
2405 | 2407 | try: |
|
2406 | 2408 | xfile = open(fname) |
|
2407 | 2409 | except: |
|
2408 | 2410 | print >> Term.cerr, \ |
|
2409 | 2411 | 'Could not open file <%s> for safe execution.' % fname |
|
2410 | 2412 | syspath_cleanup() |
|
2411 | 2413 | return None |
|
2412 | 2414 | |
|
2413 | 2415 | kw.setdefault('islog',0) |
|
2414 | 2416 | kw.setdefault('quiet',1) |
|
2415 | 2417 | kw.setdefault('exit_ignore',0) |
|
2416 | 2418 | first = xfile.readline() |
|
2417 | 2419 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
2418 | 2420 | xfile.close() |
|
2419 | 2421 | # line by line execution |
|
2420 | 2422 | if first.startswith(loghead) or kw['islog']: |
|
2421 | 2423 | print 'Loading log file <%s> one line at a time...' % fname |
|
2422 | 2424 | if kw['quiet']: |
|
2423 | 2425 | stdout_save = sys.stdout |
|
2424 | 2426 | sys.stdout = StringIO.StringIO() |
|
2425 | 2427 | try: |
|
2426 | 2428 | globs,locs = where[0:2] |
|
2427 | 2429 | except: |
|
2428 | 2430 | try: |
|
2429 | 2431 | globs = locs = where[0] |
|
2430 | 2432 | except: |
|
2431 | 2433 | globs = locs = globals() |
|
2432 | 2434 | badblocks = [] |
|
2433 | 2435 | |
|
2434 | 2436 | # we also need to identify indented blocks of code when replaying |
|
2435 | 2437 | # logs and put them together before passing them to an exec |
|
2436 | 2438 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2437 | 2439 | # file. It's easiest if we swallow the whole thing in memory |
|
2438 | 2440 | # first, and manually walk through the lines list moving the |
|
2439 | 2441 | # counter ourselves. |
|
2440 | 2442 | indent_re = re.compile('\s+\S') |
|
2441 | 2443 | xfile = open(fname) |
|
2442 | 2444 | filelines = xfile.readlines() |
|
2443 | 2445 | xfile.close() |
|
2444 | 2446 | nlines = len(filelines) |
|
2445 | 2447 | lnum = 0 |
|
2446 | 2448 | while lnum < nlines: |
|
2447 | 2449 | line = filelines[lnum] |
|
2448 | 2450 | lnum += 1 |
|
2449 | 2451 | # don't re-insert logger status info into cache |
|
2450 | 2452 | if line.startswith('#log#'): |
|
2451 | 2453 | continue |
|
2452 | 2454 | else: |
|
2453 | 2455 | # build a block of code (maybe a single line) for execution |
|
2454 | 2456 | block = line |
|
2455 | 2457 | try: |
|
2456 | 2458 | next = filelines[lnum] # lnum has already incremented |
|
2457 | 2459 | except: |
|
2458 | 2460 | next = None |
|
2459 | 2461 | while next and indent_re.match(next): |
|
2460 | 2462 | block += next |
|
2461 | 2463 | lnum += 1 |
|
2462 | 2464 | try: |
|
2463 | 2465 | next = filelines[lnum] |
|
2464 | 2466 | except: |
|
2465 | 2467 | next = None |
|
2466 | 2468 | # now execute the block of one or more lines |
|
2467 | 2469 | try: |
|
2468 | 2470 | exec block in globs,locs |
|
2469 | 2471 | except SystemExit: |
|
2470 | 2472 | pass |
|
2471 | 2473 | except: |
|
2472 | 2474 | badblocks.append(block.rstrip()) |
|
2473 | 2475 | if kw['quiet']: # restore stdout |
|
2474 | 2476 | sys.stdout.close() |
|
2475 | 2477 | sys.stdout = stdout_save |
|
2476 | 2478 | print 'Finished replaying log file <%s>' % fname |
|
2477 | 2479 | if badblocks: |
|
2478 | 2480 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
2479 | 2481 | '<%s> reported errors:' % fname) |
|
2480 | 2482 | |
|
2481 | 2483 | for badline in badblocks: |
|
2482 | 2484 | print >> sys.stderr, badline |
|
2483 | 2485 | else: # regular file execution |
|
2484 | 2486 | try: |
|
2485 | 2487 | if sys.platform == 'win32' and sys.version_info < (2,5,1): |
|
2486 | 2488 | # Work around a bug in Python for Windows. The bug was |
|
2487 | 2489 | # fixed in in Python 2.5 r54159 and 54158, but that's still |
|
2488 | 2490 | # SVN Python as of March/07. For details, see: |
|
2489 | 2491 | # http://projects.scipy.org/ipython/ipython/ticket/123 |
|
2490 | 2492 | try: |
|
2491 | 2493 | globs,locs = where[0:2] |
|
2492 | 2494 | except: |
|
2493 | 2495 | try: |
|
2494 | 2496 | globs = locs = where[0] |
|
2495 | 2497 | except: |
|
2496 | 2498 | globs = locs = globals() |
|
2497 | 2499 | exec file(fname) in globs,locs |
|
2498 | 2500 | else: |
|
2499 | 2501 | execfile(fname,*where) |
|
2500 | 2502 | except SyntaxError: |
|
2501 | 2503 | self.showsyntaxerror() |
|
2502 | 2504 | warn('Failure executing file: <%s>' % fname) |
|
2503 | 2505 | except SystemExit,status: |
|
2504 | 2506 | # Code that correctly sets the exit status flag to success (0) |
|
2505 | 2507 | # shouldn't be bothered with a traceback. Note that a plain |
|
2506 | 2508 | # sys.exit() does NOT set the message to 0 (it's empty) so that |
|
2507 | 2509 | # will still get a traceback. Note that the structure of the |
|
2508 | 2510 | # SystemExit exception changed between Python 2.4 and 2.5, so |
|
2509 | 2511 | # the checks must be done in a version-dependent way. |
|
2510 | 2512 | show = False |
|
2511 | 2513 | |
|
2512 | 2514 | if sys.version_info[:2] > (2,5): |
|
2513 | 2515 | if status.message!=0 and not kw['exit_ignore']: |
|
2514 | 2516 | show = True |
|
2515 | 2517 | else: |
|
2516 | 2518 | if status.code and not kw['exit_ignore']: |
|
2517 | 2519 | show = True |
|
2518 | 2520 | if show: |
|
2519 | 2521 | self.showtraceback() |
|
2520 | 2522 | warn('Failure executing file: <%s>' % fname) |
|
2521 | 2523 | except: |
|
2522 | 2524 | self.showtraceback() |
|
2523 | 2525 | warn('Failure executing file: <%s>' % fname) |
|
2524 | 2526 | |
|
2525 | 2527 | syspath_cleanup() |
|
2526 | 2528 | |
|
2527 | 2529 | #************************* end of file <iplib.py> ***************************** |
General Comments 0
You need to be logged in to leave comments.
Login now