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