~ipython-dev/ipython/0.10.1

« back to all changes in this revision

Viewing changes to IPython/iplib.py

  • Committer: Fernando Perez
  • Date: 2008-06-02 01:26:30 UTC
  • mfrom: (0.1.130 ipython-local)
  • Revision ID: fernando.perez@berkeley.edu-20080602012630-m14vezrhydzvahf8
Merge in all development done in bzr since February 16 2008.

At that time, a clean bzr branch was started from the SVN tree, but
without SVN history.  That SVN history has now been used as the basis
of this branch, and the development done on the history-less BZR
branch has been added and is the content of this merge.  

This branch will be the new official main line of development in
Launchpad (equivalent to the old SVN trunk).

Show diffs side-by-side

added added

removed removed

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