##// END OF EJS Templates
Merge pull request #998 from minrk/homedir...
Min RK -
r5391:351c8fc4 merge
parent child Browse files
Show More
@@ -1,2710 +1,2705 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Main IPython class."""
2 """Main IPython class."""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from __future__ import with_statement
17 from __future__ import with_statement
18 from __future__ import absolute_import
18 from __future__ import absolute_import
19
19
20 import __builtin__ as builtin_mod
20 import __builtin__ as builtin_mod
21 import __future__
21 import __future__
22 import abc
22 import abc
23 import ast
23 import ast
24 import atexit
24 import atexit
25 import codeop
25 import codeop
26 import inspect
26 import inspect
27 import os
27 import os
28 import re
28 import re
29 import sys
29 import sys
30 import tempfile
30 import tempfile
31 import types
31 import types
32 try:
32 try:
33 from contextlib import nested
33 from contextlib import nested
34 except:
34 except:
35 from IPython.utils.nested_context import nested
35 from IPython.utils.nested_context import nested
36
36
37 from IPython.config.configurable import SingletonConfigurable
37 from IPython.config.configurable import SingletonConfigurable
38 from IPython.core import debugger, oinspect
38 from IPython.core import debugger, oinspect
39 from IPython.core import history as ipcorehist
39 from IPython.core import history as ipcorehist
40 from IPython.core import page
40 from IPython.core import page
41 from IPython.core import prefilter
41 from IPython.core import prefilter
42 from IPython.core import shadowns
42 from IPython.core import shadowns
43 from IPython.core import ultratb
43 from IPython.core import ultratb
44 from IPython.core.alias import AliasManager, AliasError
44 from IPython.core.alias import AliasManager, AliasError
45 from IPython.core.autocall import ExitAutocall
45 from IPython.core.autocall import ExitAutocall
46 from IPython.core.builtin_trap import BuiltinTrap
46 from IPython.core.builtin_trap import BuiltinTrap
47 from IPython.core.compilerop import CachingCompiler
47 from IPython.core.compilerop import CachingCompiler
48 from IPython.core.display_trap import DisplayTrap
48 from IPython.core.display_trap import DisplayTrap
49 from IPython.core.displayhook import DisplayHook
49 from IPython.core.displayhook import DisplayHook
50 from IPython.core.displaypub import DisplayPublisher
50 from IPython.core.displaypub import DisplayPublisher
51 from IPython.core.error import TryNext, UsageError
51 from IPython.core.error import TryNext, UsageError
52 from IPython.core.extensions import ExtensionManager
52 from IPython.core.extensions import ExtensionManager
53 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
53 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
54 from IPython.core.formatters import DisplayFormatter
54 from IPython.core.formatters import DisplayFormatter
55 from IPython.core.history import HistoryManager
55 from IPython.core.history import HistoryManager
56 from IPython.core.inputsplitter import IPythonInputSplitter
56 from IPython.core.inputsplitter import IPythonInputSplitter
57 from IPython.core.logger import Logger
57 from IPython.core.logger import Logger
58 from IPython.core.macro import Macro
58 from IPython.core.macro import Macro
59 from IPython.core.magic import Magic
59 from IPython.core.magic import Magic
60 from IPython.core.payload import PayloadManager
60 from IPython.core.payload import PayloadManager
61 from IPython.core.plugin import PluginManager
61 from IPython.core.plugin import PluginManager
62 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
62 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
63 from IPython.core.profiledir import ProfileDir
63 from IPython.core.profiledir import ProfileDir
64 from IPython.external.Itpl import ItplNS
64 from IPython.external.Itpl import ItplNS
65 from IPython.utils import PyColorize
65 from IPython.utils import PyColorize
66 from IPython.utils import io
66 from IPython.utils import io
67 from IPython.utils import py3compat
67 from IPython.utils import py3compat
68 from IPython.utils.doctestreload import doctest_reload
68 from IPython.utils.doctestreload import doctest_reload
69 from IPython.utils.io import ask_yes_no, rprint
69 from IPython.utils.io import ask_yes_no, rprint
70 from IPython.utils.ipstruct import Struct
70 from IPython.utils.ipstruct import Struct
71 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
71 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
72 from IPython.utils.pickleshare import PickleShareDB
72 from IPython.utils.pickleshare import PickleShareDB
73 from IPython.utils.process import system, getoutput
73 from IPython.utils.process import system, getoutput
74 from IPython.utils.strdispatch import StrDispatch
74 from IPython.utils.strdispatch import StrDispatch
75 from IPython.utils.syspathcontext import prepended_to_syspath
75 from IPython.utils.syspathcontext import prepended_to_syspath
76 from IPython.utils.text import (num_ini_spaces, format_screen, LSString, SList,
76 from IPython.utils.text import (num_ini_spaces, format_screen, LSString, SList,
77 DollarFormatter)
77 DollarFormatter)
78 from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum,
78 from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum,
79 List, Unicode, Instance, Type)
79 List, Unicode, Instance, Type)
80 from IPython.utils.warn import warn, error, fatal
80 from IPython.utils.warn import warn, error, fatal
81 import IPython.core.hooks
81 import IPython.core.hooks
82
82
83 #-----------------------------------------------------------------------------
83 #-----------------------------------------------------------------------------
84 # Globals
84 # Globals
85 #-----------------------------------------------------------------------------
85 #-----------------------------------------------------------------------------
86
86
87 # compiled regexps for autoindent management
87 # compiled regexps for autoindent management
88 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
88 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
89
89
90 #-----------------------------------------------------------------------------
90 #-----------------------------------------------------------------------------
91 # Utilities
91 # Utilities
92 #-----------------------------------------------------------------------------
92 #-----------------------------------------------------------------------------
93
93
94 def softspace(file, newvalue):
94 def softspace(file, newvalue):
95 """Copied from code.py, to remove the dependency"""
95 """Copied from code.py, to remove the dependency"""
96
96
97 oldvalue = 0
97 oldvalue = 0
98 try:
98 try:
99 oldvalue = file.softspace
99 oldvalue = file.softspace
100 except AttributeError:
100 except AttributeError:
101 pass
101 pass
102 try:
102 try:
103 file.softspace = newvalue
103 file.softspace = newvalue
104 except (AttributeError, TypeError):
104 except (AttributeError, TypeError):
105 # "attribute-less object" or "read-only attributes"
105 # "attribute-less object" or "read-only attributes"
106 pass
106 pass
107 return oldvalue
107 return oldvalue
108
108
109
109
110 def no_op(*a, **kw): pass
110 def no_op(*a, **kw): pass
111
111
112 class NoOpContext(object):
112 class NoOpContext(object):
113 def __enter__(self): pass
113 def __enter__(self): pass
114 def __exit__(self, type, value, traceback): pass
114 def __exit__(self, type, value, traceback): pass
115 no_op_context = NoOpContext()
115 no_op_context = NoOpContext()
116
116
117 class SpaceInInput(Exception): pass
117 class SpaceInInput(Exception): pass
118
118
119 class Bunch: pass
119 class Bunch: pass
120
120
121
121
122 def get_default_colors():
122 def get_default_colors():
123 if sys.platform=='darwin':
123 if sys.platform=='darwin':
124 return "LightBG"
124 return "LightBG"
125 elif os.name=='nt':
125 elif os.name=='nt':
126 return 'Linux'
126 return 'Linux'
127 else:
127 else:
128 return 'Linux'
128 return 'Linux'
129
129
130
130
131 class SeparateUnicode(Unicode):
131 class SeparateUnicode(Unicode):
132 """A Unicode subclass to validate separate_in, separate_out, etc.
132 """A Unicode subclass to validate separate_in, separate_out, etc.
133
133
134 This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
134 This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
135 """
135 """
136
136
137 def validate(self, obj, value):
137 def validate(self, obj, value):
138 if value == '0': value = ''
138 if value == '0': value = ''
139 value = value.replace('\\n','\n')
139 value = value.replace('\\n','\n')
140 return super(SeparateUnicode, self).validate(obj, value)
140 return super(SeparateUnicode, self).validate(obj, value)
141
141
142
142
143 class ReadlineNoRecord(object):
143 class ReadlineNoRecord(object):
144 """Context manager to execute some code, then reload readline history
144 """Context manager to execute some code, then reload readline history
145 so that interactive input to the code doesn't appear when pressing up."""
145 so that interactive input to the code doesn't appear when pressing up."""
146 def __init__(self, shell):
146 def __init__(self, shell):
147 self.shell = shell
147 self.shell = shell
148 self._nested_level = 0
148 self._nested_level = 0
149
149
150 def __enter__(self):
150 def __enter__(self):
151 if self._nested_level == 0:
151 if self._nested_level == 0:
152 try:
152 try:
153 self.orig_length = self.current_length()
153 self.orig_length = self.current_length()
154 self.readline_tail = self.get_readline_tail()
154 self.readline_tail = self.get_readline_tail()
155 except (AttributeError, IndexError): # Can fail with pyreadline
155 except (AttributeError, IndexError): # Can fail with pyreadline
156 self.orig_length, self.readline_tail = 999999, []
156 self.orig_length, self.readline_tail = 999999, []
157 self._nested_level += 1
157 self._nested_level += 1
158
158
159 def __exit__(self, type, value, traceback):
159 def __exit__(self, type, value, traceback):
160 self._nested_level -= 1
160 self._nested_level -= 1
161 if self._nested_level == 0:
161 if self._nested_level == 0:
162 # Try clipping the end if it's got longer
162 # Try clipping the end if it's got longer
163 try:
163 try:
164 e = self.current_length() - self.orig_length
164 e = self.current_length() - self.orig_length
165 if e > 0:
165 if e > 0:
166 for _ in range(e):
166 for _ in range(e):
167 self.shell.readline.remove_history_item(self.orig_length)
167 self.shell.readline.remove_history_item(self.orig_length)
168
168
169 # If it still doesn't match, just reload readline history.
169 # If it still doesn't match, just reload readline history.
170 if self.current_length() != self.orig_length \
170 if self.current_length() != self.orig_length \
171 or self.get_readline_tail() != self.readline_tail:
171 or self.get_readline_tail() != self.readline_tail:
172 self.shell.refill_readline_hist()
172 self.shell.refill_readline_hist()
173 except (AttributeError, IndexError):
173 except (AttributeError, IndexError):
174 pass
174 pass
175 # Returning False will cause exceptions to propagate
175 # Returning False will cause exceptions to propagate
176 return False
176 return False
177
177
178 def current_length(self):
178 def current_length(self):
179 return self.shell.readline.get_current_history_length()
179 return self.shell.readline.get_current_history_length()
180
180
181 def get_readline_tail(self, n=10):
181 def get_readline_tail(self, n=10):
182 """Get the last n items in readline history."""
182 """Get the last n items in readline history."""
183 end = self.shell.readline.get_current_history_length() + 1
183 end = self.shell.readline.get_current_history_length() + 1
184 start = max(end-n, 1)
184 start = max(end-n, 1)
185 ghi = self.shell.readline.get_history_item
185 ghi = self.shell.readline.get_history_item
186 return [ghi(x) for x in range(start, end)]
186 return [ghi(x) for x in range(start, end)]
187
187
188
188
189 _autocall_help = """
189 _autocall_help = """
190 Make IPython automatically call any callable object even if
190 Make IPython automatically call any callable object even if
191 you didn't type explicit parentheses. For example, 'str 43' becomes 'str(43)'
191 you didn't type explicit parentheses. For example, 'str 43' becomes 'str(43)'
192 automatically. The value can be '0' to disable the feature, '1' for 'smart'
192 automatically. The value can be '0' to disable the feature, '1' for 'smart'
193 autocall, where it is not applied if there are no more arguments on the line,
193 autocall, where it is not applied if there are no more arguments on the line,
194 and '2' for 'full' autocall, where all callable objects are automatically
194 and '2' for 'full' autocall, where all callable objects are automatically
195 called (even if no arguments are present). The default is '1'.
195 called (even if no arguments are present). The default is '1'.
196 """
196 """
197
197
198 #-----------------------------------------------------------------------------
198 #-----------------------------------------------------------------------------
199 # Main IPython class
199 # Main IPython class
200 #-----------------------------------------------------------------------------
200 #-----------------------------------------------------------------------------
201
201
202 class InteractiveShell(SingletonConfigurable, Magic):
202 class InteractiveShell(SingletonConfigurable, Magic):
203 """An enhanced, interactive shell for Python."""
203 """An enhanced, interactive shell for Python."""
204
204
205 _instance = None
205 _instance = None
206
206
207 autocall = Enum((0,1,2), default_value=1, config=True, help=
207 autocall = Enum((0,1,2), default_value=1, config=True, help=
208 """
208 """
209 Make IPython automatically call any callable object even if you didn't
209 Make IPython automatically call any callable object even if you didn't
210 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
210 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
211 automatically. The value can be '0' to disable the feature, '1' for
211 automatically. The value can be '0' to disable the feature, '1' for
212 'smart' autocall, where it is not applied if there are no more
212 'smart' autocall, where it is not applied if there are no more
213 arguments on the line, and '2' for 'full' autocall, where all callable
213 arguments on the line, and '2' for 'full' autocall, where all callable
214 objects are automatically called (even if no arguments are present).
214 objects are automatically called (even if no arguments are present).
215 The default is '1'.
215 The default is '1'.
216 """
216 """
217 )
217 )
218 # TODO: remove all autoindent logic and put into frontends.
218 # TODO: remove all autoindent logic and put into frontends.
219 # We can't do this yet because even runlines uses the autoindent.
219 # We can't do this yet because even runlines uses the autoindent.
220 autoindent = CBool(True, config=True, help=
220 autoindent = CBool(True, config=True, help=
221 """
221 """
222 Autoindent IPython code entered interactively.
222 Autoindent IPython code entered interactively.
223 """
223 """
224 )
224 )
225 automagic = CBool(True, config=True, help=
225 automagic = CBool(True, config=True, help=
226 """
226 """
227 Enable magic commands to be called without the leading %.
227 Enable magic commands to be called without the leading %.
228 """
228 """
229 )
229 )
230 cache_size = Integer(1000, config=True, help=
230 cache_size = Integer(1000, config=True, help=
231 """
231 """
232 Set the size of the output cache. The default is 1000, you can
232 Set the size of the output cache. The default is 1000, you can
233 change it permanently in your config file. Setting it to 0 completely
233 change it permanently in your config file. Setting it to 0 completely
234 disables the caching system, and the minimum value accepted is 20 (if
234 disables the caching system, and the minimum value accepted is 20 (if
235 you provide a value less than 20, it is reset to 0 and a warning is
235 you provide a value less than 20, it is reset to 0 and a warning is
236 issued). This limit is defined because otherwise you'll spend more
236 issued). This limit is defined because otherwise you'll spend more
237 time re-flushing a too small cache than working
237 time re-flushing a too small cache than working
238 """
238 """
239 )
239 )
240 color_info = CBool(True, config=True, help=
240 color_info = CBool(True, config=True, help=
241 """
241 """
242 Use colors for displaying information about objects. Because this
242 Use colors for displaying information about objects. Because this
243 information is passed through a pager (like 'less'), and some pagers
243 information is passed through a pager (like 'less'), and some pagers
244 get confused with color codes, this capability can be turned off.
244 get confused with color codes, this capability can be turned off.
245 """
245 """
246 )
246 )
247 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
247 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
248 default_value=get_default_colors(), config=True,
248 default_value=get_default_colors(), config=True,
249 help="Set the color scheme (NoColor, Linux, or LightBG)."
249 help="Set the color scheme (NoColor, Linux, or LightBG)."
250 )
250 )
251 colors_force = CBool(False, help=
251 colors_force = CBool(False, help=
252 """
252 """
253 Force use of ANSI color codes, regardless of OS and readline
253 Force use of ANSI color codes, regardless of OS and readline
254 availability.
254 availability.
255 """
255 """
256 # FIXME: This is essentially a hack to allow ZMQShell to show colors
256 # FIXME: This is essentially a hack to allow ZMQShell to show colors
257 # without readline on Win32. When the ZMQ formatting system is
257 # without readline on Win32. When the ZMQ formatting system is
258 # refactored, this should be removed.
258 # refactored, this should be removed.
259 )
259 )
260 debug = CBool(False, config=True)
260 debug = CBool(False, config=True)
261 deep_reload = CBool(False, config=True, help=
261 deep_reload = CBool(False, config=True, help=
262 """
262 """
263 Enable deep (recursive) reloading by default. IPython can use the
263 Enable deep (recursive) reloading by default. IPython can use the
264 deep_reload module which reloads changes in modules recursively (it
264 deep_reload module which reloads changes in modules recursively (it
265 replaces the reload() function, so you don't need to change anything to
265 replaces the reload() function, so you don't need to change anything to
266 use it). deep_reload() forces a full reload of modules whose code may
266 use it). deep_reload() forces a full reload of modules whose code may
267 have changed, which the default reload() function does not. When
267 have changed, which the default reload() function does not. When
268 deep_reload is off, IPython will use the normal reload(), but
268 deep_reload is off, IPython will use the normal reload(), but
269 deep_reload will still be available as dreload().
269 deep_reload will still be available as dreload().
270 """
270 """
271 )
271 )
272 display_formatter = Instance(DisplayFormatter)
272 display_formatter = Instance(DisplayFormatter)
273 displayhook_class = Type(DisplayHook)
273 displayhook_class = Type(DisplayHook)
274 display_pub_class = Type(DisplayPublisher)
274 display_pub_class = Type(DisplayPublisher)
275
275
276 exit_now = CBool(False)
276 exit_now = CBool(False)
277 exiter = Instance(ExitAutocall)
277 exiter = Instance(ExitAutocall)
278 def _exiter_default(self):
278 def _exiter_default(self):
279 return ExitAutocall(self)
279 return ExitAutocall(self)
280 # Monotonically increasing execution counter
280 # Monotonically increasing execution counter
281 execution_count = Integer(1)
281 execution_count = Integer(1)
282 filename = Unicode("<ipython console>")
282 filename = Unicode("<ipython console>")
283 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
283 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
284
284
285 # Input splitter, to split entire cells of input into either individual
285 # Input splitter, to split entire cells of input into either individual
286 # interactive statements or whole blocks.
286 # interactive statements or whole blocks.
287 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
287 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
288 (), {})
288 (), {})
289 logstart = CBool(False, config=True, help=
289 logstart = CBool(False, config=True, help=
290 """
290 """
291 Start logging to the default log file.
291 Start logging to the default log file.
292 """
292 """
293 )
293 )
294 logfile = Unicode('', config=True, help=
294 logfile = Unicode('', config=True, help=
295 """
295 """
296 The name of the logfile to use.
296 The name of the logfile to use.
297 """
297 """
298 )
298 )
299 logappend = Unicode('', config=True, help=
299 logappend = Unicode('', config=True, help=
300 """
300 """
301 Start logging to the given file in append mode.
301 Start logging to the given file in append mode.
302 """
302 """
303 )
303 )
304 object_info_string_level = Enum((0,1,2), default_value=0,
304 object_info_string_level = Enum((0,1,2), default_value=0,
305 config=True)
305 config=True)
306 pdb = CBool(False, config=True, help=
306 pdb = CBool(False, config=True, help=
307 """
307 """
308 Automatically call the pdb debugger after every exception.
308 Automatically call the pdb debugger after every exception.
309 """
309 """
310 )
310 )
311 multiline_history = CBool(sys.platform != 'win32', config=True,
311 multiline_history = CBool(sys.platform != 'win32', config=True,
312 help="Save multi-line entries as one entry in readline history"
312 help="Save multi-line entries as one entry in readline history"
313 )
313 )
314
314
315 prompt_in1 = Unicode('In [\\#]: ', config=True)
315 prompt_in1 = Unicode('In [\\#]: ', config=True)
316 prompt_in2 = Unicode(' .\\D.: ', config=True)
316 prompt_in2 = Unicode(' .\\D.: ', config=True)
317 prompt_out = Unicode('Out[\\#]: ', config=True)
317 prompt_out = Unicode('Out[\\#]: ', config=True)
318 prompts_pad_left = CBool(True, config=True)
318 prompts_pad_left = CBool(True, config=True)
319 quiet = CBool(False, config=True)
319 quiet = CBool(False, config=True)
320
320
321 history_length = Integer(10000, config=True)
321 history_length = Integer(10000, config=True)
322
322
323 # The readline stuff will eventually be moved to the terminal subclass
323 # The readline stuff will eventually be moved to the terminal subclass
324 # but for now, we can't do that as readline is welded in everywhere.
324 # but for now, we can't do that as readline is welded in everywhere.
325 readline_use = CBool(True, config=True)
325 readline_use = CBool(True, config=True)
326 readline_remove_delims = Unicode('-/~', config=True)
326 readline_remove_delims = Unicode('-/~', config=True)
327 # don't use \M- bindings by default, because they
327 # don't use \M- bindings by default, because they
328 # conflict with 8-bit encodings. See gh-58,gh-88
328 # conflict with 8-bit encodings. See gh-58,gh-88
329 readline_parse_and_bind = List([
329 readline_parse_and_bind = List([
330 'tab: complete',
330 'tab: complete',
331 '"\C-l": clear-screen',
331 '"\C-l": clear-screen',
332 'set show-all-if-ambiguous on',
332 'set show-all-if-ambiguous on',
333 '"\C-o": tab-insert',
333 '"\C-o": tab-insert',
334 '"\C-r": reverse-search-history',
334 '"\C-r": reverse-search-history',
335 '"\C-s": forward-search-history',
335 '"\C-s": forward-search-history',
336 '"\C-p": history-search-backward',
336 '"\C-p": history-search-backward',
337 '"\C-n": history-search-forward',
337 '"\C-n": history-search-forward',
338 '"\e[A": history-search-backward',
338 '"\e[A": history-search-backward',
339 '"\e[B": history-search-forward',
339 '"\e[B": history-search-forward',
340 '"\C-k": kill-line',
340 '"\C-k": kill-line',
341 '"\C-u": unix-line-discard',
341 '"\C-u": unix-line-discard',
342 ], allow_none=False, config=True)
342 ], allow_none=False, config=True)
343
343
344 # TODO: this part of prompt management should be moved to the frontends.
344 # TODO: this part of prompt management should be moved to the frontends.
345 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
345 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
346 separate_in = SeparateUnicode('\n', config=True)
346 separate_in = SeparateUnicode('\n', config=True)
347 separate_out = SeparateUnicode('', config=True)
347 separate_out = SeparateUnicode('', config=True)
348 separate_out2 = SeparateUnicode('', config=True)
348 separate_out2 = SeparateUnicode('', config=True)
349 wildcards_case_sensitive = CBool(True, config=True)
349 wildcards_case_sensitive = CBool(True, config=True)
350 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
350 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
351 default_value='Context', config=True)
351 default_value='Context', config=True)
352
352
353 # Subcomponents of InteractiveShell
353 # Subcomponents of InteractiveShell
354 alias_manager = Instance('IPython.core.alias.AliasManager')
354 alias_manager = Instance('IPython.core.alias.AliasManager')
355 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
355 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
356 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
356 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
357 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
357 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
358 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
358 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
359 plugin_manager = Instance('IPython.core.plugin.PluginManager')
359 plugin_manager = Instance('IPython.core.plugin.PluginManager')
360 payload_manager = Instance('IPython.core.payload.PayloadManager')
360 payload_manager = Instance('IPython.core.payload.PayloadManager')
361 history_manager = Instance('IPython.core.history.HistoryManager')
361 history_manager = Instance('IPython.core.history.HistoryManager')
362
362
363 profile_dir = Instance('IPython.core.application.ProfileDir')
363 profile_dir = Instance('IPython.core.application.ProfileDir')
364 @property
364 @property
365 def profile(self):
365 def profile(self):
366 if self.profile_dir is not None:
366 if self.profile_dir is not None:
367 name = os.path.basename(self.profile_dir.location)
367 name = os.path.basename(self.profile_dir.location)
368 return name.replace('profile_','')
368 return name.replace('profile_','')
369
369
370
370
371 # Private interface
371 # Private interface
372 _post_execute = Instance(dict)
372 _post_execute = Instance(dict)
373
373
374 def __init__(self, config=None, ipython_dir=None, profile_dir=None,
374 def __init__(self, config=None, ipython_dir=None, profile_dir=None,
375 user_ns=None, user_global_ns=None,
375 user_ns=None, user_global_ns=None,
376 custom_exceptions=((), None)):
376 custom_exceptions=((), None)):
377
377
378 # This is where traits with a config_key argument are updated
378 # This is where traits with a config_key argument are updated
379 # from the values on config.
379 # from the values on config.
380 super(InteractiveShell, self).__init__(config=config)
380 super(InteractiveShell, self).__init__(config=config)
381 self.configurables = [self]
381 self.configurables = [self]
382
382
383 # These are relatively independent and stateless
383 # These are relatively independent and stateless
384 self.init_ipython_dir(ipython_dir)
384 self.init_ipython_dir(ipython_dir)
385 self.init_profile_dir(profile_dir)
385 self.init_profile_dir(profile_dir)
386 self.init_instance_attrs()
386 self.init_instance_attrs()
387 self.init_environment()
387 self.init_environment()
388
388
389 # Create namespaces (user_ns, user_global_ns, etc.)
389 # Create namespaces (user_ns, user_global_ns, etc.)
390 self.init_create_namespaces(user_ns, user_global_ns)
390 self.init_create_namespaces(user_ns, user_global_ns)
391 # This has to be done after init_create_namespaces because it uses
391 # This has to be done after init_create_namespaces because it uses
392 # something in self.user_ns, but before init_sys_modules, which
392 # something in self.user_ns, but before init_sys_modules, which
393 # is the first thing to modify sys.
393 # is the first thing to modify sys.
394 # TODO: When we override sys.stdout and sys.stderr before this class
394 # TODO: When we override sys.stdout and sys.stderr before this class
395 # is created, we are saving the overridden ones here. Not sure if this
395 # is created, we are saving the overridden ones here. Not sure if this
396 # is what we want to do.
396 # is what we want to do.
397 self.save_sys_module_state()
397 self.save_sys_module_state()
398 self.init_sys_modules()
398 self.init_sys_modules()
399
399
400 # While we're trying to have each part of the code directly access what
400 # While we're trying to have each part of the code directly access what
401 # it needs without keeping redundant references to objects, we have too
401 # it needs without keeping redundant references to objects, we have too
402 # much legacy code that expects ip.db to exist.
402 # much legacy code that expects ip.db to exist.
403 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
403 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
404
404
405 self.init_history()
405 self.init_history()
406 self.init_encoding()
406 self.init_encoding()
407 self.init_prefilter()
407 self.init_prefilter()
408
408
409 Magic.__init__(self, self)
409 Magic.__init__(self, self)
410
410
411 self.init_syntax_highlighting()
411 self.init_syntax_highlighting()
412 self.init_hooks()
412 self.init_hooks()
413 self.init_pushd_popd_magic()
413 self.init_pushd_popd_magic()
414 # self.init_traceback_handlers use to be here, but we moved it below
414 # self.init_traceback_handlers use to be here, but we moved it below
415 # because it and init_io have to come after init_readline.
415 # because it and init_io have to come after init_readline.
416 self.init_user_ns()
416 self.init_user_ns()
417 self.init_logger()
417 self.init_logger()
418 self.init_alias()
418 self.init_alias()
419 self.init_builtins()
419 self.init_builtins()
420
420
421 # pre_config_initialization
421 # pre_config_initialization
422
422
423 # The next section should contain everything that was in ipmaker.
423 # The next section should contain everything that was in ipmaker.
424 self.init_logstart()
424 self.init_logstart()
425
425
426 # The following was in post_config_initialization
426 # The following was in post_config_initialization
427 self.init_inspector()
427 self.init_inspector()
428 # init_readline() must come before init_io(), because init_io uses
428 # init_readline() must come before init_io(), because init_io uses
429 # readline related things.
429 # readline related things.
430 self.init_readline()
430 self.init_readline()
431 # We save this here in case user code replaces raw_input, but it needs
431 # We save this here in case user code replaces raw_input, but it needs
432 # to be after init_readline(), because PyPy's readline works by replacing
432 # to be after init_readline(), because PyPy's readline works by replacing
433 # raw_input.
433 # raw_input.
434 if py3compat.PY3:
434 if py3compat.PY3:
435 self.raw_input_original = input
435 self.raw_input_original = input
436 else:
436 else:
437 self.raw_input_original = raw_input
437 self.raw_input_original = raw_input
438 # init_completer must come after init_readline, because it needs to
438 # init_completer must come after init_readline, because it needs to
439 # know whether readline is present or not system-wide to configure the
439 # know whether readline is present or not system-wide to configure the
440 # completers, since the completion machinery can now operate
440 # completers, since the completion machinery can now operate
441 # independently of readline (e.g. over the network)
441 # independently of readline (e.g. over the network)
442 self.init_completer()
442 self.init_completer()
443 # TODO: init_io() needs to happen before init_traceback handlers
443 # TODO: init_io() needs to happen before init_traceback handlers
444 # because the traceback handlers hardcode the stdout/stderr streams.
444 # because the traceback handlers hardcode the stdout/stderr streams.
445 # This logic in in debugger.Pdb and should eventually be changed.
445 # This logic in in debugger.Pdb and should eventually be changed.
446 self.init_io()
446 self.init_io()
447 self.init_traceback_handlers(custom_exceptions)
447 self.init_traceback_handlers(custom_exceptions)
448 self.init_prompts()
448 self.init_prompts()
449 self.init_display_formatter()
449 self.init_display_formatter()
450 self.init_display_pub()
450 self.init_display_pub()
451 self.init_displayhook()
451 self.init_displayhook()
452 self.init_reload_doctest()
452 self.init_reload_doctest()
453 self.init_magics()
453 self.init_magics()
454 self.init_pdb()
454 self.init_pdb()
455 self.init_extension_manager()
455 self.init_extension_manager()
456 self.init_plugin_manager()
456 self.init_plugin_manager()
457 self.init_payload()
457 self.init_payload()
458 self.hooks.late_startup_hook()
458 self.hooks.late_startup_hook()
459 atexit.register(self.atexit_operations)
459 atexit.register(self.atexit_operations)
460
460
461 def get_ipython(self):
461 def get_ipython(self):
462 """Return the currently running IPython instance."""
462 """Return the currently running IPython instance."""
463 return self
463 return self
464
464
465 #-------------------------------------------------------------------------
465 #-------------------------------------------------------------------------
466 # Trait changed handlers
466 # Trait changed handlers
467 #-------------------------------------------------------------------------
467 #-------------------------------------------------------------------------
468
468
469 def _ipython_dir_changed(self, name, new):
469 def _ipython_dir_changed(self, name, new):
470 if not os.path.isdir(new):
470 if not os.path.isdir(new):
471 os.makedirs(new, mode = 0777)
471 os.makedirs(new, mode = 0777)
472
472
473 def set_autoindent(self,value=None):
473 def set_autoindent(self,value=None):
474 """Set the autoindent flag, checking for readline support.
474 """Set the autoindent flag, checking for readline support.
475
475
476 If called with no arguments, it acts as a toggle."""
476 If called with no arguments, it acts as a toggle."""
477
477
478 if value != 0 and not self.has_readline:
478 if value != 0 and not self.has_readline:
479 if os.name == 'posix':
479 if os.name == 'posix':
480 warn("The auto-indent feature requires the readline library")
480 warn("The auto-indent feature requires the readline library")
481 self.autoindent = 0
481 self.autoindent = 0
482 return
482 return
483 if value is None:
483 if value is None:
484 self.autoindent = not self.autoindent
484 self.autoindent = not self.autoindent
485 else:
485 else:
486 self.autoindent = value
486 self.autoindent = value
487
487
488 #-------------------------------------------------------------------------
488 #-------------------------------------------------------------------------
489 # init_* methods called by __init__
489 # init_* methods called by __init__
490 #-------------------------------------------------------------------------
490 #-------------------------------------------------------------------------
491
491
492 def init_ipython_dir(self, ipython_dir):
492 def init_ipython_dir(self, ipython_dir):
493 if ipython_dir is not None:
493 if ipython_dir is not None:
494 self.ipython_dir = ipython_dir
494 self.ipython_dir = ipython_dir
495 return
495 return
496
496
497 self.ipython_dir = get_ipython_dir()
497 self.ipython_dir = get_ipython_dir()
498
498
499 def init_profile_dir(self, profile_dir):
499 def init_profile_dir(self, profile_dir):
500 if profile_dir is not None:
500 if profile_dir is not None:
501 self.profile_dir = profile_dir
501 self.profile_dir = profile_dir
502 return
502 return
503 self.profile_dir =\
503 self.profile_dir =\
504 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
504 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
505
505
506 def init_instance_attrs(self):
506 def init_instance_attrs(self):
507 self.more = False
507 self.more = False
508
508
509 # command compiler
509 # command compiler
510 self.compile = CachingCompiler()
510 self.compile = CachingCompiler()
511
511
512 # Make an empty namespace, which extension writers can rely on both
512 # Make an empty namespace, which extension writers can rely on both
513 # existing and NEVER being used by ipython itself. This gives them a
513 # existing and NEVER being used by ipython itself. This gives them a
514 # convenient location for storing additional information and state
514 # convenient location for storing additional information and state
515 # their extensions may require, without fear of collisions with other
515 # their extensions may require, without fear of collisions with other
516 # ipython names that may develop later.
516 # ipython names that may develop later.
517 self.meta = Struct()
517 self.meta = Struct()
518
518
519 # Temporary files used for various purposes. Deleted at exit.
519 # Temporary files used for various purposes. Deleted at exit.
520 self.tempfiles = []
520 self.tempfiles = []
521
521
522 # Keep track of readline usage (later set by init_readline)
522 # Keep track of readline usage (later set by init_readline)
523 self.has_readline = False
523 self.has_readline = False
524
524
525 # keep track of where we started running (mainly for crash post-mortem)
525 # keep track of where we started running (mainly for crash post-mortem)
526 # This is not being used anywhere currently.
526 # This is not being used anywhere currently.
527 self.starting_dir = os.getcwdu()
527 self.starting_dir = os.getcwdu()
528
528
529 # Indentation management
529 # Indentation management
530 self.indent_current_nsp = 0
530 self.indent_current_nsp = 0
531
531
532 # Dict to track post-execution functions that have been registered
532 # Dict to track post-execution functions that have been registered
533 self._post_execute = {}
533 self._post_execute = {}
534
534
535 def init_environment(self):
535 def init_environment(self):
536 """Any changes we need to make to the user's environment."""
536 """Any changes we need to make to the user's environment."""
537 pass
537 pass
538
538
539 def init_encoding(self):
539 def init_encoding(self):
540 # Get system encoding at startup time. Certain terminals (like Emacs
540 # Get system encoding at startup time. Certain terminals (like Emacs
541 # under Win32 have it set to None, and we need to have a known valid
541 # under Win32 have it set to None, and we need to have a known valid
542 # encoding to use in the raw_input() method
542 # encoding to use in the raw_input() method
543 try:
543 try:
544 self.stdin_encoding = sys.stdin.encoding or 'ascii'
544 self.stdin_encoding = sys.stdin.encoding or 'ascii'
545 except AttributeError:
545 except AttributeError:
546 self.stdin_encoding = 'ascii'
546 self.stdin_encoding = 'ascii'
547
547
548 def init_syntax_highlighting(self):
548 def init_syntax_highlighting(self):
549 # Python source parser/formatter for syntax highlighting
549 # Python source parser/formatter for syntax highlighting
550 pyformat = PyColorize.Parser().format
550 pyformat = PyColorize.Parser().format
551 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
551 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
552
552
553 def init_pushd_popd_magic(self):
553 def init_pushd_popd_magic(self):
554 # for pushd/popd management
554 # for pushd/popd management
555 try:
555 self.home_dir = get_home_dir()
556 self.home_dir = get_home_dir()
557 except HomeDirError, msg:
558 fatal(msg)
559
556
560 self.dir_stack = []
557 self.dir_stack = []
561
558
562 def init_logger(self):
559 def init_logger(self):
563 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
560 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
564 logmode='rotate')
561 logmode='rotate')
565
562
566 def init_logstart(self):
563 def init_logstart(self):
567 """Initialize logging in case it was requested at the command line.
564 """Initialize logging in case it was requested at the command line.
568 """
565 """
569 if self.logappend:
566 if self.logappend:
570 self.magic_logstart(self.logappend + ' append')
567 self.magic_logstart(self.logappend + ' append')
571 elif self.logfile:
568 elif self.logfile:
572 self.magic_logstart(self.logfile)
569 self.magic_logstart(self.logfile)
573 elif self.logstart:
570 elif self.logstart:
574 self.magic_logstart()
571 self.magic_logstart()
575
572
576 def init_builtins(self):
573 def init_builtins(self):
577 self.builtin_trap = BuiltinTrap(shell=self)
574 self.builtin_trap = BuiltinTrap(shell=self)
578
575
579 def init_inspector(self):
576 def init_inspector(self):
580 # Object inspector
577 # Object inspector
581 self.inspector = oinspect.Inspector(oinspect.InspectColors,
578 self.inspector = oinspect.Inspector(oinspect.InspectColors,
582 PyColorize.ANSICodeColors,
579 PyColorize.ANSICodeColors,
583 'NoColor',
580 'NoColor',
584 self.object_info_string_level)
581 self.object_info_string_level)
585
582
586 def init_io(self):
583 def init_io(self):
587 # This will just use sys.stdout and sys.stderr. If you want to
584 # This will just use sys.stdout and sys.stderr. If you want to
588 # override sys.stdout and sys.stderr themselves, you need to do that
585 # override sys.stdout and sys.stderr themselves, you need to do that
589 # *before* instantiating this class, because io holds onto
586 # *before* instantiating this class, because io holds onto
590 # references to the underlying streams.
587 # references to the underlying streams.
591 if sys.platform == 'win32' and self.has_readline:
588 if sys.platform == 'win32' and self.has_readline:
592 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
589 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
593 else:
590 else:
594 io.stdout = io.IOStream(sys.stdout)
591 io.stdout = io.IOStream(sys.stdout)
595 io.stderr = io.IOStream(sys.stderr)
592 io.stderr = io.IOStream(sys.stderr)
596
593
597 def init_prompts(self):
594 def init_prompts(self):
598 # TODO: This is a pass for now because the prompts are managed inside
595 # TODO: This is a pass for now because the prompts are managed inside
599 # the DisplayHook. Once there is a separate prompt manager, this
596 # the DisplayHook. Once there is a separate prompt manager, this
600 # will initialize that object and all prompt related information.
597 # will initialize that object and all prompt related information.
601 pass
598 pass
602
599
603 def init_display_formatter(self):
600 def init_display_formatter(self):
604 self.display_formatter = DisplayFormatter(config=self.config)
601 self.display_formatter = DisplayFormatter(config=self.config)
605 self.configurables.append(self.display_formatter)
602 self.configurables.append(self.display_formatter)
606
603
607 def init_display_pub(self):
604 def init_display_pub(self):
608 self.display_pub = self.display_pub_class(config=self.config)
605 self.display_pub = self.display_pub_class(config=self.config)
609 self.configurables.append(self.display_pub)
606 self.configurables.append(self.display_pub)
610
607
611 def init_displayhook(self):
608 def init_displayhook(self):
612 # Initialize displayhook, set in/out prompts and printing system
609 # Initialize displayhook, set in/out prompts and printing system
613 self.displayhook = self.displayhook_class(
610 self.displayhook = self.displayhook_class(
614 config=self.config,
611 config=self.config,
615 shell=self,
612 shell=self,
616 cache_size=self.cache_size,
613 cache_size=self.cache_size,
617 input_sep = self.separate_in,
614 input_sep = self.separate_in,
618 output_sep = self.separate_out,
615 output_sep = self.separate_out,
619 output_sep2 = self.separate_out2,
616 output_sep2 = self.separate_out2,
620 ps1 = self.prompt_in1,
617 ps1 = self.prompt_in1,
621 ps2 = self.prompt_in2,
618 ps2 = self.prompt_in2,
622 ps_out = self.prompt_out,
619 ps_out = self.prompt_out,
623 pad_left = self.prompts_pad_left
620 pad_left = self.prompts_pad_left
624 )
621 )
625 self.configurables.append(self.displayhook)
622 self.configurables.append(self.displayhook)
626 # This is a context manager that installs/revmoes the displayhook at
623 # This is a context manager that installs/revmoes the displayhook at
627 # the appropriate time.
624 # the appropriate time.
628 self.display_trap = DisplayTrap(hook=self.displayhook)
625 self.display_trap = DisplayTrap(hook=self.displayhook)
629
626
630 def init_reload_doctest(self):
627 def init_reload_doctest(self):
631 # Do a proper resetting of doctest, including the necessary displayhook
628 # Do a proper resetting of doctest, including the necessary displayhook
632 # monkeypatching
629 # monkeypatching
633 try:
630 try:
634 doctest_reload()
631 doctest_reload()
635 except ImportError:
632 except ImportError:
636 warn("doctest module does not exist.")
633 warn("doctest module does not exist.")
637
634
638 #-------------------------------------------------------------------------
635 #-------------------------------------------------------------------------
639 # Things related to injections into the sys module
636 # Things related to injections into the sys module
640 #-------------------------------------------------------------------------
637 #-------------------------------------------------------------------------
641
638
642 def save_sys_module_state(self):
639 def save_sys_module_state(self):
643 """Save the state of hooks in the sys module.
640 """Save the state of hooks in the sys module.
644
641
645 This has to be called after self.user_ns is created.
642 This has to be called after self.user_ns is created.
646 """
643 """
647 self._orig_sys_module_state = {}
644 self._orig_sys_module_state = {}
648 self._orig_sys_module_state['stdin'] = sys.stdin
645 self._orig_sys_module_state['stdin'] = sys.stdin
649 self._orig_sys_module_state['stdout'] = sys.stdout
646 self._orig_sys_module_state['stdout'] = sys.stdout
650 self._orig_sys_module_state['stderr'] = sys.stderr
647 self._orig_sys_module_state['stderr'] = sys.stderr
651 self._orig_sys_module_state['excepthook'] = sys.excepthook
648 self._orig_sys_module_state['excepthook'] = sys.excepthook
652 try:
649 try:
653 self._orig_sys_modules_main_name = self.user_ns['__name__']
650 self._orig_sys_modules_main_name = self.user_ns['__name__']
654 except KeyError:
651 except KeyError:
655 pass
652 pass
656
653
657 def restore_sys_module_state(self):
654 def restore_sys_module_state(self):
658 """Restore the state of the sys module."""
655 """Restore the state of the sys module."""
659 try:
656 try:
660 for k, v in self._orig_sys_module_state.iteritems():
657 for k, v in self._orig_sys_module_state.iteritems():
661 setattr(sys, k, v)
658 setattr(sys, k, v)
662 except AttributeError:
659 except AttributeError:
663 pass
660 pass
664 # Reset what what done in self.init_sys_modules
661 # Reset what what done in self.init_sys_modules
665 try:
662 try:
666 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
663 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
667 except (AttributeError, KeyError):
664 except (AttributeError, KeyError):
668 pass
665 pass
669
666
670 #-------------------------------------------------------------------------
667 #-------------------------------------------------------------------------
671 # Things related to hooks
668 # Things related to hooks
672 #-------------------------------------------------------------------------
669 #-------------------------------------------------------------------------
673
670
674 def init_hooks(self):
671 def init_hooks(self):
675 # hooks holds pointers used for user-side customizations
672 # hooks holds pointers used for user-side customizations
676 self.hooks = Struct()
673 self.hooks = Struct()
677
674
678 self.strdispatchers = {}
675 self.strdispatchers = {}
679
676
680 # Set all default hooks, defined in the IPython.hooks module.
677 # Set all default hooks, defined in the IPython.hooks module.
681 hooks = IPython.core.hooks
678 hooks = IPython.core.hooks
682 for hook_name in hooks.__all__:
679 for hook_name in hooks.__all__:
683 # default hooks have priority 100, i.e. low; user hooks should have
680 # default hooks have priority 100, i.e. low; user hooks should have
684 # 0-100 priority
681 # 0-100 priority
685 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
682 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
686
683
687 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
684 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
688 """set_hook(name,hook) -> sets an internal IPython hook.
685 """set_hook(name,hook) -> sets an internal IPython hook.
689
686
690 IPython exposes some of its internal API as user-modifiable hooks. By
687 IPython exposes some of its internal API as user-modifiable hooks. By
691 adding your function to one of these hooks, you can modify IPython's
688 adding your function to one of these hooks, you can modify IPython's
692 behavior to call at runtime your own routines."""
689 behavior to call at runtime your own routines."""
693
690
694 # At some point in the future, this should validate the hook before it
691 # At some point in the future, this should validate the hook before it
695 # accepts it. Probably at least check that the hook takes the number
692 # accepts it. Probably at least check that the hook takes the number
696 # of args it's supposed to.
693 # of args it's supposed to.
697
694
698 f = types.MethodType(hook,self)
695 f = types.MethodType(hook,self)
699
696
700 # check if the hook is for strdispatcher first
697 # check if the hook is for strdispatcher first
701 if str_key is not None:
698 if str_key is not None:
702 sdp = self.strdispatchers.get(name, StrDispatch())
699 sdp = self.strdispatchers.get(name, StrDispatch())
703 sdp.add_s(str_key, f, priority )
700 sdp.add_s(str_key, f, priority )
704 self.strdispatchers[name] = sdp
701 self.strdispatchers[name] = sdp
705 return
702 return
706 if re_key is not None:
703 if re_key is not None:
707 sdp = self.strdispatchers.get(name, StrDispatch())
704 sdp = self.strdispatchers.get(name, StrDispatch())
708 sdp.add_re(re.compile(re_key), f, priority )
705 sdp.add_re(re.compile(re_key), f, priority )
709 self.strdispatchers[name] = sdp
706 self.strdispatchers[name] = sdp
710 return
707 return
711
708
712 dp = getattr(self.hooks, name, None)
709 dp = getattr(self.hooks, name, None)
713 if name not in IPython.core.hooks.__all__:
710 if name not in IPython.core.hooks.__all__:
714 print "Warning! Hook '%s' is not one of %s" % \
711 print "Warning! Hook '%s' is not one of %s" % \
715 (name, IPython.core.hooks.__all__ )
712 (name, IPython.core.hooks.__all__ )
716 if not dp:
713 if not dp:
717 dp = IPython.core.hooks.CommandChainDispatcher()
714 dp = IPython.core.hooks.CommandChainDispatcher()
718
715
719 try:
716 try:
720 dp.add(f,priority)
717 dp.add(f,priority)
721 except AttributeError:
718 except AttributeError:
722 # it was not commandchain, plain old func - replace
719 # it was not commandchain, plain old func - replace
723 dp = f
720 dp = f
724
721
725 setattr(self.hooks,name, dp)
722 setattr(self.hooks,name, dp)
726
723
727 def register_post_execute(self, func):
724 def register_post_execute(self, func):
728 """Register a function for calling after code execution.
725 """Register a function for calling after code execution.
729 """
726 """
730 if not callable(func):
727 if not callable(func):
731 raise ValueError('argument %s must be callable' % func)
728 raise ValueError('argument %s must be callable' % func)
732 self._post_execute[func] = True
729 self._post_execute[func] = True
733
730
734 #-------------------------------------------------------------------------
731 #-------------------------------------------------------------------------
735 # Things related to the "main" module
732 # Things related to the "main" module
736 #-------------------------------------------------------------------------
733 #-------------------------------------------------------------------------
737
734
738 def new_main_mod(self,ns=None):
735 def new_main_mod(self,ns=None):
739 """Return a new 'main' module object for user code execution.
736 """Return a new 'main' module object for user code execution.
740 """
737 """
741 main_mod = self._user_main_module
738 main_mod = self._user_main_module
742 init_fakemod_dict(main_mod,ns)
739 init_fakemod_dict(main_mod,ns)
743 return main_mod
740 return main_mod
744
741
745 def cache_main_mod(self,ns,fname):
742 def cache_main_mod(self,ns,fname):
746 """Cache a main module's namespace.
743 """Cache a main module's namespace.
747
744
748 When scripts are executed via %run, we must keep a reference to the
745 When scripts are executed via %run, we must keep a reference to the
749 namespace of their __main__ module (a FakeModule instance) around so
746 namespace of their __main__ module (a FakeModule instance) around so
750 that Python doesn't clear it, rendering objects defined therein
747 that Python doesn't clear it, rendering objects defined therein
751 useless.
748 useless.
752
749
753 This method keeps said reference in a private dict, keyed by the
750 This method keeps said reference in a private dict, keyed by the
754 absolute path of the module object (which corresponds to the script
751 absolute path of the module object (which corresponds to the script
755 path). This way, for multiple executions of the same script we only
752 path). This way, for multiple executions of the same script we only
756 keep one copy of the namespace (the last one), thus preventing memory
753 keep one copy of the namespace (the last one), thus preventing memory
757 leaks from old references while allowing the objects from the last
754 leaks from old references while allowing the objects from the last
758 execution to be accessible.
755 execution to be accessible.
759
756
760 Note: we can not allow the actual FakeModule instances to be deleted,
757 Note: we can not allow the actual FakeModule instances to be deleted,
761 because of how Python tears down modules (it hard-sets all their
758 because of how Python tears down modules (it hard-sets all their
762 references to None without regard for reference counts). This method
759 references to None without regard for reference counts). This method
763 must therefore make a *copy* of the given namespace, to allow the
760 must therefore make a *copy* of the given namespace, to allow the
764 original module's __dict__ to be cleared and reused.
761 original module's __dict__ to be cleared and reused.
765
762
766
763
767 Parameters
764 Parameters
768 ----------
765 ----------
769 ns : a namespace (a dict, typically)
766 ns : a namespace (a dict, typically)
770
767
771 fname : str
768 fname : str
772 Filename associated with the namespace.
769 Filename associated with the namespace.
773
770
774 Examples
771 Examples
775 --------
772 --------
776
773
777 In [10]: import IPython
774 In [10]: import IPython
778
775
779 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
776 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
780
777
781 In [12]: IPython.__file__ in _ip._main_ns_cache
778 In [12]: IPython.__file__ in _ip._main_ns_cache
782 Out[12]: True
779 Out[12]: True
783 """
780 """
784 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
781 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
785
782
786 def clear_main_mod_cache(self):
783 def clear_main_mod_cache(self):
787 """Clear the cache of main modules.
784 """Clear the cache of main modules.
788
785
789 Mainly for use by utilities like %reset.
786 Mainly for use by utilities like %reset.
790
787
791 Examples
788 Examples
792 --------
789 --------
793
790
794 In [15]: import IPython
791 In [15]: import IPython
795
792
796 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
793 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
797
794
798 In [17]: len(_ip._main_ns_cache) > 0
795 In [17]: len(_ip._main_ns_cache) > 0
799 Out[17]: True
796 Out[17]: True
800
797
801 In [18]: _ip.clear_main_mod_cache()
798 In [18]: _ip.clear_main_mod_cache()
802
799
803 In [19]: len(_ip._main_ns_cache) == 0
800 In [19]: len(_ip._main_ns_cache) == 0
804 Out[19]: True
801 Out[19]: True
805 """
802 """
806 self._main_ns_cache.clear()
803 self._main_ns_cache.clear()
807
804
808 #-------------------------------------------------------------------------
805 #-------------------------------------------------------------------------
809 # Things related to debugging
806 # Things related to debugging
810 #-------------------------------------------------------------------------
807 #-------------------------------------------------------------------------
811
808
812 def init_pdb(self):
809 def init_pdb(self):
813 # Set calling of pdb on exceptions
810 # Set calling of pdb on exceptions
814 # self.call_pdb is a property
811 # self.call_pdb is a property
815 self.call_pdb = self.pdb
812 self.call_pdb = self.pdb
816
813
817 def _get_call_pdb(self):
814 def _get_call_pdb(self):
818 return self._call_pdb
815 return self._call_pdb
819
816
820 def _set_call_pdb(self,val):
817 def _set_call_pdb(self,val):
821
818
822 if val not in (0,1,False,True):
819 if val not in (0,1,False,True):
823 raise ValueError,'new call_pdb value must be boolean'
820 raise ValueError,'new call_pdb value must be boolean'
824
821
825 # store value in instance
822 # store value in instance
826 self._call_pdb = val
823 self._call_pdb = val
827
824
828 # notify the actual exception handlers
825 # notify the actual exception handlers
829 self.InteractiveTB.call_pdb = val
826 self.InteractiveTB.call_pdb = val
830
827
831 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
828 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
832 'Control auto-activation of pdb at exceptions')
829 'Control auto-activation of pdb at exceptions')
833
830
834 def debugger(self,force=False):
831 def debugger(self,force=False):
835 """Call the pydb/pdb debugger.
832 """Call the pydb/pdb debugger.
836
833
837 Keywords:
834 Keywords:
838
835
839 - force(False): by default, this routine checks the instance call_pdb
836 - force(False): by default, this routine checks the instance call_pdb
840 flag and does not actually invoke the debugger if the flag is false.
837 flag and does not actually invoke the debugger if the flag is false.
841 The 'force' option forces the debugger to activate even if the flag
838 The 'force' option forces the debugger to activate even if the flag
842 is false.
839 is false.
843 """
840 """
844
841
845 if not (force or self.call_pdb):
842 if not (force or self.call_pdb):
846 return
843 return
847
844
848 if not hasattr(sys,'last_traceback'):
845 if not hasattr(sys,'last_traceback'):
849 error('No traceback has been produced, nothing to debug.')
846 error('No traceback has been produced, nothing to debug.')
850 return
847 return
851
848
852 # use pydb if available
849 # use pydb if available
853 if debugger.has_pydb:
850 if debugger.has_pydb:
854 from pydb import pm
851 from pydb import pm
855 else:
852 else:
856 # fallback to our internal debugger
853 # fallback to our internal debugger
857 pm = lambda : self.InteractiveTB.debugger(force=True)
854 pm = lambda : self.InteractiveTB.debugger(force=True)
858
855
859 with self.readline_no_record:
856 with self.readline_no_record:
860 pm()
857 pm()
861
858
862 #-------------------------------------------------------------------------
859 #-------------------------------------------------------------------------
863 # Things related to IPython's various namespaces
860 # Things related to IPython's various namespaces
864 #-------------------------------------------------------------------------
861 #-------------------------------------------------------------------------
865
862
866 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
863 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
867 # Create the namespace where the user will operate. user_ns is
864 # Create the namespace where the user will operate. user_ns is
868 # normally the only one used, and it is passed to the exec calls as
865 # normally the only one used, and it is passed to the exec calls as
869 # the locals argument. But we do carry a user_global_ns namespace
866 # the locals argument. But we do carry a user_global_ns namespace
870 # given as the exec 'globals' argument, This is useful in embedding
867 # given as the exec 'globals' argument, This is useful in embedding
871 # situations where the ipython shell opens in a context where the
868 # situations where the ipython shell opens in a context where the
872 # distinction between locals and globals is meaningful. For
869 # distinction between locals and globals is meaningful. For
873 # non-embedded contexts, it is just the same object as the user_ns dict.
870 # non-embedded contexts, it is just the same object as the user_ns dict.
874
871
875 # FIXME. For some strange reason, __builtins__ is showing up at user
872 # FIXME. For some strange reason, __builtins__ is showing up at user
876 # level as a dict instead of a module. This is a manual fix, but I
873 # level as a dict instead of a module. This is a manual fix, but I
877 # should really track down where the problem is coming from. Alex
874 # should really track down where the problem is coming from. Alex
878 # Schmolck reported this problem first.
875 # Schmolck reported this problem first.
879
876
880 # A useful post by Alex Martelli on this topic:
877 # A useful post by Alex Martelli on this topic:
881 # Re: inconsistent value from __builtins__
878 # Re: inconsistent value from __builtins__
882 # Von: Alex Martelli <aleaxit@yahoo.com>
879 # Von: Alex Martelli <aleaxit@yahoo.com>
883 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
880 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
884 # Gruppen: comp.lang.python
881 # Gruppen: comp.lang.python
885
882
886 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
883 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
887 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
884 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
888 # > <type 'dict'>
885 # > <type 'dict'>
889 # > >>> print type(__builtins__)
886 # > >>> print type(__builtins__)
890 # > <type 'module'>
887 # > <type 'module'>
891 # > Is this difference in return value intentional?
888 # > Is this difference in return value intentional?
892
889
893 # Well, it's documented that '__builtins__' can be either a dictionary
890 # Well, it's documented that '__builtins__' can be either a dictionary
894 # or a module, and it's been that way for a long time. Whether it's
891 # or a module, and it's been that way for a long time. Whether it's
895 # intentional (or sensible), I don't know. In any case, the idea is
892 # intentional (or sensible), I don't know. In any case, the idea is
896 # that if you need to access the built-in namespace directly, you
893 # that if you need to access the built-in namespace directly, you
897 # should start with "import __builtin__" (note, no 's') which will
894 # should start with "import __builtin__" (note, no 's') which will
898 # definitely give you a module. Yeah, it's somewhat confusing:-(.
895 # definitely give you a module. Yeah, it's somewhat confusing:-(.
899
896
900 # These routines return properly built dicts as needed by the rest of
897 # These routines return properly built dicts as needed by the rest of
901 # the code, and can also be used by extension writers to generate
898 # the code, and can also be used by extension writers to generate
902 # properly initialized namespaces.
899 # properly initialized namespaces.
903 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
900 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
904 user_global_ns)
901 user_global_ns)
905
902
906 # Assign namespaces
903 # Assign namespaces
907 # This is the namespace where all normal user variables live
904 # This is the namespace where all normal user variables live
908 self.user_ns = user_ns
905 self.user_ns = user_ns
909 self.user_global_ns = user_global_ns
906 self.user_global_ns = user_global_ns
910
907
911 # An auxiliary namespace that checks what parts of the user_ns were
908 # An auxiliary namespace that checks what parts of the user_ns were
912 # loaded at startup, so we can list later only variables defined in
909 # loaded at startup, so we can list later only variables defined in
913 # actual interactive use. Since it is always a subset of user_ns, it
910 # actual interactive use. Since it is always a subset of user_ns, it
914 # doesn't need to be separately tracked in the ns_table.
911 # doesn't need to be separately tracked in the ns_table.
915 self.user_ns_hidden = {}
912 self.user_ns_hidden = {}
916
913
917 # A namespace to keep track of internal data structures to prevent
914 # A namespace to keep track of internal data structures to prevent
918 # them from cluttering user-visible stuff. Will be updated later
915 # them from cluttering user-visible stuff. Will be updated later
919 self.internal_ns = {}
916 self.internal_ns = {}
920
917
921 # Now that FakeModule produces a real module, we've run into a nasty
918 # Now that FakeModule produces a real module, we've run into a nasty
922 # problem: after script execution (via %run), the module where the user
919 # problem: after script execution (via %run), the module where the user
923 # code ran is deleted. Now that this object is a true module (needed
920 # code ran is deleted. Now that this object is a true module (needed
924 # so docetst and other tools work correctly), the Python module
921 # so docetst and other tools work correctly), the Python module
925 # teardown mechanism runs over it, and sets to None every variable
922 # teardown mechanism runs over it, and sets to None every variable
926 # present in that module. Top-level references to objects from the
923 # present in that module. Top-level references to objects from the
927 # script survive, because the user_ns is updated with them. However,
924 # script survive, because the user_ns is updated with them. However,
928 # calling functions defined in the script that use other things from
925 # calling functions defined in the script that use other things from
929 # the script will fail, because the function's closure had references
926 # the script will fail, because the function's closure had references
930 # to the original objects, which are now all None. So we must protect
927 # to the original objects, which are now all None. So we must protect
931 # these modules from deletion by keeping a cache.
928 # these modules from deletion by keeping a cache.
932 #
929 #
933 # To avoid keeping stale modules around (we only need the one from the
930 # To avoid keeping stale modules around (we only need the one from the
934 # last run), we use a dict keyed with the full path to the script, so
931 # last run), we use a dict keyed with the full path to the script, so
935 # only the last version of the module is held in the cache. Note,
932 # only the last version of the module is held in the cache. Note,
936 # however, that we must cache the module *namespace contents* (their
933 # however, that we must cache the module *namespace contents* (their
937 # __dict__). Because if we try to cache the actual modules, old ones
934 # __dict__). Because if we try to cache the actual modules, old ones
938 # (uncached) could be destroyed while still holding references (such as
935 # (uncached) could be destroyed while still holding references (such as
939 # those held by GUI objects that tend to be long-lived)>
936 # those held by GUI objects that tend to be long-lived)>
940 #
937 #
941 # The %reset command will flush this cache. See the cache_main_mod()
938 # The %reset command will flush this cache. See the cache_main_mod()
942 # and clear_main_mod_cache() methods for details on use.
939 # and clear_main_mod_cache() methods for details on use.
943
940
944 # This is the cache used for 'main' namespaces
941 # This is the cache used for 'main' namespaces
945 self._main_ns_cache = {}
942 self._main_ns_cache = {}
946 # And this is the single instance of FakeModule whose __dict__ we keep
943 # And this is the single instance of FakeModule whose __dict__ we keep
947 # copying and clearing for reuse on each %run
944 # copying and clearing for reuse on each %run
948 self._user_main_module = FakeModule()
945 self._user_main_module = FakeModule()
949
946
950 # A table holding all the namespaces IPython deals with, so that
947 # A table holding all the namespaces IPython deals with, so that
951 # introspection facilities can search easily.
948 # introspection facilities can search easily.
952 self.ns_table = {'user':user_ns,
949 self.ns_table = {'user':user_ns,
953 'user_global':user_global_ns,
950 'user_global':user_global_ns,
954 'internal':self.internal_ns,
951 'internal':self.internal_ns,
955 'builtin':builtin_mod.__dict__
952 'builtin':builtin_mod.__dict__
956 }
953 }
957
954
958 # Similarly, track all namespaces where references can be held and that
955 # Similarly, track all namespaces where references can be held and that
959 # we can safely clear (so it can NOT include builtin). This one can be
956 # we can safely clear (so it can NOT include builtin). This one can be
960 # a simple list. Note that the main execution namespaces, user_ns and
957 # a simple list. Note that the main execution namespaces, user_ns and
961 # user_global_ns, can NOT be listed here, as clearing them blindly
958 # user_global_ns, can NOT be listed here, as clearing them blindly
962 # causes errors in object __del__ methods. Instead, the reset() method
959 # causes errors in object __del__ methods. Instead, the reset() method
963 # clears them manually and carefully.
960 # clears them manually and carefully.
964 self.ns_refs_table = [ self.user_ns_hidden,
961 self.ns_refs_table = [ self.user_ns_hidden,
965 self.internal_ns, self._main_ns_cache ]
962 self.internal_ns, self._main_ns_cache ]
966
963
967 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
964 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
968 """Return a valid local and global user interactive namespaces.
965 """Return a valid local and global user interactive namespaces.
969
966
970 This builds a dict with the minimal information needed to operate as a
967 This builds a dict with the minimal information needed to operate as a
971 valid IPython user namespace, which you can pass to the various
968 valid IPython user namespace, which you can pass to the various
972 embedding classes in ipython. The default implementation returns the
969 embedding classes in ipython. The default implementation returns the
973 same dict for both the locals and the globals to allow functions to
970 same dict for both the locals and the globals to allow functions to
974 refer to variables in the namespace. Customized implementations can
971 refer to variables in the namespace. Customized implementations can
975 return different dicts. The locals dictionary can actually be anything
972 return different dicts. The locals dictionary can actually be anything
976 following the basic mapping protocol of a dict, but the globals dict
973 following the basic mapping protocol of a dict, but the globals dict
977 must be a true dict, not even a subclass. It is recommended that any
974 must be a true dict, not even a subclass. It is recommended that any
978 custom object for the locals namespace synchronize with the globals
975 custom object for the locals namespace synchronize with the globals
979 dict somehow.
976 dict somehow.
980
977
981 Raises TypeError if the provided globals namespace is not a true dict.
978 Raises TypeError if the provided globals namespace is not a true dict.
982
979
983 Parameters
980 Parameters
984 ----------
981 ----------
985 user_ns : dict-like, optional
982 user_ns : dict-like, optional
986 The current user namespace. The items in this namespace should
983 The current user namespace. The items in this namespace should
987 be included in the output. If None, an appropriate blank
984 be included in the output. If None, an appropriate blank
988 namespace should be created.
985 namespace should be created.
989 user_global_ns : dict, optional
986 user_global_ns : dict, optional
990 The current user global namespace. The items in this namespace
987 The current user global namespace. The items in this namespace
991 should be included in the output. If None, an appropriate
988 should be included in the output. If None, an appropriate
992 blank namespace should be created.
989 blank namespace should be created.
993
990
994 Returns
991 Returns
995 -------
992 -------
996 A pair of dictionary-like object to be used as the local namespace
993 A pair of dictionary-like object to be used as the local namespace
997 of the interpreter and a dict to be used as the global namespace.
994 of the interpreter and a dict to be used as the global namespace.
998 """
995 """
999
996
1000
997
1001 # We must ensure that __builtin__ (without the final 's') is always
998 # We must ensure that __builtin__ (without the final 's') is always
1002 # available and pointing to the __builtin__ *module*. For more details:
999 # available and pointing to the __builtin__ *module*. For more details:
1003 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1000 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1004
1001
1005 if user_ns is None:
1002 if user_ns is None:
1006 # Set __name__ to __main__ to better match the behavior of the
1003 # Set __name__ to __main__ to better match the behavior of the
1007 # normal interpreter.
1004 # normal interpreter.
1008 user_ns = {'__name__' :'__main__',
1005 user_ns = {'__name__' :'__main__',
1009 py3compat.builtin_mod_name: builtin_mod,
1006 py3compat.builtin_mod_name: builtin_mod,
1010 '__builtins__' : builtin_mod,
1007 '__builtins__' : builtin_mod,
1011 }
1008 }
1012 else:
1009 else:
1013 user_ns.setdefault('__name__','__main__')
1010 user_ns.setdefault('__name__','__main__')
1014 user_ns.setdefault(py3compat.builtin_mod_name,builtin_mod)
1011 user_ns.setdefault(py3compat.builtin_mod_name,builtin_mod)
1015 user_ns.setdefault('__builtins__',builtin_mod)
1012 user_ns.setdefault('__builtins__',builtin_mod)
1016
1013
1017 if user_global_ns is None:
1014 if user_global_ns is None:
1018 user_global_ns = user_ns
1015 user_global_ns = user_ns
1019 if type(user_global_ns) is not dict:
1016 if type(user_global_ns) is not dict:
1020 raise TypeError("user_global_ns must be a true dict; got %r"
1017 raise TypeError("user_global_ns must be a true dict; got %r"
1021 % type(user_global_ns))
1018 % type(user_global_ns))
1022
1019
1023 return user_ns, user_global_ns
1020 return user_ns, user_global_ns
1024
1021
1025 def init_sys_modules(self):
1022 def init_sys_modules(self):
1026 # We need to insert into sys.modules something that looks like a
1023 # We need to insert into sys.modules something that looks like a
1027 # module but which accesses the IPython namespace, for shelve and
1024 # module but which accesses the IPython namespace, for shelve and
1028 # pickle to work interactively. Normally they rely on getting
1025 # pickle to work interactively. Normally they rely on getting
1029 # everything out of __main__, but for embedding purposes each IPython
1026 # everything out of __main__, but for embedding purposes each IPython
1030 # instance has its own private namespace, so we can't go shoving
1027 # instance has its own private namespace, so we can't go shoving
1031 # everything into __main__.
1028 # everything into __main__.
1032
1029
1033 # note, however, that we should only do this for non-embedded
1030 # note, however, that we should only do this for non-embedded
1034 # ipythons, which really mimic the __main__.__dict__ with their own
1031 # ipythons, which really mimic the __main__.__dict__ with their own
1035 # namespace. Embedded instances, on the other hand, should not do
1032 # namespace. Embedded instances, on the other hand, should not do
1036 # this because they need to manage the user local/global namespaces
1033 # this because they need to manage the user local/global namespaces
1037 # only, but they live within a 'normal' __main__ (meaning, they
1034 # only, but they live within a 'normal' __main__ (meaning, they
1038 # shouldn't overtake the execution environment of the script they're
1035 # shouldn't overtake the execution environment of the script they're
1039 # embedded in).
1036 # embedded in).
1040
1037
1041 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1038 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1042
1039
1043 try:
1040 try:
1044 main_name = self.user_ns['__name__']
1041 main_name = self.user_ns['__name__']
1045 except KeyError:
1042 except KeyError:
1046 raise KeyError('user_ns dictionary MUST have a "__name__" key')
1043 raise KeyError('user_ns dictionary MUST have a "__name__" key')
1047 else:
1044 else:
1048 sys.modules[main_name] = FakeModule(self.user_ns)
1045 sys.modules[main_name] = FakeModule(self.user_ns)
1049
1046
1050 def init_user_ns(self):
1047 def init_user_ns(self):
1051 """Initialize all user-visible namespaces to their minimum defaults.
1048 """Initialize all user-visible namespaces to their minimum defaults.
1052
1049
1053 Certain history lists are also initialized here, as they effectively
1050 Certain history lists are also initialized here, as they effectively
1054 act as user namespaces.
1051 act as user namespaces.
1055
1052
1056 Notes
1053 Notes
1057 -----
1054 -----
1058 All data structures here are only filled in, they are NOT reset by this
1055 All data structures here are only filled in, they are NOT reset by this
1059 method. If they were not empty before, data will simply be added to
1056 method. If they were not empty before, data will simply be added to
1060 therm.
1057 therm.
1061 """
1058 """
1062 # This function works in two parts: first we put a few things in
1059 # This function works in two parts: first we put a few things in
1063 # user_ns, and we sync that contents into user_ns_hidden so that these
1060 # user_ns, and we sync that contents into user_ns_hidden so that these
1064 # initial variables aren't shown by %who. After the sync, we add the
1061 # initial variables aren't shown by %who. After the sync, we add the
1065 # rest of what we *do* want the user to see with %who even on a new
1062 # rest of what we *do* want the user to see with %who even on a new
1066 # session (probably nothing, so theye really only see their own stuff)
1063 # session (probably nothing, so theye really only see their own stuff)
1067
1064
1068 # The user dict must *always* have a __builtin__ reference to the
1065 # The user dict must *always* have a __builtin__ reference to the
1069 # Python standard __builtin__ namespace, which must be imported.
1066 # Python standard __builtin__ namespace, which must be imported.
1070 # This is so that certain operations in prompt evaluation can be
1067 # This is so that certain operations in prompt evaluation can be
1071 # reliably executed with builtins. Note that we can NOT use
1068 # reliably executed with builtins. Note that we can NOT use
1072 # __builtins__ (note the 's'), because that can either be a dict or a
1069 # __builtins__ (note the 's'), because that can either be a dict or a
1073 # module, and can even mutate at runtime, depending on the context
1070 # module, and can even mutate at runtime, depending on the context
1074 # (Python makes no guarantees on it). In contrast, __builtin__ is
1071 # (Python makes no guarantees on it). In contrast, __builtin__ is
1075 # always a module object, though it must be explicitly imported.
1072 # always a module object, though it must be explicitly imported.
1076
1073
1077 # For more details:
1074 # For more details:
1078 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1075 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1079 ns = dict(__builtin__ = builtin_mod)
1076 ns = dict(__builtin__ = builtin_mod)
1080
1077
1081 # Put 'help' in the user namespace
1078 # Put 'help' in the user namespace
1082 try:
1079 try:
1083 from site import _Helper
1080 from site import _Helper
1084 ns['help'] = _Helper()
1081 ns['help'] = _Helper()
1085 except ImportError:
1082 except ImportError:
1086 warn('help() not available - check site.py')
1083 warn('help() not available - check site.py')
1087
1084
1088 # make global variables for user access to the histories
1085 # make global variables for user access to the histories
1089 ns['_ih'] = self.history_manager.input_hist_parsed
1086 ns['_ih'] = self.history_manager.input_hist_parsed
1090 ns['_oh'] = self.history_manager.output_hist
1087 ns['_oh'] = self.history_manager.output_hist
1091 ns['_dh'] = self.history_manager.dir_hist
1088 ns['_dh'] = self.history_manager.dir_hist
1092
1089
1093 ns['_sh'] = shadowns
1090 ns['_sh'] = shadowns
1094
1091
1095 # user aliases to input and output histories. These shouldn't show up
1092 # user aliases to input and output histories. These shouldn't show up
1096 # in %who, as they can have very large reprs.
1093 # in %who, as they can have very large reprs.
1097 ns['In'] = self.history_manager.input_hist_parsed
1094 ns['In'] = self.history_manager.input_hist_parsed
1098 ns['Out'] = self.history_manager.output_hist
1095 ns['Out'] = self.history_manager.output_hist
1099
1096
1100 # Store myself as the public api!!!
1097 # Store myself as the public api!!!
1101 ns['get_ipython'] = self.get_ipython
1098 ns['get_ipython'] = self.get_ipython
1102
1099
1103 ns['exit'] = self.exiter
1100 ns['exit'] = self.exiter
1104 ns['quit'] = self.exiter
1101 ns['quit'] = self.exiter
1105
1102
1106 # Sync what we've added so far to user_ns_hidden so these aren't seen
1103 # Sync what we've added so far to user_ns_hidden so these aren't seen
1107 # by %who
1104 # by %who
1108 self.user_ns_hidden.update(ns)
1105 self.user_ns_hidden.update(ns)
1109
1106
1110 # Anything put into ns now would show up in %who. Think twice before
1107 # Anything put into ns now would show up in %who. Think twice before
1111 # putting anything here, as we really want %who to show the user their
1108 # putting anything here, as we really want %who to show the user their
1112 # stuff, not our variables.
1109 # stuff, not our variables.
1113
1110
1114 # Finally, update the real user's namespace
1111 # Finally, update the real user's namespace
1115 self.user_ns.update(ns)
1112 self.user_ns.update(ns)
1116
1113
1117 def reset(self, new_session=True):
1114 def reset(self, new_session=True):
1118 """Clear all internal namespaces, and attempt to release references to
1115 """Clear all internal namespaces, and attempt to release references to
1119 user objects.
1116 user objects.
1120
1117
1121 If new_session is True, a new history session will be opened.
1118 If new_session is True, a new history session will be opened.
1122 """
1119 """
1123 # Clear histories
1120 # Clear histories
1124 self.history_manager.reset(new_session)
1121 self.history_manager.reset(new_session)
1125 # Reset counter used to index all histories
1122 # Reset counter used to index all histories
1126 if new_session:
1123 if new_session:
1127 self.execution_count = 1
1124 self.execution_count = 1
1128
1125
1129 # Flush cached output items
1126 # Flush cached output items
1130 if self.displayhook.do_full_cache:
1127 if self.displayhook.do_full_cache:
1131 self.displayhook.flush()
1128 self.displayhook.flush()
1132
1129
1133 # Restore the user namespaces to minimal usability
1130 # Restore the user namespaces to minimal usability
1134 for ns in self.ns_refs_table:
1131 for ns in self.ns_refs_table:
1135 ns.clear()
1132 ns.clear()
1136
1133
1137 # The main execution namespaces must be cleared very carefully,
1134 # The main execution namespaces must be cleared very carefully,
1138 # skipping the deletion of the builtin-related keys, because doing so
1135 # skipping the deletion of the builtin-related keys, because doing so
1139 # would cause errors in many object's __del__ methods.
1136 # would cause errors in many object's __del__ methods.
1140 for ns in [self.user_ns, self.user_global_ns]:
1137 for ns in [self.user_ns, self.user_global_ns]:
1141 drop_keys = set(ns.keys())
1138 drop_keys = set(ns.keys())
1142 drop_keys.discard('__builtin__')
1139 drop_keys.discard('__builtin__')
1143 drop_keys.discard('__builtins__')
1140 drop_keys.discard('__builtins__')
1144 for k in drop_keys:
1141 for k in drop_keys:
1145 del ns[k]
1142 del ns[k]
1146
1143
1147 # Restore the user namespaces to minimal usability
1144 # Restore the user namespaces to minimal usability
1148 self.init_user_ns()
1145 self.init_user_ns()
1149
1146
1150 # Restore the default and user aliases
1147 # Restore the default and user aliases
1151 self.alias_manager.clear_aliases()
1148 self.alias_manager.clear_aliases()
1152 self.alias_manager.init_aliases()
1149 self.alias_manager.init_aliases()
1153
1150
1154 # Flush the private list of module references kept for script
1151 # Flush the private list of module references kept for script
1155 # execution protection
1152 # execution protection
1156 self.clear_main_mod_cache()
1153 self.clear_main_mod_cache()
1157
1154
1158 # Clear out the namespace from the last %run
1155 # Clear out the namespace from the last %run
1159 self.new_main_mod()
1156 self.new_main_mod()
1160
1157
1161 def del_var(self, varname, by_name=False):
1158 def del_var(self, varname, by_name=False):
1162 """Delete a variable from the various namespaces, so that, as
1159 """Delete a variable from the various namespaces, so that, as
1163 far as possible, we're not keeping any hidden references to it.
1160 far as possible, we're not keeping any hidden references to it.
1164
1161
1165 Parameters
1162 Parameters
1166 ----------
1163 ----------
1167 varname : str
1164 varname : str
1168 The name of the variable to delete.
1165 The name of the variable to delete.
1169 by_name : bool
1166 by_name : bool
1170 If True, delete variables with the given name in each
1167 If True, delete variables with the given name in each
1171 namespace. If False (default), find the variable in the user
1168 namespace. If False (default), find the variable in the user
1172 namespace, and delete references to it.
1169 namespace, and delete references to it.
1173 """
1170 """
1174 if varname in ('__builtin__', '__builtins__'):
1171 if varname in ('__builtin__', '__builtins__'):
1175 raise ValueError("Refusing to delete %s" % varname)
1172 raise ValueError("Refusing to delete %s" % varname)
1176 ns_refs = self.ns_refs_table + [self.user_ns,
1173 ns_refs = self.ns_refs_table + [self.user_ns,
1177 self.user_global_ns, self._user_main_module.__dict__] +\
1174 self.user_global_ns, self._user_main_module.__dict__] +\
1178 self._main_ns_cache.values()
1175 self._main_ns_cache.values()
1179
1176
1180 if by_name: # Delete by name
1177 if by_name: # Delete by name
1181 for ns in ns_refs:
1178 for ns in ns_refs:
1182 try:
1179 try:
1183 del ns[varname]
1180 del ns[varname]
1184 except KeyError:
1181 except KeyError:
1185 pass
1182 pass
1186 else: # Delete by object
1183 else: # Delete by object
1187 try:
1184 try:
1188 obj = self.user_ns[varname]
1185 obj = self.user_ns[varname]
1189 except KeyError:
1186 except KeyError:
1190 raise NameError("name '%s' is not defined" % varname)
1187 raise NameError("name '%s' is not defined" % varname)
1191 # Also check in output history
1188 # Also check in output history
1192 ns_refs.append(self.history_manager.output_hist)
1189 ns_refs.append(self.history_manager.output_hist)
1193 for ns in ns_refs:
1190 for ns in ns_refs:
1194 to_delete = [n for n, o in ns.iteritems() if o is obj]
1191 to_delete = [n for n, o in ns.iteritems() if o is obj]
1195 for name in to_delete:
1192 for name in to_delete:
1196 del ns[name]
1193 del ns[name]
1197
1194
1198 # displayhook keeps extra references, but not in a dictionary
1195 # displayhook keeps extra references, but not in a dictionary
1199 for name in ('_', '__', '___'):
1196 for name in ('_', '__', '___'):
1200 if getattr(self.displayhook, name) is obj:
1197 if getattr(self.displayhook, name) is obj:
1201 setattr(self.displayhook, name, None)
1198 setattr(self.displayhook, name, None)
1202
1199
1203 def reset_selective(self, regex=None):
1200 def reset_selective(self, regex=None):
1204 """Clear selective variables from internal namespaces based on a
1201 """Clear selective variables from internal namespaces based on a
1205 specified regular expression.
1202 specified regular expression.
1206
1203
1207 Parameters
1204 Parameters
1208 ----------
1205 ----------
1209 regex : string or compiled pattern, optional
1206 regex : string or compiled pattern, optional
1210 A regular expression pattern that will be used in searching
1207 A regular expression pattern that will be used in searching
1211 variable names in the users namespaces.
1208 variable names in the users namespaces.
1212 """
1209 """
1213 if regex is not None:
1210 if regex is not None:
1214 try:
1211 try:
1215 m = re.compile(regex)
1212 m = re.compile(regex)
1216 except TypeError:
1213 except TypeError:
1217 raise TypeError('regex must be a string or compiled pattern')
1214 raise TypeError('regex must be a string or compiled pattern')
1218 # Search for keys in each namespace that match the given regex
1215 # Search for keys in each namespace that match the given regex
1219 # If a match is found, delete the key/value pair.
1216 # If a match is found, delete the key/value pair.
1220 for ns in self.ns_refs_table:
1217 for ns in self.ns_refs_table:
1221 for var in ns:
1218 for var in ns:
1222 if m.search(var):
1219 if m.search(var):
1223 del ns[var]
1220 del ns[var]
1224
1221
1225 def push(self, variables, interactive=True):
1222 def push(self, variables, interactive=True):
1226 """Inject a group of variables into the IPython user namespace.
1223 """Inject a group of variables into the IPython user namespace.
1227
1224
1228 Parameters
1225 Parameters
1229 ----------
1226 ----------
1230 variables : dict, str or list/tuple of str
1227 variables : dict, str or list/tuple of str
1231 The variables to inject into the user's namespace. If a dict, a
1228 The variables to inject into the user's namespace. If a dict, a
1232 simple update is done. If a str, the string is assumed to have
1229 simple update is done. If a str, the string is assumed to have
1233 variable names separated by spaces. A list/tuple of str can also
1230 variable names separated by spaces. A list/tuple of str can also
1234 be used to give the variable names. If just the variable names are
1231 be used to give the variable names. If just the variable names are
1235 give (list/tuple/str) then the variable values looked up in the
1232 give (list/tuple/str) then the variable values looked up in the
1236 callers frame.
1233 callers frame.
1237 interactive : bool
1234 interactive : bool
1238 If True (default), the variables will be listed with the ``who``
1235 If True (default), the variables will be listed with the ``who``
1239 magic.
1236 magic.
1240 """
1237 """
1241 vdict = None
1238 vdict = None
1242
1239
1243 # We need a dict of name/value pairs to do namespace updates.
1240 # We need a dict of name/value pairs to do namespace updates.
1244 if isinstance(variables, dict):
1241 if isinstance(variables, dict):
1245 vdict = variables
1242 vdict = variables
1246 elif isinstance(variables, (basestring, list, tuple)):
1243 elif isinstance(variables, (basestring, list, tuple)):
1247 if isinstance(variables, basestring):
1244 if isinstance(variables, basestring):
1248 vlist = variables.split()
1245 vlist = variables.split()
1249 else:
1246 else:
1250 vlist = variables
1247 vlist = variables
1251 vdict = {}
1248 vdict = {}
1252 cf = sys._getframe(1)
1249 cf = sys._getframe(1)
1253 for name in vlist:
1250 for name in vlist:
1254 try:
1251 try:
1255 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1252 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1256 except:
1253 except:
1257 print ('Could not get variable %s from %s' %
1254 print ('Could not get variable %s from %s' %
1258 (name,cf.f_code.co_name))
1255 (name,cf.f_code.co_name))
1259 else:
1256 else:
1260 raise ValueError('variables must be a dict/str/list/tuple')
1257 raise ValueError('variables must be a dict/str/list/tuple')
1261
1258
1262 # Propagate variables to user namespace
1259 # Propagate variables to user namespace
1263 self.user_ns.update(vdict)
1260 self.user_ns.update(vdict)
1264
1261
1265 # And configure interactive visibility
1262 # And configure interactive visibility
1266 config_ns = self.user_ns_hidden
1263 config_ns = self.user_ns_hidden
1267 if interactive:
1264 if interactive:
1268 for name, val in vdict.iteritems():
1265 for name, val in vdict.iteritems():
1269 config_ns.pop(name, None)
1266 config_ns.pop(name, None)
1270 else:
1267 else:
1271 for name,val in vdict.iteritems():
1268 for name,val in vdict.iteritems():
1272 config_ns[name] = val
1269 config_ns[name] = val
1273
1270
1274 def drop_by_id(self, variables):
1271 def drop_by_id(self, variables):
1275 """Remove a dict of variables from the user namespace, if they are the
1272 """Remove a dict of variables from the user namespace, if they are the
1276 same as the values in the dictionary.
1273 same as the values in the dictionary.
1277
1274
1278 This is intended for use by extensions: variables that they've added can
1275 This is intended for use by extensions: variables that they've added can
1279 be taken back out if they are unloaded, without removing any that the
1276 be taken back out if they are unloaded, without removing any that the
1280 user has overwritten.
1277 user has overwritten.
1281
1278
1282 Parameters
1279 Parameters
1283 ----------
1280 ----------
1284 variables : dict
1281 variables : dict
1285 A dictionary mapping object names (as strings) to the objects.
1282 A dictionary mapping object names (as strings) to the objects.
1286 """
1283 """
1287 for name, obj in variables.iteritems():
1284 for name, obj in variables.iteritems():
1288 if name in self.user_ns and self.user_ns[name] is obj:
1285 if name in self.user_ns and self.user_ns[name] is obj:
1289 del self.user_ns[name]
1286 del self.user_ns[name]
1290 self.user_ns_hidden.pop(name, None)
1287 self.user_ns_hidden.pop(name, None)
1291
1288
1292 #-------------------------------------------------------------------------
1289 #-------------------------------------------------------------------------
1293 # Things related to object introspection
1290 # Things related to object introspection
1294 #-------------------------------------------------------------------------
1291 #-------------------------------------------------------------------------
1295
1292
1296 def _ofind(self, oname, namespaces=None):
1293 def _ofind(self, oname, namespaces=None):
1297 """Find an object in the available namespaces.
1294 """Find an object in the available namespaces.
1298
1295
1299 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1296 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1300
1297
1301 Has special code to detect magic functions.
1298 Has special code to detect magic functions.
1302 """
1299 """
1303 oname = oname.strip()
1300 oname = oname.strip()
1304 #print '1- oname: <%r>' % oname # dbg
1301 #print '1- oname: <%r>' % oname # dbg
1305 if not py3compat.isidentifier(oname.lstrip(ESC_MAGIC), dotted=True):
1302 if not py3compat.isidentifier(oname.lstrip(ESC_MAGIC), dotted=True):
1306 return dict(found=False)
1303 return dict(found=False)
1307
1304
1308 alias_ns = None
1305 alias_ns = None
1309 if namespaces is None:
1306 if namespaces is None:
1310 # Namespaces to search in:
1307 # Namespaces to search in:
1311 # Put them in a list. The order is important so that we
1308 # Put them in a list. The order is important so that we
1312 # find things in the same order that Python finds them.
1309 # find things in the same order that Python finds them.
1313 namespaces = [ ('Interactive', self.user_ns),
1310 namespaces = [ ('Interactive', self.user_ns),
1314 ('IPython internal', self.internal_ns),
1311 ('IPython internal', self.internal_ns),
1315 ('Python builtin', builtin_mod.__dict__),
1312 ('Python builtin', builtin_mod.__dict__),
1316 ('Alias', self.alias_manager.alias_table),
1313 ('Alias', self.alias_manager.alias_table),
1317 ]
1314 ]
1318 alias_ns = self.alias_manager.alias_table
1315 alias_ns = self.alias_manager.alias_table
1319
1316
1320 # initialize results to 'null'
1317 # initialize results to 'null'
1321 found = False; obj = None; ospace = None; ds = None;
1318 found = False; obj = None; ospace = None; ds = None;
1322 ismagic = False; isalias = False; parent = None
1319 ismagic = False; isalias = False; parent = None
1323
1320
1324 # We need to special-case 'print', which as of python2.6 registers as a
1321 # We need to special-case 'print', which as of python2.6 registers as a
1325 # function but should only be treated as one if print_function was
1322 # function but should only be treated as one if print_function was
1326 # loaded with a future import. In this case, just bail.
1323 # loaded with a future import. In this case, just bail.
1327 if (oname == 'print' and not py3compat.PY3 and not \
1324 if (oname == 'print' and not py3compat.PY3 and not \
1328 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1325 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1329 return {'found':found, 'obj':obj, 'namespace':ospace,
1326 return {'found':found, 'obj':obj, 'namespace':ospace,
1330 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1327 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1331
1328
1332 # Look for the given name by splitting it in parts. If the head is
1329 # Look for the given name by splitting it in parts. If the head is
1333 # found, then we look for all the remaining parts as members, and only
1330 # found, then we look for all the remaining parts as members, and only
1334 # declare success if we can find them all.
1331 # declare success if we can find them all.
1335 oname_parts = oname.split('.')
1332 oname_parts = oname.split('.')
1336 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1333 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1337 for nsname,ns in namespaces:
1334 for nsname,ns in namespaces:
1338 try:
1335 try:
1339 obj = ns[oname_head]
1336 obj = ns[oname_head]
1340 except KeyError:
1337 except KeyError:
1341 continue
1338 continue
1342 else:
1339 else:
1343 #print 'oname_rest:', oname_rest # dbg
1340 #print 'oname_rest:', oname_rest # dbg
1344 for part in oname_rest:
1341 for part in oname_rest:
1345 try:
1342 try:
1346 parent = obj
1343 parent = obj
1347 obj = getattr(obj,part)
1344 obj = getattr(obj,part)
1348 except:
1345 except:
1349 # Blanket except b/c some badly implemented objects
1346 # Blanket except b/c some badly implemented objects
1350 # allow __getattr__ to raise exceptions other than
1347 # allow __getattr__ to raise exceptions other than
1351 # AttributeError, which then crashes IPython.
1348 # AttributeError, which then crashes IPython.
1352 break
1349 break
1353 else:
1350 else:
1354 # If we finish the for loop (no break), we got all members
1351 # If we finish the for loop (no break), we got all members
1355 found = True
1352 found = True
1356 ospace = nsname
1353 ospace = nsname
1357 if ns == alias_ns:
1354 if ns == alias_ns:
1358 isalias = True
1355 isalias = True
1359 break # namespace loop
1356 break # namespace loop
1360
1357
1361 # Try to see if it's magic
1358 # Try to see if it's magic
1362 if not found:
1359 if not found:
1363 if oname.startswith(ESC_MAGIC):
1360 if oname.startswith(ESC_MAGIC):
1364 oname = oname[1:]
1361 oname = oname[1:]
1365 obj = getattr(self,'magic_'+oname,None)
1362 obj = getattr(self,'magic_'+oname,None)
1366 if obj is not None:
1363 if obj is not None:
1367 found = True
1364 found = True
1368 ospace = 'IPython internal'
1365 ospace = 'IPython internal'
1369 ismagic = True
1366 ismagic = True
1370
1367
1371 # Last try: special-case some literals like '', [], {}, etc:
1368 # Last try: special-case some literals like '', [], {}, etc:
1372 if not found and oname_head in ["''",'""','[]','{}','()']:
1369 if not found and oname_head in ["''",'""','[]','{}','()']:
1373 obj = eval(oname_head)
1370 obj = eval(oname_head)
1374 found = True
1371 found = True
1375 ospace = 'Interactive'
1372 ospace = 'Interactive'
1376
1373
1377 return {'found':found, 'obj':obj, 'namespace':ospace,
1374 return {'found':found, 'obj':obj, 'namespace':ospace,
1378 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1375 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1379
1376
1380 def _ofind_property(self, oname, info):
1377 def _ofind_property(self, oname, info):
1381 """Second part of object finding, to look for property details."""
1378 """Second part of object finding, to look for property details."""
1382 if info.found:
1379 if info.found:
1383 # Get the docstring of the class property if it exists.
1380 # Get the docstring of the class property if it exists.
1384 path = oname.split('.')
1381 path = oname.split('.')
1385 root = '.'.join(path[:-1])
1382 root = '.'.join(path[:-1])
1386 if info.parent is not None:
1383 if info.parent is not None:
1387 try:
1384 try:
1388 target = getattr(info.parent, '__class__')
1385 target = getattr(info.parent, '__class__')
1389 # The object belongs to a class instance.
1386 # The object belongs to a class instance.
1390 try:
1387 try:
1391 target = getattr(target, path[-1])
1388 target = getattr(target, path[-1])
1392 # The class defines the object.
1389 # The class defines the object.
1393 if isinstance(target, property):
1390 if isinstance(target, property):
1394 oname = root + '.__class__.' + path[-1]
1391 oname = root + '.__class__.' + path[-1]
1395 info = Struct(self._ofind(oname))
1392 info = Struct(self._ofind(oname))
1396 except AttributeError: pass
1393 except AttributeError: pass
1397 except AttributeError: pass
1394 except AttributeError: pass
1398
1395
1399 # We return either the new info or the unmodified input if the object
1396 # We return either the new info or the unmodified input if the object
1400 # hadn't been found
1397 # hadn't been found
1401 return info
1398 return info
1402
1399
1403 def _object_find(self, oname, namespaces=None):
1400 def _object_find(self, oname, namespaces=None):
1404 """Find an object and return a struct with info about it."""
1401 """Find an object and return a struct with info about it."""
1405 inf = Struct(self._ofind(oname, namespaces))
1402 inf = Struct(self._ofind(oname, namespaces))
1406 return Struct(self._ofind_property(oname, inf))
1403 return Struct(self._ofind_property(oname, inf))
1407
1404
1408 def _inspect(self, meth, oname, namespaces=None, **kw):
1405 def _inspect(self, meth, oname, namespaces=None, **kw):
1409 """Generic interface to the inspector system.
1406 """Generic interface to the inspector system.
1410
1407
1411 This function is meant to be called by pdef, pdoc & friends."""
1408 This function is meant to be called by pdef, pdoc & friends."""
1412 info = self._object_find(oname)
1409 info = self._object_find(oname)
1413 if info.found:
1410 if info.found:
1414 pmethod = getattr(self.inspector, meth)
1411 pmethod = getattr(self.inspector, meth)
1415 formatter = format_screen if info.ismagic else None
1412 formatter = format_screen if info.ismagic else None
1416 if meth == 'pdoc':
1413 if meth == 'pdoc':
1417 pmethod(info.obj, oname, formatter)
1414 pmethod(info.obj, oname, formatter)
1418 elif meth == 'pinfo':
1415 elif meth == 'pinfo':
1419 pmethod(info.obj, oname, formatter, info, **kw)
1416 pmethod(info.obj, oname, formatter, info, **kw)
1420 else:
1417 else:
1421 pmethod(info.obj, oname)
1418 pmethod(info.obj, oname)
1422 else:
1419 else:
1423 print 'Object `%s` not found.' % oname
1420 print 'Object `%s` not found.' % oname
1424 return 'not found' # so callers can take other action
1421 return 'not found' # so callers can take other action
1425
1422
1426 def object_inspect(self, oname):
1423 def object_inspect(self, oname):
1427 with self.builtin_trap:
1424 with self.builtin_trap:
1428 info = self._object_find(oname)
1425 info = self._object_find(oname)
1429 if info.found:
1426 if info.found:
1430 return self.inspector.info(info.obj, oname, info=info)
1427 return self.inspector.info(info.obj, oname, info=info)
1431 else:
1428 else:
1432 return oinspect.object_info(name=oname, found=False)
1429 return oinspect.object_info(name=oname, found=False)
1433
1430
1434 #-------------------------------------------------------------------------
1431 #-------------------------------------------------------------------------
1435 # Things related to history management
1432 # Things related to history management
1436 #-------------------------------------------------------------------------
1433 #-------------------------------------------------------------------------
1437
1434
1438 def init_history(self):
1435 def init_history(self):
1439 """Sets up the command history, and starts regular autosaves."""
1436 """Sets up the command history, and starts regular autosaves."""
1440 self.history_manager = HistoryManager(shell=self, config=self.config)
1437 self.history_manager = HistoryManager(shell=self, config=self.config)
1441 self.configurables.append(self.history_manager)
1438 self.configurables.append(self.history_manager)
1442
1439
1443 #-------------------------------------------------------------------------
1440 #-------------------------------------------------------------------------
1444 # Things related to exception handling and tracebacks (not debugging)
1441 # Things related to exception handling and tracebacks (not debugging)
1445 #-------------------------------------------------------------------------
1442 #-------------------------------------------------------------------------
1446
1443
1447 def init_traceback_handlers(self, custom_exceptions):
1444 def init_traceback_handlers(self, custom_exceptions):
1448 # Syntax error handler.
1445 # Syntax error handler.
1449 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1446 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1450
1447
1451 # The interactive one is initialized with an offset, meaning we always
1448 # The interactive one is initialized with an offset, meaning we always
1452 # want to remove the topmost item in the traceback, which is our own
1449 # want to remove the topmost item in the traceback, which is our own
1453 # internal code. Valid modes: ['Plain','Context','Verbose']
1450 # internal code. Valid modes: ['Plain','Context','Verbose']
1454 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1451 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1455 color_scheme='NoColor',
1452 color_scheme='NoColor',
1456 tb_offset = 1,
1453 tb_offset = 1,
1457 check_cache=self.compile.check_cache)
1454 check_cache=self.compile.check_cache)
1458
1455
1459 # The instance will store a pointer to the system-wide exception hook,
1456 # The instance will store a pointer to the system-wide exception hook,
1460 # so that runtime code (such as magics) can access it. This is because
1457 # so that runtime code (such as magics) can access it. This is because
1461 # during the read-eval loop, it may get temporarily overwritten.
1458 # during the read-eval loop, it may get temporarily overwritten.
1462 self.sys_excepthook = sys.excepthook
1459 self.sys_excepthook = sys.excepthook
1463
1460
1464 # and add any custom exception handlers the user may have specified
1461 # and add any custom exception handlers the user may have specified
1465 self.set_custom_exc(*custom_exceptions)
1462 self.set_custom_exc(*custom_exceptions)
1466
1463
1467 # Set the exception mode
1464 # Set the exception mode
1468 self.InteractiveTB.set_mode(mode=self.xmode)
1465 self.InteractiveTB.set_mode(mode=self.xmode)
1469
1466
1470 def set_custom_exc(self, exc_tuple, handler):
1467 def set_custom_exc(self, exc_tuple, handler):
1471 """set_custom_exc(exc_tuple,handler)
1468 """set_custom_exc(exc_tuple,handler)
1472
1469
1473 Set a custom exception handler, which will be called if any of the
1470 Set a custom exception handler, which will be called if any of the
1474 exceptions in exc_tuple occur in the mainloop (specifically, in the
1471 exceptions in exc_tuple occur in the mainloop (specifically, in the
1475 run_code() method).
1472 run_code() method).
1476
1473
1477 Parameters
1474 Parameters
1478 ----------
1475 ----------
1479
1476
1480 exc_tuple : tuple of exception classes
1477 exc_tuple : tuple of exception classes
1481 A *tuple* of exception classes, for which to call the defined
1478 A *tuple* of exception classes, for which to call the defined
1482 handler. It is very important that you use a tuple, and NOT A
1479 handler. It is very important that you use a tuple, and NOT A
1483 LIST here, because of the way Python's except statement works. If
1480 LIST here, because of the way Python's except statement works. If
1484 you only want to trap a single exception, use a singleton tuple::
1481 you only want to trap a single exception, use a singleton tuple::
1485
1482
1486 exc_tuple == (MyCustomException,)
1483 exc_tuple == (MyCustomException,)
1487
1484
1488 handler : callable
1485 handler : callable
1489 handler must have the following signature::
1486 handler must have the following signature::
1490
1487
1491 def my_handler(self, etype, value, tb, tb_offset=None):
1488 def my_handler(self, etype, value, tb, tb_offset=None):
1492 ...
1489 ...
1493 return structured_traceback
1490 return structured_traceback
1494
1491
1495 Your handler must return a structured traceback (a list of strings),
1492 Your handler must return a structured traceback (a list of strings),
1496 or None.
1493 or None.
1497
1494
1498 This will be made into an instance method (via types.MethodType)
1495 This will be made into an instance method (via types.MethodType)
1499 of IPython itself, and it will be called if any of the exceptions
1496 of IPython itself, and it will be called if any of the exceptions
1500 listed in the exc_tuple are caught. If the handler is None, an
1497 listed in the exc_tuple are caught. If the handler is None, an
1501 internal basic one is used, which just prints basic info.
1498 internal basic one is used, which just prints basic info.
1502
1499
1503 To protect IPython from crashes, if your handler ever raises an
1500 To protect IPython from crashes, if your handler ever raises an
1504 exception or returns an invalid result, it will be immediately
1501 exception or returns an invalid result, it will be immediately
1505 disabled.
1502 disabled.
1506
1503
1507 WARNING: by putting in your own exception handler into IPython's main
1504 WARNING: by putting in your own exception handler into IPython's main
1508 execution loop, you run a very good chance of nasty crashes. This
1505 execution loop, you run a very good chance of nasty crashes. This
1509 facility should only be used if you really know what you are doing."""
1506 facility should only be used if you really know what you are doing."""
1510
1507
1511 assert type(exc_tuple)==type(()) , \
1508 assert type(exc_tuple)==type(()) , \
1512 "The custom exceptions must be given AS A TUPLE."
1509 "The custom exceptions must be given AS A TUPLE."
1513
1510
1514 def dummy_handler(self,etype,value,tb,tb_offset=None):
1511 def dummy_handler(self,etype,value,tb,tb_offset=None):
1515 print '*** Simple custom exception handler ***'
1512 print '*** Simple custom exception handler ***'
1516 print 'Exception type :',etype
1513 print 'Exception type :',etype
1517 print 'Exception value:',value
1514 print 'Exception value:',value
1518 print 'Traceback :',tb
1515 print 'Traceback :',tb
1519 #print 'Source code :','\n'.join(self.buffer)
1516 #print 'Source code :','\n'.join(self.buffer)
1520
1517
1521 def validate_stb(stb):
1518 def validate_stb(stb):
1522 """validate structured traceback return type
1519 """validate structured traceback return type
1523
1520
1524 return type of CustomTB *should* be a list of strings, but allow
1521 return type of CustomTB *should* be a list of strings, but allow
1525 single strings or None, which are harmless.
1522 single strings or None, which are harmless.
1526
1523
1527 This function will *always* return a list of strings,
1524 This function will *always* return a list of strings,
1528 and will raise a TypeError if stb is inappropriate.
1525 and will raise a TypeError if stb is inappropriate.
1529 """
1526 """
1530 msg = "CustomTB must return list of strings, not %r" % stb
1527 msg = "CustomTB must return list of strings, not %r" % stb
1531 if stb is None:
1528 if stb is None:
1532 return []
1529 return []
1533 elif isinstance(stb, basestring):
1530 elif isinstance(stb, basestring):
1534 return [stb]
1531 return [stb]
1535 elif not isinstance(stb, list):
1532 elif not isinstance(stb, list):
1536 raise TypeError(msg)
1533 raise TypeError(msg)
1537 # it's a list
1534 # it's a list
1538 for line in stb:
1535 for line in stb:
1539 # check every element
1536 # check every element
1540 if not isinstance(line, basestring):
1537 if not isinstance(line, basestring):
1541 raise TypeError(msg)
1538 raise TypeError(msg)
1542 return stb
1539 return stb
1543
1540
1544 if handler is None:
1541 if handler is None:
1545 wrapped = dummy_handler
1542 wrapped = dummy_handler
1546 else:
1543 else:
1547 def wrapped(self,etype,value,tb,tb_offset=None):
1544 def wrapped(self,etype,value,tb,tb_offset=None):
1548 """wrap CustomTB handler, to protect IPython from user code
1545 """wrap CustomTB handler, to protect IPython from user code
1549
1546
1550 This makes it harder (but not impossible) for custom exception
1547 This makes it harder (but not impossible) for custom exception
1551 handlers to crash IPython.
1548 handlers to crash IPython.
1552 """
1549 """
1553 try:
1550 try:
1554 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1551 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1555 return validate_stb(stb)
1552 return validate_stb(stb)
1556 except:
1553 except:
1557 # clear custom handler immediately
1554 # clear custom handler immediately
1558 self.set_custom_exc((), None)
1555 self.set_custom_exc((), None)
1559 print >> io.stderr, "Custom TB Handler failed, unregistering"
1556 print >> io.stderr, "Custom TB Handler failed, unregistering"
1560 # show the exception in handler first
1557 # show the exception in handler first
1561 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1558 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1562 print >> io.stdout, self.InteractiveTB.stb2text(stb)
1559 print >> io.stdout, self.InteractiveTB.stb2text(stb)
1563 print >> io.stdout, "The original exception:"
1560 print >> io.stdout, "The original exception:"
1564 stb = self.InteractiveTB.structured_traceback(
1561 stb = self.InteractiveTB.structured_traceback(
1565 (etype,value,tb), tb_offset=tb_offset
1562 (etype,value,tb), tb_offset=tb_offset
1566 )
1563 )
1567 return stb
1564 return stb
1568
1565
1569 self.CustomTB = types.MethodType(wrapped,self)
1566 self.CustomTB = types.MethodType(wrapped,self)
1570 self.custom_exceptions = exc_tuple
1567 self.custom_exceptions = exc_tuple
1571
1568
1572 def excepthook(self, etype, value, tb):
1569 def excepthook(self, etype, value, tb):
1573 """One more defense for GUI apps that call sys.excepthook.
1570 """One more defense for GUI apps that call sys.excepthook.
1574
1571
1575 GUI frameworks like wxPython trap exceptions and call
1572 GUI frameworks like wxPython trap exceptions and call
1576 sys.excepthook themselves. I guess this is a feature that
1573 sys.excepthook themselves. I guess this is a feature that
1577 enables them to keep running after exceptions that would
1574 enables them to keep running after exceptions that would
1578 otherwise kill their mainloop. This is a bother for IPython
1575 otherwise kill their mainloop. This is a bother for IPython
1579 which excepts to catch all of the program exceptions with a try:
1576 which excepts to catch all of the program exceptions with a try:
1580 except: statement.
1577 except: statement.
1581
1578
1582 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1579 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1583 any app directly invokes sys.excepthook, it will look to the user like
1580 any app directly invokes sys.excepthook, it will look to the user like
1584 IPython crashed. In order to work around this, we can disable the
1581 IPython crashed. In order to work around this, we can disable the
1585 CrashHandler and replace it with this excepthook instead, which prints a
1582 CrashHandler and replace it with this excepthook instead, which prints a
1586 regular traceback using our InteractiveTB. In this fashion, apps which
1583 regular traceback using our InteractiveTB. In this fashion, apps which
1587 call sys.excepthook will generate a regular-looking exception from
1584 call sys.excepthook will generate a regular-looking exception from
1588 IPython, and the CrashHandler will only be triggered by real IPython
1585 IPython, and the CrashHandler will only be triggered by real IPython
1589 crashes.
1586 crashes.
1590
1587
1591 This hook should be used sparingly, only in places which are not likely
1588 This hook should be used sparingly, only in places which are not likely
1592 to be true IPython errors.
1589 to be true IPython errors.
1593 """
1590 """
1594 self.showtraceback((etype,value,tb),tb_offset=0)
1591 self.showtraceback((etype,value,tb),tb_offset=0)
1595
1592
1596 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1593 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1597 exception_only=False):
1594 exception_only=False):
1598 """Display the exception that just occurred.
1595 """Display the exception that just occurred.
1599
1596
1600 If nothing is known about the exception, this is the method which
1597 If nothing is known about the exception, this is the method which
1601 should be used throughout the code for presenting user tracebacks,
1598 should be used throughout the code for presenting user tracebacks,
1602 rather than directly invoking the InteractiveTB object.
1599 rather than directly invoking the InteractiveTB object.
1603
1600
1604 A specific showsyntaxerror() also exists, but this method can take
1601 A specific showsyntaxerror() also exists, but this method can take
1605 care of calling it if needed, so unless you are explicitly catching a
1602 care of calling it if needed, so unless you are explicitly catching a
1606 SyntaxError exception, don't try to analyze the stack manually and
1603 SyntaxError exception, don't try to analyze the stack manually and
1607 simply call this method."""
1604 simply call this method."""
1608
1605
1609 try:
1606 try:
1610 if exc_tuple is None:
1607 if exc_tuple is None:
1611 etype, value, tb = sys.exc_info()
1608 etype, value, tb = sys.exc_info()
1612 else:
1609 else:
1613 etype, value, tb = exc_tuple
1610 etype, value, tb = exc_tuple
1614
1611
1615 if etype is None:
1612 if etype is None:
1616 if hasattr(sys, 'last_type'):
1613 if hasattr(sys, 'last_type'):
1617 etype, value, tb = sys.last_type, sys.last_value, \
1614 etype, value, tb = sys.last_type, sys.last_value, \
1618 sys.last_traceback
1615 sys.last_traceback
1619 else:
1616 else:
1620 self.write_err('No traceback available to show.\n')
1617 self.write_err('No traceback available to show.\n')
1621 return
1618 return
1622
1619
1623 if etype is SyntaxError:
1620 if etype is SyntaxError:
1624 # Though this won't be called by syntax errors in the input
1621 # Though this won't be called by syntax errors in the input
1625 # line, there may be SyntaxError cases with imported code.
1622 # line, there may be SyntaxError cases with imported code.
1626 self.showsyntaxerror(filename)
1623 self.showsyntaxerror(filename)
1627 elif etype is UsageError:
1624 elif etype is UsageError:
1628 self.write_err("UsageError: %s" % value)
1625 self.write_err("UsageError: %s" % value)
1629 else:
1626 else:
1630 # WARNING: these variables are somewhat deprecated and not
1627 # WARNING: these variables are somewhat deprecated and not
1631 # necessarily safe to use in a threaded environment, but tools
1628 # necessarily safe to use in a threaded environment, but tools
1632 # like pdb depend on their existence, so let's set them. If we
1629 # like pdb depend on their existence, so let's set them. If we
1633 # find problems in the field, we'll need to revisit their use.
1630 # find problems in the field, we'll need to revisit their use.
1634 sys.last_type = etype
1631 sys.last_type = etype
1635 sys.last_value = value
1632 sys.last_value = value
1636 sys.last_traceback = tb
1633 sys.last_traceback = tb
1637 if etype in self.custom_exceptions:
1634 if etype in self.custom_exceptions:
1638 stb = self.CustomTB(etype, value, tb, tb_offset)
1635 stb = self.CustomTB(etype, value, tb, tb_offset)
1639 else:
1636 else:
1640 if exception_only:
1637 if exception_only:
1641 stb = ['An exception has occurred, use %tb to see '
1638 stb = ['An exception has occurred, use %tb to see '
1642 'the full traceback.\n']
1639 'the full traceback.\n']
1643 stb.extend(self.InteractiveTB.get_exception_only(etype,
1640 stb.extend(self.InteractiveTB.get_exception_only(etype,
1644 value))
1641 value))
1645 else:
1642 else:
1646 stb = self.InteractiveTB.structured_traceback(etype,
1643 stb = self.InteractiveTB.structured_traceback(etype,
1647 value, tb, tb_offset=tb_offset)
1644 value, tb, tb_offset=tb_offset)
1648
1645
1649 self._showtraceback(etype, value, stb)
1646 self._showtraceback(etype, value, stb)
1650 if self.call_pdb:
1647 if self.call_pdb:
1651 # drop into debugger
1648 # drop into debugger
1652 self.debugger(force=True)
1649 self.debugger(force=True)
1653 return
1650 return
1654
1651
1655 # Actually show the traceback
1652 # Actually show the traceback
1656 self._showtraceback(etype, value, stb)
1653 self._showtraceback(etype, value, stb)
1657
1654
1658 except KeyboardInterrupt:
1655 except KeyboardInterrupt:
1659 self.write_err("\nKeyboardInterrupt\n")
1656 self.write_err("\nKeyboardInterrupt\n")
1660
1657
1661 def _showtraceback(self, etype, evalue, stb):
1658 def _showtraceback(self, etype, evalue, stb):
1662 """Actually show a traceback.
1659 """Actually show a traceback.
1663
1660
1664 Subclasses may override this method to put the traceback on a different
1661 Subclasses may override this method to put the traceback on a different
1665 place, like a side channel.
1662 place, like a side channel.
1666 """
1663 """
1667 print >> io.stdout, self.InteractiveTB.stb2text(stb)
1664 print >> io.stdout, self.InteractiveTB.stb2text(stb)
1668
1665
1669 def showsyntaxerror(self, filename=None):
1666 def showsyntaxerror(self, filename=None):
1670 """Display the syntax error that just occurred.
1667 """Display the syntax error that just occurred.
1671
1668
1672 This doesn't display a stack trace because there isn't one.
1669 This doesn't display a stack trace because there isn't one.
1673
1670
1674 If a filename is given, it is stuffed in the exception instead
1671 If a filename is given, it is stuffed in the exception instead
1675 of what was there before (because Python's parser always uses
1672 of what was there before (because Python's parser always uses
1676 "<string>" when reading from a string).
1673 "<string>" when reading from a string).
1677 """
1674 """
1678 etype, value, last_traceback = sys.exc_info()
1675 etype, value, last_traceback = sys.exc_info()
1679
1676
1680 # See note about these variables in showtraceback() above
1677 # See note about these variables in showtraceback() above
1681 sys.last_type = etype
1678 sys.last_type = etype
1682 sys.last_value = value
1679 sys.last_value = value
1683 sys.last_traceback = last_traceback
1680 sys.last_traceback = last_traceback
1684
1681
1685 if filename and etype is SyntaxError:
1682 if filename and etype is SyntaxError:
1686 # Work hard to stuff the correct filename in the exception
1683 # Work hard to stuff the correct filename in the exception
1687 try:
1684 try:
1688 msg, (dummy_filename, lineno, offset, line) = value
1685 msg, (dummy_filename, lineno, offset, line) = value
1689 except:
1686 except:
1690 # Not the format we expect; leave it alone
1687 # Not the format we expect; leave it alone
1691 pass
1688 pass
1692 else:
1689 else:
1693 # Stuff in the right filename
1690 # Stuff in the right filename
1694 try:
1691 try:
1695 # Assume SyntaxError is a class exception
1692 # Assume SyntaxError is a class exception
1696 value = SyntaxError(msg, (filename, lineno, offset, line))
1693 value = SyntaxError(msg, (filename, lineno, offset, line))
1697 except:
1694 except:
1698 # If that failed, assume SyntaxError is a string
1695 # If that failed, assume SyntaxError is a string
1699 value = msg, (filename, lineno, offset, line)
1696 value = msg, (filename, lineno, offset, line)
1700 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1697 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1701 self._showtraceback(etype, value, stb)
1698 self._showtraceback(etype, value, stb)
1702
1699
1703 # This is overridden in TerminalInteractiveShell to show a message about
1700 # This is overridden in TerminalInteractiveShell to show a message about
1704 # the %paste magic.
1701 # the %paste magic.
1705 def showindentationerror(self):
1702 def showindentationerror(self):
1706 """Called by run_cell when there's an IndentationError in code entered
1703 """Called by run_cell when there's an IndentationError in code entered
1707 at the prompt.
1704 at the prompt.
1708
1705
1709 This is overridden in TerminalInteractiveShell to show a message about
1706 This is overridden in TerminalInteractiveShell to show a message about
1710 the %paste magic."""
1707 the %paste magic."""
1711 self.showsyntaxerror()
1708 self.showsyntaxerror()
1712
1709
1713 #-------------------------------------------------------------------------
1710 #-------------------------------------------------------------------------
1714 # Things related to readline
1711 # Things related to readline
1715 #-------------------------------------------------------------------------
1712 #-------------------------------------------------------------------------
1716
1713
1717 def init_readline(self):
1714 def init_readline(self):
1718 """Command history completion/saving/reloading."""
1715 """Command history completion/saving/reloading."""
1719
1716
1720 if self.readline_use:
1717 if self.readline_use:
1721 import IPython.utils.rlineimpl as readline
1718 import IPython.utils.rlineimpl as readline
1722
1719
1723 self.rl_next_input = None
1720 self.rl_next_input = None
1724 self.rl_do_indent = False
1721 self.rl_do_indent = False
1725
1722
1726 if not self.readline_use or not readline.have_readline:
1723 if not self.readline_use or not readline.have_readline:
1727 self.has_readline = False
1724 self.has_readline = False
1728 self.readline = None
1725 self.readline = None
1729 # Set a number of methods that depend on readline to be no-op
1726 # Set a number of methods that depend on readline to be no-op
1730 self.readline_no_record = no_op_context
1727 self.readline_no_record = no_op_context
1731 self.set_readline_completer = no_op
1728 self.set_readline_completer = no_op
1732 self.set_custom_completer = no_op
1729 self.set_custom_completer = no_op
1733 self.set_completer_frame = no_op
1730 self.set_completer_frame = no_op
1734 if self.readline_use:
1731 if self.readline_use:
1735 warn('Readline services not available or not loaded.')
1732 warn('Readline services not available or not loaded.')
1736 else:
1733 else:
1737 self.has_readline = True
1734 self.has_readline = True
1738 self.readline = readline
1735 self.readline = readline
1739 sys.modules['readline'] = readline
1736 sys.modules['readline'] = readline
1740
1737
1741 # Platform-specific configuration
1738 # Platform-specific configuration
1742 if os.name == 'nt':
1739 if os.name == 'nt':
1743 # FIXME - check with Frederick to see if we can harmonize
1740 # FIXME - check with Frederick to see if we can harmonize
1744 # naming conventions with pyreadline to avoid this
1741 # naming conventions with pyreadline to avoid this
1745 # platform-dependent check
1742 # platform-dependent check
1746 self.readline_startup_hook = readline.set_pre_input_hook
1743 self.readline_startup_hook = readline.set_pre_input_hook
1747 else:
1744 else:
1748 self.readline_startup_hook = readline.set_startup_hook
1745 self.readline_startup_hook = readline.set_startup_hook
1749
1746
1750 # Load user's initrc file (readline config)
1747 # Load user's initrc file (readline config)
1751 # Or if libedit is used, load editrc.
1748 # Or if libedit is used, load editrc.
1752 inputrc_name = os.environ.get('INPUTRC')
1749 inputrc_name = os.environ.get('INPUTRC')
1753 if inputrc_name is None:
1750 if inputrc_name is None:
1754 home_dir = get_home_dir()
1751 inputrc_name = '.inputrc'
1755 if home_dir is not None:
1752 if readline.uses_libedit:
1756 inputrc_name = '.inputrc'
1753 inputrc_name = '.editrc'
1757 if readline.uses_libedit:
1754 inputrc_name = os.path.join(self.home_dir, inputrc_name)
1758 inputrc_name = '.editrc'
1759 inputrc_name = os.path.join(home_dir, inputrc_name)
1760 if os.path.isfile(inputrc_name):
1755 if os.path.isfile(inputrc_name):
1761 try:
1756 try:
1762 readline.read_init_file(inputrc_name)
1757 readline.read_init_file(inputrc_name)
1763 except:
1758 except:
1764 warn('Problems reading readline initialization file <%s>'
1759 warn('Problems reading readline initialization file <%s>'
1765 % inputrc_name)
1760 % inputrc_name)
1766
1761
1767 # Configure readline according to user's prefs
1762 # Configure readline according to user's prefs
1768 # This is only done if GNU readline is being used. If libedit
1763 # This is only done if GNU readline is being used. If libedit
1769 # is being used (as on Leopard) the readline config is
1764 # is being used (as on Leopard) the readline config is
1770 # not run as the syntax for libedit is different.
1765 # not run as the syntax for libedit is different.
1771 if not readline.uses_libedit:
1766 if not readline.uses_libedit:
1772 for rlcommand in self.readline_parse_and_bind:
1767 for rlcommand in self.readline_parse_and_bind:
1773 #print "loading rl:",rlcommand # dbg
1768 #print "loading rl:",rlcommand # dbg
1774 readline.parse_and_bind(rlcommand)
1769 readline.parse_and_bind(rlcommand)
1775
1770
1776 # Remove some chars from the delimiters list. If we encounter
1771 # Remove some chars from the delimiters list. If we encounter
1777 # unicode chars, discard them.
1772 # unicode chars, discard them.
1778 delims = readline.get_completer_delims()
1773 delims = readline.get_completer_delims()
1779 if not py3compat.PY3:
1774 if not py3compat.PY3:
1780 delims = delims.encode("ascii", "ignore")
1775 delims = delims.encode("ascii", "ignore")
1781 for d in self.readline_remove_delims:
1776 for d in self.readline_remove_delims:
1782 delims = delims.replace(d, "")
1777 delims = delims.replace(d, "")
1783 delims = delims.replace(ESC_MAGIC, '')
1778 delims = delims.replace(ESC_MAGIC, '')
1784 readline.set_completer_delims(delims)
1779 readline.set_completer_delims(delims)
1785 # otherwise we end up with a monster history after a while:
1780 # otherwise we end up with a monster history after a while:
1786 readline.set_history_length(self.history_length)
1781 readline.set_history_length(self.history_length)
1787
1782
1788 self.refill_readline_hist()
1783 self.refill_readline_hist()
1789 self.readline_no_record = ReadlineNoRecord(self)
1784 self.readline_no_record = ReadlineNoRecord(self)
1790
1785
1791 # Configure auto-indent for all platforms
1786 # Configure auto-indent for all platforms
1792 self.set_autoindent(self.autoindent)
1787 self.set_autoindent(self.autoindent)
1793
1788
1794 def refill_readline_hist(self):
1789 def refill_readline_hist(self):
1795 # Load the last 1000 lines from history
1790 # Load the last 1000 lines from history
1796 self.readline.clear_history()
1791 self.readline.clear_history()
1797 stdin_encoding = sys.stdin.encoding or "utf-8"
1792 stdin_encoding = sys.stdin.encoding or "utf-8"
1798 last_cell = u""
1793 last_cell = u""
1799 for _, _, cell in self.history_manager.get_tail(1000,
1794 for _, _, cell in self.history_manager.get_tail(1000,
1800 include_latest=True):
1795 include_latest=True):
1801 # Ignore blank lines and consecutive duplicates
1796 # Ignore blank lines and consecutive duplicates
1802 cell = cell.rstrip()
1797 cell = cell.rstrip()
1803 if cell and (cell != last_cell):
1798 if cell and (cell != last_cell):
1804 if self.multiline_history:
1799 if self.multiline_history:
1805 self.readline.add_history(py3compat.unicode_to_str(cell,
1800 self.readline.add_history(py3compat.unicode_to_str(cell,
1806 stdin_encoding))
1801 stdin_encoding))
1807 else:
1802 else:
1808 for line in cell.splitlines():
1803 for line in cell.splitlines():
1809 self.readline.add_history(py3compat.unicode_to_str(line,
1804 self.readline.add_history(py3compat.unicode_to_str(line,
1810 stdin_encoding))
1805 stdin_encoding))
1811 last_cell = cell
1806 last_cell = cell
1812
1807
1813 def set_next_input(self, s):
1808 def set_next_input(self, s):
1814 """ Sets the 'default' input string for the next command line.
1809 """ Sets the 'default' input string for the next command line.
1815
1810
1816 Requires readline.
1811 Requires readline.
1817
1812
1818 Example:
1813 Example:
1819
1814
1820 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1815 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1821 [D:\ipython]|2> Hello Word_ # cursor is here
1816 [D:\ipython]|2> Hello Word_ # cursor is here
1822 """
1817 """
1823 self.rl_next_input = py3compat.cast_bytes_py2(s)
1818 self.rl_next_input = py3compat.cast_bytes_py2(s)
1824
1819
1825 # Maybe move this to the terminal subclass?
1820 # Maybe move this to the terminal subclass?
1826 def pre_readline(self):
1821 def pre_readline(self):
1827 """readline hook to be used at the start of each line.
1822 """readline hook to be used at the start of each line.
1828
1823
1829 Currently it handles auto-indent only."""
1824 Currently it handles auto-indent only."""
1830
1825
1831 if self.rl_do_indent:
1826 if self.rl_do_indent:
1832 self.readline.insert_text(self._indent_current_str())
1827 self.readline.insert_text(self._indent_current_str())
1833 if self.rl_next_input is not None:
1828 if self.rl_next_input is not None:
1834 self.readline.insert_text(self.rl_next_input)
1829 self.readline.insert_text(self.rl_next_input)
1835 self.rl_next_input = None
1830 self.rl_next_input = None
1836
1831
1837 def _indent_current_str(self):
1832 def _indent_current_str(self):
1838 """return the current level of indentation as a string"""
1833 """return the current level of indentation as a string"""
1839 return self.input_splitter.indent_spaces * ' '
1834 return self.input_splitter.indent_spaces * ' '
1840
1835
1841 #-------------------------------------------------------------------------
1836 #-------------------------------------------------------------------------
1842 # Things related to text completion
1837 # Things related to text completion
1843 #-------------------------------------------------------------------------
1838 #-------------------------------------------------------------------------
1844
1839
1845 def init_completer(self):
1840 def init_completer(self):
1846 """Initialize the completion machinery.
1841 """Initialize the completion machinery.
1847
1842
1848 This creates completion machinery that can be used by client code,
1843 This creates completion machinery that can be used by client code,
1849 either interactively in-process (typically triggered by the readline
1844 either interactively in-process (typically triggered by the readline
1850 library), programatically (such as in test suites) or out-of-prcess
1845 library), programatically (such as in test suites) or out-of-prcess
1851 (typically over the network by remote frontends).
1846 (typically over the network by remote frontends).
1852 """
1847 """
1853 from IPython.core.completer import IPCompleter
1848 from IPython.core.completer import IPCompleter
1854 from IPython.core.completerlib import (module_completer,
1849 from IPython.core.completerlib import (module_completer,
1855 magic_run_completer, cd_completer)
1850 magic_run_completer, cd_completer)
1856
1851
1857 self.Completer = IPCompleter(shell=self,
1852 self.Completer = IPCompleter(shell=self,
1858 namespace=self.user_ns,
1853 namespace=self.user_ns,
1859 global_namespace=self.user_global_ns,
1854 global_namespace=self.user_global_ns,
1860 alias_table=self.alias_manager.alias_table,
1855 alias_table=self.alias_manager.alias_table,
1861 use_readline=self.has_readline,
1856 use_readline=self.has_readline,
1862 config=self.config,
1857 config=self.config,
1863 )
1858 )
1864 self.configurables.append(self.Completer)
1859 self.configurables.append(self.Completer)
1865
1860
1866 # Add custom completers to the basic ones built into IPCompleter
1861 # Add custom completers to the basic ones built into IPCompleter
1867 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1862 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1868 self.strdispatchers['complete_command'] = sdisp
1863 self.strdispatchers['complete_command'] = sdisp
1869 self.Completer.custom_completers = sdisp
1864 self.Completer.custom_completers = sdisp
1870
1865
1871 self.set_hook('complete_command', module_completer, str_key = 'import')
1866 self.set_hook('complete_command', module_completer, str_key = 'import')
1872 self.set_hook('complete_command', module_completer, str_key = 'from')
1867 self.set_hook('complete_command', module_completer, str_key = 'from')
1873 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1868 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1874 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1869 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1875
1870
1876 # Only configure readline if we truly are using readline. IPython can
1871 # Only configure readline if we truly are using readline. IPython can
1877 # do tab-completion over the network, in GUIs, etc, where readline
1872 # do tab-completion over the network, in GUIs, etc, where readline
1878 # itself may be absent
1873 # itself may be absent
1879 if self.has_readline:
1874 if self.has_readline:
1880 self.set_readline_completer()
1875 self.set_readline_completer()
1881
1876
1882 def complete(self, text, line=None, cursor_pos=None):
1877 def complete(self, text, line=None, cursor_pos=None):
1883 """Return the completed text and a list of completions.
1878 """Return the completed text and a list of completions.
1884
1879
1885 Parameters
1880 Parameters
1886 ----------
1881 ----------
1887
1882
1888 text : string
1883 text : string
1889 A string of text to be completed on. It can be given as empty and
1884 A string of text to be completed on. It can be given as empty and
1890 instead a line/position pair are given. In this case, the
1885 instead a line/position pair are given. In this case, the
1891 completer itself will split the line like readline does.
1886 completer itself will split the line like readline does.
1892
1887
1893 line : string, optional
1888 line : string, optional
1894 The complete line that text is part of.
1889 The complete line that text is part of.
1895
1890
1896 cursor_pos : int, optional
1891 cursor_pos : int, optional
1897 The position of the cursor on the input line.
1892 The position of the cursor on the input line.
1898
1893
1899 Returns
1894 Returns
1900 -------
1895 -------
1901 text : string
1896 text : string
1902 The actual text that was completed.
1897 The actual text that was completed.
1903
1898
1904 matches : list
1899 matches : list
1905 A sorted list with all possible completions.
1900 A sorted list with all possible completions.
1906
1901
1907 The optional arguments allow the completion to take more context into
1902 The optional arguments allow the completion to take more context into
1908 account, and are part of the low-level completion API.
1903 account, and are part of the low-level completion API.
1909
1904
1910 This is a wrapper around the completion mechanism, similar to what
1905 This is a wrapper around the completion mechanism, similar to what
1911 readline does at the command line when the TAB key is hit. By
1906 readline does at the command line when the TAB key is hit. By
1912 exposing it as a method, it can be used by other non-readline
1907 exposing it as a method, it can be used by other non-readline
1913 environments (such as GUIs) for text completion.
1908 environments (such as GUIs) for text completion.
1914
1909
1915 Simple usage example:
1910 Simple usage example:
1916
1911
1917 In [1]: x = 'hello'
1912 In [1]: x = 'hello'
1918
1913
1919 In [2]: _ip.complete('x.l')
1914 In [2]: _ip.complete('x.l')
1920 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1915 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1921 """
1916 """
1922
1917
1923 # Inject names into __builtin__ so we can complete on the added names.
1918 # Inject names into __builtin__ so we can complete on the added names.
1924 with self.builtin_trap:
1919 with self.builtin_trap:
1925 return self.Completer.complete(text, line, cursor_pos)
1920 return self.Completer.complete(text, line, cursor_pos)
1926
1921
1927 def set_custom_completer(self, completer, pos=0):
1922 def set_custom_completer(self, completer, pos=0):
1928 """Adds a new custom completer function.
1923 """Adds a new custom completer function.
1929
1924
1930 The position argument (defaults to 0) is the index in the completers
1925 The position argument (defaults to 0) is the index in the completers
1931 list where you want the completer to be inserted."""
1926 list where you want the completer to be inserted."""
1932
1927
1933 newcomp = types.MethodType(completer,self.Completer)
1928 newcomp = types.MethodType(completer,self.Completer)
1934 self.Completer.matchers.insert(pos,newcomp)
1929 self.Completer.matchers.insert(pos,newcomp)
1935
1930
1936 def set_readline_completer(self):
1931 def set_readline_completer(self):
1937 """Reset readline's completer to be our own."""
1932 """Reset readline's completer to be our own."""
1938 self.readline.set_completer(self.Completer.rlcomplete)
1933 self.readline.set_completer(self.Completer.rlcomplete)
1939
1934
1940 def set_completer_frame(self, frame=None):
1935 def set_completer_frame(self, frame=None):
1941 """Set the frame of the completer."""
1936 """Set the frame of the completer."""
1942 if frame:
1937 if frame:
1943 self.Completer.namespace = frame.f_locals
1938 self.Completer.namespace = frame.f_locals
1944 self.Completer.global_namespace = frame.f_globals
1939 self.Completer.global_namespace = frame.f_globals
1945 else:
1940 else:
1946 self.Completer.namespace = self.user_ns
1941 self.Completer.namespace = self.user_ns
1947 self.Completer.global_namespace = self.user_global_ns
1942 self.Completer.global_namespace = self.user_global_ns
1948
1943
1949 #-------------------------------------------------------------------------
1944 #-------------------------------------------------------------------------
1950 # Things related to magics
1945 # Things related to magics
1951 #-------------------------------------------------------------------------
1946 #-------------------------------------------------------------------------
1952
1947
1953 def init_magics(self):
1948 def init_magics(self):
1954 # FIXME: Move the color initialization to the DisplayHook, which
1949 # FIXME: Move the color initialization to the DisplayHook, which
1955 # should be split into a prompt manager and displayhook. We probably
1950 # should be split into a prompt manager and displayhook. We probably
1956 # even need a centralize colors management object.
1951 # even need a centralize colors management object.
1957 self.magic_colors(self.colors)
1952 self.magic_colors(self.colors)
1958 # History was moved to a separate module
1953 # History was moved to a separate module
1959 from . import history
1954 from . import history
1960 history.init_ipython(self)
1955 history.init_ipython(self)
1961
1956
1962 def magic(self, arg_s, next_input=None):
1957 def magic(self, arg_s, next_input=None):
1963 """Call a magic function by name.
1958 """Call a magic function by name.
1964
1959
1965 Input: a string containing the name of the magic function to call and
1960 Input: a string containing the name of the magic function to call and
1966 any additional arguments to be passed to the magic.
1961 any additional arguments to be passed to the magic.
1967
1962
1968 magic('name -opt foo bar') is equivalent to typing at the ipython
1963 magic('name -opt foo bar') is equivalent to typing at the ipython
1969 prompt:
1964 prompt:
1970
1965
1971 In[1]: %name -opt foo bar
1966 In[1]: %name -opt foo bar
1972
1967
1973 To call a magic without arguments, simply use magic('name').
1968 To call a magic without arguments, simply use magic('name').
1974
1969
1975 This provides a proper Python function to call IPython's magics in any
1970 This provides a proper Python function to call IPython's magics in any
1976 valid Python code you can type at the interpreter, including loops and
1971 valid Python code you can type at the interpreter, including loops and
1977 compound statements.
1972 compound statements.
1978 """
1973 """
1979 # Allow setting the next input - this is used if the user does `a=abs?`.
1974 # Allow setting the next input - this is used if the user does `a=abs?`.
1980 # We do this first so that magic functions can override it.
1975 # We do this first so that magic functions can override it.
1981 if next_input:
1976 if next_input:
1982 self.set_next_input(next_input)
1977 self.set_next_input(next_input)
1983
1978
1984 args = arg_s.split(' ',1)
1979 args = arg_s.split(' ',1)
1985 magic_name = args[0]
1980 magic_name = args[0]
1986 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1981 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1987
1982
1988 try:
1983 try:
1989 magic_args = args[1]
1984 magic_args = args[1]
1990 except IndexError:
1985 except IndexError:
1991 magic_args = ''
1986 magic_args = ''
1992 fn = getattr(self,'magic_'+magic_name,None)
1987 fn = getattr(self,'magic_'+magic_name,None)
1993 if fn is None:
1988 if fn is None:
1994 error("Magic function `%s` not found." % magic_name)
1989 error("Magic function `%s` not found." % magic_name)
1995 else:
1990 else:
1996 magic_args = self.var_expand(magic_args,1)
1991 magic_args = self.var_expand(magic_args,1)
1997 # Grab local namespace if we need it:
1992 # Grab local namespace if we need it:
1998 if getattr(fn, "needs_local_scope", False):
1993 if getattr(fn, "needs_local_scope", False):
1999 self._magic_locals = sys._getframe(1).f_locals
1994 self._magic_locals = sys._getframe(1).f_locals
2000 with self.builtin_trap:
1995 with self.builtin_trap:
2001 result = fn(magic_args)
1996 result = fn(magic_args)
2002 # Ensure we're not keeping object references around:
1997 # Ensure we're not keeping object references around:
2003 self._magic_locals = {}
1998 self._magic_locals = {}
2004 return result
1999 return result
2005
2000
2006 def define_magic(self, magicname, func):
2001 def define_magic(self, magicname, func):
2007 """Expose own function as magic function for ipython
2002 """Expose own function as magic function for ipython
2008
2003
2009 def foo_impl(self,parameter_s=''):
2004 def foo_impl(self,parameter_s=''):
2010 'My very own magic!. (Use docstrings, IPython reads them).'
2005 'My very own magic!. (Use docstrings, IPython reads them).'
2011 print 'Magic function. Passed parameter is between < >:'
2006 print 'Magic function. Passed parameter is between < >:'
2012 print '<%s>' % parameter_s
2007 print '<%s>' % parameter_s
2013 print 'The self object is:',self
2008 print 'The self object is:',self
2014
2009
2015 self.define_magic('foo',foo_impl)
2010 self.define_magic('foo',foo_impl)
2016 """
2011 """
2017 im = types.MethodType(func,self)
2012 im = types.MethodType(func,self)
2018 old = getattr(self, "magic_" + magicname, None)
2013 old = getattr(self, "magic_" + magicname, None)
2019 setattr(self, "magic_" + magicname, im)
2014 setattr(self, "magic_" + magicname, im)
2020 return old
2015 return old
2021
2016
2022 #-------------------------------------------------------------------------
2017 #-------------------------------------------------------------------------
2023 # Things related to macros
2018 # Things related to macros
2024 #-------------------------------------------------------------------------
2019 #-------------------------------------------------------------------------
2025
2020
2026 def define_macro(self, name, themacro):
2021 def define_macro(self, name, themacro):
2027 """Define a new macro
2022 """Define a new macro
2028
2023
2029 Parameters
2024 Parameters
2030 ----------
2025 ----------
2031 name : str
2026 name : str
2032 The name of the macro.
2027 The name of the macro.
2033 themacro : str or Macro
2028 themacro : str or Macro
2034 The action to do upon invoking the macro. If a string, a new
2029 The action to do upon invoking the macro. If a string, a new
2035 Macro object is created by passing the string to it.
2030 Macro object is created by passing the string to it.
2036 """
2031 """
2037
2032
2038 from IPython.core import macro
2033 from IPython.core import macro
2039
2034
2040 if isinstance(themacro, basestring):
2035 if isinstance(themacro, basestring):
2041 themacro = macro.Macro(themacro)
2036 themacro = macro.Macro(themacro)
2042 if not isinstance(themacro, macro.Macro):
2037 if not isinstance(themacro, macro.Macro):
2043 raise ValueError('A macro must be a string or a Macro instance.')
2038 raise ValueError('A macro must be a string or a Macro instance.')
2044 self.user_ns[name] = themacro
2039 self.user_ns[name] = themacro
2045
2040
2046 #-------------------------------------------------------------------------
2041 #-------------------------------------------------------------------------
2047 # Things related to the running of system commands
2042 # Things related to the running of system commands
2048 #-------------------------------------------------------------------------
2043 #-------------------------------------------------------------------------
2049
2044
2050 def system_piped(self, cmd):
2045 def system_piped(self, cmd):
2051 """Call the given cmd in a subprocess, piping stdout/err
2046 """Call the given cmd in a subprocess, piping stdout/err
2052
2047
2053 Parameters
2048 Parameters
2054 ----------
2049 ----------
2055 cmd : str
2050 cmd : str
2056 Command to execute (can not end in '&', as background processes are
2051 Command to execute (can not end in '&', as background processes are
2057 not supported. Should not be a command that expects input
2052 not supported. Should not be a command that expects input
2058 other than simple text.
2053 other than simple text.
2059 """
2054 """
2060 if cmd.rstrip().endswith('&'):
2055 if cmd.rstrip().endswith('&'):
2061 # this is *far* from a rigorous test
2056 # this is *far* from a rigorous test
2062 # We do not support backgrounding processes because we either use
2057 # We do not support backgrounding processes because we either use
2063 # pexpect or pipes to read from. Users can always just call
2058 # pexpect or pipes to read from. Users can always just call
2064 # os.system() or use ip.system=ip.system_raw
2059 # os.system() or use ip.system=ip.system_raw
2065 # if they really want a background process.
2060 # if they really want a background process.
2066 raise OSError("Background processes not supported.")
2061 raise OSError("Background processes not supported.")
2067
2062
2068 # we explicitly do NOT return the subprocess status code, because
2063 # we explicitly do NOT return the subprocess status code, because
2069 # a non-None value would trigger :func:`sys.displayhook` calls.
2064 # a non-None value would trigger :func:`sys.displayhook` calls.
2070 # Instead, we store the exit_code in user_ns.
2065 # Instead, we store the exit_code in user_ns.
2071 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=2))
2066 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=2))
2072
2067
2073 def system_raw(self, cmd):
2068 def system_raw(self, cmd):
2074 """Call the given cmd in a subprocess using os.system
2069 """Call the given cmd in a subprocess using os.system
2075
2070
2076 Parameters
2071 Parameters
2077 ----------
2072 ----------
2078 cmd : str
2073 cmd : str
2079 Command to execute.
2074 Command to execute.
2080 """
2075 """
2081 cmd = self.var_expand(cmd, depth=2)
2076 cmd = self.var_expand(cmd, depth=2)
2082 # protect os.system from UNC paths on Windows, which it can't handle:
2077 # protect os.system from UNC paths on Windows, which it can't handle:
2083 if sys.platform == 'win32':
2078 if sys.platform == 'win32':
2084 from IPython.utils._process_win32 import AvoidUNCPath
2079 from IPython.utils._process_win32 import AvoidUNCPath
2085 with AvoidUNCPath() as path:
2080 with AvoidUNCPath() as path:
2086 if path is not None:
2081 if path is not None:
2087 cmd = '"pushd %s &&"%s' % (path, cmd)
2082 cmd = '"pushd %s &&"%s' % (path, cmd)
2088 cmd = py3compat.unicode_to_str(cmd)
2083 cmd = py3compat.unicode_to_str(cmd)
2089 ec = os.system(cmd)
2084 ec = os.system(cmd)
2090 else:
2085 else:
2091 cmd = py3compat.unicode_to_str(cmd)
2086 cmd = py3compat.unicode_to_str(cmd)
2092 ec = os.system(cmd)
2087 ec = os.system(cmd)
2093
2088
2094 # We explicitly do NOT return the subprocess status code, because
2089 # We explicitly do NOT return the subprocess status code, because
2095 # a non-None value would trigger :func:`sys.displayhook` calls.
2090 # a non-None value would trigger :func:`sys.displayhook` calls.
2096 # Instead, we store the exit_code in user_ns.
2091 # Instead, we store the exit_code in user_ns.
2097 self.user_ns['_exit_code'] = ec
2092 self.user_ns['_exit_code'] = ec
2098
2093
2099 # use piped system by default, because it is better behaved
2094 # use piped system by default, because it is better behaved
2100 system = system_piped
2095 system = system_piped
2101
2096
2102 def getoutput(self, cmd, split=True):
2097 def getoutput(self, cmd, split=True):
2103 """Get output (possibly including stderr) from a subprocess.
2098 """Get output (possibly including stderr) from a subprocess.
2104
2099
2105 Parameters
2100 Parameters
2106 ----------
2101 ----------
2107 cmd : str
2102 cmd : str
2108 Command to execute (can not end in '&', as background processes are
2103 Command to execute (can not end in '&', as background processes are
2109 not supported.
2104 not supported.
2110 split : bool, optional
2105 split : bool, optional
2111
2106
2112 If True, split the output into an IPython SList. Otherwise, an
2107 If True, split the output into an IPython SList. Otherwise, an
2113 IPython LSString is returned. These are objects similar to normal
2108 IPython LSString is returned. These are objects similar to normal
2114 lists and strings, with a few convenience attributes for easier
2109 lists and strings, with a few convenience attributes for easier
2115 manipulation of line-based output. You can use '?' on them for
2110 manipulation of line-based output. You can use '?' on them for
2116 details.
2111 details.
2117 """
2112 """
2118 if cmd.rstrip().endswith('&'):
2113 if cmd.rstrip().endswith('&'):
2119 # this is *far* from a rigorous test
2114 # this is *far* from a rigorous test
2120 raise OSError("Background processes not supported.")
2115 raise OSError("Background processes not supported.")
2121 out = getoutput(self.var_expand(cmd, depth=2))
2116 out = getoutput(self.var_expand(cmd, depth=2))
2122 if split:
2117 if split:
2123 out = SList(out.splitlines())
2118 out = SList(out.splitlines())
2124 else:
2119 else:
2125 out = LSString(out)
2120 out = LSString(out)
2126 return out
2121 return out
2127
2122
2128 #-------------------------------------------------------------------------
2123 #-------------------------------------------------------------------------
2129 # Things related to aliases
2124 # Things related to aliases
2130 #-------------------------------------------------------------------------
2125 #-------------------------------------------------------------------------
2131
2126
2132 def init_alias(self):
2127 def init_alias(self):
2133 self.alias_manager = AliasManager(shell=self, config=self.config)
2128 self.alias_manager = AliasManager(shell=self, config=self.config)
2134 self.configurables.append(self.alias_manager)
2129 self.configurables.append(self.alias_manager)
2135 self.ns_table['alias'] = self.alias_manager.alias_table,
2130 self.ns_table['alias'] = self.alias_manager.alias_table,
2136
2131
2137 #-------------------------------------------------------------------------
2132 #-------------------------------------------------------------------------
2138 # Things related to extensions and plugins
2133 # Things related to extensions and plugins
2139 #-------------------------------------------------------------------------
2134 #-------------------------------------------------------------------------
2140
2135
2141 def init_extension_manager(self):
2136 def init_extension_manager(self):
2142 self.extension_manager = ExtensionManager(shell=self, config=self.config)
2137 self.extension_manager = ExtensionManager(shell=self, config=self.config)
2143 self.configurables.append(self.extension_manager)
2138 self.configurables.append(self.extension_manager)
2144
2139
2145 def init_plugin_manager(self):
2140 def init_plugin_manager(self):
2146 self.plugin_manager = PluginManager(config=self.config)
2141 self.plugin_manager = PluginManager(config=self.config)
2147 self.configurables.append(self.plugin_manager)
2142 self.configurables.append(self.plugin_manager)
2148
2143
2149
2144
2150 #-------------------------------------------------------------------------
2145 #-------------------------------------------------------------------------
2151 # Things related to payloads
2146 # Things related to payloads
2152 #-------------------------------------------------------------------------
2147 #-------------------------------------------------------------------------
2153
2148
2154 def init_payload(self):
2149 def init_payload(self):
2155 self.payload_manager = PayloadManager(config=self.config)
2150 self.payload_manager = PayloadManager(config=self.config)
2156 self.configurables.append(self.payload_manager)
2151 self.configurables.append(self.payload_manager)
2157
2152
2158 #-------------------------------------------------------------------------
2153 #-------------------------------------------------------------------------
2159 # Things related to the prefilter
2154 # Things related to the prefilter
2160 #-------------------------------------------------------------------------
2155 #-------------------------------------------------------------------------
2161
2156
2162 def init_prefilter(self):
2157 def init_prefilter(self):
2163 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
2158 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
2164 self.configurables.append(self.prefilter_manager)
2159 self.configurables.append(self.prefilter_manager)
2165 # Ultimately this will be refactored in the new interpreter code, but
2160 # Ultimately this will be refactored in the new interpreter code, but
2166 # for now, we should expose the main prefilter method (there's legacy
2161 # for now, we should expose the main prefilter method (there's legacy
2167 # code out there that may rely on this).
2162 # code out there that may rely on this).
2168 self.prefilter = self.prefilter_manager.prefilter_lines
2163 self.prefilter = self.prefilter_manager.prefilter_lines
2169
2164
2170 def auto_rewrite_input(self, cmd):
2165 def auto_rewrite_input(self, cmd):
2171 """Print to the screen the rewritten form of the user's command.
2166 """Print to the screen the rewritten form of the user's command.
2172
2167
2173 This shows visual feedback by rewriting input lines that cause
2168 This shows visual feedback by rewriting input lines that cause
2174 automatic calling to kick in, like::
2169 automatic calling to kick in, like::
2175
2170
2176 /f x
2171 /f x
2177
2172
2178 into::
2173 into::
2179
2174
2180 ------> f(x)
2175 ------> f(x)
2181
2176
2182 after the user's input prompt. This helps the user understand that the
2177 after the user's input prompt. This helps the user understand that the
2183 input line was transformed automatically by IPython.
2178 input line was transformed automatically by IPython.
2184 """
2179 """
2185 rw = self.displayhook.prompt1.auto_rewrite() + cmd
2180 rw = self.displayhook.prompt1.auto_rewrite() + cmd
2186
2181
2187 try:
2182 try:
2188 # plain ascii works better w/ pyreadline, on some machines, so
2183 # plain ascii works better w/ pyreadline, on some machines, so
2189 # we use it and only print uncolored rewrite if we have unicode
2184 # we use it and only print uncolored rewrite if we have unicode
2190 rw = str(rw)
2185 rw = str(rw)
2191 print >> io.stdout, rw
2186 print >> io.stdout, rw
2192 except UnicodeEncodeError:
2187 except UnicodeEncodeError:
2193 print "------> " + cmd
2188 print "------> " + cmd
2194
2189
2195 #-------------------------------------------------------------------------
2190 #-------------------------------------------------------------------------
2196 # Things related to extracting values/expressions from kernel and user_ns
2191 # Things related to extracting values/expressions from kernel and user_ns
2197 #-------------------------------------------------------------------------
2192 #-------------------------------------------------------------------------
2198
2193
2199 def _simple_error(self):
2194 def _simple_error(self):
2200 etype, value = sys.exc_info()[:2]
2195 etype, value = sys.exc_info()[:2]
2201 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
2196 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
2202
2197
2203 def user_variables(self, names):
2198 def user_variables(self, names):
2204 """Get a list of variable names from the user's namespace.
2199 """Get a list of variable names from the user's namespace.
2205
2200
2206 Parameters
2201 Parameters
2207 ----------
2202 ----------
2208 names : list of strings
2203 names : list of strings
2209 A list of names of variables to be read from the user namespace.
2204 A list of names of variables to be read from the user namespace.
2210
2205
2211 Returns
2206 Returns
2212 -------
2207 -------
2213 A dict, keyed by the input names and with the repr() of each value.
2208 A dict, keyed by the input names and with the repr() of each value.
2214 """
2209 """
2215 out = {}
2210 out = {}
2216 user_ns = self.user_ns
2211 user_ns = self.user_ns
2217 for varname in names:
2212 for varname in names:
2218 try:
2213 try:
2219 value = repr(user_ns[varname])
2214 value = repr(user_ns[varname])
2220 except:
2215 except:
2221 value = self._simple_error()
2216 value = self._simple_error()
2222 out[varname] = value
2217 out[varname] = value
2223 return out
2218 return out
2224
2219
2225 def user_expressions(self, expressions):
2220 def user_expressions(self, expressions):
2226 """Evaluate a dict of expressions in the user's namespace.
2221 """Evaluate a dict of expressions in the user's namespace.
2227
2222
2228 Parameters
2223 Parameters
2229 ----------
2224 ----------
2230 expressions : dict
2225 expressions : dict
2231 A dict with string keys and string values. The expression values
2226 A dict with string keys and string values. The expression values
2232 should be valid Python expressions, each of which will be evaluated
2227 should be valid Python expressions, each of which will be evaluated
2233 in the user namespace.
2228 in the user namespace.
2234
2229
2235 Returns
2230 Returns
2236 -------
2231 -------
2237 A dict, keyed like the input expressions dict, with the repr() of each
2232 A dict, keyed like the input expressions dict, with the repr() of each
2238 value.
2233 value.
2239 """
2234 """
2240 out = {}
2235 out = {}
2241 user_ns = self.user_ns
2236 user_ns = self.user_ns
2242 global_ns = self.user_global_ns
2237 global_ns = self.user_global_ns
2243 for key, expr in expressions.iteritems():
2238 for key, expr in expressions.iteritems():
2244 try:
2239 try:
2245 value = repr(eval(expr, global_ns, user_ns))
2240 value = repr(eval(expr, global_ns, user_ns))
2246 except:
2241 except:
2247 value = self._simple_error()
2242 value = self._simple_error()
2248 out[key] = value
2243 out[key] = value
2249 return out
2244 return out
2250
2245
2251 #-------------------------------------------------------------------------
2246 #-------------------------------------------------------------------------
2252 # Things related to the running of code
2247 # Things related to the running of code
2253 #-------------------------------------------------------------------------
2248 #-------------------------------------------------------------------------
2254
2249
2255 def ex(self, cmd):
2250 def ex(self, cmd):
2256 """Execute a normal python statement in user namespace."""
2251 """Execute a normal python statement in user namespace."""
2257 with self.builtin_trap:
2252 with self.builtin_trap:
2258 exec cmd in self.user_global_ns, self.user_ns
2253 exec cmd in self.user_global_ns, self.user_ns
2259
2254
2260 def ev(self, expr):
2255 def ev(self, expr):
2261 """Evaluate python expression expr in user namespace.
2256 """Evaluate python expression expr in user namespace.
2262
2257
2263 Returns the result of evaluation
2258 Returns the result of evaluation
2264 """
2259 """
2265 with self.builtin_trap:
2260 with self.builtin_trap:
2266 return eval(expr, self.user_global_ns, self.user_ns)
2261 return eval(expr, self.user_global_ns, self.user_ns)
2267
2262
2268 def safe_execfile(self, fname, *where, **kw):
2263 def safe_execfile(self, fname, *where, **kw):
2269 """A safe version of the builtin execfile().
2264 """A safe version of the builtin execfile().
2270
2265
2271 This version will never throw an exception, but instead print
2266 This version will never throw an exception, but instead print
2272 helpful error messages to the screen. This only works on pure
2267 helpful error messages to the screen. This only works on pure
2273 Python files with the .py extension.
2268 Python files with the .py extension.
2274
2269
2275 Parameters
2270 Parameters
2276 ----------
2271 ----------
2277 fname : string
2272 fname : string
2278 The name of the file to be executed.
2273 The name of the file to be executed.
2279 where : tuple
2274 where : tuple
2280 One or two namespaces, passed to execfile() as (globals,locals).
2275 One or two namespaces, passed to execfile() as (globals,locals).
2281 If only one is given, it is passed as both.
2276 If only one is given, it is passed as both.
2282 exit_ignore : bool (False)
2277 exit_ignore : bool (False)
2283 If True, then silence SystemExit for non-zero status (it is always
2278 If True, then silence SystemExit for non-zero status (it is always
2284 silenced for zero status, as it is so common).
2279 silenced for zero status, as it is so common).
2285 raise_exceptions : bool (False)
2280 raise_exceptions : bool (False)
2286 If True raise exceptions everywhere. Meant for testing.
2281 If True raise exceptions everywhere. Meant for testing.
2287
2282
2288 """
2283 """
2289 kw.setdefault('exit_ignore', False)
2284 kw.setdefault('exit_ignore', False)
2290 kw.setdefault('raise_exceptions', False)
2285 kw.setdefault('raise_exceptions', False)
2291
2286
2292 fname = os.path.abspath(os.path.expanduser(fname))
2287 fname = os.path.abspath(os.path.expanduser(fname))
2293
2288
2294 # Make sure we can open the file
2289 # Make sure we can open the file
2295 try:
2290 try:
2296 with open(fname) as thefile:
2291 with open(fname) as thefile:
2297 pass
2292 pass
2298 except:
2293 except:
2299 warn('Could not open file <%s> for safe execution.' % fname)
2294 warn('Could not open file <%s> for safe execution.' % fname)
2300 return
2295 return
2301
2296
2302 # Find things also in current directory. This is needed to mimic the
2297 # Find things also in current directory. This is needed to mimic the
2303 # behavior of running a script from the system command line, where
2298 # behavior of running a script from the system command line, where
2304 # Python inserts the script's directory into sys.path
2299 # Python inserts the script's directory into sys.path
2305 dname = os.path.dirname(fname)
2300 dname = os.path.dirname(fname)
2306
2301
2307 with prepended_to_syspath(dname):
2302 with prepended_to_syspath(dname):
2308 try:
2303 try:
2309 py3compat.execfile(fname,*where)
2304 py3compat.execfile(fname,*where)
2310 except SystemExit, status:
2305 except SystemExit, status:
2311 # If the call was made with 0 or None exit status (sys.exit(0)
2306 # If the call was made with 0 or None exit status (sys.exit(0)
2312 # or sys.exit() ), don't bother showing a traceback, as both of
2307 # or sys.exit() ), don't bother showing a traceback, as both of
2313 # these are considered normal by the OS:
2308 # these are considered normal by the OS:
2314 # > python -c'import sys;sys.exit(0)'; echo $?
2309 # > python -c'import sys;sys.exit(0)'; echo $?
2315 # 0
2310 # 0
2316 # > python -c'import sys;sys.exit()'; echo $?
2311 # > python -c'import sys;sys.exit()'; echo $?
2317 # 0
2312 # 0
2318 # For other exit status, we show the exception unless
2313 # For other exit status, we show the exception unless
2319 # explicitly silenced, but only in short form.
2314 # explicitly silenced, but only in short form.
2320 if kw['raise_exceptions']:
2315 if kw['raise_exceptions']:
2321 raise
2316 raise
2322 if status.code not in (0, None) and not kw['exit_ignore']:
2317 if status.code not in (0, None) and not kw['exit_ignore']:
2323 self.showtraceback(exception_only=True)
2318 self.showtraceback(exception_only=True)
2324 except:
2319 except:
2325 if kw['raise_exceptions']:
2320 if kw['raise_exceptions']:
2326 raise
2321 raise
2327 self.showtraceback()
2322 self.showtraceback()
2328
2323
2329 def safe_execfile_ipy(self, fname):
2324 def safe_execfile_ipy(self, fname):
2330 """Like safe_execfile, but for .ipy files with IPython syntax.
2325 """Like safe_execfile, but for .ipy files with IPython syntax.
2331
2326
2332 Parameters
2327 Parameters
2333 ----------
2328 ----------
2334 fname : str
2329 fname : str
2335 The name of the file to execute. The filename must have a
2330 The name of the file to execute. The filename must have a
2336 .ipy extension.
2331 .ipy extension.
2337 """
2332 """
2338 fname = os.path.abspath(os.path.expanduser(fname))
2333 fname = os.path.abspath(os.path.expanduser(fname))
2339
2334
2340 # Make sure we can open the file
2335 # Make sure we can open the file
2341 try:
2336 try:
2342 with open(fname) as thefile:
2337 with open(fname) as thefile:
2343 pass
2338 pass
2344 except:
2339 except:
2345 warn('Could not open file <%s> for safe execution.' % fname)
2340 warn('Could not open file <%s> for safe execution.' % fname)
2346 return
2341 return
2347
2342
2348 # Find things also in current directory. This is needed to mimic the
2343 # Find things also in current directory. This is needed to mimic the
2349 # behavior of running a script from the system command line, where
2344 # behavior of running a script from the system command line, where
2350 # Python inserts the script's directory into sys.path
2345 # Python inserts the script's directory into sys.path
2351 dname = os.path.dirname(fname)
2346 dname = os.path.dirname(fname)
2352
2347
2353 with prepended_to_syspath(dname):
2348 with prepended_to_syspath(dname):
2354 try:
2349 try:
2355 with open(fname) as thefile:
2350 with open(fname) as thefile:
2356 # self.run_cell currently captures all exceptions
2351 # self.run_cell currently captures all exceptions
2357 # raised in user code. It would be nice if there were
2352 # raised in user code. It would be nice if there were
2358 # versions of runlines, execfile that did raise, so
2353 # versions of runlines, execfile that did raise, so
2359 # we could catch the errors.
2354 # we could catch the errors.
2360 self.run_cell(thefile.read(), store_history=False)
2355 self.run_cell(thefile.read(), store_history=False)
2361 except:
2356 except:
2362 self.showtraceback()
2357 self.showtraceback()
2363 warn('Unknown failure executing file: <%s>' % fname)
2358 warn('Unknown failure executing file: <%s>' % fname)
2364
2359
2365 def run_cell(self, raw_cell, store_history=False):
2360 def run_cell(self, raw_cell, store_history=False):
2366 """Run a complete IPython cell.
2361 """Run a complete IPython cell.
2367
2362
2368 Parameters
2363 Parameters
2369 ----------
2364 ----------
2370 raw_cell : str
2365 raw_cell : str
2371 The code (including IPython code such as %magic functions) to run.
2366 The code (including IPython code such as %magic functions) to run.
2372 store_history : bool
2367 store_history : bool
2373 If True, the raw and translated cell will be stored in IPython's
2368 If True, the raw and translated cell will be stored in IPython's
2374 history. For user code calling back into IPython's machinery, this
2369 history. For user code calling back into IPython's machinery, this
2375 should be set to False.
2370 should be set to False.
2376 """
2371 """
2377 if (not raw_cell) or raw_cell.isspace():
2372 if (not raw_cell) or raw_cell.isspace():
2378 return
2373 return
2379
2374
2380 for line in raw_cell.splitlines():
2375 for line in raw_cell.splitlines():
2381 self.input_splitter.push(line)
2376 self.input_splitter.push(line)
2382 cell = self.input_splitter.source_reset()
2377 cell = self.input_splitter.source_reset()
2383
2378
2384 with self.builtin_trap:
2379 with self.builtin_trap:
2385 prefilter_failed = False
2380 prefilter_failed = False
2386 if len(cell.splitlines()) == 1:
2381 if len(cell.splitlines()) == 1:
2387 try:
2382 try:
2388 # use prefilter_lines to handle trailing newlines
2383 # use prefilter_lines to handle trailing newlines
2389 # restore trailing newline for ast.parse
2384 # restore trailing newline for ast.parse
2390 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2385 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2391 except AliasError as e:
2386 except AliasError as e:
2392 error(e)
2387 error(e)
2393 prefilter_failed = True
2388 prefilter_failed = True
2394 except Exception:
2389 except Exception:
2395 # don't allow prefilter errors to crash IPython
2390 # don't allow prefilter errors to crash IPython
2396 self.showtraceback()
2391 self.showtraceback()
2397 prefilter_failed = True
2392 prefilter_failed = True
2398
2393
2399 # Store raw and processed history
2394 # Store raw and processed history
2400 if store_history:
2395 if store_history:
2401 self.history_manager.store_inputs(self.execution_count,
2396 self.history_manager.store_inputs(self.execution_count,
2402 cell, raw_cell)
2397 cell, raw_cell)
2403
2398
2404 self.logger.log(cell, raw_cell)
2399 self.logger.log(cell, raw_cell)
2405
2400
2406 if not prefilter_failed:
2401 if not prefilter_failed:
2407 # don't run if prefilter failed
2402 # don't run if prefilter failed
2408 cell_name = self.compile.cache(cell, self.execution_count)
2403 cell_name = self.compile.cache(cell, self.execution_count)
2409
2404
2410 with self.display_trap:
2405 with self.display_trap:
2411 try:
2406 try:
2412 code_ast = self.compile.ast_parse(cell, filename=cell_name)
2407 code_ast = self.compile.ast_parse(cell, filename=cell_name)
2413 except IndentationError:
2408 except IndentationError:
2414 self.showindentationerror()
2409 self.showindentationerror()
2415 self.execution_count += 1
2410 self.execution_count += 1
2416 return None
2411 return None
2417 except (OverflowError, SyntaxError, ValueError, TypeError,
2412 except (OverflowError, SyntaxError, ValueError, TypeError,
2418 MemoryError):
2413 MemoryError):
2419 self.showsyntaxerror()
2414 self.showsyntaxerror()
2420 self.execution_count += 1
2415 self.execution_count += 1
2421 return None
2416 return None
2422
2417
2423 self.run_ast_nodes(code_ast.body, cell_name,
2418 self.run_ast_nodes(code_ast.body, cell_name,
2424 interactivity="last_expr")
2419 interactivity="last_expr")
2425
2420
2426 # Execute any registered post-execution functions.
2421 # Execute any registered post-execution functions.
2427 for func, status in self._post_execute.iteritems():
2422 for func, status in self._post_execute.iteritems():
2428 if not status:
2423 if not status:
2429 continue
2424 continue
2430 try:
2425 try:
2431 func()
2426 func()
2432 except KeyboardInterrupt:
2427 except KeyboardInterrupt:
2433 print >> io.stderr, "\nKeyboardInterrupt"
2428 print >> io.stderr, "\nKeyboardInterrupt"
2434 except Exception:
2429 except Exception:
2435 print >> io.stderr, "Disabling failed post-execution function: %s" % func
2430 print >> io.stderr, "Disabling failed post-execution function: %s" % func
2436 self.showtraceback()
2431 self.showtraceback()
2437 # Deactivate failing function
2432 # Deactivate failing function
2438 self._post_execute[func] = False
2433 self._post_execute[func] = False
2439
2434
2440 if store_history:
2435 if store_history:
2441 # Write output to the database. Does nothing unless
2436 # Write output to the database. Does nothing unless
2442 # history output logging is enabled.
2437 # history output logging is enabled.
2443 self.history_manager.store_output(self.execution_count)
2438 self.history_manager.store_output(self.execution_count)
2444 # Each cell is a *single* input, regardless of how many lines it has
2439 # Each cell is a *single* input, regardless of how many lines it has
2445 self.execution_count += 1
2440 self.execution_count += 1
2446
2441
2447 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'):
2442 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'):
2448 """Run a sequence of AST nodes. The execution mode depends on the
2443 """Run a sequence of AST nodes. The execution mode depends on the
2449 interactivity parameter.
2444 interactivity parameter.
2450
2445
2451 Parameters
2446 Parameters
2452 ----------
2447 ----------
2453 nodelist : list
2448 nodelist : list
2454 A sequence of AST nodes to run.
2449 A sequence of AST nodes to run.
2455 cell_name : str
2450 cell_name : str
2456 Will be passed to the compiler as the filename of the cell. Typically
2451 Will be passed to the compiler as the filename of the cell. Typically
2457 the value returned by ip.compile.cache(cell).
2452 the value returned by ip.compile.cache(cell).
2458 interactivity : str
2453 interactivity : str
2459 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2454 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2460 run interactively (displaying output from expressions). 'last_expr'
2455 run interactively (displaying output from expressions). 'last_expr'
2461 will run the last node interactively only if it is an expression (i.e.
2456 will run the last node interactively only if it is an expression (i.e.
2462 expressions in loops or other blocks are not displayed. Other values
2457 expressions in loops or other blocks are not displayed. Other values
2463 for this parameter will raise a ValueError.
2458 for this parameter will raise a ValueError.
2464 """
2459 """
2465 if not nodelist:
2460 if not nodelist:
2466 return
2461 return
2467
2462
2468 if interactivity == 'last_expr':
2463 if interactivity == 'last_expr':
2469 if isinstance(nodelist[-1], ast.Expr):
2464 if isinstance(nodelist[-1], ast.Expr):
2470 interactivity = "last"
2465 interactivity = "last"
2471 else:
2466 else:
2472 interactivity = "none"
2467 interactivity = "none"
2473
2468
2474 if interactivity == 'none':
2469 if interactivity == 'none':
2475 to_run_exec, to_run_interactive = nodelist, []
2470 to_run_exec, to_run_interactive = nodelist, []
2476 elif interactivity == 'last':
2471 elif interactivity == 'last':
2477 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2472 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2478 elif interactivity == 'all':
2473 elif interactivity == 'all':
2479 to_run_exec, to_run_interactive = [], nodelist
2474 to_run_exec, to_run_interactive = [], nodelist
2480 else:
2475 else:
2481 raise ValueError("Interactivity was %r" % interactivity)
2476 raise ValueError("Interactivity was %r" % interactivity)
2482
2477
2483 exec_count = self.execution_count
2478 exec_count = self.execution_count
2484
2479
2485 try:
2480 try:
2486 for i, node in enumerate(to_run_exec):
2481 for i, node in enumerate(to_run_exec):
2487 mod = ast.Module([node])
2482 mod = ast.Module([node])
2488 code = self.compile(mod, cell_name, "exec")
2483 code = self.compile(mod, cell_name, "exec")
2489 if self.run_code(code):
2484 if self.run_code(code):
2490 return True
2485 return True
2491
2486
2492 for i, node in enumerate(to_run_interactive):
2487 for i, node in enumerate(to_run_interactive):
2493 mod = ast.Interactive([node])
2488 mod = ast.Interactive([node])
2494 code = self.compile(mod, cell_name, "single")
2489 code = self.compile(mod, cell_name, "single")
2495 if self.run_code(code):
2490 if self.run_code(code):
2496 return True
2491 return True
2497 except:
2492 except:
2498 # It's possible to have exceptions raised here, typically by
2493 # It's possible to have exceptions raised here, typically by
2499 # compilation of odd code (such as a naked 'return' outside a
2494 # compilation of odd code (such as a naked 'return' outside a
2500 # function) that did parse but isn't valid. Typically the exception
2495 # function) that did parse but isn't valid. Typically the exception
2501 # is a SyntaxError, but it's safest just to catch anything and show
2496 # is a SyntaxError, but it's safest just to catch anything and show
2502 # the user a traceback.
2497 # the user a traceback.
2503
2498
2504 # We do only one try/except outside the loop to minimize the impact
2499 # We do only one try/except outside the loop to minimize the impact
2505 # on runtime, and also because if any node in the node list is
2500 # on runtime, and also because if any node in the node list is
2506 # broken, we should stop execution completely.
2501 # broken, we should stop execution completely.
2507 self.showtraceback()
2502 self.showtraceback()
2508
2503
2509 return False
2504 return False
2510
2505
2511 def run_code(self, code_obj):
2506 def run_code(self, code_obj):
2512 """Execute a code object.
2507 """Execute a code object.
2513
2508
2514 When an exception occurs, self.showtraceback() is called to display a
2509 When an exception occurs, self.showtraceback() is called to display a
2515 traceback.
2510 traceback.
2516
2511
2517 Parameters
2512 Parameters
2518 ----------
2513 ----------
2519 code_obj : code object
2514 code_obj : code object
2520 A compiled code object, to be executed
2515 A compiled code object, to be executed
2521 post_execute : bool [default: True]
2516 post_execute : bool [default: True]
2522 whether to call post_execute hooks after this particular execution.
2517 whether to call post_execute hooks after this particular execution.
2523
2518
2524 Returns
2519 Returns
2525 -------
2520 -------
2526 False : successful execution.
2521 False : successful execution.
2527 True : an error occurred.
2522 True : an error occurred.
2528 """
2523 """
2529
2524
2530 # Set our own excepthook in case the user code tries to call it
2525 # Set our own excepthook in case the user code tries to call it
2531 # directly, so that the IPython crash handler doesn't get triggered
2526 # directly, so that the IPython crash handler doesn't get triggered
2532 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2527 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2533
2528
2534 # we save the original sys.excepthook in the instance, in case config
2529 # we save the original sys.excepthook in the instance, in case config
2535 # code (such as magics) needs access to it.
2530 # code (such as magics) needs access to it.
2536 self.sys_excepthook = old_excepthook
2531 self.sys_excepthook = old_excepthook
2537 outflag = 1 # happens in more places, so it's easier as default
2532 outflag = 1 # happens in more places, so it's easier as default
2538 try:
2533 try:
2539 try:
2534 try:
2540 self.hooks.pre_run_code_hook()
2535 self.hooks.pre_run_code_hook()
2541 #rprint('Running code', repr(code_obj)) # dbg
2536 #rprint('Running code', repr(code_obj)) # dbg
2542 exec code_obj in self.user_global_ns, self.user_ns
2537 exec code_obj in self.user_global_ns, self.user_ns
2543 finally:
2538 finally:
2544 # Reset our crash handler in place
2539 # Reset our crash handler in place
2545 sys.excepthook = old_excepthook
2540 sys.excepthook = old_excepthook
2546 except SystemExit:
2541 except SystemExit:
2547 self.showtraceback(exception_only=True)
2542 self.showtraceback(exception_only=True)
2548 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2543 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2549 except self.custom_exceptions:
2544 except self.custom_exceptions:
2550 etype,value,tb = sys.exc_info()
2545 etype,value,tb = sys.exc_info()
2551 self.CustomTB(etype,value,tb)
2546 self.CustomTB(etype,value,tb)
2552 except:
2547 except:
2553 self.showtraceback()
2548 self.showtraceback()
2554 else:
2549 else:
2555 outflag = 0
2550 outflag = 0
2556 if softspace(sys.stdout, 0):
2551 if softspace(sys.stdout, 0):
2557 print
2552 print
2558
2553
2559 return outflag
2554 return outflag
2560
2555
2561 # For backwards compatibility
2556 # For backwards compatibility
2562 runcode = run_code
2557 runcode = run_code
2563
2558
2564 #-------------------------------------------------------------------------
2559 #-------------------------------------------------------------------------
2565 # Things related to GUI support and pylab
2560 # Things related to GUI support and pylab
2566 #-------------------------------------------------------------------------
2561 #-------------------------------------------------------------------------
2567
2562
2568 def enable_pylab(self, gui=None, import_all=True):
2563 def enable_pylab(self, gui=None, import_all=True):
2569 raise NotImplementedError('Implement enable_pylab in a subclass')
2564 raise NotImplementedError('Implement enable_pylab in a subclass')
2570
2565
2571 #-------------------------------------------------------------------------
2566 #-------------------------------------------------------------------------
2572 # Utilities
2567 # Utilities
2573 #-------------------------------------------------------------------------
2568 #-------------------------------------------------------------------------
2574
2569
2575 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
2570 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
2576 """Expand python variables in a string.
2571 """Expand python variables in a string.
2577
2572
2578 The depth argument indicates how many frames above the caller should
2573 The depth argument indicates how many frames above the caller should
2579 be walked to look for the local namespace where to expand variables.
2574 be walked to look for the local namespace where to expand variables.
2580
2575
2581 The global namespace for expansion is always the user's interactive
2576 The global namespace for expansion is always the user's interactive
2582 namespace.
2577 namespace.
2583 """
2578 """
2584 ns = self.user_ns.copy()
2579 ns = self.user_ns.copy()
2585 ns.update(sys._getframe(depth+1).f_locals)
2580 ns.update(sys._getframe(depth+1).f_locals)
2586 ns.pop('self', None)
2581 ns.pop('self', None)
2587 return formatter.format(cmd, **ns)
2582 return formatter.format(cmd, **ns)
2588
2583
2589 def mktempfile(self, data=None, prefix='ipython_edit_'):
2584 def mktempfile(self, data=None, prefix='ipython_edit_'):
2590 """Make a new tempfile and return its filename.
2585 """Make a new tempfile and return its filename.
2591
2586
2592 This makes a call to tempfile.mktemp, but it registers the created
2587 This makes a call to tempfile.mktemp, but it registers the created
2593 filename internally so ipython cleans it up at exit time.
2588 filename internally so ipython cleans it up at exit time.
2594
2589
2595 Optional inputs:
2590 Optional inputs:
2596
2591
2597 - data(None): if data is given, it gets written out to the temp file
2592 - data(None): if data is given, it gets written out to the temp file
2598 immediately, and the file is closed again."""
2593 immediately, and the file is closed again."""
2599
2594
2600 filename = tempfile.mktemp('.py', prefix)
2595 filename = tempfile.mktemp('.py', prefix)
2601 self.tempfiles.append(filename)
2596 self.tempfiles.append(filename)
2602
2597
2603 if data:
2598 if data:
2604 tmp_file = open(filename,'w')
2599 tmp_file = open(filename,'w')
2605 tmp_file.write(data)
2600 tmp_file.write(data)
2606 tmp_file.close()
2601 tmp_file.close()
2607 return filename
2602 return filename
2608
2603
2609 # TODO: This should be removed when Term is refactored.
2604 # TODO: This should be removed when Term is refactored.
2610 def write(self,data):
2605 def write(self,data):
2611 """Write a string to the default output"""
2606 """Write a string to the default output"""
2612 io.stdout.write(data)
2607 io.stdout.write(data)
2613
2608
2614 # TODO: This should be removed when Term is refactored.
2609 # TODO: This should be removed when Term is refactored.
2615 def write_err(self,data):
2610 def write_err(self,data):
2616 """Write a string to the default error output"""
2611 """Write a string to the default error output"""
2617 io.stderr.write(data)
2612 io.stderr.write(data)
2618
2613
2619 def ask_yes_no(self, prompt, default=None):
2614 def ask_yes_no(self, prompt, default=None):
2620 if self.quiet:
2615 if self.quiet:
2621 return True
2616 return True
2622 return ask_yes_no(prompt,default)
2617 return ask_yes_no(prompt,default)
2623
2618
2624 def show_usage(self):
2619 def show_usage(self):
2625 """Show a usage message"""
2620 """Show a usage message"""
2626 page.page(IPython.core.usage.interactive_usage)
2621 page.page(IPython.core.usage.interactive_usage)
2627
2622
2628 def find_user_code(self, target, raw=True):
2623 def find_user_code(self, target, raw=True):
2629 """Get a code string from history, file, or a string or macro.
2624 """Get a code string from history, file, or a string or macro.
2630
2625
2631 This is mainly used by magic functions.
2626 This is mainly used by magic functions.
2632
2627
2633 Parameters
2628 Parameters
2634 ----------
2629 ----------
2635 target : str
2630 target : str
2636 A string specifying code to retrieve. This will be tried respectively
2631 A string specifying code to retrieve. This will be tried respectively
2637 as: ranges of input history (see %history for syntax), a filename, or
2632 as: ranges of input history (see %history for syntax), a filename, or
2638 an expression evaluating to a string or Macro in the user namespace.
2633 an expression evaluating to a string or Macro in the user namespace.
2639 raw : bool
2634 raw : bool
2640 If true (default), retrieve raw history. Has no effect on the other
2635 If true (default), retrieve raw history. Has no effect on the other
2641 retrieval mechanisms.
2636 retrieval mechanisms.
2642
2637
2643 Returns
2638 Returns
2644 -------
2639 -------
2645 A string of code.
2640 A string of code.
2646
2641
2647 ValueError is raised if nothing is found, and TypeError if it evaluates
2642 ValueError is raised if nothing is found, and TypeError if it evaluates
2648 to an object of another type. In each case, .args[0] is a printable
2643 to an object of another type. In each case, .args[0] is a printable
2649 message.
2644 message.
2650 """
2645 """
2651 code = self.extract_input_lines(target, raw=raw) # Grab history
2646 code = self.extract_input_lines(target, raw=raw) # Grab history
2652 if code:
2647 if code:
2653 return code
2648 return code
2654 if os.path.isfile(target): # Read file
2649 if os.path.isfile(target): # Read file
2655 return open(target, "r").read()
2650 return open(target, "r").read()
2656
2651
2657 try: # User namespace
2652 try: # User namespace
2658 codeobj = eval(target, self.user_ns)
2653 codeobj = eval(target, self.user_ns)
2659 except Exception:
2654 except Exception:
2660 raise ValueError(("'%s' was not found in history, as a file, nor in"
2655 raise ValueError(("'%s' was not found in history, as a file, nor in"
2661 " the user namespace.") % target)
2656 " the user namespace.") % target)
2662 if isinstance(codeobj, basestring):
2657 if isinstance(codeobj, basestring):
2663 return codeobj
2658 return codeobj
2664 elif isinstance(codeobj, Macro):
2659 elif isinstance(codeobj, Macro):
2665 return codeobj.value
2660 return codeobj.value
2666
2661
2667 raise TypeError("%s is neither a string nor a macro." % target,
2662 raise TypeError("%s is neither a string nor a macro." % target,
2668 codeobj)
2663 codeobj)
2669
2664
2670 #-------------------------------------------------------------------------
2665 #-------------------------------------------------------------------------
2671 # Things related to IPython exiting
2666 # Things related to IPython exiting
2672 #-------------------------------------------------------------------------
2667 #-------------------------------------------------------------------------
2673 def atexit_operations(self):
2668 def atexit_operations(self):
2674 """This will be executed at the time of exit.
2669 """This will be executed at the time of exit.
2675
2670
2676 Cleanup operations and saving of persistent data that is done
2671 Cleanup operations and saving of persistent data that is done
2677 unconditionally by IPython should be performed here.
2672 unconditionally by IPython should be performed here.
2678
2673
2679 For things that may depend on startup flags or platform specifics (such
2674 For things that may depend on startup flags or platform specifics (such
2680 as having readline or not), register a separate atexit function in the
2675 as having readline or not), register a separate atexit function in the
2681 code that has the appropriate information, rather than trying to
2676 code that has the appropriate information, rather than trying to
2682 clutter
2677 clutter
2683 """
2678 """
2684 # Close the history session (this stores the end time and line count)
2679 # Close the history session (this stores the end time and line count)
2685 # this must be *before* the tempfile cleanup, in case of temporary
2680 # this must be *before* the tempfile cleanup, in case of temporary
2686 # history db
2681 # history db
2687 self.history_manager.end_session()
2682 self.history_manager.end_session()
2688
2683
2689 # Cleanup all tempfiles left around
2684 # Cleanup all tempfiles left around
2690 for tfile in self.tempfiles:
2685 for tfile in self.tempfiles:
2691 try:
2686 try:
2692 os.unlink(tfile)
2687 os.unlink(tfile)
2693 except OSError:
2688 except OSError:
2694 pass
2689 pass
2695
2690
2696 # Clear all user namespaces to release all references cleanly.
2691 # Clear all user namespaces to release all references cleanly.
2697 self.reset(new_session=False)
2692 self.reset(new_session=False)
2698
2693
2699 # Run user hooks
2694 # Run user hooks
2700 self.hooks.shutdown_hook()
2695 self.hooks.shutdown_hook()
2701
2696
2702 def cleanup(self):
2697 def cleanup(self):
2703 self.restore_sys_module_state()
2698 self.restore_sys_module_state()
2704
2699
2705
2700
2706 class InteractiveShellABC(object):
2701 class InteractiveShellABC(object):
2707 """An abstract base class for InteractiveShell."""
2702 """An abstract base class for InteractiveShell."""
2708 __metaclass__ = abc.ABCMeta
2703 __metaclass__ = abc.ABCMeta
2709
2704
2710 InteractiveShellABC.register(InteractiveShell)
2705 InteractiveShellABC.register(InteractiveShell)
@@ -1,521 +1,461 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Utilities for path handling.
3 Utilities for path handling.
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import os
17 import os
18 import sys
18 import sys
19 import tempfile
19 import tempfile
20 import warnings
20 import warnings
21 from hashlib import md5
21 from hashlib import md5
22
22
23 import IPython
23 import IPython
24 from IPython.utils.process import system
24 from IPython.utils.process import system
25 from IPython.utils.importstring import import_item
25 from IPython.utils.importstring import import_item
26 from IPython.utils import py3compat
26 from IPython.utils import py3compat
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Code
29 # Code
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32 fs_encoding = sys.getfilesystemencoding()
32 fs_encoding = sys.getfilesystemencoding()
33
33
34 def _get_long_path_name(path):
34 def _get_long_path_name(path):
35 """Dummy no-op."""
35 """Dummy no-op."""
36 return path
36 return path
37
37
38 def _writable_dir(path):
38 def _writable_dir(path):
39 """Whether `path` is a directory, to which the user has write access."""
39 """Whether `path` is a directory, to which the user has write access."""
40 return os.path.isdir(path) and os.access(path, os.W_OK)
40 return os.path.isdir(path) and os.access(path, os.W_OK)
41
41
42 if sys.platform == 'win32':
42 if sys.platform == 'win32':
43 def _get_long_path_name(path):
43 def _get_long_path_name(path):
44 """Get a long path name (expand ~) on Windows using ctypes.
44 """Get a long path name (expand ~) on Windows using ctypes.
45
45
46 Examples
46 Examples
47 --------
47 --------
48
48
49 >>> get_long_path_name('c:\\docume~1')
49 >>> get_long_path_name('c:\\docume~1')
50 u'c:\\\\Documents and Settings'
50 u'c:\\\\Documents and Settings'
51
51
52 """
52 """
53 try:
53 try:
54 import ctypes
54 import ctypes
55 except ImportError:
55 except ImportError:
56 raise ImportError('you need to have ctypes installed for this to work')
56 raise ImportError('you need to have ctypes installed for this to work')
57 _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
57 _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
58 _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p,
58 _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p,
59 ctypes.c_uint ]
59 ctypes.c_uint ]
60
60
61 buf = ctypes.create_unicode_buffer(260)
61 buf = ctypes.create_unicode_buffer(260)
62 rv = _GetLongPathName(path, buf, 260)
62 rv = _GetLongPathName(path, buf, 260)
63 if rv == 0 or rv > 260:
63 if rv == 0 or rv > 260:
64 return path
64 return path
65 else:
65 else:
66 return buf.value
66 return buf.value
67
67
68
68
69 def get_long_path_name(path):
69 def get_long_path_name(path):
70 """Expand a path into its long form.
70 """Expand a path into its long form.
71
71
72 On Windows this expands any ~ in the paths. On other platforms, it is
72 On Windows this expands any ~ in the paths. On other platforms, it is
73 a null operation.
73 a null operation.
74 """
74 """
75 return _get_long_path_name(path)
75 return _get_long_path_name(path)
76
76
77
77
78 def unquote_filename(name, win32=(sys.platform=='win32')):
78 def unquote_filename(name, win32=(sys.platform=='win32')):
79 """ On Windows, remove leading and trailing quotes from filenames.
79 """ On Windows, remove leading and trailing quotes from filenames.
80 """
80 """
81 if win32:
81 if win32:
82 if name.startswith(("'", '"')) and name.endswith(("'", '"')):
82 if name.startswith(("'", '"')) and name.endswith(("'", '"')):
83 name = name[1:-1]
83 name = name[1:-1]
84 return name
84 return name
85
85
86
86
87 def get_py_filename(name, force_win32=None):
87 def get_py_filename(name, force_win32=None):
88 """Return a valid python filename in the current directory.
88 """Return a valid python filename in the current directory.
89
89
90 If the given name is not a file, it adds '.py' and searches again.
90 If the given name is not a file, it adds '.py' and searches again.
91 Raises IOError with an informative message if the file isn't found.
91 Raises IOError with an informative message if the file isn't found.
92
92
93 On Windows, apply Windows semantics to the filename. In particular, remove
93 On Windows, apply Windows semantics to the filename. In particular, remove
94 any quoting that has been applied to it. This option can be forced for
94 any quoting that has been applied to it. This option can be forced for
95 testing purposes.
95 testing purposes.
96 """
96 """
97
97
98 name = os.path.expanduser(name)
98 name = os.path.expanduser(name)
99 if force_win32 is None:
99 if force_win32 is None:
100 win32 = (sys.platform == 'win32')
100 win32 = (sys.platform == 'win32')
101 else:
101 else:
102 win32 = force_win32
102 win32 = force_win32
103 name = unquote_filename(name, win32=win32)
103 name = unquote_filename(name, win32=win32)
104 if not os.path.isfile(name) and not name.endswith('.py'):
104 if not os.path.isfile(name) and not name.endswith('.py'):
105 name += '.py'
105 name += '.py'
106 if os.path.isfile(name):
106 if os.path.isfile(name):
107 return name
107 return name
108 else:
108 else:
109 raise IOError,'File `%s` not found.' % name
109 raise IOError,'File `%s` not found.' % name
110
110
111
111
112 def filefind(filename, path_dirs=None):
112 def filefind(filename, path_dirs=None):
113 """Find a file by looking through a sequence of paths.
113 """Find a file by looking through a sequence of paths.
114
114
115 This iterates through a sequence of paths looking for a file and returns
115 This iterates through a sequence of paths looking for a file and returns
116 the full, absolute path of the first occurence of the file. If no set of
116 the full, absolute path of the first occurence of the file. If no set of
117 path dirs is given, the filename is tested as is, after running through
117 path dirs is given, the filename is tested as is, after running through
118 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
118 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
119
119
120 filefind('myfile.txt')
120 filefind('myfile.txt')
121
121
122 will find the file in the current working dir, but::
122 will find the file in the current working dir, but::
123
123
124 filefind('~/myfile.txt')
124 filefind('~/myfile.txt')
125
125
126 Will find the file in the users home directory. This function does not
126 Will find the file in the users home directory. This function does not
127 automatically try any paths, such as the cwd or the user's home directory.
127 automatically try any paths, such as the cwd or the user's home directory.
128
128
129 Parameters
129 Parameters
130 ----------
130 ----------
131 filename : str
131 filename : str
132 The filename to look for.
132 The filename to look for.
133 path_dirs : str, None or sequence of str
133 path_dirs : str, None or sequence of str
134 The sequence of paths to look for the file in. If None, the filename
134 The sequence of paths to look for the file in. If None, the filename
135 need to be absolute or be in the cwd. If a string, the string is
135 need to be absolute or be in the cwd. If a string, the string is
136 put into a sequence and the searched. If a sequence, walk through
136 put into a sequence and the searched. If a sequence, walk through
137 each element and join with ``filename``, calling :func:`expandvars`
137 each element and join with ``filename``, calling :func:`expandvars`
138 and :func:`expanduser` before testing for existence.
138 and :func:`expanduser` before testing for existence.
139
139
140 Returns
140 Returns
141 -------
141 -------
142 Raises :exc:`IOError` or returns absolute path to file.
142 Raises :exc:`IOError` or returns absolute path to file.
143 """
143 """
144
144
145 # If paths are quoted, abspath gets confused, strip them...
145 # If paths are quoted, abspath gets confused, strip them...
146 filename = filename.strip('"').strip("'")
146 filename = filename.strip('"').strip("'")
147 # If the input is an absolute path, just check it exists
147 # If the input is an absolute path, just check it exists
148 if os.path.isabs(filename) and os.path.isfile(filename):
148 if os.path.isabs(filename) and os.path.isfile(filename):
149 return filename
149 return filename
150
150
151 if path_dirs is None:
151 if path_dirs is None:
152 path_dirs = ("",)
152 path_dirs = ("",)
153 elif isinstance(path_dirs, basestring):
153 elif isinstance(path_dirs, basestring):
154 path_dirs = (path_dirs,)
154 path_dirs = (path_dirs,)
155
155
156 for path in path_dirs:
156 for path in path_dirs:
157 if path == '.': path = os.getcwdu()
157 if path == '.': path = os.getcwdu()
158 testname = expand_path(os.path.join(path, filename))
158 testname = expand_path(os.path.join(path, filename))
159 if os.path.isfile(testname):
159 if os.path.isfile(testname):
160 return os.path.abspath(testname)
160 return os.path.abspath(testname)
161
161
162 raise IOError("File %r does not exist in any of the search paths: %r" %
162 raise IOError("File %r does not exist in any of the search paths: %r" %
163 (filename, path_dirs) )
163 (filename, path_dirs) )
164
164
165
165
166 class HomeDirError(Exception):
166 class HomeDirError(Exception):
167 pass
167 pass
168
168
169
169
170 def get_home_dir():
170 def get_home_dir(require_writable=False):
171 """Return the closest possible equivalent to a 'home' directory.
171 """Return the 'home' directory, as a unicode string.
172
173 * On POSIX, we try $HOME.
174 * On Windows we try:
175 - %HOMESHARE%
176 - %HOMEDRIVE\%HOMEPATH%
177 - %USERPROFILE%
178 - Registry hack for My Documents
179 - %HOME%: rare, but some people with unix-like setups may have defined it
180 * On Dos C:\
181
182 Currently only Posix and NT are implemented, a HomeDirError exception is
183 raised for all other OSes.
184 """
185
172
186 env = os.environ
173 * First, check for frozen env in case of py2exe
174 * Otherwise, defer to os.path.expanduser('~')
175
176 See stdlib docs for how this is determined.
177 $HOME is first priority on *ALL* platforms.
178
179 Parameters
180 ----------
181
182 require_writable : bool [default: False]
183 if True:
184 guarantees the return value is a writable directory, otherwise
185 raises HomeDirError
186 if False:
187 The path is resolved, but it is not guaranteed to exist or be writable.
188 """
187
189
188 # first, check py2exe distribution root directory for _ipython.
190 # first, check py2exe distribution root directory for _ipython.
189 # This overrides all. Normally does not exist.
191 # This overrides all. Normally does not exist.
190
192
191 if hasattr(sys, "frozen"): #Is frozen by py2exe
193 if hasattr(sys, "frozen"): #Is frozen by py2exe
192 if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file
194 if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file
193 root, rest = IPython.__file__.lower().split('library.zip')
195 root, rest = IPython.__file__.lower().split('library.zip')
194 else:
196 else:
195 root=os.path.join(os.path.split(IPython.__file__)[0],"../../")
197 root=os.path.join(os.path.split(IPython.__file__)[0],"../../")
196 root=os.path.abspath(root).rstrip('\\')
198 root=os.path.abspath(root).rstrip('\\')
197 if _writable_dir(os.path.join(root, '_ipython')):
199 if _writable_dir(os.path.join(root, '_ipython')):
198 os.environ["IPYKITROOT"] = root
200 os.environ["IPYKITROOT"] = root
199 return py3compat.cast_unicode(root, fs_encoding)
201 return py3compat.cast_unicode(root, fs_encoding)
200
202
201 if os.name == 'posix':
203 homedir = os.path.expanduser('~')
202 # Linux, Unix, AIX, OS X
204
203 try:
205 if not _writable_dir(homedir) and os.name == 'nt':
204 homedir = env['HOME']
206 # expanduser failed, use the registry to get the 'My Documents' folder.
205 except KeyError:
206 # Last-ditch attempt at finding a suitable $HOME, on systems where
207 # it may not be defined in the environment but the system shell
208 # still knows it - reported once as:
209 # https://github.com/ipython/ipython/issues/154
210 from subprocess import Popen, PIPE
211 homedir = Popen('echo $HOME', shell=True,
212 stdout=PIPE).communicate()[0].strip()
213 if homedir:
214 return py3compat.cast_unicode(homedir, fs_encoding)
215 else:
216 raise HomeDirError('Undefined $HOME, IPython cannot proceed.')
217 else:
218 return py3compat.cast_unicode(homedir, fs_encoding)
219 elif os.name == 'nt':
220 # Now for win9x, XP, Vista, 7?
221 # For some strange reason all of these return 'nt' for os.name.
222 # First look for a network home directory. This will return the UNC
223 # path (\\server\\Users\%username%) not the mapped path (Z:\). This
224 # is needed when running IPython on cluster where all paths have to
225 # be UNC.
226 try:
227 homedir = env['HOMESHARE']
228 except KeyError:
229 pass
230 else:
231 if _writable_dir(homedir):
232 return py3compat.cast_unicode(homedir, fs_encoding)
233
234 # Now look for a local home directory
235 try:
236 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
237 except KeyError:
238 pass
239 else:
240 if _writable_dir(homedir):
241 return py3compat.cast_unicode(homedir, fs_encoding)
242
243 # Now the users profile directory
244 try:
245 homedir = os.path.join(env['USERPROFILE'])
246 except KeyError:
247 pass
248 else:
249 if _writable_dir(homedir):
250 return py3compat.cast_unicode(homedir, fs_encoding)
251
252 # Use the registry to get the 'My Documents' folder.
253 try:
207 try:
254 import _winreg as wreg
208 import _winreg as wreg
255 key = wreg.OpenKey(
209 key = wreg.OpenKey(
256 wreg.HKEY_CURRENT_USER,
210 wreg.HKEY_CURRENT_USER,
257 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
211 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
258 )
212 )
259 homedir = wreg.QueryValueEx(key,'Personal')[0]
213 homedir = wreg.QueryValueEx(key,'Personal')[0]
260 key.Close()
214 key.Close()
261 except:
215 except:
262 pass
216 pass
263 else:
217
264 if _writable_dir(homedir):
218 if (not require_writable) or _writable_dir(homedir):
265 return py3compat.cast_unicode(homedir, fs_encoding)
219 return py3compat.cast_unicode(homedir, fs_encoding)
266
267 # A user with a lot of unix tools in win32 may have defined $HOME.
268 # Try this as a last ditch option.
269 try:
270 homedir = env['HOME']
271 except KeyError:
272 pass
273 else:
274 if _writable_dir(homedir):
275 return py3compat.cast_unicode(homedir, fs_encoding)
276
277 # If all else fails, raise HomeDirError
278 raise HomeDirError('No valid home directory could be found')
279 elif os.name == 'dos':
280 # Desperate, may do absurd things in classic MacOS. May work under DOS.
281 return u'C:\\'
282 else:
220 else:
283 raise HomeDirError('No valid home directory could be found for your OS')
221 raise HomeDirError('%s is not a writable dir, '
222 'set $HOME environment variable to override' % homedir)
284
223
285 def get_xdg_dir():
224 def get_xdg_dir():
286 """Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
225 """Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
287
226
288 This is only for posix (Linux,Unix,OS X, etc) systems.
227 This is only for posix (Linux,Unix,OS X, etc) systems.
289 """
228 """
290
229
291 env = os.environ
230 env = os.environ
292
231
293 if os.name == 'posix':
232 if os.name == 'posix':
294 # Linux, Unix, AIX, OS X
233 # Linux, Unix, AIX, OS X
295 # use ~/.config if not set OR empty
234 # use ~/.config if empty OR not set
296 xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
235 xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
297 if xdg and _writable_dir(xdg):
236 if xdg and _writable_dir(xdg):
298 return py3compat.cast_unicode(xdg, fs_encoding)
237 return py3compat.cast_unicode(xdg, fs_encoding)
299
238
300 return None
239 return None
301
240
302
241
303 def get_ipython_dir():
242 def get_ipython_dir():
304 """Get the IPython directory for this platform and user.
243 """Get the IPython directory for this platform and user.
305
244
306 This uses the logic in `get_home_dir` to find the home directory
245 This uses the logic in `get_home_dir` to find the home directory
307 and then adds .ipython to the end of the path.
246 and then adds .ipython to the end of the path.
308 """
247 """
309
248
310 env = os.environ
249 env = os.environ
311 pjoin = os.path.join
250 pjoin = os.path.join
312
251
313
252
314 ipdir_def = '.ipython'
253 ipdir_def = '.ipython'
315 xdg_def = 'ipython'
254 xdg_def = 'ipython'
316
255
317 home_dir = get_home_dir()
256 home_dir = get_home_dir()
318 xdg_dir = get_xdg_dir()
257 xdg_dir = get_xdg_dir()
258
319 # import pdb; pdb.set_trace() # dbg
259 # import pdb; pdb.set_trace() # dbg
320 ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None))
260 ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None))
321 if ipdir is None:
261 if ipdir is None:
322 # not set explicitly, use XDG_CONFIG_HOME or HOME
262 # not set explicitly, use XDG_CONFIG_HOME or HOME
323 home_ipdir = pjoin(home_dir, ipdir_def)
263 home_ipdir = pjoin(home_dir, ipdir_def)
324 if xdg_dir:
264 if xdg_dir:
325 # use XDG, as long as the user isn't already
265 # use XDG, as long as the user isn't already
326 # using $HOME/.ipython and *not* XDG/ipython
266 # using $HOME/.ipython and *not* XDG/ipython
327
267
328 xdg_ipdir = pjoin(xdg_dir, xdg_def)
268 xdg_ipdir = pjoin(xdg_dir, xdg_def)
329
269
330 if _writable_dir(xdg_ipdir) or not _writable_dir(home_ipdir):
270 if _writable_dir(xdg_ipdir) or not _writable_dir(home_ipdir):
331 ipdir = xdg_ipdir
271 ipdir = xdg_ipdir
332
272
333 if ipdir is None:
273 if ipdir is None:
334 # not using XDG
274 # not using XDG
335 ipdir = home_ipdir
275 ipdir = home_ipdir
336
276
337 ipdir = os.path.normpath(os.path.expanduser(ipdir))
277 ipdir = os.path.normpath(os.path.expanduser(ipdir))
338
278
339 if os.path.exists(ipdir) and not _writable_dir(ipdir):
279 if os.path.exists(ipdir) and not _writable_dir(ipdir):
340 # ipdir exists, but is not writable
280 # ipdir exists, but is not writable
341 warnings.warn("IPython dir '%s' is not a writable location,"
281 warnings.warn("IPython dir '%s' is not a writable location,"
342 " using a temp directory."%ipdir)
282 " using a temp directory."%ipdir)
343 ipdir = tempfile.mkdtemp()
283 ipdir = tempfile.mkdtemp()
344 elif not os.path.exists(ipdir):
284 elif not os.path.exists(ipdir):
345 parent = ipdir.rsplit(os.path.sep, 1)[0]
285 parent = ipdir.rsplit(os.path.sep, 1)[0]
346 if not _writable_dir(parent):
286 if not _writable_dir(parent):
347 # ipdir does not exist and parent isn't writable
287 # ipdir does not exist and parent isn't writable
348 warnings.warn("IPython parent '%s' is not a writable location,"
288 warnings.warn("IPython parent '%s' is not a writable location,"
349 " using a temp directory."%parent)
289 " using a temp directory."%parent)
350 ipdir = tempfile.mkdtemp()
290 ipdir = tempfile.mkdtemp()
351
291
352 return py3compat.cast_unicode(ipdir, fs_encoding)
292 return py3compat.cast_unicode(ipdir, fs_encoding)
353
293
354
294
355 def get_ipython_package_dir():
295 def get_ipython_package_dir():
356 """Get the base directory where IPython itself is installed."""
296 """Get the base directory where IPython itself is installed."""
357 ipdir = os.path.dirname(IPython.__file__)
297 ipdir = os.path.dirname(IPython.__file__)
358 return py3compat.cast_unicode(ipdir, fs_encoding)
298 return py3compat.cast_unicode(ipdir, fs_encoding)
359
299
360
300
361 def get_ipython_module_path(module_str):
301 def get_ipython_module_path(module_str):
362 """Find the path to an IPython module in this version of IPython.
302 """Find the path to an IPython module in this version of IPython.
363
303
364 This will always find the version of the module that is in this importable
304 This will always find the version of the module that is in this importable
365 IPython package. This will always return the path to the ``.py``
305 IPython package. This will always return the path to the ``.py``
366 version of the module.
306 version of the module.
367 """
307 """
368 if module_str == 'IPython':
308 if module_str == 'IPython':
369 return os.path.join(get_ipython_package_dir(), '__init__.py')
309 return os.path.join(get_ipython_package_dir(), '__init__.py')
370 mod = import_item(module_str)
310 mod = import_item(module_str)
371 the_path = mod.__file__.replace('.pyc', '.py')
311 the_path = mod.__file__.replace('.pyc', '.py')
372 the_path = the_path.replace('.pyo', '.py')
312 the_path = the_path.replace('.pyo', '.py')
373 return py3compat.cast_unicode(the_path, fs_encoding)
313 return py3compat.cast_unicode(the_path, fs_encoding)
374
314
375 def locate_profile(profile='default'):
315 def locate_profile(profile='default'):
376 """Find the path to the folder associated with a given profile.
316 """Find the path to the folder associated with a given profile.
377
317
378 I.e. find $IPYTHON_DIR/profile_whatever.
318 I.e. find $IPYTHON_DIR/profile_whatever.
379 """
319 """
380 from IPython.core.profiledir import ProfileDir, ProfileDirError
320 from IPython.core.profiledir import ProfileDir, ProfileDirError
381 try:
321 try:
382 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
322 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
383 except ProfileDirError:
323 except ProfileDirError:
384 # IOError makes more sense when people are expecting a path
324 # IOError makes more sense when people are expecting a path
385 raise IOError("Couldn't find profile %r" % profile)
325 raise IOError("Couldn't find profile %r" % profile)
386 return pd.location
326 return pd.location
387
327
388 def expand_path(s):
328 def expand_path(s):
389 """Expand $VARS and ~names in a string, like a shell
329 """Expand $VARS and ~names in a string, like a shell
390
330
391 :Examples:
331 :Examples:
392
332
393 In [2]: os.environ['FOO']='test'
333 In [2]: os.environ['FOO']='test'
394
334
395 In [3]: expand_path('variable FOO is $FOO')
335 In [3]: expand_path('variable FOO is $FOO')
396 Out[3]: 'variable FOO is test'
336 Out[3]: 'variable FOO is test'
397 """
337 """
398 # This is a pretty subtle hack. When expand user is given a UNC path
338 # This is a pretty subtle hack. When expand user is given a UNC path
399 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
339 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
400 # the $ to get (\\server\share\%username%). I think it considered $
340 # the $ to get (\\server\share\%username%). I think it considered $
401 # alone an empty var. But, we need the $ to remains there (it indicates
341 # alone an empty var. But, we need the $ to remains there (it indicates
402 # a hidden share).
342 # a hidden share).
403 if os.name=='nt':
343 if os.name=='nt':
404 s = s.replace('$\\', 'IPYTHON_TEMP')
344 s = s.replace('$\\', 'IPYTHON_TEMP')
405 s = os.path.expandvars(os.path.expanduser(s))
345 s = os.path.expandvars(os.path.expanduser(s))
406 if os.name=='nt':
346 if os.name=='nt':
407 s = s.replace('IPYTHON_TEMP', '$\\')
347 s = s.replace('IPYTHON_TEMP', '$\\')
408 return s
348 return s
409
349
410
350
411 def target_outdated(target,deps):
351 def target_outdated(target,deps):
412 """Determine whether a target is out of date.
352 """Determine whether a target is out of date.
413
353
414 target_outdated(target,deps) -> 1/0
354 target_outdated(target,deps) -> 1/0
415
355
416 deps: list of filenames which MUST exist.
356 deps: list of filenames which MUST exist.
417 target: single filename which may or may not exist.
357 target: single filename which may or may not exist.
418
358
419 If target doesn't exist or is older than any file listed in deps, return
359 If target doesn't exist or is older than any file listed in deps, return
420 true, otherwise return false.
360 true, otherwise return false.
421 """
361 """
422 try:
362 try:
423 target_time = os.path.getmtime(target)
363 target_time = os.path.getmtime(target)
424 except os.error:
364 except os.error:
425 return 1
365 return 1
426 for dep in deps:
366 for dep in deps:
427 dep_time = os.path.getmtime(dep)
367 dep_time = os.path.getmtime(dep)
428 if dep_time > target_time:
368 if dep_time > target_time:
429 #print "For target",target,"Dep failed:",dep # dbg
369 #print "For target",target,"Dep failed:",dep # dbg
430 #print "times (dep,tar):",dep_time,target_time # dbg
370 #print "times (dep,tar):",dep_time,target_time # dbg
431 return 1
371 return 1
432 return 0
372 return 0
433
373
434
374
435 def target_update(target,deps,cmd):
375 def target_update(target,deps,cmd):
436 """Update a target with a given command given a list of dependencies.
376 """Update a target with a given command given a list of dependencies.
437
377
438 target_update(target,deps,cmd) -> runs cmd if target is outdated.
378 target_update(target,deps,cmd) -> runs cmd if target is outdated.
439
379
440 This is just a wrapper around target_outdated() which calls the given
380 This is just a wrapper around target_outdated() which calls the given
441 command if target is outdated."""
381 command if target is outdated."""
442
382
443 if target_outdated(target,deps):
383 if target_outdated(target,deps):
444 system(cmd)
384 system(cmd)
445
385
446 def filehash(path):
386 def filehash(path):
447 """Make an MD5 hash of a file, ignoring any differences in line
387 """Make an MD5 hash of a file, ignoring any differences in line
448 ending characters."""
388 ending characters."""
449 with open(path, "rU") as f:
389 with open(path, "rU") as f:
450 return md5(py3compat.str_to_bytes(f.read())).hexdigest()
390 return md5(py3compat.str_to_bytes(f.read())).hexdigest()
451
391
452 # If the config is unmodified from the default, we'll just delete it.
392 # If the config is unmodified from the default, we'll just delete it.
453 # These are consistent for 0.10.x, thankfully. We're not going to worry about
393 # These are consistent for 0.10.x, thankfully. We're not going to worry about
454 # older versions.
394 # older versions.
455 old_config_md5 = {'ipy_user_conf.py': 'fc108bedff4b9a00f91fa0a5999140d3',
395 old_config_md5 = {'ipy_user_conf.py': 'fc108bedff4b9a00f91fa0a5999140d3',
456 'ipythonrc': '12a68954f3403eea2eec09dc8fe5a9b5'}
396 'ipythonrc': '12a68954f3403eea2eec09dc8fe5a9b5'}
457
397
458 def check_for_old_config(ipython_dir=None):
398 def check_for_old_config(ipython_dir=None):
459 """Check for old config files, and present a warning if they exist.
399 """Check for old config files, and present a warning if they exist.
460
400
461 A link to the docs of the new config is included in the message.
401 A link to the docs of the new config is included in the message.
462
402
463 This should mitigate confusion with the transition to the new
403 This should mitigate confusion with the transition to the new
464 config system in 0.11.
404 config system in 0.11.
465 """
405 """
466 if ipython_dir is None:
406 if ipython_dir is None:
467 ipython_dir = get_ipython_dir()
407 ipython_dir = get_ipython_dir()
468
408
469 old_configs = ['ipy_user_conf.py', 'ipythonrc', 'ipython_config.py']
409 old_configs = ['ipy_user_conf.py', 'ipythonrc', 'ipython_config.py']
470 warned = False
410 warned = False
471 for cfg in old_configs:
411 for cfg in old_configs:
472 f = os.path.join(ipython_dir, cfg)
412 f = os.path.join(ipython_dir, cfg)
473 if os.path.exists(f):
413 if os.path.exists(f):
474 if filehash(f) == old_config_md5.get(cfg, ''):
414 if filehash(f) == old_config_md5.get(cfg, ''):
475 os.unlink(f)
415 os.unlink(f)
476 else:
416 else:
477 warnings.warn("Found old IPython config file %r (modified by user)"%f)
417 warnings.warn("Found old IPython config file %r (modified by user)"%f)
478 warned = True
418 warned = True
479
419
480 if warned:
420 if warned:
481 warnings.warn("""
421 warnings.warn("""
482 The IPython configuration system has changed as of 0.11, and these files will
422 The IPython configuration system has changed as of 0.11, and these files will
483 be ignored. See http://ipython.github.com/ipython-doc/dev/config for details
423 be ignored. See http://ipython.github.com/ipython-doc/dev/config for details
484 of the new config system.
424 of the new config system.
485 To start configuring IPython, do `ipython profile create`, and edit
425 To start configuring IPython, do `ipython profile create`, and edit
486 `ipython_config.py` in <ipython_dir>/profile_default.
426 `ipython_config.py` in <ipython_dir>/profile_default.
487 If you need to leave the old config files in place for an older version of
427 If you need to leave the old config files in place for an older version of
488 IPython and want to suppress this warning message, set
428 IPython and want to suppress this warning message, set
489 `c.InteractiveShellApp.ignore_old_config=True` in the new config.""")
429 `c.InteractiveShellApp.ignore_old_config=True` in the new config.""")
490
430
491 def get_security_file(filename, profile='default'):
431 def get_security_file(filename, profile='default'):
492 """Return the absolute path of a security file given by filename and profile
432 """Return the absolute path of a security file given by filename and profile
493
433
494 This allows users and developers to find security files without
434 This allows users and developers to find security files without
495 knowledge of the IPython directory structure. The search path
435 knowledge of the IPython directory structure. The search path
496 will be ['.', profile.security_dir]
436 will be ['.', profile.security_dir]
497
437
498 Parameters
438 Parameters
499 ----------
439 ----------
500
440
501 filename : str
441 filename : str
502 The file to be found. If it is passed as an absolute path, it will
442 The file to be found. If it is passed as an absolute path, it will
503 simply be returned.
443 simply be returned.
504 profile : str [default: 'default']
444 profile : str [default: 'default']
505 The name of the profile to search. Leaving this unspecified
445 The name of the profile to search. Leaving this unspecified
506 The file to be found. If it is passed as an absolute path, fname will
446 The file to be found. If it is passed as an absolute path, fname will
507 simply be returned.
447 simply be returned.
508
448
509 Returns
449 Returns
510 -------
450 -------
511 Raises :exc:`IOError` if file not found or returns absolute path to file.
451 Raises :exc:`IOError` if file not found or returns absolute path to file.
512 """
452 """
513 # import here, because profiledir also imports from utils.path
453 # import here, because profiledir also imports from utils.path
514 from IPython.core.profiledir import ProfileDir
454 from IPython.core.profiledir import ProfileDir
515 try:
455 try:
516 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
456 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
517 except Exception:
457 except Exception:
518 # will raise ProfileDirError if no such profile
458 # will raise ProfileDirError if no such profile
519 raise IOError("Profile %r not found")
459 raise IOError("Profile %r not found")
520 return filefind(filename, ['.', pd.security_dir])
460 return filefind(filename, ['.', pd.security_dir])
521
461
@@ -1,445 +1,407 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Tests for IPython.utils.path.py"""
2 """Tests for IPython.utils.path.py"""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2008-2011 The IPython Development Team
5 # Copyright (C) 2008-2011 The IPython Development Team
6 #
6 #
7 # Distributed under the terms of the BSD License. The full license is in
7 # Distributed under the terms of the BSD License. The full license is in
8 # the file COPYING, distributed as part of this software.
8 # the file COPYING, distributed as part of this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 from __future__ import with_statement
15 from __future__ import with_statement
16
16
17 import os
17 import os
18 import shutil
18 import shutil
19 import sys
19 import sys
20 import tempfile
20 import tempfile
21 from io import StringIO
21 from io import StringIO
22
22
23 from os.path import join, abspath, split
23 from os.path import join, abspath, split
24
24
25 import nose.tools as nt
25 import nose.tools as nt
26
26
27 from nose import with_setup
27 from nose import with_setup
28
28
29 import IPython
29 import IPython
30 from IPython.testing import decorators as dec
30 from IPython.testing import decorators as dec
31 from IPython.testing.decorators import skip_if_not_win32, skip_win32
31 from IPython.testing.decorators import skip_if_not_win32, skip_win32
32 from IPython.testing.tools import make_tempfile, AssertPrints
32 from IPython.testing.tools import make_tempfile, AssertPrints
33 from IPython.utils import path, io
33 from IPython.utils import path, io
34 from IPython.utils import py3compat
34 from IPython.utils import py3compat
35
35
36 # Platform-dependent imports
36 # Platform-dependent imports
37 try:
37 try:
38 import _winreg as wreg
38 import _winreg as wreg
39 except ImportError:
39 except ImportError:
40 #Fake _winreg module on none windows platforms
40 #Fake _winreg module on none windows platforms
41 import types
41 import types
42 wr_name = "winreg" if py3compat.PY3 else "_winreg"
42 wr_name = "winreg" if py3compat.PY3 else "_winreg"
43 sys.modules[wr_name] = types.ModuleType(wr_name)
43 sys.modules[wr_name] = types.ModuleType(wr_name)
44 import _winreg as wreg
44 import _winreg as wreg
45 #Add entries that needs to be stubbed by the testing code
45 #Add entries that needs to be stubbed by the testing code
46 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
46 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
47
47
48 try:
48 try:
49 reload
49 reload
50 except NameError: # Python 3
50 except NameError: # Python 3
51 from imp import reload
51 from imp import reload
52
52
53 #-----------------------------------------------------------------------------
53 #-----------------------------------------------------------------------------
54 # Globals
54 # Globals
55 #-----------------------------------------------------------------------------
55 #-----------------------------------------------------------------------------
56 env = os.environ
56 env = os.environ
57 TEST_FILE_PATH = split(abspath(__file__))[0]
57 TEST_FILE_PATH = split(abspath(__file__))[0]
58 TMP_TEST_DIR = tempfile.mkdtemp()
58 TMP_TEST_DIR = tempfile.mkdtemp()
59 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
59 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
60 XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir")
60 XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir")
61 IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython')
61 IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython')
62 #
62 #
63 # Setup/teardown functions/decorators
63 # Setup/teardown functions/decorators
64 #
64 #
65
65
66 def setup():
66 def setup():
67 """Setup testenvironment for the module:
67 """Setup testenvironment for the module:
68
68
69 - Adds dummy home dir tree
69 - Adds dummy home dir tree
70 """
70 """
71 # Do not mask exceptions here. In particular, catching WindowsError is a
71 # Do not mask exceptions here. In particular, catching WindowsError is a
72 # problem because that exception is only defined on Windows...
72 # problem because that exception is only defined on Windows...
73 os.makedirs(IP_TEST_DIR)
73 os.makedirs(IP_TEST_DIR)
74 os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython'))
74 os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython'))
75
75
76
76
77 def teardown():
77 def teardown():
78 """Teardown testenvironment for the module:
78 """Teardown testenvironment for the module:
79
79
80 - Remove dummy home dir tree
80 - Remove dummy home dir tree
81 """
81 """
82 # Note: we remove the parent test dir, which is the root of all test
82 # Note: we remove the parent test dir, which is the root of all test
83 # subdirs we may have created. Use shutil instead of os.removedirs, so
83 # subdirs we may have created. Use shutil instead of os.removedirs, so
84 # that non-empty directories are all recursively removed.
84 # that non-empty directories are all recursively removed.
85 shutil.rmtree(TMP_TEST_DIR)
85 shutil.rmtree(TMP_TEST_DIR)
86
86
87
87
88 def setup_environment():
88 def setup_environment():
89 """Setup testenvironment for some functions that are tested
89 """Setup testenvironment for some functions that are tested
90 in this module. In particular this functions stores attributes
90 in this module. In particular this functions stores attributes
91 and other things that we need to stub in some test functions.
91 and other things that we need to stub in some test functions.
92 This needs to be done on a function level and not module level because
92 This needs to be done on a function level and not module level because
93 each testfunction needs a pristine environment.
93 each testfunction needs a pristine environment.
94 """
94 """
95 global oldstuff, platformstuff
95 global oldstuff, platformstuff
96 oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__, os.getcwd())
96 oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__, os.getcwd())
97
97
98 if os.name == 'nt':
98 if os.name == 'nt':
99 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
99 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
100
100
101
101
102 def teardown_environment():
102 def teardown_environment():
103 """Restore things that were remebered by the setup_environment function
103 """Restore things that were remebered by the setup_environment function
104 """
104 """
105 (oldenv, os.name, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
105 (oldenv, os.name, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
106 os.chdir(old_wd)
106 os.chdir(old_wd)
107 reload(path)
107 reload(path)
108
108
109 for key in env.keys():
109 for key in env.keys():
110 if key not in oldenv:
110 if key not in oldenv:
111 del env[key]
111 del env[key]
112 env.update(oldenv)
112 env.update(oldenv)
113 if hasattr(sys, 'frozen'):
113 if hasattr(sys, 'frozen'):
114 del sys.frozen
114 del sys.frozen
115 if os.name == 'nt':
115 if os.name == 'nt':
116 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
116 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
117
117
118 # Build decorator that uses the setup_environment/setup_environment
118 # Build decorator that uses the setup_environment/setup_environment
119 with_environment = with_setup(setup_environment, teardown_environment)
119 with_environment = with_setup(setup_environment, teardown_environment)
120
120
121
122 @skip_if_not_win32
121 @skip_if_not_win32
123 @with_environment
122 @with_environment
124 def test_get_home_dir_1():
123 def test_get_home_dir_1():
125 """Testcase for py2exe logic, un-compressed lib
124 """Testcase for py2exe logic, un-compressed lib
126 """
125 """
127 sys.frozen = True
126 sys.frozen = True
128
127
129 #fake filename for IPython.__init__
128 #fake filename for IPython.__init__
130 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
129 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
131
130
132 home_dir = path.get_home_dir()
131 home_dir = path.get_home_dir()
133 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
132 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
134
133
135
134
136 @skip_if_not_win32
135 @skip_if_not_win32
137 @with_environment
136 @with_environment
138 def test_get_home_dir_2():
137 def test_get_home_dir_2():
139 """Testcase for py2exe logic, compressed lib
138 """Testcase for py2exe logic, compressed lib
140 """
139 """
141 sys.frozen = True
140 sys.frozen = True
142 #fake filename for IPython.__init__
141 #fake filename for IPython.__init__
143 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
142 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
144
143
145 home_dir = path.get_home_dir()
144 home_dir = path.get_home_dir(True)
146 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
145 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
147
146
148
147
149 @with_environment
148 @with_environment
150 @skip_win32
151 def test_get_home_dir_3():
149 def test_get_home_dir_3():
152 """Testcase $HOME is set, then use its value as home directory."""
150 """get_home_dir() uses $HOME if set"""
153 env["HOME"] = HOME_TEST_DIR
151 env["HOME"] = HOME_TEST_DIR
154 home_dir = path.get_home_dir()
152 home_dir = path.get_home_dir(True)
155 nt.assert_equal(home_dir, env["HOME"])
153 nt.assert_equal(home_dir, env["HOME"])
156
154
157
155
158 @with_environment
156 @with_environment
159 @skip_win32
160 def test_get_home_dir_4():
157 def test_get_home_dir_4():
161 """Testcase $HOME is not set, os=='posix'.
158 """get_home_dir() still works if $HOME is not set"""
162 This should fail with HomeDirError"""
163
159
164 os.name = 'posix'
165 if 'HOME' in env: del env['HOME']
160 if 'HOME' in env: del env['HOME']
166 nt.assert_raises(path.HomeDirError, path.get_home_dir)
161 # this should still succeed, but we don't know what the answer should be
162 home = path.get_home_dir(True)
163 nt.assert_true(path._writable_dir(home))
167
164
168
169 @skip_if_not_win32
170 @with_environment
165 @with_environment
171 def test_get_home_dir_5():
166 def test_get_home_dir_5():
172 """Using HOMEDRIVE + HOMEPATH, os=='nt'.
167 """raise HomeDirError if $HOME is specified, but not a writable dir"""
173
168 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
174 HOMESHARE is missing.
169 # set os.name = posix, to prevent My Documents fallback on Windows
175 """
170 os.name = 'posix'
176
171 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
177 os.name = 'nt'
178 env.pop('HOMESHARE', None)
179 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR)
180 home_dir = path.get_home_dir()
181 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
182
183
184 @skip_if_not_win32
185 @with_environment
186 def test_get_home_dir_6():
187 """Using USERPROFILE, os=='nt'.
188
189 HOMESHARE, HOMEDRIVE, HOMEPATH are missing.
190 """
191
192 os.name = 'nt'
193 env.pop('HOMESHARE', None)
194 env.pop('HOMEDRIVE', None)
195 env.pop('HOMEPATH', None)
196 env["USERPROFILE"] = abspath(HOME_TEST_DIR)
197 home_dir = path.get_home_dir()
198 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
199
200
201 @skip_if_not_win32
202 @with_environment
203 def test_get_home_dir_7():
204 """Using HOMESHARE, os=='nt'."""
205
206 os.name = 'nt'
207 env["HOMESHARE"] = abspath(HOME_TEST_DIR)
208 home_dir = path.get_home_dir()
209 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
210
172
211
173
212 # Should we stub wreg fully so we can run the test on all platforms?
174 # Should we stub wreg fully so we can run the test on all platforms?
213 @skip_if_not_win32
175 @skip_if_not_win32
214 @with_environment
176 @with_environment
215 def test_get_home_dir_8():
177 def test_get_home_dir_8():
216 """Using registry hack for 'My Documents', os=='nt'
178 """Using registry hack for 'My Documents', os=='nt'
217
179
218 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
180 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
219 """
181 """
220 os.name = 'nt'
182 os.name = 'nt'
221 # Remove from stub environment all keys that may be set
183 # Remove from stub environment all keys that may be set
222 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
184 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
223 env.pop(key, None)
185 env.pop(key, None)
224
186
225 #Stub windows registry functions
187 #Stub windows registry functions
226 def OpenKey(x, y):
188 def OpenKey(x, y):
227 class key:
189 class key:
228 def Close(self):
190 def Close(self):
229 pass
191 pass
230 return key()
192 return key()
231 def QueryValueEx(x, y):
193 def QueryValueEx(x, y):
232 return [abspath(HOME_TEST_DIR)]
194 return [abspath(HOME_TEST_DIR)]
233
195
234 wreg.OpenKey = OpenKey
196 wreg.OpenKey = OpenKey
235 wreg.QueryValueEx = QueryValueEx
197 wreg.QueryValueEx = QueryValueEx
236
198
237 home_dir = path.get_home_dir()
199 home_dir = path.get_home_dir()
238 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
200 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
239
201
240
202
241 @with_environment
203 @with_environment
242 def test_get_ipython_dir_1():
204 def test_get_ipython_dir_1():
243 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
205 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
244 env_ipdir = os.path.join("someplace", ".ipython")
206 env_ipdir = os.path.join("someplace", ".ipython")
245 path._writable_dir = lambda path: True
207 path._writable_dir = lambda path: True
246 env['IPYTHON_DIR'] = env_ipdir
208 env['IPYTHON_DIR'] = env_ipdir
247 ipdir = path.get_ipython_dir()
209 ipdir = path.get_ipython_dir()
248 nt.assert_equal(ipdir, env_ipdir)
210 nt.assert_equal(ipdir, env_ipdir)
249
211
250
212
251 @with_environment
213 @with_environment
252 def test_get_ipython_dir_2():
214 def test_get_ipython_dir_2():
253 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
215 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
254 path.get_home_dir = lambda : "someplace"
216 path.get_home_dir = lambda : "someplace"
255 path.get_xdg_dir = lambda : None
217 path.get_xdg_dir = lambda : None
256 path._writable_dir = lambda path: True
218 path._writable_dir = lambda path: True
257 os.name = "posix"
219 os.name = "posix"
258 env.pop('IPYTHON_DIR', None)
220 env.pop('IPYTHON_DIR', None)
259 env.pop('IPYTHONDIR', None)
221 env.pop('IPYTHONDIR', None)
260 env.pop('XDG_CONFIG_HOME', None)
222 env.pop('XDG_CONFIG_HOME', None)
261 ipdir = path.get_ipython_dir()
223 ipdir = path.get_ipython_dir()
262 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
224 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
263
225
264 @with_environment
226 @with_environment
265 def test_get_ipython_dir_3():
227 def test_get_ipython_dir_3():
266 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
228 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
267 path.get_home_dir = lambda : "someplace"
229 path.get_home_dir = lambda : "someplace"
268 path._writable_dir = lambda path: True
230 path._writable_dir = lambda path: True
269 os.name = "posix"
231 os.name = "posix"
270 env.pop('IPYTHON_DIR', None)
232 env.pop('IPYTHON_DIR', None)
271 env.pop('IPYTHONDIR', None)
233 env.pop('IPYTHONDIR', None)
272 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
234 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
273 ipdir = path.get_ipython_dir()
235 ipdir = path.get_ipython_dir()
274 nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython"))
236 nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython"))
275
237
276 @with_environment
238 @with_environment
277 def test_get_ipython_dir_4():
239 def test_get_ipython_dir_4():
278 """test_get_ipython_dir_4, use XDG if both exist."""
240 """test_get_ipython_dir_4, use XDG if both exist."""
279 path.get_home_dir = lambda : HOME_TEST_DIR
241 path.get_home_dir = lambda : HOME_TEST_DIR
280 os.name = "posix"
242 os.name = "posix"
281 env.pop('IPYTHON_DIR', None)
243 env.pop('IPYTHON_DIR', None)
282 env.pop('IPYTHONDIR', None)
244 env.pop('IPYTHONDIR', None)
283 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
245 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
284 xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython")
246 xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython")
285 ipdir = path.get_ipython_dir()
247 ipdir = path.get_ipython_dir()
286 nt.assert_equal(ipdir, xdg_ipdir)
248 nt.assert_equal(ipdir, xdg_ipdir)
287
249
288 @with_environment
250 @with_environment
289 def test_get_ipython_dir_5():
251 def test_get_ipython_dir_5():
290 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
252 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
291 path.get_home_dir = lambda : HOME_TEST_DIR
253 path.get_home_dir = lambda : HOME_TEST_DIR
292 os.name = "posix"
254 os.name = "posix"
293 env.pop('IPYTHON_DIR', None)
255 env.pop('IPYTHON_DIR', None)
294 env.pop('IPYTHONDIR', None)
256 env.pop('IPYTHONDIR', None)
295 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
257 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
296 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
258 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
297 ipdir = path.get_ipython_dir()
259 ipdir = path.get_ipython_dir()
298 nt.assert_equal(ipdir, IP_TEST_DIR)
260 nt.assert_equal(ipdir, IP_TEST_DIR)
299
261
300 @with_environment
262 @with_environment
301 def test_get_ipython_dir_6():
263 def test_get_ipython_dir_6():
302 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
264 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
303 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
265 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
304 os.mkdir(xdg)
266 os.mkdir(xdg)
305 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
267 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
306 path.get_home_dir = lambda : HOME_TEST_DIR
268 path.get_home_dir = lambda : HOME_TEST_DIR
307 path.get_xdg_dir = lambda : xdg
269 path.get_xdg_dir = lambda : xdg
308 os.name = "posix"
270 os.name = "posix"
309 env.pop('IPYTHON_DIR', None)
271 env.pop('IPYTHON_DIR', None)
310 env.pop('IPYTHONDIR', None)
272 env.pop('IPYTHONDIR', None)
311 env.pop('XDG_CONFIG_HOME', None)
273 env.pop('XDG_CONFIG_HOME', None)
312 xdg_ipdir = os.path.join(xdg, "ipython")
274 xdg_ipdir = os.path.join(xdg, "ipython")
313 ipdir = path.get_ipython_dir()
275 ipdir = path.get_ipython_dir()
314 nt.assert_equal(ipdir, xdg_ipdir)
276 nt.assert_equal(ipdir, xdg_ipdir)
315
277
316 @with_environment
278 @with_environment
317 def test_get_ipython_dir_7():
279 def test_get_ipython_dir_7():
318 """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR"""
280 """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR"""
319 path._writable_dir = lambda path: True
281 path._writable_dir = lambda path: True
320 home_dir = os.path.expanduser('~')
282 home_dir = os.path.expanduser('~')
321 env['IPYTHON_DIR'] = os.path.join('~', 'somewhere')
283 env['IPYTHON_DIR'] = os.path.join('~', 'somewhere')
322 ipdir = path.get_ipython_dir()
284 ipdir = path.get_ipython_dir()
323 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
285 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
324
286
325
287
326 @with_environment
288 @with_environment
327 def test_get_xdg_dir_1():
289 def test_get_xdg_dir_1():
328 """test_get_xdg_dir_1, check xdg_dir"""
290 """test_get_xdg_dir_1, check xdg_dir"""
329 reload(path)
291 reload(path)
330 path._writable_dir = lambda path: True
292 path._writable_dir = lambda path: True
331 path.get_home_dir = lambda : 'somewhere'
293 path.get_home_dir = lambda : 'somewhere'
332 os.name = "posix"
294 os.name = "posix"
333 env.pop('IPYTHON_DIR', None)
295 env.pop('IPYTHON_DIR', None)
334 env.pop('IPYTHONDIR', None)
296 env.pop('IPYTHONDIR', None)
335 env.pop('XDG_CONFIG_HOME', None)
297 env.pop('XDG_CONFIG_HOME', None)
336
298
337 nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config'))
299 nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config'))
338
300
339
301
340 @with_environment
302 @with_environment
341 def test_get_xdg_dir_1():
303 def test_get_xdg_dir_1():
342 """test_get_xdg_dir_1, check nonexistant xdg_dir"""
304 """test_get_xdg_dir_1, check nonexistant xdg_dir"""
343 reload(path)
305 reload(path)
344 path.get_home_dir = lambda : HOME_TEST_DIR
306 path.get_home_dir = lambda : HOME_TEST_DIR
345 os.name = "posix"
307 os.name = "posix"
346 env.pop('IPYTHON_DIR', None)
308 env.pop('IPYTHON_DIR', None)
347 env.pop('IPYTHONDIR', None)
309 env.pop('IPYTHONDIR', None)
348 env.pop('XDG_CONFIG_HOME', None)
310 env.pop('XDG_CONFIG_HOME', None)
349 nt.assert_equal(path.get_xdg_dir(), None)
311 nt.assert_equal(path.get_xdg_dir(), None)
350
312
351 @with_environment
313 @with_environment
352 def test_get_xdg_dir_2():
314 def test_get_xdg_dir_2():
353 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
315 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
354 reload(path)
316 reload(path)
355 path.get_home_dir = lambda : HOME_TEST_DIR
317 path.get_home_dir = lambda : HOME_TEST_DIR
356 os.name = "posix"
318 os.name = "posix"
357 env.pop('IPYTHON_DIR', None)
319 env.pop('IPYTHON_DIR', None)
358 env.pop('IPYTHONDIR', None)
320 env.pop('IPYTHONDIR', None)
359 env.pop('XDG_CONFIG_HOME', None)
321 env.pop('XDG_CONFIG_HOME', None)
360 cfgdir=os.path.join(path.get_home_dir(), '.config')
322 cfgdir=os.path.join(path.get_home_dir(), '.config')
361 os.makedirs(cfgdir)
323 os.makedirs(cfgdir)
362
324
363 nt.assert_equal(path.get_xdg_dir(), cfgdir)
325 nt.assert_equal(path.get_xdg_dir(), cfgdir)
364
326
365 def test_filefind():
327 def test_filefind():
366 """Various tests for filefind"""
328 """Various tests for filefind"""
367 f = tempfile.NamedTemporaryFile()
329 f = tempfile.NamedTemporaryFile()
368 # print 'fname:',f.name
330 # print 'fname:',f.name
369 alt_dirs = path.get_ipython_dir()
331 alt_dirs = path.get_ipython_dir()
370 t = path.filefind(f.name, alt_dirs)
332 t = path.filefind(f.name, alt_dirs)
371 # print 'found:',t
333 # print 'found:',t
372
334
373
335
374 def test_get_ipython_package_dir():
336 def test_get_ipython_package_dir():
375 ipdir = path.get_ipython_package_dir()
337 ipdir = path.get_ipython_package_dir()
376 nt.assert_true(os.path.isdir(ipdir))
338 nt.assert_true(os.path.isdir(ipdir))
377
339
378
340
379 def test_get_ipython_module_path():
341 def test_get_ipython_module_path():
380 ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp')
342 ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp')
381 nt.assert_true(os.path.isfile(ipapp_path))
343 nt.assert_true(os.path.isfile(ipapp_path))
382
344
383
345
384 @dec.skip_if_not_win32
346 @dec.skip_if_not_win32
385 def test_get_long_path_name_win32():
347 def test_get_long_path_name_win32():
386 p = path.get_long_path_name('c:\\docume~1')
348 p = path.get_long_path_name('c:\\docume~1')
387 nt.assert_equals(p,u'c:\\Documents and Settings')
349 nt.assert_equals(p,u'c:\\Documents and Settings')
388
350
389
351
390 @dec.skip_win32
352 @dec.skip_win32
391 def test_get_long_path_name():
353 def test_get_long_path_name():
392 p = path.get_long_path_name('/usr/local')
354 p = path.get_long_path_name('/usr/local')
393 nt.assert_equals(p,'/usr/local')
355 nt.assert_equals(p,'/usr/local')
394
356
395 @dec.skip_win32 # can't create not-user-writable dir on win
357 @dec.skip_win32 # can't create not-user-writable dir on win
396 @with_environment
358 @with_environment
397 def test_not_writable_ipdir():
359 def test_not_writable_ipdir():
398 tmpdir = tempfile.mkdtemp()
360 tmpdir = tempfile.mkdtemp()
399 os.name = "posix"
361 os.name = "posix"
400 env.pop('IPYTHON_DIR', None)
362 env.pop('IPYTHON_DIR', None)
401 env.pop('IPYTHONDIR', None)
363 env.pop('IPYTHONDIR', None)
402 env.pop('XDG_CONFIG_HOME', None)
364 env.pop('XDG_CONFIG_HOME', None)
403 env['HOME'] = tmpdir
365 env['HOME'] = tmpdir
404 ipdir = os.path.join(tmpdir, '.ipython')
366 ipdir = os.path.join(tmpdir, '.ipython')
405 os.mkdir(ipdir)
367 os.mkdir(ipdir)
406 os.chmod(ipdir, 600)
368 os.chmod(ipdir, 600)
407 with AssertPrints('is not a writable location', channel='stderr'):
369 with AssertPrints('is not a writable location', channel='stderr'):
408 ipdir = path.get_ipython_dir()
370 ipdir = path.get_ipython_dir()
409 env.pop('IPYTHON_DIR', None)
371 env.pop('IPYTHON_DIR', None)
410
372
411 def test_unquote_filename():
373 def test_unquote_filename():
412 for win32 in (True, False):
374 for win32 in (True, False):
413 nt.assert_equals(path.unquote_filename('foo.py', win32=win32), 'foo.py')
375 nt.assert_equals(path.unquote_filename('foo.py', win32=win32), 'foo.py')
414 nt.assert_equals(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
376 nt.assert_equals(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
415 nt.assert_equals(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
377 nt.assert_equals(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
416 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
378 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
417 nt.assert_equals(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
379 nt.assert_equals(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
418 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
380 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
419 nt.assert_equals(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
381 nt.assert_equals(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
420 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
382 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
421 nt.assert_equals(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
383 nt.assert_equals(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
422 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
384 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
423
385
424 @with_environment
386 @with_environment
425 def test_get_py_filename():
387 def test_get_py_filename():
426 os.chdir(TMP_TEST_DIR)
388 os.chdir(TMP_TEST_DIR)
427 for win32 in (True, False):
389 for win32 in (True, False):
428 with make_tempfile('foo.py'):
390 with make_tempfile('foo.py'):
429 nt.assert_equals(path.get_py_filename('foo.py', force_win32=win32), 'foo.py')
391 nt.assert_equals(path.get_py_filename('foo.py', force_win32=win32), 'foo.py')
430 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo.py')
392 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo.py')
431 with make_tempfile('foo'):
393 with make_tempfile('foo'):
432 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo')
394 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo')
433 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
395 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
434 nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32)
396 nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32)
435 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
397 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
436 true_fn = 'foo with spaces.py'
398 true_fn = 'foo with spaces.py'
437 with make_tempfile(true_fn):
399 with make_tempfile(true_fn):
438 nt.assert_equals(path.get_py_filename('foo with spaces', force_win32=win32), true_fn)
400 nt.assert_equals(path.get_py_filename('foo with spaces', force_win32=win32), true_fn)
439 nt.assert_equals(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn)
401 nt.assert_equals(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn)
440 if win32:
402 if win32:
441 nt.assert_equals(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn)
403 nt.assert_equals(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn)
442 nt.assert_equals(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn)
404 nt.assert_equals(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn)
443 else:
405 else:
444 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False)
406 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False)
445 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False)
407 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False)
General Comments 0
You need to be logged in to leave comments. Login now