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