##// END OF EJS Templates
First round of 'complete_command' hook, implements customizable command line ...
vivainio -
Show More

The requested changes are too big and content was truncated. Show full diff

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