##// END OF EJS Templates
%reset now takes optional in/out/dhist/array args...
Paul Ivanov -
Show More
@@ -1,321 +1,321 b''
1 1 """Implementations for various useful completers.
2 2
3 3 These are all loaded by default by IPython.
4 4 """
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (C) 2010-2011 The IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 from __future__ import print_function
17 17
18 18 # Stdlib imports
19 19 import glob
20 20 import inspect
21 21 import os
22 22 import re
23 23 import sys
24 24
25 25 # Third-party imports
26 26 from time import time
27 27 from zipimport import zipimporter
28 28
29 29 # Our own imports
30 30 from IPython.core.completer import expand_user, compress_user
31 31 from IPython.core.error import TryNext
32 32 from IPython.utils import py3compat
33 33 from IPython.utils._process_common import arg_split
34 34
35 35 # FIXME: this should be pulled in with the right call via the component system
36 36 from IPython.core.ipapi import get as get_ipython
37 37
38 38 #-----------------------------------------------------------------------------
39 39 # Globals and constants
40 40 #-----------------------------------------------------------------------------
41 41
42 42 # Time in seconds after which the rootmodules will be stored permanently in the
43 43 # ipython ip.db database (kept in the user's .ipython dir).
44 44 TIMEOUT_STORAGE = 2
45 45
46 46 # Time in seconds after which we give up
47 47 TIMEOUT_GIVEUP = 20
48 48
49 49 # Regular expression for the python import statement
50 50 import_re = re.compile(r'.*(\.so|\.py[cod]?)$')
51 51
52 52 # RE for the ipython %run command (python + ipython scripts)
53 53 magic_run_re = re.compile(r'.*(\.ipy|\.py[w]?)$')
54 54
55 55 #-----------------------------------------------------------------------------
56 56 # Local utilities
57 57 #-----------------------------------------------------------------------------
58 58
59 59 def module_list(path):
60 60 """
61 61 Return the list containing the names of the modules available in the given
62 62 folder.
63 63 """
64 64
65 65 if os.path.isdir(path):
66 66 folder_list = os.listdir(path)
67 67 elif path.endswith('.egg'):
68 68 try:
69 69 folder_list = [f for f in zipimporter(path)._files]
70 70 except:
71 71 folder_list = []
72 72 else:
73 73 folder_list = []
74 74
75 75 if not folder_list:
76 76 return []
77 77
78 78 # A few local constants to be used in loops below
79 79 isfile = os.path.isfile
80 80 pjoin = os.path.join
81 81 basename = os.path.basename
82 82
83 83 # Now find actual path matches for packages or modules
84 84 folder_list = [p for p in folder_list
85 85 if isfile(pjoin(path, p,'__init__.py'))
86 86 or import_re.match(p) ]
87 87
88 88 return [basename(p).split('.')[0] for p in folder_list]
89 89
90 90 def get_root_modules():
91 91 """
92 92 Returns a list containing the names of all the modules available in the
93 93 folders of the pythonpath.
94 94 """
95 95 ip = get_ipython()
96 96
97 97 if 'rootmodules' in ip.db:
98 98 return ip.db['rootmodules']
99 99
100 100 t = time()
101 101 store = False
102 102 modules = list(sys.builtin_module_names)
103 103 for path in sys.path:
104 104 modules += module_list(path)
105 105 if time() - t >= TIMEOUT_STORAGE and not store:
106 106 store = True
107 107 print("\nCaching the list of root modules, please wait!")
108 108 print("(This will only be done once - type '%rehashx' to "
109 109 "reset cache!)\n")
110 110 sys.stdout.flush()
111 111 if time() - t > TIMEOUT_GIVEUP:
112 112 print("This is taking too long, we give up.\n")
113 113 ip.db['rootmodules'] = []
114 114 return []
115 115
116 116 modules = set(modules)
117 117 if '__init__' in modules:
118 118 modules.remove('__init__')
119 119 modules = list(modules)
120 120 if store:
121 121 ip.db['rootmodules'] = modules
122 122 return modules
123 123
124 124
125 125 def is_importable(module, attr, only_modules):
126 126 if only_modules:
127 127 return inspect.ismodule(getattr(module, attr))
128 128 else:
129 129 return not(attr[:2] == '__' and attr[-2:] == '__')
130 130
131 131
132 132 def try_import(mod, only_modules=False):
133 133 try:
134 134 m = __import__(mod)
135 135 except:
136 136 return []
137 137 mods = mod.split('.')
138 138 for module in mods[1:]:
139 139 m = getattr(m, module)
140 140
141 141 m_is_init = hasattr(m, '__file__') and '__init__' in m.__file__
142 142
143 143 completions = []
144 144 if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
145 145 completions.extend( [attr for attr in dir(m) if
146 146 is_importable(m, attr, only_modules)])
147 147
148 148 completions.extend(getattr(m, '__all__', []))
149 149 if m_is_init:
150 150 completions.extend(module_list(os.path.dirname(m.__file__)))
151 151 completions = set(completions)
152 152 if '__init__' in completions:
153 153 completions.remove('__init__')
154 154 return list(completions)
155 155
156 156
157 157 #-----------------------------------------------------------------------------
158 158 # Completion-related functions.
159 159 #-----------------------------------------------------------------------------
160 160
161 161 def quick_completer(cmd, completions):
162 162 """ Easily create a trivial completer for a command.
163 163
164 164 Takes either a list of completions, or all completions in string (that will
165 165 be split on whitespace).
166 166
167 167 Example::
168 168
169 169 [d:\ipython]|1> import ipy_completers
170 170 [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
171 171 [d:\ipython]|3> foo b<TAB>
172 172 bar baz
173 173 [d:\ipython]|3> foo ba
174 174 """
175 175
176 176 if isinstance(completions, basestring):
177 177 completions = completions.split()
178 178
179 179 def do_complete(self, event):
180 180 return completions
181 181
182 182 get_ipython().set_hook('complete_command',do_complete, str_key = cmd)
183 183
184 184 def module_completion(line):
185 185 """
186 186 Returns a list containing the completion possibilities for an import line.
187 187
188 188 The line looks like this :
189 189 'import xml.d'
190 190 'from xml.dom import'
191 191 """
192 192
193 193 words = line.split(' ')
194 194 nwords = len(words)
195 195
196 196 # from whatever <tab> -> 'import '
197 197 if nwords == 3 and words[0] == 'from':
198 198 return ['import ']
199 199
200 200 # 'from xy<tab>' or 'import xy<tab>'
201 201 if nwords < 3 and (words[0] in ['import','from']) :
202 202 if nwords == 1:
203 203 return get_root_modules()
204 204 mod = words[1].split('.')
205 205 if len(mod) < 2:
206 206 return get_root_modules()
207 207 completion_list = try_import('.'.join(mod[:-1]), True)
208 208 return ['.'.join(mod[:-1] + [el]) for el in completion_list]
209 209
210 210 # 'from xyz import abc<tab>'
211 211 if nwords >= 3 and words[0] == 'from':
212 212 mod = words[1]
213 213 return try_import(mod)
214 214
215 215 #-----------------------------------------------------------------------------
216 216 # Completers
217 217 #-----------------------------------------------------------------------------
218 218 # These all have the func(self, event) signature to be used as custom
219 219 # completers
220 220
221 221 def module_completer(self,event):
222 222 """Give completions after user has typed 'import ...' or 'from ...'"""
223 223
224 224 # This works in all versions of python. While 2.5 has
225 225 # pkgutil.walk_packages(), that particular routine is fairly dangerous,
226 226 # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full
227 227 # of possibly problematic side effects.
228 228 # This search the folders in the sys.path for available modules.
229 229
230 230 return module_completion(event.line)
231 231
232 232 # FIXME: there's a lot of logic common to the run, cd and builtin file
233 233 # completers, that is currently reimplemented in each.
234 234
235 235 def magic_run_completer(self, event):
236 236 """Complete files that end in .py or .ipy for the %run command.
237 237 """
238 238 comps = arg_split(event.line, strict=False)
239 239 relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"")
240 240
241 241 #print("\nev=", event) # dbg
242 242 #print("rp=", relpath) # dbg
243 243 #print('comps=', comps) # dbg
244 244
245 245 lglob = glob.glob
246 246 isdir = os.path.isdir
247 247 relpath, tilde_expand, tilde_val = expand_user(relpath)
248 248
249 249 dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)]
250 250
251 251 # Find if the user has already typed the first filename, after which we
252 252 # should complete on all files, since after the first one other files may
253 253 # be arguments to the input script.
254 254
255 255 if filter(magic_run_re.match, comps):
256 256 pys = [f.replace('\\','/') for f in lglob('*')]
257 257 else:
258 258 pys = [f.replace('\\','/')
259 259 for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') +
260 260 lglob(relpath + '*.pyw')]
261 261 #print('run comp:', dirs+pys) # dbg
262 262 return [compress_user(p, tilde_expand, tilde_val) for p in dirs+pys]
263 263
264 264
265 265 def cd_completer(self, event):
266 266 """Completer function for cd, which only returns directories."""
267 267 ip = get_ipython()
268 268 relpath = event.symbol
269 269
270 270 #print(event) # dbg
271 271 if event.line.endswith('-b') or ' -b ' in event.line:
272 272 # return only bookmark completions
273 273 bkms = self.db.get('bookmarks', None)
274 274 if bkms:
275 275 return bkms.keys()
276 276 else:
277 277 return []
278 278
279 279 if event.symbol == '-':
280 280 width_dh = str(len(str(len(ip.user_ns['_dh']) + 1)))
281 281 # jump in directory history by number
282 282 fmt = '-%0' + width_dh +'d [%s]'
283 283 ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
284 284 if len(ents) > 1:
285 285 return ents
286 286 return []
287 287
288 288 if event.symbol.startswith('--'):
289 289 return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']]
290 290
291 291 # Expand ~ in path and normalize directory separators.
292 292 relpath, tilde_expand, tilde_val = expand_user(relpath)
293 293 relpath = relpath.replace('\\','/')
294 294
295 295 found = []
296 296 for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*')
297 297 if os.path.isdir(f)]:
298 298 if ' ' in d:
299 299 # we don't want to deal with any of that, complex code
300 300 # for this is elsewhere
301 301 raise TryNext
302 302
303 303 found.append(d)
304 304
305 305 if not found:
306 306 if os.path.isdir(relpath):
307 307 return [compress_user(relpath, tilde_expand, tilde_val)]
308 308
309 309 # if no completions so far, try bookmarks
310 310 bks = self.db.get('bookmarks',{}).iterkeys()
311 311 bkmatches = [s for s in bks if s.startswith(event.symbol)]
312 312 if bkmatches:
313 313 return bkmatches
314 314
315 315 raise TryNext
316 316
317 317 return [compress_user(p, tilde_expand, tilde_val) for p in found]
318 318
319 def clear_completer(self, event):
320 "A completer for %clear magic"
321 return 'in out array dhist'.split()
319 def reset_completer(self, event):
320 "A completer for %reset magic"
321 return '-f -s in out array dhist'.split()
@@ -1,2749 +1,2749 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from __future__ import with_statement
18 18 from __future__ import absolute_import
19 19
20 20 import __builtin__ as builtin_mod
21 21 import __future__
22 22 import abc
23 23 import ast
24 24 import atexit
25 25 import codeop
26 26 import inspect
27 27 import os
28 28 import re
29 29 import sys
30 30 import tempfile
31 31 import types
32 32
33 33 try:
34 34 from contextlib import nested
35 35 except:
36 36 from IPython.utils.nested_context import nested
37 37
38 38 from IPython.config.configurable import SingletonConfigurable
39 39 from IPython.core import debugger, oinspect
40 40 from IPython.core import history as ipcorehist
41 41 from IPython.core import page
42 42 from IPython.core import prefilter
43 43 from IPython.core import shadowns
44 44 from IPython.core import ultratb
45 45 from IPython.core.alias import AliasManager, AliasError
46 46 from IPython.core.autocall import ExitAutocall
47 47 from IPython.core.builtin_trap import BuiltinTrap
48 48 from IPython.core.compilerop import CachingCompiler
49 49 from IPython.core.display_trap import DisplayTrap
50 50 from IPython.core.displayhook import DisplayHook
51 51 from IPython.core.displaypub import DisplayPublisher
52 52 from IPython.core.error import TryNext, UsageError
53 53 from IPython.core.extensions import ExtensionManager
54 54 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
55 55 from IPython.core.formatters import DisplayFormatter
56 56 from IPython.core.history import HistoryManager
57 57 from IPython.core.inputsplitter import IPythonInputSplitter
58 58 from IPython.core.logger import Logger
59 59 from IPython.core.macro import Macro
60 60 from IPython.core.magic import Magic
61 61 from IPython.core.payload import PayloadManager
62 62 from IPython.core.plugin import PluginManager
63 63 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
64 64 from IPython.core.profiledir import ProfileDir
65 65 from IPython.core.pylabtools import pylab_activate
66 66 from IPython.core.prompts import PromptManager
67 67 from IPython.utils import PyColorize
68 68 from IPython.utils import io
69 69 from IPython.utils import py3compat
70 70 from IPython.utils.doctestreload import doctest_reload
71 71 from IPython.utils.io import ask_yes_no, rprint
72 72 from IPython.utils.ipstruct import Struct
73 73 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
74 74 from IPython.utils.pickleshare import PickleShareDB
75 75 from IPython.utils.process import system, getoutput
76 76 from IPython.utils.strdispatch import StrDispatch
77 77 from IPython.utils.syspathcontext import prepended_to_syspath
78 78 from IPython.utils.text import (num_ini_spaces, format_screen, LSString, SList,
79 79 DollarFormatter)
80 80 from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum,
81 81 List, Unicode, Instance, Type)
82 82 from IPython.utils.warn import warn, error, fatal
83 83 import IPython.core.hooks
84 84
85 85 #-----------------------------------------------------------------------------
86 86 # Globals
87 87 #-----------------------------------------------------------------------------
88 88
89 89 # compiled regexps for autoindent management
90 90 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
91 91
92 92 #-----------------------------------------------------------------------------
93 93 # Utilities
94 94 #-----------------------------------------------------------------------------
95 95
96 96 def softspace(file, newvalue):
97 97 """Copied from code.py, to remove the dependency"""
98 98
99 99 oldvalue = 0
100 100 try:
101 101 oldvalue = file.softspace
102 102 except AttributeError:
103 103 pass
104 104 try:
105 105 file.softspace = newvalue
106 106 except (AttributeError, TypeError):
107 107 # "attribute-less object" or "read-only attributes"
108 108 pass
109 109 return oldvalue
110 110
111 111
112 112 def no_op(*a, **kw): pass
113 113
114 114 class NoOpContext(object):
115 115 def __enter__(self): pass
116 116 def __exit__(self, type, value, traceback): pass
117 117 no_op_context = NoOpContext()
118 118
119 119 class SpaceInInput(Exception): pass
120 120
121 121 class Bunch: pass
122 122
123 123
124 124 def get_default_colors():
125 125 if sys.platform=='darwin':
126 126 return "LightBG"
127 127 elif os.name=='nt':
128 128 return 'Linux'
129 129 else:
130 130 return 'Linux'
131 131
132 132
133 133 class SeparateUnicode(Unicode):
134 134 """A Unicode subclass to validate separate_in, separate_out, etc.
135 135
136 136 This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
137 137 """
138 138
139 139 def validate(self, obj, value):
140 140 if value == '0': value = ''
141 141 value = value.replace('\\n','\n')
142 142 return super(SeparateUnicode, self).validate(obj, value)
143 143
144 144
145 145 class ReadlineNoRecord(object):
146 146 """Context manager to execute some code, then reload readline history
147 147 so that interactive input to the code doesn't appear when pressing up."""
148 148 def __init__(self, shell):
149 149 self.shell = shell
150 150 self._nested_level = 0
151 151
152 152 def __enter__(self):
153 153 if self._nested_level == 0:
154 154 try:
155 155 self.orig_length = self.current_length()
156 156 self.readline_tail = self.get_readline_tail()
157 157 except (AttributeError, IndexError): # Can fail with pyreadline
158 158 self.orig_length, self.readline_tail = 999999, []
159 159 self._nested_level += 1
160 160
161 161 def __exit__(self, type, value, traceback):
162 162 self._nested_level -= 1
163 163 if self._nested_level == 0:
164 164 # Try clipping the end if it's got longer
165 165 try:
166 166 e = self.current_length() - self.orig_length
167 167 if e > 0:
168 168 for _ in range(e):
169 169 self.shell.readline.remove_history_item(self.orig_length)
170 170
171 171 # If it still doesn't match, just reload readline history.
172 172 if self.current_length() != self.orig_length \
173 173 or self.get_readline_tail() != self.readline_tail:
174 174 self.shell.refill_readline_hist()
175 175 except (AttributeError, IndexError):
176 176 pass
177 177 # Returning False will cause exceptions to propagate
178 178 return False
179 179
180 180 def current_length(self):
181 181 return self.shell.readline.get_current_history_length()
182 182
183 183 def get_readline_tail(self, n=10):
184 184 """Get the last n items in readline history."""
185 185 end = self.shell.readline.get_current_history_length() + 1
186 186 start = max(end-n, 1)
187 187 ghi = self.shell.readline.get_history_item
188 188 return [ghi(x) for x in range(start, end)]
189 189
190 190 #-----------------------------------------------------------------------------
191 191 # Main IPython class
192 192 #-----------------------------------------------------------------------------
193 193
194 194 class InteractiveShell(SingletonConfigurable, Magic):
195 195 """An enhanced, interactive shell for Python."""
196 196
197 197 _instance = None
198 198
199 199 autocall = Enum((0,1,2), default_value=0, config=True, help=
200 200 """
201 201 Make IPython automatically call any callable object even if you didn't
202 202 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
203 203 automatically. The value can be '0' to disable the feature, '1' for
204 204 'smart' autocall, where it is not applied if there are no more
205 205 arguments on the line, and '2' for 'full' autocall, where all callable
206 206 objects are automatically called (even if no arguments are present).
207 207 """
208 208 )
209 209 # TODO: remove all autoindent logic and put into frontends.
210 210 # We can't do this yet because even runlines uses the autoindent.
211 211 autoindent = CBool(True, config=True, help=
212 212 """
213 213 Autoindent IPython code entered interactively.
214 214 """
215 215 )
216 216 automagic = CBool(True, config=True, help=
217 217 """
218 218 Enable magic commands to be called without the leading %.
219 219 """
220 220 )
221 221 cache_size = Integer(1000, config=True, help=
222 222 """
223 223 Set the size of the output cache. The default is 1000, you can
224 224 change it permanently in your config file. Setting it to 0 completely
225 225 disables the caching system, and the minimum value accepted is 20 (if
226 226 you provide a value less than 20, it is reset to 0 and a warning is
227 227 issued). This limit is defined because otherwise you'll spend more
228 228 time re-flushing a too small cache than working
229 229 """
230 230 )
231 231 color_info = CBool(True, config=True, help=
232 232 """
233 233 Use colors for displaying information about objects. Because this
234 234 information is passed through a pager (like 'less'), and some pagers
235 235 get confused with color codes, this capability can be turned off.
236 236 """
237 237 )
238 238 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
239 239 default_value=get_default_colors(), config=True,
240 240 help="Set the color scheme (NoColor, Linux, or LightBG)."
241 241 )
242 242 colors_force = CBool(False, help=
243 243 """
244 244 Force use of ANSI color codes, regardless of OS and readline
245 245 availability.
246 246 """
247 247 # FIXME: This is essentially a hack to allow ZMQShell to show colors
248 248 # without readline on Win32. When the ZMQ formatting system is
249 249 # refactored, this should be removed.
250 250 )
251 251 debug = CBool(False, config=True)
252 252 deep_reload = CBool(False, config=True, help=
253 253 """
254 254 Enable deep (recursive) reloading by default. IPython can use the
255 255 deep_reload module which reloads changes in modules recursively (it
256 256 replaces the reload() function, so you don't need to change anything to
257 257 use it). deep_reload() forces a full reload of modules whose code may
258 258 have changed, which the default reload() function does not. When
259 259 deep_reload is off, IPython will use the normal reload(), but
260 260 deep_reload will still be available as dreload().
261 261 """
262 262 )
263 263 disable_failing_post_execute = CBool(False, config=True,
264 264 help="Don't call post-execute functions that have failed in the past."""
265 265 )
266 266 display_formatter = Instance(DisplayFormatter)
267 267 displayhook_class = Type(DisplayHook)
268 268 display_pub_class = Type(DisplayPublisher)
269 269
270 270 exit_now = CBool(False)
271 271 exiter = Instance(ExitAutocall)
272 272 def _exiter_default(self):
273 273 return ExitAutocall(self)
274 274 # Monotonically increasing execution counter
275 275 execution_count = Integer(1)
276 276 filename = Unicode("<ipython console>")
277 277 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
278 278
279 279 # Input splitter, to split entire cells of input into either individual
280 280 # interactive statements or whole blocks.
281 281 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
282 282 (), {})
283 283 logstart = CBool(False, config=True, help=
284 284 """
285 285 Start logging to the default log file.
286 286 """
287 287 )
288 288 logfile = Unicode('', config=True, help=
289 289 """
290 290 The name of the logfile to use.
291 291 """
292 292 )
293 293 logappend = Unicode('', config=True, help=
294 294 """
295 295 Start logging to the given file in append mode.
296 296 """
297 297 )
298 298 object_info_string_level = Enum((0,1,2), default_value=0,
299 299 config=True)
300 300 pdb = CBool(False, config=True, help=
301 301 """
302 302 Automatically call the pdb debugger after every exception.
303 303 """
304 304 )
305 305 multiline_history = CBool(sys.platform != 'win32', config=True,
306 306 help="Save multi-line entries as one entry in readline history"
307 307 )
308 308
309 309 # deprecated prompt traits:
310 310
311 311 prompt_in1 = Unicode('In [\\#]: ', config=True,
312 312 help="Deprecated, use PromptManager.in_template")
313 313 prompt_in2 = Unicode(' .\\D.: ', config=True,
314 314 help="Deprecated, use PromptManager.in2_template")
315 315 prompt_out = Unicode('Out[\\#]: ', config=True,
316 316 help="Deprecated, use PromptManager.out_template")
317 317 prompts_pad_left = CBool(True, config=True,
318 318 help="Deprecated, use PromptManager.justify")
319 319
320 320 def _prompt_trait_changed(self, name, old, new):
321 321 table = {
322 322 'prompt_in1' : 'in_template',
323 323 'prompt_in2' : 'in2_template',
324 324 'prompt_out' : 'out_template',
325 325 'prompts_pad_left' : 'justify',
326 326 }
327 327 warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}\n".format(
328 328 name=name, newname=table[name])
329 329 )
330 330 # protect against weird cases where self.config may not exist:
331 331 if self.config is not None:
332 332 # propagate to corresponding PromptManager trait
333 333 setattr(self.config.PromptManager, table[name], new)
334 334
335 335 _prompt_in1_changed = _prompt_trait_changed
336 336 _prompt_in2_changed = _prompt_trait_changed
337 337 _prompt_out_changed = _prompt_trait_changed
338 338 _prompt_pad_left_changed = _prompt_trait_changed
339 339
340 340 show_rewritten_input = CBool(True, config=True,
341 341 help="Show rewritten input, e.g. for autocall."
342 342 )
343 343
344 344 quiet = CBool(False, config=True)
345 345
346 346 history_length = Integer(10000, config=True)
347 347
348 348 # The readline stuff will eventually be moved to the terminal subclass
349 349 # but for now, we can't do that as readline is welded in everywhere.
350 350 readline_use = CBool(True, config=True)
351 351 readline_remove_delims = Unicode('-/~', config=True)
352 352 # don't use \M- bindings by default, because they
353 353 # conflict with 8-bit encodings. See gh-58,gh-88
354 354 readline_parse_and_bind = List([
355 355 'tab: complete',
356 356 '"\C-l": clear-screen',
357 357 'set show-all-if-ambiguous on',
358 358 '"\C-o": tab-insert',
359 359 '"\C-r": reverse-search-history',
360 360 '"\C-s": forward-search-history',
361 361 '"\C-p": history-search-backward',
362 362 '"\C-n": history-search-forward',
363 363 '"\e[A": history-search-backward',
364 364 '"\e[B": history-search-forward',
365 365 '"\C-k": kill-line',
366 366 '"\C-u": unix-line-discard',
367 367 ], allow_none=False, config=True)
368 368
369 369 # TODO: this part of prompt management should be moved to the frontends.
370 370 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
371 371 separate_in = SeparateUnicode('\n', config=True)
372 372 separate_out = SeparateUnicode('', config=True)
373 373 separate_out2 = SeparateUnicode('', config=True)
374 374 wildcards_case_sensitive = CBool(True, config=True)
375 375 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
376 376 default_value='Context', config=True)
377 377
378 378 # Subcomponents of InteractiveShell
379 379 alias_manager = Instance('IPython.core.alias.AliasManager')
380 380 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
381 381 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
382 382 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
383 383 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
384 384 plugin_manager = Instance('IPython.core.plugin.PluginManager')
385 385 payload_manager = Instance('IPython.core.payload.PayloadManager')
386 386 history_manager = Instance('IPython.core.history.HistoryManager')
387 387
388 388 profile_dir = Instance('IPython.core.application.ProfileDir')
389 389 @property
390 390 def profile(self):
391 391 if self.profile_dir is not None:
392 392 name = os.path.basename(self.profile_dir.location)
393 393 return name.replace('profile_','')
394 394
395 395
396 396 # Private interface
397 397 _post_execute = Instance(dict)
398 398
399 399 def __init__(self, config=None, ipython_dir=None, profile_dir=None,
400 400 user_module=None, user_ns=None,
401 401 custom_exceptions=((), None)):
402 402
403 403 # This is where traits with a config_key argument are updated
404 404 # from the values on config.
405 405 super(InteractiveShell, self).__init__(config=config)
406 406 self.configurables = [self]
407 407
408 408 # These are relatively independent and stateless
409 409 self.init_ipython_dir(ipython_dir)
410 410 self.init_profile_dir(profile_dir)
411 411 self.init_instance_attrs()
412 412 self.init_environment()
413 413
414 414 # Create namespaces (user_ns, user_global_ns, etc.)
415 415 self.init_create_namespaces(user_module, user_ns)
416 416 # This has to be done after init_create_namespaces because it uses
417 417 # something in self.user_ns, but before init_sys_modules, which
418 418 # is the first thing to modify sys.
419 419 # TODO: When we override sys.stdout and sys.stderr before this class
420 420 # is created, we are saving the overridden ones here. Not sure if this
421 421 # is what we want to do.
422 422 self.save_sys_module_state()
423 423 self.init_sys_modules()
424 424
425 425 # While we're trying to have each part of the code directly access what
426 426 # it needs without keeping redundant references to objects, we have too
427 427 # much legacy code that expects ip.db to exist.
428 428 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
429 429
430 430 self.init_history()
431 431 self.init_encoding()
432 432 self.init_prefilter()
433 433
434 434 Magic.__init__(self, self)
435 435
436 436 self.init_syntax_highlighting()
437 437 self.init_hooks()
438 438 self.init_pushd_popd_magic()
439 439 # self.init_traceback_handlers use to be here, but we moved it below
440 440 # because it and init_io have to come after init_readline.
441 441 self.init_user_ns()
442 442 self.init_logger()
443 443 self.init_alias()
444 444 self.init_builtins()
445 445
446 446 # pre_config_initialization
447 447
448 448 # The next section should contain everything that was in ipmaker.
449 449 self.init_logstart()
450 450
451 451 # The following was in post_config_initialization
452 452 self.init_inspector()
453 453 # init_readline() must come before init_io(), because init_io uses
454 454 # readline related things.
455 455 self.init_readline()
456 456 # We save this here in case user code replaces raw_input, but it needs
457 457 # to be after init_readline(), because PyPy's readline works by replacing
458 458 # raw_input.
459 459 if py3compat.PY3:
460 460 self.raw_input_original = input
461 461 else:
462 462 self.raw_input_original = raw_input
463 463 # init_completer must come after init_readline, because it needs to
464 464 # know whether readline is present or not system-wide to configure the
465 465 # completers, since the completion machinery can now operate
466 466 # independently of readline (e.g. over the network)
467 467 self.init_completer()
468 468 # TODO: init_io() needs to happen before init_traceback handlers
469 469 # because the traceback handlers hardcode the stdout/stderr streams.
470 470 # This logic in in debugger.Pdb and should eventually be changed.
471 471 self.init_io()
472 472 self.init_traceback_handlers(custom_exceptions)
473 473 self.init_prompts()
474 474 self.init_display_formatter()
475 475 self.init_display_pub()
476 476 self.init_displayhook()
477 477 self.init_reload_doctest()
478 478 self.init_magics()
479 479 self.init_pdb()
480 480 self.init_extension_manager()
481 481 self.init_plugin_manager()
482 482 self.init_payload()
483 483 self.hooks.late_startup_hook()
484 484 atexit.register(self.atexit_operations)
485 485
486 486 def get_ipython(self):
487 487 """Return the currently running IPython instance."""
488 488 return self
489 489
490 490 #-------------------------------------------------------------------------
491 491 # Trait changed handlers
492 492 #-------------------------------------------------------------------------
493 493
494 494 def _ipython_dir_changed(self, name, new):
495 495 if not os.path.isdir(new):
496 496 os.makedirs(new, mode = 0777)
497 497
498 498 def set_autoindent(self,value=None):
499 499 """Set the autoindent flag, checking for readline support.
500 500
501 501 If called with no arguments, it acts as a toggle."""
502 502
503 503 if value != 0 and not self.has_readline:
504 504 if os.name == 'posix':
505 505 warn("The auto-indent feature requires the readline library")
506 506 self.autoindent = 0
507 507 return
508 508 if value is None:
509 509 self.autoindent = not self.autoindent
510 510 else:
511 511 self.autoindent = value
512 512
513 513 #-------------------------------------------------------------------------
514 514 # init_* methods called by __init__
515 515 #-------------------------------------------------------------------------
516 516
517 517 def init_ipython_dir(self, ipython_dir):
518 518 if ipython_dir is not None:
519 519 self.ipython_dir = ipython_dir
520 520 return
521 521
522 522 self.ipython_dir = get_ipython_dir()
523 523
524 524 def init_profile_dir(self, profile_dir):
525 525 if profile_dir is not None:
526 526 self.profile_dir = profile_dir
527 527 return
528 528 self.profile_dir =\
529 529 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
530 530
531 531 def init_instance_attrs(self):
532 532 self.more = False
533 533
534 534 # command compiler
535 535 self.compile = CachingCompiler()
536 536
537 537 # Make an empty namespace, which extension writers can rely on both
538 538 # existing and NEVER being used by ipython itself. This gives them a
539 539 # convenient location for storing additional information and state
540 540 # their extensions may require, without fear of collisions with other
541 541 # ipython names that may develop later.
542 542 self.meta = Struct()
543 543
544 544 # Temporary files used for various purposes. Deleted at exit.
545 545 self.tempfiles = []
546 546
547 547 # Keep track of readline usage (later set by init_readline)
548 548 self.has_readline = False
549 549
550 550 # keep track of where we started running (mainly for crash post-mortem)
551 551 # This is not being used anywhere currently.
552 552 self.starting_dir = os.getcwdu()
553 553
554 554 # Indentation management
555 555 self.indent_current_nsp = 0
556 556
557 557 # Dict to track post-execution functions that have been registered
558 558 self._post_execute = {}
559 559
560 560 def init_environment(self):
561 561 """Any changes we need to make to the user's environment."""
562 562 pass
563 563
564 564 def init_encoding(self):
565 565 # Get system encoding at startup time. Certain terminals (like Emacs
566 566 # under Win32 have it set to None, and we need to have a known valid
567 567 # encoding to use in the raw_input() method
568 568 try:
569 569 self.stdin_encoding = sys.stdin.encoding or 'ascii'
570 570 except AttributeError:
571 571 self.stdin_encoding = 'ascii'
572 572
573 573 def init_syntax_highlighting(self):
574 574 # Python source parser/formatter for syntax highlighting
575 575 pyformat = PyColorize.Parser().format
576 576 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
577 577
578 578 def init_pushd_popd_magic(self):
579 579 # for pushd/popd management
580 580 self.home_dir = get_home_dir()
581 581
582 582 self.dir_stack = []
583 583
584 584 def init_logger(self):
585 585 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
586 586 logmode='rotate')
587 587
588 588 def init_logstart(self):
589 589 """Initialize logging in case it was requested at the command line.
590 590 """
591 591 if self.logappend:
592 592 self.magic_logstart(self.logappend + ' append')
593 593 elif self.logfile:
594 594 self.magic_logstart(self.logfile)
595 595 elif self.logstart:
596 596 self.magic_logstart()
597 597
598 598 def init_builtins(self):
599 599 # A single, static flag that we set to True. Its presence indicates
600 600 # that an IPython shell has been created, and we make no attempts at
601 601 # removing on exit or representing the existence of more than one
602 602 # IPython at a time.
603 603 builtin_mod.__dict__['__IPYTHON__'] = True
604 604
605 605 # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to
606 606 # manage on enter/exit, but with all our shells it's virtually
607 607 # impossible to get all the cases right. We're leaving the name in for
608 608 # those who adapted their codes to check for this flag, but will
609 609 # eventually remove it after a few more releases.
610 610 builtin_mod.__dict__['__IPYTHON__active'] = \
611 611 'Deprecated, check for __IPYTHON__'
612 612
613 613 self.builtin_trap = BuiltinTrap(shell=self)
614 614
615 615 def init_inspector(self):
616 616 # Object inspector
617 617 self.inspector = oinspect.Inspector(oinspect.InspectColors,
618 618 PyColorize.ANSICodeColors,
619 619 'NoColor',
620 620 self.object_info_string_level)
621 621
622 622 def init_io(self):
623 623 # This will just use sys.stdout and sys.stderr. If you want to
624 624 # override sys.stdout and sys.stderr themselves, you need to do that
625 625 # *before* instantiating this class, because io holds onto
626 626 # references to the underlying streams.
627 627 if sys.platform == 'win32' and self.has_readline:
628 628 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
629 629 else:
630 630 io.stdout = io.IOStream(sys.stdout)
631 631 io.stderr = io.IOStream(sys.stderr)
632 632
633 633 def init_prompts(self):
634 634 self.prompt_manager = PromptManager(shell=self, config=self.config)
635 635 self.configurables.append(self.prompt_manager)
636 636
637 637 def init_display_formatter(self):
638 638 self.display_formatter = DisplayFormatter(config=self.config)
639 639 self.configurables.append(self.display_formatter)
640 640
641 641 def init_display_pub(self):
642 642 self.display_pub = self.display_pub_class(config=self.config)
643 643 self.configurables.append(self.display_pub)
644 644
645 645 def init_displayhook(self):
646 646 # Initialize displayhook, set in/out prompts and printing system
647 647 self.displayhook = self.displayhook_class(
648 648 config=self.config,
649 649 shell=self,
650 650 cache_size=self.cache_size,
651 651 )
652 652 self.configurables.append(self.displayhook)
653 653 # This is a context manager that installs/revmoes the displayhook at
654 654 # the appropriate time.
655 655 self.display_trap = DisplayTrap(hook=self.displayhook)
656 656
657 657 def init_reload_doctest(self):
658 658 # Do a proper resetting of doctest, including the necessary displayhook
659 659 # monkeypatching
660 660 try:
661 661 doctest_reload()
662 662 except ImportError:
663 663 warn("doctest module does not exist.")
664 664
665 665 #-------------------------------------------------------------------------
666 666 # Things related to injections into the sys module
667 667 #-------------------------------------------------------------------------
668 668
669 669 def save_sys_module_state(self):
670 670 """Save the state of hooks in the sys module.
671 671
672 672 This has to be called after self.user_module is created.
673 673 """
674 674 self._orig_sys_module_state = {}
675 675 self._orig_sys_module_state['stdin'] = sys.stdin
676 676 self._orig_sys_module_state['stdout'] = sys.stdout
677 677 self._orig_sys_module_state['stderr'] = sys.stderr
678 678 self._orig_sys_module_state['excepthook'] = sys.excepthook
679 679 self._orig_sys_modules_main_name = self.user_module.__name__
680 680
681 681 def restore_sys_module_state(self):
682 682 """Restore the state of the sys module."""
683 683 try:
684 684 for k, v in self._orig_sys_module_state.iteritems():
685 685 setattr(sys, k, v)
686 686 except AttributeError:
687 687 pass
688 688 # Reset what what done in self.init_sys_modules
689 689 sys.modules[self.user_module.__name__] = self._orig_sys_modules_main_name
690 690
691 691 #-------------------------------------------------------------------------
692 692 # Things related to hooks
693 693 #-------------------------------------------------------------------------
694 694
695 695 def init_hooks(self):
696 696 # hooks holds pointers used for user-side customizations
697 697 self.hooks = Struct()
698 698
699 699 self.strdispatchers = {}
700 700
701 701 # Set all default hooks, defined in the IPython.hooks module.
702 702 hooks = IPython.core.hooks
703 703 for hook_name in hooks.__all__:
704 704 # default hooks have priority 100, i.e. low; user hooks should have
705 705 # 0-100 priority
706 706 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
707 707
708 708 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
709 709 """set_hook(name,hook) -> sets an internal IPython hook.
710 710
711 711 IPython exposes some of its internal API as user-modifiable hooks. By
712 712 adding your function to one of these hooks, you can modify IPython's
713 713 behavior to call at runtime your own routines."""
714 714
715 715 # At some point in the future, this should validate the hook before it
716 716 # accepts it. Probably at least check that the hook takes the number
717 717 # of args it's supposed to.
718 718
719 719 f = types.MethodType(hook,self)
720 720
721 721 # check if the hook is for strdispatcher first
722 722 if str_key is not None:
723 723 sdp = self.strdispatchers.get(name, StrDispatch())
724 724 sdp.add_s(str_key, f, priority )
725 725 self.strdispatchers[name] = sdp
726 726 return
727 727 if re_key is not None:
728 728 sdp = self.strdispatchers.get(name, StrDispatch())
729 729 sdp.add_re(re.compile(re_key), f, priority )
730 730 self.strdispatchers[name] = sdp
731 731 return
732 732
733 733 dp = getattr(self.hooks, name, None)
734 734 if name not in IPython.core.hooks.__all__:
735 735 print "Warning! Hook '%s' is not one of %s" % \
736 736 (name, IPython.core.hooks.__all__ )
737 737 if not dp:
738 738 dp = IPython.core.hooks.CommandChainDispatcher()
739 739
740 740 try:
741 741 dp.add(f,priority)
742 742 except AttributeError:
743 743 # it was not commandchain, plain old func - replace
744 744 dp = f
745 745
746 746 setattr(self.hooks,name, dp)
747 747
748 748 def register_post_execute(self, func):
749 749 """Register a function for calling after code execution.
750 750 """
751 751 if not callable(func):
752 752 raise ValueError('argument %s must be callable' % func)
753 753 self._post_execute[func] = True
754 754
755 755 #-------------------------------------------------------------------------
756 756 # Things related to the "main" module
757 757 #-------------------------------------------------------------------------
758 758
759 759 def new_main_mod(self,ns=None):
760 760 """Return a new 'main' module object for user code execution.
761 761 """
762 762 main_mod = self._user_main_module
763 763 init_fakemod_dict(main_mod,ns)
764 764 return main_mod
765 765
766 766 def cache_main_mod(self,ns,fname):
767 767 """Cache a main module's namespace.
768 768
769 769 When scripts are executed via %run, we must keep a reference to the
770 770 namespace of their __main__ module (a FakeModule instance) around so
771 771 that Python doesn't clear it, rendering objects defined therein
772 772 useless.
773 773
774 774 This method keeps said reference in a private dict, keyed by the
775 775 absolute path of the module object (which corresponds to the script
776 776 path). This way, for multiple executions of the same script we only
777 777 keep one copy of the namespace (the last one), thus preventing memory
778 778 leaks from old references while allowing the objects from the last
779 779 execution to be accessible.
780 780
781 781 Note: we can not allow the actual FakeModule instances to be deleted,
782 782 because of how Python tears down modules (it hard-sets all their
783 783 references to None without regard for reference counts). This method
784 784 must therefore make a *copy* of the given namespace, to allow the
785 785 original module's __dict__ to be cleared and reused.
786 786
787 787
788 788 Parameters
789 789 ----------
790 790 ns : a namespace (a dict, typically)
791 791
792 792 fname : str
793 793 Filename associated with the namespace.
794 794
795 795 Examples
796 796 --------
797 797
798 798 In [10]: import IPython
799 799
800 800 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
801 801
802 802 In [12]: IPython.__file__ in _ip._main_ns_cache
803 803 Out[12]: True
804 804 """
805 805 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
806 806
807 807 def clear_main_mod_cache(self):
808 808 """Clear the cache of main modules.
809 809
810 810 Mainly for use by utilities like %reset.
811 811
812 812 Examples
813 813 --------
814 814
815 815 In [15]: import IPython
816 816
817 817 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
818 818
819 819 In [17]: len(_ip._main_ns_cache) > 0
820 820 Out[17]: True
821 821
822 822 In [18]: _ip.clear_main_mod_cache()
823 823
824 824 In [19]: len(_ip._main_ns_cache) == 0
825 825 Out[19]: True
826 826 """
827 827 self._main_ns_cache.clear()
828 828
829 829 #-------------------------------------------------------------------------
830 830 # Things related to debugging
831 831 #-------------------------------------------------------------------------
832 832
833 833 def init_pdb(self):
834 834 # Set calling of pdb on exceptions
835 835 # self.call_pdb is a property
836 836 self.call_pdb = self.pdb
837 837
838 838 def _get_call_pdb(self):
839 839 return self._call_pdb
840 840
841 841 def _set_call_pdb(self,val):
842 842
843 843 if val not in (0,1,False,True):
844 844 raise ValueError,'new call_pdb value must be boolean'
845 845
846 846 # store value in instance
847 847 self._call_pdb = val
848 848
849 849 # notify the actual exception handlers
850 850 self.InteractiveTB.call_pdb = val
851 851
852 852 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
853 853 'Control auto-activation of pdb at exceptions')
854 854
855 855 def debugger(self,force=False):
856 856 """Call the pydb/pdb debugger.
857 857
858 858 Keywords:
859 859
860 860 - force(False): by default, this routine checks the instance call_pdb
861 861 flag and does not actually invoke the debugger if the flag is false.
862 862 The 'force' option forces the debugger to activate even if the flag
863 863 is false.
864 864 """
865 865
866 866 if not (force or self.call_pdb):
867 867 return
868 868
869 869 if not hasattr(sys,'last_traceback'):
870 870 error('No traceback has been produced, nothing to debug.')
871 871 return
872 872
873 873 # use pydb if available
874 874 if debugger.has_pydb:
875 875 from pydb import pm
876 876 else:
877 877 # fallback to our internal debugger
878 878 pm = lambda : self.InteractiveTB.debugger(force=True)
879 879
880 880 with self.readline_no_record:
881 881 pm()
882 882
883 883 #-------------------------------------------------------------------------
884 884 # Things related to IPython's various namespaces
885 885 #-------------------------------------------------------------------------
886 886 default_user_namespaces = True
887 887
888 888 def init_create_namespaces(self, user_module=None, user_ns=None):
889 889 # Create the namespace where the user will operate. user_ns is
890 890 # normally the only one used, and it is passed to the exec calls as
891 891 # the locals argument. But we do carry a user_global_ns namespace
892 892 # given as the exec 'globals' argument, This is useful in embedding
893 893 # situations where the ipython shell opens in a context where the
894 894 # distinction between locals and globals is meaningful. For
895 895 # non-embedded contexts, it is just the same object as the user_ns dict.
896 896
897 897 # FIXME. For some strange reason, __builtins__ is showing up at user
898 898 # level as a dict instead of a module. This is a manual fix, but I
899 899 # should really track down where the problem is coming from. Alex
900 900 # Schmolck reported this problem first.
901 901
902 902 # A useful post by Alex Martelli on this topic:
903 903 # Re: inconsistent value from __builtins__
904 904 # Von: Alex Martelli <aleaxit@yahoo.com>
905 905 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
906 906 # Gruppen: comp.lang.python
907 907
908 908 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
909 909 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
910 910 # > <type 'dict'>
911 911 # > >>> print type(__builtins__)
912 912 # > <type 'module'>
913 913 # > Is this difference in return value intentional?
914 914
915 915 # Well, it's documented that '__builtins__' can be either a dictionary
916 916 # or a module, and it's been that way for a long time. Whether it's
917 917 # intentional (or sensible), I don't know. In any case, the idea is
918 918 # that if you need to access the built-in namespace directly, you
919 919 # should start with "import __builtin__" (note, no 's') which will
920 920 # definitely give you a module. Yeah, it's somewhat confusing:-(.
921 921
922 922 # These routines return a properly built module and dict as needed by
923 923 # the rest of the code, and can also be used by extension writers to
924 924 # generate properly initialized namespaces.
925 925 if (user_ns is not None) or (user_module is not None):
926 926 self.default_user_namespaces = False
927 927 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
928 928
929 929 # A record of hidden variables we have added to the user namespace, so
930 930 # we can list later only variables defined in actual interactive use.
931 931 self.user_ns_hidden = set()
932 932
933 933 # Now that FakeModule produces a real module, we've run into a nasty
934 934 # problem: after script execution (via %run), the module where the user
935 935 # code ran is deleted. Now that this object is a true module (needed
936 936 # so docetst and other tools work correctly), the Python module
937 937 # teardown mechanism runs over it, and sets to None every variable
938 938 # present in that module. Top-level references to objects from the
939 939 # script survive, because the user_ns is updated with them. However,
940 940 # calling functions defined in the script that use other things from
941 941 # the script will fail, because the function's closure had references
942 942 # to the original objects, which are now all None. So we must protect
943 943 # these modules from deletion by keeping a cache.
944 944 #
945 945 # To avoid keeping stale modules around (we only need the one from the
946 946 # last run), we use a dict keyed with the full path to the script, so
947 947 # only the last version of the module is held in the cache. Note,
948 948 # however, that we must cache the module *namespace contents* (their
949 949 # __dict__). Because if we try to cache the actual modules, old ones
950 950 # (uncached) could be destroyed while still holding references (such as
951 951 # those held by GUI objects that tend to be long-lived)>
952 952 #
953 953 # The %reset command will flush this cache. See the cache_main_mod()
954 954 # and clear_main_mod_cache() methods for details on use.
955 955
956 956 # This is the cache used for 'main' namespaces
957 957 self._main_ns_cache = {}
958 958 # And this is the single instance of FakeModule whose __dict__ we keep
959 959 # copying and clearing for reuse on each %run
960 960 self._user_main_module = FakeModule()
961 961
962 962 # A table holding all the namespaces IPython deals with, so that
963 963 # introspection facilities can search easily.
964 964 self.ns_table = {'user_global':self.user_module.__dict__,
965 965 'user_local':self.user_ns,
966 966 'builtin':builtin_mod.__dict__
967 967 }
968 968
969 969 @property
970 970 def user_global_ns(self):
971 971 return self.user_module.__dict__
972 972
973 973 def prepare_user_module(self, user_module=None, user_ns=None):
974 974 """Prepare the module and namespace in which user code will be run.
975 975
976 976 When IPython is started normally, both parameters are None: a new module
977 977 is created automatically, and its __dict__ used as the namespace.
978 978
979 979 If only user_module is provided, its __dict__ is used as the namespace.
980 980 If only user_ns is provided, a dummy module is created, and user_ns
981 981 becomes the global namespace. If both are provided (as they may be
982 982 when embedding), user_ns is the local namespace, and user_module
983 983 provides the global namespace.
984 984
985 985 Parameters
986 986 ----------
987 987 user_module : module, optional
988 988 The current user module in which IPython is being run. If None,
989 989 a clean module will be created.
990 990 user_ns : dict, optional
991 991 A namespace in which to run interactive commands.
992 992
993 993 Returns
994 994 -------
995 995 A tuple of user_module and user_ns, each properly initialised.
996 996 """
997 997 if user_module is None and user_ns is not None:
998 998 user_ns.setdefault("__name__", "__main__")
999 999 class DummyMod(object):
1000 1000 "A dummy module used for IPython's interactive namespace."
1001 1001 pass
1002 1002 user_module = DummyMod()
1003 1003 user_module.__dict__ = user_ns
1004 1004
1005 1005 if user_module is None:
1006 1006 user_module = types.ModuleType("__main__",
1007 1007 doc="Automatically created module for IPython interactive environment")
1008 1008
1009 1009 # We must ensure that __builtin__ (without the final 's') is always
1010 1010 # available and pointing to the __builtin__ *module*. For more details:
1011 1011 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1012 1012 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1013 1013 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1014 1014
1015 1015 if user_ns is None:
1016 1016 user_ns = user_module.__dict__
1017 1017
1018 1018 return user_module, user_ns
1019 1019
1020 1020 def init_sys_modules(self):
1021 1021 # We need to insert into sys.modules something that looks like a
1022 1022 # module but which accesses the IPython namespace, for shelve and
1023 1023 # pickle to work interactively. Normally they rely on getting
1024 1024 # everything out of __main__, but for embedding purposes each IPython
1025 1025 # instance has its own private namespace, so we can't go shoving
1026 1026 # everything into __main__.
1027 1027
1028 1028 # note, however, that we should only do this for non-embedded
1029 1029 # ipythons, which really mimic the __main__.__dict__ with their own
1030 1030 # namespace. Embedded instances, on the other hand, should not do
1031 1031 # this because they need to manage the user local/global namespaces
1032 1032 # only, but they live within a 'normal' __main__ (meaning, they
1033 1033 # shouldn't overtake the execution environment of the script they're
1034 1034 # embedded in).
1035 1035
1036 1036 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1037 1037 main_name = self.user_module.__name__
1038 1038 sys.modules[main_name] = self.user_module
1039 1039
1040 1040 def init_user_ns(self):
1041 1041 """Initialize all user-visible namespaces to their minimum defaults.
1042 1042
1043 1043 Certain history lists are also initialized here, as they effectively
1044 1044 act as user namespaces.
1045 1045
1046 1046 Notes
1047 1047 -----
1048 1048 All data structures here are only filled in, they are NOT reset by this
1049 1049 method. If they were not empty before, data will simply be added to
1050 1050 therm.
1051 1051 """
1052 1052 # This function works in two parts: first we put a few things in
1053 1053 # user_ns, and we sync that contents into user_ns_hidden so that these
1054 1054 # initial variables aren't shown by %who. After the sync, we add the
1055 1055 # rest of what we *do* want the user to see with %who even on a new
1056 1056 # session (probably nothing, so theye really only see their own stuff)
1057 1057
1058 1058 # The user dict must *always* have a __builtin__ reference to the
1059 1059 # Python standard __builtin__ namespace, which must be imported.
1060 1060 # This is so that certain operations in prompt evaluation can be
1061 1061 # reliably executed with builtins. Note that we can NOT use
1062 1062 # __builtins__ (note the 's'), because that can either be a dict or a
1063 1063 # module, and can even mutate at runtime, depending on the context
1064 1064 # (Python makes no guarantees on it). In contrast, __builtin__ is
1065 1065 # always a module object, though it must be explicitly imported.
1066 1066
1067 1067 # For more details:
1068 1068 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1069 1069 ns = dict()
1070 1070
1071 1071 # Put 'help' in the user namespace
1072 1072 try:
1073 1073 from site import _Helper
1074 1074 ns['help'] = _Helper()
1075 1075 except ImportError:
1076 1076 warn('help() not available - check site.py')
1077 1077
1078 1078 # make global variables for user access to the histories
1079 1079 ns['_ih'] = self.history_manager.input_hist_parsed
1080 1080 ns['_oh'] = self.history_manager.output_hist
1081 1081 ns['_dh'] = self.history_manager.dir_hist
1082 1082
1083 1083 ns['_sh'] = shadowns
1084 1084
1085 1085 # user aliases to input and output histories. These shouldn't show up
1086 1086 # in %who, as they can have very large reprs.
1087 1087 ns['In'] = self.history_manager.input_hist_parsed
1088 1088 ns['Out'] = self.history_manager.output_hist
1089 1089
1090 1090 # Store myself as the public api!!!
1091 1091 ns['get_ipython'] = self.get_ipython
1092 1092
1093 1093 ns['exit'] = self.exiter
1094 1094 ns['quit'] = self.exiter
1095 1095
1096 1096 # Sync what we've added so far to user_ns_hidden so these aren't seen
1097 1097 # by %who
1098 1098 self.user_ns_hidden.update(ns)
1099 1099
1100 1100 # Anything put into ns now would show up in %who. Think twice before
1101 1101 # putting anything here, as we really want %who to show the user their
1102 1102 # stuff, not our variables.
1103 1103
1104 1104 # Finally, update the real user's namespace
1105 1105 self.user_ns.update(ns)
1106 1106
1107 1107 @property
1108 1108 def all_ns_refs(self):
1109 1109 """Get a list of references to all the namespace dictionaries in which
1110 1110 IPython might store a user-created object.
1111 1111
1112 1112 Note that this does not include the displayhook, which also caches
1113 1113 objects from the output."""
1114 1114 return [self.user_ns, self.user_global_ns,
1115 1115 self._user_main_module.__dict__] + self._main_ns_cache.values()
1116 1116
1117 1117 def reset(self, new_session=True):
1118 1118 """Clear all internal namespaces, and attempt to release references to
1119 1119 user objects.
1120 1120
1121 1121 If new_session is True, a new history session will be opened.
1122 1122 """
1123 1123 # Clear histories
1124 1124 self.history_manager.reset(new_session)
1125 1125 # Reset counter used to index all histories
1126 1126 if new_session:
1127 1127 self.execution_count = 1
1128 1128
1129 1129 # Flush cached output items
1130 1130 if self.displayhook.do_full_cache:
1131 1131 self.displayhook.flush()
1132 1132
1133 1133 # The main execution namespaces must be cleared very carefully,
1134 1134 # skipping the deletion of the builtin-related keys, because doing so
1135 1135 # would cause errors in many object's __del__ methods.
1136 1136 if self.user_ns is not self.user_global_ns:
1137 1137 self.user_ns.clear()
1138 1138 ns = self.user_global_ns
1139 1139 drop_keys = set(ns.keys())
1140 1140 drop_keys.discard('__builtin__')
1141 1141 drop_keys.discard('__builtins__')
1142 1142 drop_keys.discard('__name__')
1143 1143 for k in drop_keys:
1144 1144 del ns[k]
1145 1145
1146 1146 self.user_ns_hidden.clear()
1147 1147
1148 1148 # Restore the user namespaces to minimal usability
1149 1149 self.init_user_ns()
1150 1150
1151 1151 # Restore the default and user aliases
1152 1152 self.alias_manager.clear_aliases()
1153 1153 self.alias_manager.init_aliases()
1154 1154
1155 1155 # Flush the private list of module references kept for script
1156 1156 # execution protection
1157 1157 self.clear_main_mod_cache()
1158 1158
1159 1159 # Clear out the namespace from the last %run
1160 1160 self.new_main_mod()
1161 1161
1162 1162 def del_var(self, varname, by_name=False):
1163 1163 """Delete a variable from the various namespaces, so that, as
1164 1164 far as possible, we're not keeping any hidden references to it.
1165 1165
1166 1166 Parameters
1167 1167 ----------
1168 1168 varname : str
1169 1169 The name of the variable to delete.
1170 1170 by_name : bool
1171 1171 If True, delete variables with the given name in each
1172 1172 namespace. If False (default), find the variable in the user
1173 1173 namespace, and delete references to it.
1174 1174 """
1175 1175 if varname in ('__builtin__', '__builtins__'):
1176 1176 raise ValueError("Refusing to delete %s" % varname)
1177 1177
1178 1178 ns_refs = self.all_ns_refs
1179 1179
1180 1180 if by_name: # Delete by name
1181 1181 for ns in ns_refs:
1182 1182 try:
1183 1183 del ns[varname]
1184 1184 except KeyError:
1185 1185 pass
1186 1186 else: # Delete by object
1187 1187 try:
1188 1188 obj = self.user_ns[varname]
1189 1189 except KeyError:
1190 1190 raise NameError("name '%s' is not defined" % varname)
1191 1191 # Also check in output history
1192 1192 ns_refs.append(self.history_manager.output_hist)
1193 1193 for ns in ns_refs:
1194 1194 to_delete = [n for n, o in ns.iteritems() if o is obj]
1195 1195 for name in to_delete:
1196 1196 del ns[name]
1197 1197
1198 1198 # displayhook keeps extra references, but not in a dictionary
1199 1199 for name in ('_', '__', '___'):
1200 1200 if getattr(self.displayhook, name) is obj:
1201 1201 setattr(self.displayhook, name, None)
1202 1202
1203 1203 def reset_selective(self, regex=None):
1204 1204 """Clear selective variables from internal namespaces based on a
1205 1205 specified regular expression.
1206 1206
1207 1207 Parameters
1208 1208 ----------
1209 1209 regex : string or compiled pattern, optional
1210 1210 A regular expression pattern that will be used in searching
1211 1211 variable names in the users namespaces.
1212 1212 """
1213 1213 if regex is not None:
1214 1214 try:
1215 1215 m = re.compile(regex)
1216 1216 except TypeError:
1217 1217 raise TypeError('regex must be a string or compiled pattern')
1218 1218 # Search for keys in each namespace that match the given regex
1219 1219 # If a match is found, delete the key/value pair.
1220 1220 for ns in self.all_ns_refs:
1221 1221 for var in ns:
1222 1222 if m.search(var):
1223 1223 del ns[var]
1224 1224
1225 1225 def push(self, variables, interactive=True):
1226 1226 """Inject a group of variables into the IPython user namespace.
1227 1227
1228 1228 Parameters
1229 1229 ----------
1230 1230 variables : dict, str or list/tuple of str
1231 1231 The variables to inject into the user's namespace. If a dict, a
1232 1232 simple update is done. If a str, the string is assumed to have
1233 1233 variable names separated by spaces. A list/tuple of str can also
1234 1234 be used to give the variable names. If just the variable names are
1235 1235 give (list/tuple/str) then the variable values looked up in the
1236 1236 callers frame.
1237 1237 interactive : bool
1238 1238 If True (default), the variables will be listed with the ``who``
1239 1239 magic.
1240 1240 """
1241 1241 vdict = None
1242 1242
1243 1243 # We need a dict of name/value pairs to do namespace updates.
1244 1244 if isinstance(variables, dict):
1245 1245 vdict = variables
1246 1246 elif isinstance(variables, (basestring, list, tuple)):
1247 1247 if isinstance(variables, basestring):
1248 1248 vlist = variables.split()
1249 1249 else:
1250 1250 vlist = variables
1251 1251 vdict = {}
1252 1252 cf = sys._getframe(1)
1253 1253 for name in vlist:
1254 1254 try:
1255 1255 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1256 1256 except:
1257 1257 print ('Could not get variable %s from %s' %
1258 1258 (name,cf.f_code.co_name))
1259 1259 else:
1260 1260 raise ValueError('variables must be a dict/str/list/tuple')
1261 1261
1262 1262 # Propagate variables to user namespace
1263 1263 self.user_ns.update(vdict)
1264 1264
1265 1265 # And configure interactive visibility
1266 1266 user_ns_hidden = self.user_ns_hidden
1267 1267 if interactive:
1268 1268 user_ns_hidden.difference_update(vdict)
1269 1269 else:
1270 1270 user_ns_hidden.update(vdict)
1271 1271
1272 1272 def drop_by_id(self, variables):
1273 1273 """Remove a dict of variables from the user namespace, if they are the
1274 1274 same as the values in the dictionary.
1275 1275
1276 1276 This is intended for use by extensions: variables that they've added can
1277 1277 be taken back out if they are unloaded, without removing any that the
1278 1278 user has overwritten.
1279 1279
1280 1280 Parameters
1281 1281 ----------
1282 1282 variables : dict
1283 1283 A dictionary mapping object names (as strings) to the objects.
1284 1284 """
1285 1285 for name, obj in variables.iteritems():
1286 1286 if name in self.user_ns and self.user_ns[name] is obj:
1287 1287 del self.user_ns[name]
1288 1288 self.user_ns_hidden.discard(name)
1289 1289
1290 1290 #-------------------------------------------------------------------------
1291 1291 # Things related to object introspection
1292 1292 #-------------------------------------------------------------------------
1293 1293
1294 1294 def _ofind(self, oname, namespaces=None):
1295 1295 """Find an object in the available namespaces.
1296 1296
1297 1297 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1298 1298
1299 1299 Has special code to detect magic functions.
1300 1300 """
1301 1301 oname = oname.strip()
1302 1302 #print '1- oname: <%r>' % oname # dbg
1303 1303 if not py3compat.isidentifier(oname.lstrip(ESC_MAGIC), dotted=True):
1304 1304 return dict(found=False)
1305 1305
1306 1306 alias_ns = None
1307 1307 if namespaces is None:
1308 1308 # Namespaces to search in:
1309 1309 # Put them in a list. The order is important so that we
1310 1310 # find things in the same order that Python finds them.
1311 1311 namespaces = [ ('Interactive', self.user_ns),
1312 1312 ('Interactive (global)', self.user_global_ns),
1313 1313 ('Python builtin', builtin_mod.__dict__),
1314 1314 ('Alias', self.alias_manager.alias_table),
1315 1315 ]
1316 1316 alias_ns = self.alias_manager.alias_table
1317 1317
1318 1318 # initialize results to 'null'
1319 1319 found = False; obj = None; ospace = None; ds = None;
1320 1320 ismagic = False; isalias = False; parent = None
1321 1321
1322 1322 # We need to special-case 'print', which as of python2.6 registers as a
1323 1323 # function but should only be treated as one if print_function was
1324 1324 # loaded with a future import. In this case, just bail.
1325 1325 if (oname == 'print' and not py3compat.PY3 and not \
1326 1326 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1327 1327 return {'found':found, 'obj':obj, 'namespace':ospace,
1328 1328 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1329 1329
1330 1330 # Look for the given name by splitting it in parts. If the head is
1331 1331 # found, then we look for all the remaining parts as members, and only
1332 1332 # declare success if we can find them all.
1333 1333 oname_parts = oname.split('.')
1334 1334 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1335 1335 for nsname,ns in namespaces:
1336 1336 try:
1337 1337 obj = ns[oname_head]
1338 1338 except KeyError:
1339 1339 continue
1340 1340 else:
1341 1341 #print 'oname_rest:', oname_rest # dbg
1342 1342 for part in oname_rest:
1343 1343 try:
1344 1344 parent = obj
1345 1345 obj = getattr(obj,part)
1346 1346 except:
1347 1347 # Blanket except b/c some badly implemented objects
1348 1348 # allow __getattr__ to raise exceptions other than
1349 1349 # AttributeError, which then crashes IPython.
1350 1350 break
1351 1351 else:
1352 1352 # If we finish the for loop (no break), we got all members
1353 1353 found = True
1354 1354 ospace = nsname
1355 1355 if ns == alias_ns:
1356 1356 isalias = True
1357 1357 break # namespace loop
1358 1358
1359 1359 # Try to see if it's magic
1360 1360 if not found:
1361 1361 if oname.startswith(ESC_MAGIC):
1362 1362 oname = oname[1:]
1363 1363 obj = getattr(self,'magic_'+oname,None)
1364 1364 if obj is not None:
1365 1365 found = True
1366 1366 ospace = 'IPython internal'
1367 1367 ismagic = True
1368 1368
1369 1369 # Last try: special-case some literals like '', [], {}, etc:
1370 1370 if not found and oname_head in ["''",'""','[]','{}','()']:
1371 1371 obj = eval(oname_head)
1372 1372 found = True
1373 1373 ospace = 'Interactive'
1374 1374
1375 1375 return {'found':found, 'obj':obj, 'namespace':ospace,
1376 1376 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1377 1377
1378 1378 def _ofind_property(self, oname, info):
1379 1379 """Second part of object finding, to look for property details."""
1380 1380 if info.found:
1381 1381 # Get the docstring of the class property if it exists.
1382 1382 path = oname.split('.')
1383 1383 root = '.'.join(path[:-1])
1384 1384 if info.parent is not None:
1385 1385 try:
1386 1386 target = getattr(info.parent, '__class__')
1387 1387 # The object belongs to a class instance.
1388 1388 try:
1389 1389 target = getattr(target, path[-1])
1390 1390 # The class defines the object.
1391 1391 if isinstance(target, property):
1392 1392 oname = root + '.__class__.' + path[-1]
1393 1393 info = Struct(self._ofind(oname))
1394 1394 except AttributeError: pass
1395 1395 except AttributeError: pass
1396 1396
1397 1397 # We return either the new info or the unmodified input if the object
1398 1398 # hadn't been found
1399 1399 return info
1400 1400
1401 1401 def _object_find(self, oname, namespaces=None):
1402 1402 """Find an object and return a struct with info about it."""
1403 1403 inf = Struct(self._ofind(oname, namespaces))
1404 1404 return Struct(self._ofind_property(oname, inf))
1405 1405
1406 1406 def _inspect(self, meth, oname, namespaces=None, **kw):
1407 1407 """Generic interface to the inspector system.
1408 1408
1409 1409 This function is meant to be called by pdef, pdoc & friends."""
1410 1410 info = self._object_find(oname)
1411 1411 if info.found:
1412 1412 pmethod = getattr(self.inspector, meth)
1413 1413 formatter = format_screen if info.ismagic else None
1414 1414 if meth == 'pdoc':
1415 1415 pmethod(info.obj, oname, formatter)
1416 1416 elif meth == 'pinfo':
1417 1417 pmethod(info.obj, oname, formatter, info, **kw)
1418 1418 else:
1419 1419 pmethod(info.obj, oname)
1420 1420 else:
1421 1421 print 'Object `%s` not found.' % oname
1422 1422 return 'not found' # so callers can take other action
1423 1423
1424 1424 def object_inspect(self, oname):
1425 1425 with self.builtin_trap:
1426 1426 info = self._object_find(oname)
1427 1427 if info.found:
1428 1428 return self.inspector.info(info.obj, oname, info=info)
1429 1429 else:
1430 1430 return oinspect.object_info(name=oname, found=False)
1431 1431
1432 1432 #-------------------------------------------------------------------------
1433 1433 # Things related to history management
1434 1434 #-------------------------------------------------------------------------
1435 1435
1436 1436 def init_history(self):
1437 1437 """Sets up the command history, and starts regular autosaves."""
1438 1438 self.history_manager = HistoryManager(shell=self, config=self.config)
1439 1439 self.configurables.append(self.history_manager)
1440 1440
1441 1441 #-------------------------------------------------------------------------
1442 1442 # Things related to exception handling and tracebacks (not debugging)
1443 1443 #-------------------------------------------------------------------------
1444 1444
1445 1445 def init_traceback_handlers(self, custom_exceptions):
1446 1446 # Syntax error handler.
1447 1447 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1448 1448
1449 1449 # The interactive one is initialized with an offset, meaning we always
1450 1450 # want to remove the topmost item in the traceback, which is our own
1451 1451 # internal code. Valid modes: ['Plain','Context','Verbose']
1452 1452 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1453 1453 color_scheme='NoColor',
1454 1454 tb_offset = 1,
1455 1455 check_cache=self.compile.check_cache)
1456 1456
1457 1457 # The instance will store a pointer to the system-wide exception hook,
1458 1458 # so that runtime code (such as magics) can access it. This is because
1459 1459 # during the read-eval loop, it may get temporarily overwritten.
1460 1460 self.sys_excepthook = sys.excepthook
1461 1461
1462 1462 # and add any custom exception handlers the user may have specified
1463 1463 self.set_custom_exc(*custom_exceptions)
1464 1464
1465 1465 # Set the exception mode
1466 1466 self.InteractiveTB.set_mode(mode=self.xmode)
1467 1467
1468 1468 def set_custom_exc(self, exc_tuple, handler):
1469 1469 """set_custom_exc(exc_tuple,handler)
1470 1470
1471 1471 Set a custom exception handler, which will be called if any of the
1472 1472 exceptions in exc_tuple occur in the mainloop (specifically, in the
1473 1473 run_code() method).
1474 1474
1475 1475 Parameters
1476 1476 ----------
1477 1477
1478 1478 exc_tuple : tuple of exception classes
1479 1479 A *tuple* of exception classes, for which to call the defined
1480 1480 handler. It is very important that you use a tuple, and NOT A
1481 1481 LIST here, because of the way Python's except statement works. If
1482 1482 you only want to trap a single exception, use a singleton tuple::
1483 1483
1484 1484 exc_tuple == (MyCustomException,)
1485 1485
1486 1486 handler : callable
1487 1487 handler must have the following signature::
1488 1488
1489 1489 def my_handler(self, etype, value, tb, tb_offset=None):
1490 1490 ...
1491 1491 return structured_traceback
1492 1492
1493 1493 Your handler must return a structured traceback (a list of strings),
1494 1494 or None.
1495 1495
1496 1496 This will be made into an instance method (via types.MethodType)
1497 1497 of IPython itself, and it will be called if any of the exceptions
1498 1498 listed in the exc_tuple are caught. If the handler is None, an
1499 1499 internal basic one is used, which just prints basic info.
1500 1500
1501 1501 To protect IPython from crashes, if your handler ever raises an
1502 1502 exception or returns an invalid result, it will be immediately
1503 1503 disabled.
1504 1504
1505 1505 WARNING: by putting in your own exception handler into IPython's main
1506 1506 execution loop, you run a very good chance of nasty crashes. This
1507 1507 facility should only be used if you really know what you are doing."""
1508 1508
1509 1509 assert type(exc_tuple)==type(()) , \
1510 1510 "The custom exceptions must be given AS A TUPLE."
1511 1511
1512 1512 def dummy_handler(self,etype,value,tb,tb_offset=None):
1513 1513 print '*** Simple custom exception handler ***'
1514 1514 print 'Exception type :',etype
1515 1515 print 'Exception value:',value
1516 1516 print 'Traceback :',tb
1517 1517 #print 'Source code :','\n'.join(self.buffer)
1518 1518
1519 1519 def validate_stb(stb):
1520 1520 """validate structured traceback return type
1521 1521
1522 1522 return type of CustomTB *should* be a list of strings, but allow
1523 1523 single strings or None, which are harmless.
1524 1524
1525 1525 This function will *always* return a list of strings,
1526 1526 and will raise a TypeError if stb is inappropriate.
1527 1527 """
1528 1528 msg = "CustomTB must return list of strings, not %r" % stb
1529 1529 if stb is None:
1530 1530 return []
1531 1531 elif isinstance(stb, basestring):
1532 1532 return [stb]
1533 1533 elif not isinstance(stb, list):
1534 1534 raise TypeError(msg)
1535 1535 # it's a list
1536 1536 for line in stb:
1537 1537 # check every element
1538 1538 if not isinstance(line, basestring):
1539 1539 raise TypeError(msg)
1540 1540 return stb
1541 1541
1542 1542 if handler is None:
1543 1543 wrapped = dummy_handler
1544 1544 else:
1545 1545 def wrapped(self,etype,value,tb,tb_offset=None):
1546 1546 """wrap CustomTB handler, to protect IPython from user code
1547 1547
1548 1548 This makes it harder (but not impossible) for custom exception
1549 1549 handlers to crash IPython.
1550 1550 """
1551 1551 try:
1552 1552 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1553 1553 return validate_stb(stb)
1554 1554 except:
1555 1555 # clear custom handler immediately
1556 1556 self.set_custom_exc((), None)
1557 1557 print >> io.stderr, "Custom TB Handler failed, unregistering"
1558 1558 # show the exception in handler first
1559 1559 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1560 1560 print >> io.stdout, self.InteractiveTB.stb2text(stb)
1561 1561 print >> io.stdout, "The original exception:"
1562 1562 stb = self.InteractiveTB.structured_traceback(
1563 1563 (etype,value,tb), tb_offset=tb_offset
1564 1564 )
1565 1565 return stb
1566 1566
1567 1567 self.CustomTB = types.MethodType(wrapped,self)
1568 1568 self.custom_exceptions = exc_tuple
1569 1569
1570 1570 def excepthook(self, etype, value, tb):
1571 1571 """One more defense for GUI apps that call sys.excepthook.
1572 1572
1573 1573 GUI frameworks like wxPython trap exceptions and call
1574 1574 sys.excepthook themselves. I guess this is a feature that
1575 1575 enables them to keep running after exceptions that would
1576 1576 otherwise kill their mainloop. This is a bother for IPython
1577 1577 which excepts to catch all of the program exceptions with a try:
1578 1578 except: statement.
1579 1579
1580 1580 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1581 1581 any app directly invokes sys.excepthook, it will look to the user like
1582 1582 IPython crashed. In order to work around this, we can disable the
1583 1583 CrashHandler and replace it with this excepthook instead, which prints a
1584 1584 regular traceback using our InteractiveTB. In this fashion, apps which
1585 1585 call sys.excepthook will generate a regular-looking exception from
1586 1586 IPython, and the CrashHandler will only be triggered by real IPython
1587 1587 crashes.
1588 1588
1589 1589 This hook should be used sparingly, only in places which are not likely
1590 1590 to be true IPython errors.
1591 1591 """
1592 1592 self.showtraceback((etype,value,tb),tb_offset=0)
1593 1593
1594 1594 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1595 1595 exception_only=False):
1596 1596 """Display the exception that just occurred.
1597 1597
1598 1598 If nothing is known about the exception, this is the method which
1599 1599 should be used throughout the code for presenting user tracebacks,
1600 1600 rather than directly invoking the InteractiveTB object.
1601 1601
1602 1602 A specific showsyntaxerror() also exists, but this method can take
1603 1603 care of calling it if needed, so unless you are explicitly catching a
1604 1604 SyntaxError exception, don't try to analyze the stack manually and
1605 1605 simply call this method."""
1606 1606
1607 1607 try:
1608 1608 if exc_tuple is None:
1609 1609 etype, value, tb = sys.exc_info()
1610 1610 else:
1611 1611 etype, value, tb = exc_tuple
1612 1612
1613 1613 if etype is None:
1614 1614 if hasattr(sys, 'last_type'):
1615 1615 etype, value, tb = sys.last_type, sys.last_value, \
1616 1616 sys.last_traceback
1617 1617 else:
1618 1618 self.write_err('No traceback available to show.\n')
1619 1619 return
1620 1620
1621 1621 if etype is SyntaxError:
1622 1622 # Though this won't be called by syntax errors in the input
1623 1623 # line, there may be SyntaxError cases with imported code.
1624 1624 self.showsyntaxerror(filename)
1625 1625 elif etype is UsageError:
1626 1626 self.write_err("UsageError: %s" % value)
1627 1627 else:
1628 1628 # WARNING: these variables are somewhat deprecated and not
1629 1629 # necessarily safe to use in a threaded environment, but tools
1630 1630 # like pdb depend on their existence, so let's set them. If we
1631 1631 # find problems in the field, we'll need to revisit their use.
1632 1632 sys.last_type = etype
1633 1633 sys.last_value = value
1634 1634 sys.last_traceback = tb
1635 1635 if etype in self.custom_exceptions:
1636 1636 stb = self.CustomTB(etype, value, tb, tb_offset)
1637 1637 else:
1638 1638 if exception_only:
1639 1639 stb = ['An exception has occurred, use %tb to see '
1640 1640 'the full traceback.\n']
1641 1641 stb.extend(self.InteractiveTB.get_exception_only(etype,
1642 1642 value))
1643 1643 else:
1644 1644 stb = self.InteractiveTB.structured_traceback(etype,
1645 1645 value, tb, tb_offset=tb_offset)
1646 1646
1647 1647 self._showtraceback(etype, value, stb)
1648 1648 if self.call_pdb:
1649 1649 # drop into debugger
1650 1650 self.debugger(force=True)
1651 1651 return
1652 1652
1653 1653 # Actually show the traceback
1654 1654 self._showtraceback(etype, value, stb)
1655 1655
1656 1656 except KeyboardInterrupt:
1657 1657 self.write_err("\nKeyboardInterrupt\n")
1658 1658
1659 1659 def _showtraceback(self, etype, evalue, stb):
1660 1660 """Actually show a traceback.
1661 1661
1662 1662 Subclasses may override this method to put the traceback on a different
1663 1663 place, like a side channel.
1664 1664 """
1665 1665 print >> io.stdout, self.InteractiveTB.stb2text(stb)
1666 1666
1667 1667 def showsyntaxerror(self, filename=None):
1668 1668 """Display the syntax error that just occurred.
1669 1669
1670 1670 This doesn't display a stack trace because there isn't one.
1671 1671
1672 1672 If a filename is given, it is stuffed in the exception instead
1673 1673 of what was there before (because Python's parser always uses
1674 1674 "<string>" when reading from a string).
1675 1675 """
1676 1676 etype, value, last_traceback = sys.exc_info()
1677 1677
1678 1678 # See note about these variables in showtraceback() above
1679 1679 sys.last_type = etype
1680 1680 sys.last_value = value
1681 1681 sys.last_traceback = last_traceback
1682 1682
1683 1683 if filename and etype is SyntaxError:
1684 1684 try:
1685 1685 value.filename = filename
1686 1686 except:
1687 1687 # Not the format we expect; leave it alone
1688 1688 pass
1689 1689
1690 1690 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1691 1691 self._showtraceback(etype, value, stb)
1692 1692
1693 1693 # This is overridden in TerminalInteractiveShell to show a message about
1694 1694 # the %paste magic.
1695 1695 def showindentationerror(self):
1696 1696 """Called by run_cell when there's an IndentationError in code entered
1697 1697 at the prompt.
1698 1698
1699 1699 This is overridden in TerminalInteractiveShell to show a message about
1700 1700 the %paste magic."""
1701 1701 self.showsyntaxerror()
1702 1702
1703 1703 #-------------------------------------------------------------------------
1704 1704 # Things related to readline
1705 1705 #-------------------------------------------------------------------------
1706 1706
1707 1707 def init_readline(self):
1708 1708 """Command history completion/saving/reloading."""
1709 1709
1710 1710 if self.readline_use:
1711 1711 import IPython.utils.rlineimpl as readline
1712 1712
1713 1713 self.rl_next_input = None
1714 1714 self.rl_do_indent = False
1715 1715
1716 1716 if not self.readline_use or not readline.have_readline:
1717 1717 self.has_readline = False
1718 1718 self.readline = None
1719 1719 # Set a number of methods that depend on readline to be no-op
1720 1720 self.readline_no_record = no_op_context
1721 1721 self.set_readline_completer = no_op
1722 1722 self.set_custom_completer = no_op
1723 1723 self.set_completer_frame = no_op
1724 1724 if self.readline_use:
1725 1725 warn('Readline services not available or not loaded.')
1726 1726 else:
1727 1727 self.has_readline = True
1728 1728 self.readline = readline
1729 1729 sys.modules['readline'] = readline
1730 1730
1731 1731 # Platform-specific configuration
1732 1732 if os.name == 'nt':
1733 1733 # FIXME - check with Frederick to see if we can harmonize
1734 1734 # naming conventions with pyreadline to avoid this
1735 1735 # platform-dependent check
1736 1736 self.readline_startup_hook = readline.set_pre_input_hook
1737 1737 else:
1738 1738 self.readline_startup_hook = readline.set_startup_hook
1739 1739
1740 1740 # Load user's initrc file (readline config)
1741 1741 # Or if libedit is used, load editrc.
1742 1742 inputrc_name = os.environ.get('INPUTRC')
1743 1743 if inputrc_name is None:
1744 1744 inputrc_name = '.inputrc'
1745 1745 if readline.uses_libedit:
1746 1746 inputrc_name = '.editrc'
1747 1747 inputrc_name = os.path.join(self.home_dir, inputrc_name)
1748 1748 if os.path.isfile(inputrc_name):
1749 1749 try:
1750 1750 readline.read_init_file(inputrc_name)
1751 1751 except:
1752 1752 warn('Problems reading readline initialization file <%s>'
1753 1753 % inputrc_name)
1754 1754
1755 1755 # Configure readline according to user's prefs
1756 1756 # This is only done if GNU readline is being used. If libedit
1757 1757 # is being used (as on Leopard) the readline config is
1758 1758 # not run as the syntax for libedit is different.
1759 1759 if not readline.uses_libedit:
1760 1760 for rlcommand in self.readline_parse_and_bind:
1761 1761 #print "loading rl:",rlcommand # dbg
1762 1762 readline.parse_and_bind(rlcommand)
1763 1763
1764 1764 # Remove some chars from the delimiters list. If we encounter
1765 1765 # unicode chars, discard them.
1766 1766 delims = readline.get_completer_delims()
1767 1767 if not py3compat.PY3:
1768 1768 delims = delims.encode("ascii", "ignore")
1769 1769 for d in self.readline_remove_delims:
1770 1770 delims = delims.replace(d, "")
1771 1771 delims = delims.replace(ESC_MAGIC, '')
1772 1772 readline.set_completer_delims(delims)
1773 1773 # otherwise we end up with a monster history after a while:
1774 1774 readline.set_history_length(self.history_length)
1775 1775
1776 1776 self.refill_readline_hist()
1777 1777 self.readline_no_record = ReadlineNoRecord(self)
1778 1778
1779 1779 # Configure auto-indent for all platforms
1780 1780 self.set_autoindent(self.autoindent)
1781 1781
1782 1782 def refill_readline_hist(self):
1783 1783 # Load the last 1000 lines from history
1784 1784 self.readline.clear_history()
1785 1785 stdin_encoding = sys.stdin.encoding or "utf-8"
1786 1786 last_cell = u""
1787 1787 for _, _, cell in self.history_manager.get_tail(1000,
1788 1788 include_latest=True):
1789 1789 # Ignore blank lines and consecutive duplicates
1790 1790 cell = cell.rstrip()
1791 1791 if cell and (cell != last_cell):
1792 1792 if self.multiline_history:
1793 1793 self.readline.add_history(py3compat.unicode_to_str(cell,
1794 1794 stdin_encoding))
1795 1795 else:
1796 1796 for line in cell.splitlines():
1797 1797 self.readline.add_history(py3compat.unicode_to_str(line,
1798 1798 stdin_encoding))
1799 1799 last_cell = cell
1800 1800
1801 1801 def set_next_input(self, s):
1802 1802 """ Sets the 'default' input string for the next command line.
1803 1803
1804 1804 Requires readline.
1805 1805
1806 1806 Example:
1807 1807
1808 1808 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1809 1809 [D:\ipython]|2> Hello Word_ # cursor is here
1810 1810 """
1811 1811 self.rl_next_input = py3compat.cast_bytes_py2(s)
1812 1812
1813 1813 # Maybe move this to the terminal subclass?
1814 1814 def pre_readline(self):
1815 1815 """readline hook to be used at the start of each line.
1816 1816
1817 1817 Currently it handles auto-indent only."""
1818 1818
1819 1819 if self.rl_do_indent:
1820 1820 self.readline.insert_text(self._indent_current_str())
1821 1821 if self.rl_next_input is not None:
1822 1822 self.readline.insert_text(self.rl_next_input)
1823 1823 self.rl_next_input = None
1824 1824
1825 1825 def _indent_current_str(self):
1826 1826 """return the current level of indentation as a string"""
1827 1827 return self.input_splitter.indent_spaces * ' '
1828 1828
1829 1829 #-------------------------------------------------------------------------
1830 1830 # Things related to text completion
1831 1831 #-------------------------------------------------------------------------
1832 1832
1833 1833 def init_completer(self):
1834 1834 """Initialize the completion machinery.
1835 1835
1836 1836 This creates completion machinery that can be used by client code,
1837 1837 either interactively in-process (typically triggered by the readline
1838 1838 library), programatically (such as in test suites) or out-of-prcess
1839 1839 (typically over the network by remote frontends).
1840 1840 """
1841 1841 from IPython.core.completer import IPCompleter
1842 1842 from IPython.core.completerlib import (module_completer,
1843 magic_run_completer, cd_completer, clear_completer)
1843 magic_run_completer, cd_completer, reset_completer)
1844 1844
1845 1845 self.Completer = IPCompleter(shell=self,
1846 1846 namespace=self.user_ns,
1847 1847 global_namespace=self.user_global_ns,
1848 1848 alias_table=self.alias_manager.alias_table,
1849 1849 use_readline=self.has_readline,
1850 1850 config=self.config,
1851 1851 )
1852 1852 self.configurables.append(self.Completer)
1853 1853
1854 1854 # Add custom completers to the basic ones built into IPCompleter
1855 1855 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1856 1856 self.strdispatchers['complete_command'] = sdisp
1857 1857 self.Completer.custom_completers = sdisp
1858 1858
1859 1859 self.set_hook('complete_command', module_completer, str_key = 'import')
1860 1860 self.set_hook('complete_command', module_completer, str_key = 'from')
1861 1861 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1862 1862 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1863 self.set_hook('complete_command', clear_completer, str_key = '%clear')
1863 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1864 1864
1865 1865 # Only configure readline if we truly are using readline. IPython can
1866 1866 # do tab-completion over the network, in GUIs, etc, where readline
1867 1867 # itself may be absent
1868 1868 if self.has_readline:
1869 1869 self.set_readline_completer()
1870 1870
1871 1871 def complete(self, text, line=None, cursor_pos=None):
1872 1872 """Return the completed text and a list of completions.
1873 1873
1874 1874 Parameters
1875 1875 ----------
1876 1876
1877 1877 text : string
1878 1878 A string of text to be completed on. It can be given as empty and
1879 1879 instead a line/position pair are given. In this case, the
1880 1880 completer itself will split the line like readline does.
1881 1881
1882 1882 line : string, optional
1883 1883 The complete line that text is part of.
1884 1884
1885 1885 cursor_pos : int, optional
1886 1886 The position of the cursor on the input line.
1887 1887
1888 1888 Returns
1889 1889 -------
1890 1890 text : string
1891 1891 The actual text that was completed.
1892 1892
1893 1893 matches : list
1894 1894 A sorted list with all possible completions.
1895 1895
1896 1896 The optional arguments allow the completion to take more context into
1897 1897 account, and are part of the low-level completion API.
1898 1898
1899 1899 This is a wrapper around the completion mechanism, similar to what
1900 1900 readline does at the command line when the TAB key is hit. By
1901 1901 exposing it as a method, it can be used by other non-readline
1902 1902 environments (such as GUIs) for text completion.
1903 1903
1904 1904 Simple usage example:
1905 1905
1906 1906 In [1]: x = 'hello'
1907 1907
1908 1908 In [2]: _ip.complete('x.l')
1909 1909 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1910 1910 """
1911 1911
1912 1912 # Inject names into __builtin__ so we can complete on the added names.
1913 1913 with self.builtin_trap:
1914 1914 return self.Completer.complete(text, line, cursor_pos)
1915 1915
1916 1916 def set_custom_completer(self, completer, pos=0):
1917 1917 """Adds a new custom completer function.
1918 1918
1919 1919 The position argument (defaults to 0) is the index in the completers
1920 1920 list where you want the completer to be inserted."""
1921 1921
1922 1922 newcomp = types.MethodType(completer,self.Completer)
1923 1923 self.Completer.matchers.insert(pos,newcomp)
1924 1924
1925 1925 def set_readline_completer(self):
1926 1926 """Reset readline's completer to be our own."""
1927 1927 self.readline.set_completer(self.Completer.rlcomplete)
1928 1928
1929 1929 def set_completer_frame(self, frame=None):
1930 1930 """Set the frame of the completer."""
1931 1931 if frame:
1932 1932 self.Completer.namespace = frame.f_locals
1933 1933 self.Completer.global_namespace = frame.f_globals
1934 1934 else:
1935 1935 self.Completer.namespace = self.user_ns
1936 1936 self.Completer.global_namespace = self.user_global_ns
1937 1937
1938 1938 #-------------------------------------------------------------------------
1939 1939 # Things related to magics
1940 1940 #-------------------------------------------------------------------------
1941 1941
1942 1942 def init_magics(self):
1943 1943 # FIXME: Move the color initialization to the DisplayHook, which
1944 1944 # should be split into a prompt manager and displayhook. We probably
1945 1945 # even need a centralize colors management object.
1946 1946 self.magic_colors(self.colors)
1947 1947 # History was moved to a separate module
1948 1948 from IPython.core import history
1949 1949 history.init_ipython(self)
1950 1950
1951 1951 def magic(self, arg_s, next_input=None):
1952 1952 """Call a magic function by name.
1953 1953
1954 1954 Input: a string containing the name of the magic function to call and
1955 1955 any additional arguments to be passed to the magic.
1956 1956
1957 1957 magic('name -opt foo bar') is equivalent to typing at the ipython
1958 1958 prompt:
1959 1959
1960 1960 In[1]: %name -opt foo bar
1961 1961
1962 1962 To call a magic without arguments, simply use magic('name').
1963 1963
1964 1964 This provides a proper Python function to call IPython's magics in any
1965 1965 valid Python code you can type at the interpreter, including loops and
1966 1966 compound statements.
1967 1967 """
1968 1968 # Allow setting the next input - this is used if the user does `a=abs?`.
1969 1969 # We do this first so that magic functions can override it.
1970 1970 if next_input:
1971 1971 self.set_next_input(next_input)
1972 1972
1973 1973 args = arg_s.split(' ',1)
1974 1974 magic_name = args[0]
1975 1975 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1976 1976
1977 1977 try:
1978 1978 magic_args = args[1]
1979 1979 except IndexError:
1980 1980 magic_args = ''
1981 1981 fn = getattr(self,'magic_'+magic_name,None)
1982 1982 if fn is None:
1983 1983 error("Magic function `%s` not found." % magic_name)
1984 1984 else:
1985 1985 magic_args = self.var_expand(magic_args,1)
1986 1986 # Grab local namespace if we need it:
1987 1987 if getattr(fn, "needs_local_scope", False):
1988 1988 self._magic_locals = sys._getframe(1).f_locals
1989 1989 with self.builtin_trap:
1990 1990 result = fn(magic_args)
1991 1991 # Ensure we're not keeping object references around:
1992 1992 self._magic_locals = {}
1993 1993 return result
1994 1994
1995 1995 def define_magic(self, magicname, func):
1996 1996 """Expose own function as magic function for ipython
1997 1997
1998 1998 Example::
1999 1999
2000 2000 def foo_impl(self,parameter_s=''):
2001 2001 'My very own magic!. (Use docstrings, IPython reads them).'
2002 2002 print 'Magic function. Passed parameter is between < >:'
2003 2003 print '<%s>' % parameter_s
2004 2004 print 'The self object is:', self
2005 2005
2006 2006 ip.define_magic('foo',foo_impl)
2007 2007 """
2008 2008 im = types.MethodType(func,self)
2009 2009 old = getattr(self, "magic_" + magicname, None)
2010 2010 setattr(self, "magic_" + magicname, im)
2011 2011 return old
2012 2012
2013 2013 #-------------------------------------------------------------------------
2014 2014 # Things related to macros
2015 2015 #-------------------------------------------------------------------------
2016 2016
2017 2017 def define_macro(self, name, themacro):
2018 2018 """Define a new macro
2019 2019
2020 2020 Parameters
2021 2021 ----------
2022 2022 name : str
2023 2023 The name of the macro.
2024 2024 themacro : str or Macro
2025 2025 The action to do upon invoking the macro. If a string, a new
2026 2026 Macro object is created by passing the string to it.
2027 2027 """
2028 2028
2029 2029 from IPython.core import macro
2030 2030
2031 2031 if isinstance(themacro, basestring):
2032 2032 themacro = macro.Macro(themacro)
2033 2033 if not isinstance(themacro, macro.Macro):
2034 2034 raise ValueError('A macro must be a string or a Macro instance.')
2035 2035 self.user_ns[name] = themacro
2036 2036
2037 2037 #-------------------------------------------------------------------------
2038 2038 # Things related to the running of system commands
2039 2039 #-------------------------------------------------------------------------
2040 2040
2041 2041 def system_piped(self, cmd):
2042 2042 """Call the given cmd in a subprocess, piping stdout/err
2043 2043
2044 2044 Parameters
2045 2045 ----------
2046 2046 cmd : str
2047 2047 Command to execute (can not end in '&', as background processes are
2048 2048 not supported. Should not be a command that expects input
2049 2049 other than simple text.
2050 2050 """
2051 2051 if cmd.rstrip().endswith('&'):
2052 2052 # this is *far* from a rigorous test
2053 2053 # We do not support backgrounding processes because we either use
2054 2054 # pexpect or pipes to read from. Users can always just call
2055 2055 # os.system() or use ip.system=ip.system_raw
2056 2056 # if they really want a background process.
2057 2057 raise OSError("Background processes not supported.")
2058 2058
2059 2059 # we explicitly do NOT return the subprocess status code, because
2060 2060 # a non-None value would trigger :func:`sys.displayhook` calls.
2061 2061 # Instead, we store the exit_code in user_ns.
2062 2062 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=2))
2063 2063
2064 2064 def system_raw(self, cmd):
2065 2065 """Call the given cmd in a subprocess using os.system
2066 2066
2067 2067 Parameters
2068 2068 ----------
2069 2069 cmd : str
2070 2070 Command to execute.
2071 2071 """
2072 2072 cmd = self.var_expand(cmd, depth=2)
2073 2073 # protect os.system from UNC paths on Windows, which it can't handle:
2074 2074 if sys.platform == 'win32':
2075 2075 from IPython.utils._process_win32 import AvoidUNCPath
2076 2076 with AvoidUNCPath() as path:
2077 2077 if path is not None:
2078 2078 cmd = '"pushd %s &&"%s' % (path, cmd)
2079 2079 cmd = py3compat.unicode_to_str(cmd)
2080 2080 ec = os.system(cmd)
2081 2081 else:
2082 2082 cmd = py3compat.unicode_to_str(cmd)
2083 2083 ec = os.system(cmd)
2084 2084
2085 2085 # We explicitly do NOT return the subprocess status code, because
2086 2086 # a non-None value would trigger :func:`sys.displayhook` calls.
2087 2087 # Instead, we store the exit_code in user_ns.
2088 2088 self.user_ns['_exit_code'] = ec
2089 2089
2090 2090 # use piped system by default, because it is better behaved
2091 2091 system = system_piped
2092 2092
2093 2093 def getoutput(self, cmd, split=True):
2094 2094 """Get output (possibly including stderr) from a subprocess.
2095 2095
2096 2096 Parameters
2097 2097 ----------
2098 2098 cmd : str
2099 2099 Command to execute (can not end in '&', as background processes are
2100 2100 not supported.
2101 2101 split : bool, optional
2102 2102
2103 2103 If True, split the output into an IPython SList. Otherwise, an
2104 2104 IPython LSString is returned. These are objects similar to normal
2105 2105 lists and strings, with a few convenience attributes for easier
2106 2106 manipulation of line-based output. You can use '?' on them for
2107 2107 details.
2108 2108 """
2109 2109 if cmd.rstrip().endswith('&'):
2110 2110 # this is *far* from a rigorous test
2111 2111 raise OSError("Background processes not supported.")
2112 2112 out = getoutput(self.var_expand(cmd, depth=2))
2113 2113 if split:
2114 2114 out = SList(out.splitlines())
2115 2115 else:
2116 2116 out = LSString(out)
2117 2117 return out
2118 2118
2119 2119 #-------------------------------------------------------------------------
2120 2120 # Things related to aliases
2121 2121 #-------------------------------------------------------------------------
2122 2122
2123 2123 def init_alias(self):
2124 2124 self.alias_manager = AliasManager(shell=self, config=self.config)
2125 2125 self.configurables.append(self.alias_manager)
2126 2126 self.ns_table['alias'] = self.alias_manager.alias_table,
2127 2127
2128 2128 #-------------------------------------------------------------------------
2129 2129 # Things related to extensions and plugins
2130 2130 #-------------------------------------------------------------------------
2131 2131
2132 2132 def init_extension_manager(self):
2133 2133 self.extension_manager = ExtensionManager(shell=self, config=self.config)
2134 2134 self.configurables.append(self.extension_manager)
2135 2135
2136 2136 def init_plugin_manager(self):
2137 2137 self.plugin_manager = PluginManager(config=self.config)
2138 2138 self.configurables.append(self.plugin_manager)
2139 2139
2140 2140
2141 2141 #-------------------------------------------------------------------------
2142 2142 # Things related to payloads
2143 2143 #-------------------------------------------------------------------------
2144 2144
2145 2145 def init_payload(self):
2146 2146 self.payload_manager = PayloadManager(config=self.config)
2147 2147 self.configurables.append(self.payload_manager)
2148 2148
2149 2149 #-------------------------------------------------------------------------
2150 2150 # Things related to the prefilter
2151 2151 #-------------------------------------------------------------------------
2152 2152
2153 2153 def init_prefilter(self):
2154 2154 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
2155 2155 self.configurables.append(self.prefilter_manager)
2156 2156 # Ultimately this will be refactored in the new interpreter code, but
2157 2157 # for now, we should expose the main prefilter method (there's legacy
2158 2158 # code out there that may rely on this).
2159 2159 self.prefilter = self.prefilter_manager.prefilter_lines
2160 2160
2161 2161 def auto_rewrite_input(self, cmd):
2162 2162 """Print to the screen the rewritten form of the user's command.
2163 2163
2164 2164 This shows visual feedback by rewriting input lines that cause
2165 2165 automatic calling to kick in, like::
2166 2166
2167 2167 /f x
2168 2168
2169 2169 into::
2170 2170
2171 2171 ------> f(x)
2172 2172
2173 2173 after the user's input prompt. This helps the user understand that the
2174 2174 input line was transformed automatically by IPython.
2175 2175 """
2176 2176 if not self.show_rewritten_input:
2177 2177 return
2178 2178
2179 2179 rw = self.prompt_manager.render('rewrite') + cmd
2180 2180
2181 2181 try:
2182 2182 # plain ascii works better w/ pyreadline, on some machines, so
2183 2183 # we use it and only print uncolored rewrite if we have unicode
2184 2184 rw = str(rw)
2185 2185 print >> io.stdout, rw
2186 2186 except UnicodeEncodeError:
2187 2187 print "------> " + cmd
2188 2188
2189 2189 #-------------------------------------------------------------------------
2190 2190 # Things related to extracting values/expressions from kernel and user_ns
2191 2191 #-------------------------------------------------------------------------
2192 2192
2193 2193 def _simple_error(self):
2194 2194 etype, value = sys.exc_info()[:2]
2195 2195 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
2196 2196
2197 2197 def user_variables(self, names):
2198 2198 """Get a list of variable names from the user's namespace.
2199 2199
2200 2200 Parameters
2201 2201 ----------
2202 2202 names : list of strings
2203 2203 A list of names of variables to be read from the user namespace.
2204 2204
2205 2205 Returns
2206 2206 -------
2207 2207 A dict, keyed by the input names and with the repr() of each value.
2208 2208 """
2209 2209 out = {}
2210 2210 user_ns = self.user_ns
2211 2211 for varname in names:
2212 2212 try:
2213 2213 value = repr(user_ns[varname])
2214 2214 except:
2215 2215 value = self._simple_error()
2216 2216 out[varname] = value
2217 2217 return out
2218 2218
2219 2219 def user_expressions(self, expressions):
2220 2220 """Evaluate a dict of expressions in the user's namespace.
2221 2221
2222 2222 Parameters
2223 2223 ----------
2224 2224 expressions : dict
2225 2225 A dict with string keys and string values. The expression values
2226 2226 should be valid Python expressions, each of which will be evaluated
2227 2227 in the user namespace.
2228 2228
2229 2229 Returns
2230 2230 -------
2231 2231 A dict, keyed like the input expressions dict, with the repr() of each
2232 2232 value.
2233 2233 """
2234 2234 out = {}
2235 2235 user_ns = self.user_ns
2236 2236 global_ns = self.user_global_ns
2237 2237 for key, expr in expressions.iteritems():
2238 2238 try:
2239 2239 value = repr(eval(expr, global_ns, user_ns))
2240 2240 except:
2241 2241 value = self._simple_error()
2242 2242 out[key] = value
2243 2243 return out
2244 2244
2245 2245 #-------------------------------------------------------------------------
2246 2246 # Things related to the running of code
2247 2247 #-------------------------------------------------------------------------
2248 2248
2249 2249 def ex(self, cmd):
2250 2250 """Execute a normal python statement in user namespace."""
2251 2251 with self.builtin_trap:
2252 2252 exec cmd in self.user_global_ns, self.user_ns
2253 2253
2254 2254 def ev(self, expr):
2255 2255 """Evaluate python expression expr in user namespace.
2256 2256
2257 2257 Returns the result of evaluation
2258 2258 """
2259 2259 with self.builtin_trap:
2260 2260 return eval(expr, self.user_global_ns, self.user_ns)
2261 2261
2262 2262 def safe_execfile(self, fname, *where, **kw):
2263 2263 """A safe version of the builtin execfile().
2264 2264
2265 2265 This version will never throw an exception, but instead print
2266 2266 helpful error messages to the screen. This only works on pure
2267 2267 Python files with the .py extension.
2268 2268
2269 2269 Parameters
2270 2270 ----------
2271 2271 fname : string
2272 2272 The name of the file to be executed.
2273 2273 where : tuple
2274 2274 One or two namespaces, passed to execfile() as (globals,locals).
2275 2275 If only one is given, it is passed as both.
2276 2276 exit_ignore : bool (False)
2277 2277 If True, then silence SystemExit for non-zero status (it is always
2278 2278 silenced for zero status, as it is so common).
2279 2279 raise_exceptions : bool (False)
2280 2280 If True raise exceptions everywhere. Meant for testing.
2281 2281
2282 2282 """
2283 2283 kw.setdefault('exit_ignore', False)
2284 2284 kw.setdefault('raise_exceptions', False)
2285 2285
2286 2286 fname = os.path.abspath(os.path.expanduser(fname))
2287 2287
2288 2288 # Make sure we can open the file
2289 2289 try:
2290 2290 with open(fname) as thefile:
2291 2291 pass
2292 2292 except:
2293 2293 warn('Could not open file <%s> for safe execution.' % fname)
2294 2294 return
2295 2295
2296 2296 # Find things also in current directory. This is needed to mimic the
2297 2297 # behavior of running a script from the system command line, where
2298 2298 # Python inserts the script's directory into sys.path
2299 2299 dname = os.path.dirname(fname)
2300 2300
2301 2301 with prepended_to_syspath(dname):
2302 2302 try:
2303 2303 py3compat.execfile(fname,*where)
2304 2304 except SystemExit, status:
2305 2305 # If the call was made with 0 or None exit status (sys.exit(0)
2306 2306 # or sys.exit() ), don't bother showing a traceback, as both of
2307 2307 # these are considered normal by the OS:
2308 2308 # > python -c'import sys;sys.exit(0)'; echo $?
2309 2309 # 0
2310 2310 # > python -c'import sys;sys.exit()'; echo $?
2311 2311 # 0
2312 2312 # For other exit status, we show the exception unless
2313 2313 # explicitly silenced, but only in short form.
2314 2314 if kw['raise_exceptions']:
2315 2315 raise
2316 2316 if status.code not in (0, None) and not kw['exit_ignore']:
2317 2317 self.showtraceback(exception_only=True)
2318 2318 except:
2319 2319 if kw['raise_exceptions']:
2320 2320 raise
2321 2321 self.showtraceback()
2322 2322
2323 2323 def safe_execfile_ipy(self, fname):
2324 2324 """Like safe_execfile, but for .ipy files with IPython syntax.
2325 2325
2326 2326 Parameters
2327 2327 ----------
2328 2328 fname : str
2329 2329 The name of the file to execute. The filename must have a
2330 2330 .ipy extension.
2331 2331 """
2332 2332 fname = os.path.abspath(os.path.expanduser(fname))
2333 2333
2334 2334 # Make sure we can open the file
2335 2335 try:
2336 2336 with open(fname) as thefile:
2337 2337 pass
2338 2338 except:
2339 2339 warn('Could not open file <%s> for safe execution.' % fname)
2340 2340 return
2341 2341
2342 2342 # Find things also in current directory. This is needed to mimic the
2343 2343 # behavior of running a script from the system command line, where
2344 2344 # Python inserts the script's directory into sys.path
2345 2345 dname = os.path.dirname(fname)
2346 2346
2347 2347 with prepended_to_syspath(dname):
2348 2348 try:
2349 2349 with open(fname) as thefile:
2350 2350 # self.run_cell currently captures all exceptions
2351 2351 # raised in user code. It would be nice if there were
2352 2352 # versions of runlines, execfile that did raise, so
2353 2353 # we could catch the errors.
2354 2354 self.run_cell(thefile.read(), store_history=False)
2355 2355 except:
2356 2356 self.showtraceback()
2357 2357 warn('Unknown failure executing file: <%s>' % fname)
2358 2358
2359 2359 def run_cell(self, raw_cell, store_history=False):
2360 2360 """Run a complete IPython cell.
2361 2361
2362 2362 Parameters
2363 2363 ----------
2364 2364 raw_cell : str
2365 2365 The code (including IPython code such as %magic functions) to run.
2366 2366 store_history : bool
2367 2367 If True, the raw and translated cell will be stored in IPython's
2368 2368 history. For user code calling back into IPython's machinery, this
2369 2369 should be set to False.
2370 2370 """
2371 2371 if (not raw_cell) or raw_cell.isspace():
2372 2372 return
2373 2373
2374 2374 for line in raw_cell.splitlines():
2375 2375 self.input_splitter.push(line)
2376 2376 cell = self.input_splitter.source_reset()
2377 2377
2378 2378 with self.builtin_trap:
2379 2379 prefilter_failed = False
2380 2380 if len(cell.splitlines()) == 1:
2381 2381 try:
2382 2382 # use prefilter_lines to handle trailing newlines
2383 2383 # restore trailing newline for ast.parse
2384 2384 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2385 2385 except AliasError as e:
2386 2386 error(e)
2387 2387 prefilter_failed = True
2388 2388 except Exception:
2389 2389 # don't allow prefilter errors to crash IPython
2390 2390 self.showtraceback()
2391 2391 prefilter_failed = True
2392 2392
2393 2393 # Store raw and processed history
2394 2394 if store_history:
2395 2395 self.history_manager.store_inputs(self.execution_count,
2396 2396 cell, raw_cell)
2397 2397
2398 2398 self.logger.log(cell, raw_cell)
2399 2399
2400 2400 if not prefilter_failed:
2401 2401 # don't run if prefilter failed
2402 2402 cell_name = self.compile.cache(cell, self.execution_count)
2403 2403
2404 2404 with self.display_trap:
2405 2405 try:
2406 2406 code_ast = self.compile.ast_parse(cell, filename=cell_name)
2407 2407 except IndentationError:
2408 2408 self.showindentationerror()
2409 2409 if store_history:
2410 2410 self.execution_count += 1
2411 2411 return None
2412 2412 except (OverflowError, SyntaxError, ValueError, TypeError,
2413 2413 MemoryError):
2414 2414 self.showsyntaxerror()
2415 2415 if store_history:
2416 2416 self.execution_count += 1
2417 2417 return None
2418 2418
2419 2419 self.run_ast_nodes(code_ast.body, cell_name,
2420 2420 interactivity="last_expr")
2421 2421
2422 2422 # Execute any registered post-execution functions.
2423 2423 for func, status in self._post_execute.iteritems():
2424 2424 if self.disable_failing_post_execute and not status:
2425 2425 continue
2426 2426 try:
2427 2427 func()
2428 2428 except KeyboardInterrupt:
2429 2429 print >> io.stderr, "\nKeyboardInterrupt"
2430 2430 except Exception:
2431 2431 # register as failing:
2432 2432 self._post_execute[func] = False
2433 2433 self.showtraceback()
2434 2434 print >> io.stderr, '\n'.join([
2435 2435 "post-execution function %r produced an error." % func,
2436 2436 "If this problem persists, you can disable failing post-exec functions with:",
2437 2437 "",
2438 2438 " get_ipython().disable_failing_post_execute = True"
2439 2439 ])
2440 2440
2441 2441 if store_history:
2442 2442 # Write output to the database. Does nothing unless
2443 2443 # history output logging is enabled.
2444 2444 self.history_manager.store_output(self.execution_count)
2445 2445 # Each cell is a *single* input, regardless of how many lines it has
2446 2446 self.execution_count += 1
2447 2447
2448 2448 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'):
2449 2449 """Run a sequence of AST nodes. The execution mode depends on the
2450 2450 interactivity parameter.
2451 2451
2452 2452 Parameters
2453 2453 ----------
2454 2454 nodelist : list
2455 2455 A sequence of AST nodes to run.
2456 2456 cell_name : str
2457 2457 Will be passed to the compiler as the filename of the cell. Typically
2458 2458 the value returned by ip.compile.cache(cell).
2459 2459 interactivity : str
2460 2460 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2461 2461 run interactively (displaying output from expressions). 'last_expr'
2462 2462 will run the last node interactively only if it is an expression (i.e.
2463 2463 expressions in loops or other blocks are not displayed. Other values
2464 2464 for this parameter will raise a ValueError.
2465 2465 """
2466 2466 if not nodelist:
2467 2467 return
2468 2468
2469 2469 if interactivity == 'last_expr':
2470 2470 if isinstance(nodelist[-1], ast.Expr):
2471 2471 interactivity = "last"
2472 2472 else:
2473 2473 interactivity = "none"
2474 2474
2475 2475 if interactivity == 'none':
2476 2476 to_run_exec, to_run_interactive = nodelist, []
2477 2477 elif interactivity == 'last':
2478 2478 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2479 2479 elif interactivity == 'all':
2480 2480 to_run_exec, to_run_interactive = [], nodelist
2481 2481 else:
2482 2482 raise ValueError("Interactivity was %r" % interactivity)
2483 2483
2484 2484 exec_count = self.execution_count
2485 2485
2486 2486 try:
2487 2487 for i, node in enumerate(to_run_exec):
2488 2488 mod = ast.Module([node])
2489 2489 code = self.compile(mod, cell_name, "exec")
2490 2490 if self.run_code(code):
2491 2491 return True
2492 2492
2493 2493 for i, node in enumerate(to_run_interactive):
2494 2494 mod = ast.Interactive([node])
2495 2495 code = self.compile(mod, cell_name, "single")
2496 2496 if self.run_code(code):
2497 2497 return True
2498 2498 except:
2499 2499 # It's possible to have exceptions raised here, typically by
2500 2500 # compilation of odd code (such as a naked 'return' outside a
2501 2501 # function) that did parse but isn't valid. Typically the exception
2502 2502 # is a SyntaxError, but it's safest just to catch anything and show
2503 2503 # the user a traceback.
2504 2504
2505 2505 # We do only one try/except outside the loop to minimize the impact
2506 2506 # on runtime, and also because if any node in the node list is
2507 2507 # broken, we should stop execution completely.
2508 2508 self.showtraceback()
2509 2509
2510 2510 return False
2511 2511
2512 2512 def run_code(self, code_obj):
2513 2513 """Execute a code object.
2514 2514
2515 2515 When an exception occurs, self.showtraceback() is called to display a
2516 2516 traceback.
2517 2517
2518 2518 Parameters
2519 2519 ----------
2520 2520 code_obj : code object
2521 2521 A compiled code object, to be executed
2522 2522 post_execute : bool [default: True]
2523 2523 whether to call post_execute hooks after this particular execution.
2524 2524
2525 2525 Returns
2526 2526 -------
2527 2527 False : successful execution.
2528 2528 True : an error occurred.
2529 2529 """
2530 2530
2531 2531 # Set our own excepthook in case the user code tries to call it
2532 2532 # directly, so that the IPython crash handler doesn't get triggered
2533 2533 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2534 2534
2535 2535 # we save the original sys.excepthook in the instance, in case config
2536 2536 # code (such as magics) needs access to it.
2537 2537 self.sys_excepthook = old_excepthook
2538 2538 outflag = 1 # happens in more places, so it's easier as default
2539 2539 try:
2540 2540 try:
2541 2541 self.hooks.pre_run_code_hook()
2542 2542 #rprint('Running code', repr(code_obj)) # dbg
2543 2543 exec code_obj in self.user_global_ns, self.user_ns
2544 2544 finally:
2545 2545 # Reset our crash handler in place
2546 2546 sys.excepthook = old_excepthook
2547 2547 except SystemExit:
2548 2548 self.showtraceback(exception_only=True)
2549 2549 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2550 2550 except self.custom_exceptions:
2551 2551 etype,value,tb = sys.exc_info()
2552 2552 self.CustomTB(etype,value,tb)
2553 2553 except:
2554 2554 self.showtraceback()
2555 2555 else:
2556 2556 outflag = 0
2557 2557 if softspace(sys.stdout, 0):
2558 2558 print
2559 2559
2560 2560 return outflag
2561 2561
2562 2562 # For backwards compatibility
2563 2563 runcode = run_code
2564 2564
2565 2565 #-------------------------------------------------------------------------
2566 2566 # Things related to GUI support and pylab
2567 2567 #-------------------------------------------------------------------------
2568 2568
2569 2569 def enable_gui(self, gui=None):
2570 2570 raise NotImplementedError('Implement enable_gui in a subclass')
2571 2571
2572 2572 def enable_pylab(self, gui=None, import_all=True):
2573 2573 """Activate pylab support at runtime.
2574 2574
2575 2575 This turns on support for matplotlib, preloads into the interactive
2576 2576 namespace all of numpy and pylab, and configures IPython to correctly
2577 2577 interact with the GUI event loop. The GUI backend to be used can be
2578 2578 optionally selected with the optional :param:`gui` argument.
2579 2579
2580 2580 Parameters
2581 2581 ----------
2582 2582 gui : optional, string
2583 2583
2584 2584 If given, dictates the choice of matplotlib GUI backend to use
2585 2585 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2586 2586 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2587 2587 matplotlib (as dictated by the matplotlib build-time options plus the
2588 2588 user's matplotlibrc configuration file). Note that not all backends
2589 2589 make sense in all contexts, for example a terminal ipython can't
2590 2590 display figures inline.
2591 2591 """
2592 2592
2593 2593 # We want to prevent the loading of pylab to pollute the user's
2594 2594 # namespace as shown by the %who* magics, so we execute the activation
2595 2595 # code in an empty namespace, and we update *both* user_ns and
2596 2596 # user_ns_hidden with this information.
2597 2597 ns = {}
2598 2598 try:
2599 2599 gui = pylab_activate(ns, gui, import_all, self)
2600 2600 except KeyError:
2601 2601 error("Backend %r not supported" % gui)
2602 2602 return
2603 2603 self.user_ns.update(ns)
2604 2604 self.user_ns_hidden.update(ns)
2605 2605 # Now we must activate the gui pylab wants to use, and fix %run to take
2606 2606 # plot updates into account
2607 2607 self.enable_gui(gui)
2608 2608 self.magic_run = self._pylab_magic_run
2609 2609
2610 2610 #-------------------------------------------------------------------------
2611 2611 # Utilities
2612 2612 #-------------------------------------------------------------------------
2613 2613
2614 2614 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
2615 2615 """Expand python variables in a string.
2616 2616
2617 2617 The depth argument indicates how many frames above the caller should
2618 2618 be walked to look for the local namespace where to expand variables.
2619 2619
2620 2620 The global namespace for expansion is always the user's interactive
2621 2621 namespace.
2622 2622 """
2623 2623 ns = self.user_ns.copy()
2624 2624 ns.update(sys._getframe(depth+1).f_locals)
2625 2625 ns.pop('self', None)
2626 2626 return formatter.format(cmd, **ns)
2627 2627
2628 2628 def mktempfile(self, data=None, prefix='ipython_edit_'):
2629 2629 """Make a new tempfile and return its filename.
2630 2630
2631 2631 This makes a call to tempfile.mktemp, but it registers the created
2632 2632 filename internally so ipython cleans it up at exit time.
2633 2633
2634 2634 Optional inputs:
2635 2635
2636 2636 - data(None): if data is given, it gets written out to the temp file
2637 2637 immediately, and the file is closed again."""
2638 2638
2639 2639 filename = tempfile.mktemp('.py', prefix)
2640 2640 self.tempfiles.append(filename)
2641 2641
2642 2642 if data:
2643 2643 tmp_file = open(filename,'w')
2644 2644 tmp_file.write(data)
2645 2645 tmp_file.close()
2646 2646 return filename
2647 2647
2648 2648 # TODO: This should be removed when Term is refactored.
2649 2649 def write(self,data):
2650 2650 """Write a string to the default output"""
2651 2651 io.stdout.write(data)
2652 2652
2653 2653 # TODO: This should be removed when Term is refactored.
2654 2654 def write_err(self,data):
2655 2655 """Write a string to the default error output"""
2656 2656 io.stderr.write(data)
2657 2657
2658 2658 def ask_yes_no(self, prompt, default=None):
2659 2659 if self.quiet:
2660 2660 return True
2661 2661 return ask_yes_no(prompt,default)
2662 2662
2663 2663 def show_usage(self):
2664 2664 """Show a usage message"""
2665 2665 page.page(IPython.core.usage.interactive_usage)
2666 2666
2667 2667 def find_user_code(self, target, raw=True):
2668 2668 """Get a code string from history, file, or a string or macro.
2669 2669
2670 2670 This is mainly used by magic functions.
2671 2671
2672 2672 Parameters
2673 2673 ----------
2674 2674 target : str
2675 2675 A string specifying code to retrieve. This will be tried respectively
2676 2676 as: ranges of input history (see %history for syntax), a filename, or
2677 2677 an expression evaluating to a string or Macro in the user namespace.
2678 2678 raw : bool
2679 2679 If true (default), retrieve raw history. Has no effect on the other
2680 2680 retrieval mechanisms.
2681 2681
2682 2682 Returns
2683 2683 -------
2684 2684 A string of code.
2685 2685
2686 2686 ValueError is raised if nothing is found, and TypeError if it evaluates
2687 2687 to an object of another type. In each case, .args[0] is a printable
2688 2688 message.
2689 2689 """
2690 2690 code = self.extract_input_lines(target, raw=raw) # Grab history
2691 2691 if code:
2692 2692 return code
2693 2693 if os.path.isfile(target): # Read file
2694 2694 return open(target, "r").read()
2695 2695
2696 2696 try: # User namespace
2697 2697 codeobj = eval(target, self.user_ns)
2698 2698 except Exception:
2699 2699 raise ValueError(("'%s' was not found in history, as a file, nor in"
2700 2700 " the user namespace.") % target)
2701 2701 if isinstance(codeobj, basestring):
2702 2702 return codeobj
2703 2703 elif isinstance(codeobj, Macro):
2704 2704 return codeobj.value
2705 2705
2706 2706 raise TypeError("%s is neither a string nor a macro." % target,
2707 2707 codeobj)
2708 2708
2709 2709 #-------------------------------------------------------------------------
2710 2710 # Things related to IPython exiting
2711 2711 #-------------------------------------------------------------------------
2712 2712 def atexit_operations(self):
2713 2713 """This will be executed at the time of exit.
2714 2714
2715 2715 Cleanup operations and saving of persistent data that is done
2716 2716 unconditionally by IPython should be performed here.
2717 2717
2718 2718 For things that may depend on startup flags or platform specifics (such
2719 2719 as having readline or not), register a separate atexit function in the
2720 2720 code that has the appropriate information, rather than trying to
2721 2721 clutter
2722 2722 """
2723 2723 # Close the history session (this stores the end time and line count)
2724 2724 # this must be *before* the tempfile cleanup, in case of temporary
2725 2725 # history db
2726 2726 self.history_manager.end_session()
2727 2727
2728 2728 # Cleanup all tempfiles left around
2729 2729 for tfile in self.tempfiles:
2730 2730 try:
2731 2731 os.unlink(tfile)
2732 2732 except OSError:
2733 2733 pass
2734 2734
2735 2735 # Clear all user namespaces to release all references cleanly.
2736 2736 self.reset(new_session=False)
2737 2737
2738 2738 # Run user hooks
2739 2739 self.hooks.shutdown_hook()
2740 2740
2741 2741 def cleanup(self):
2742 2742 self.restore_sys_module_state()
2743 2743
2744 2744
2745 2745 class InteractiveShellABC(object):
2746 2746 """An abstract base class for InteractiveShell."""
2747 2747 __metaclass__ = abc.ABCMeta
2748 2748
2749 2749 InteractiveShellABC.register(InteractiveShell)
@@ -1,3757 +1,3770 b''
1 1 # encoding: utf-8
2 2 """Magic functions for InteractiveShell.
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 7 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
8 8 # Copyright (C) 2008-2011 The IPython Development Team
9 9
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 import __builtin__ as builtin_mod
19 19 import __future__
20 20 import bdb
21 21 import inspect
22 22 import imp
23 23 import os
24 24 import sys
25 25 import shutil
26 26 import re
27 27 import time
28 28 import gc
29 29 from StringIO import StringIO
30 30 from getopt import getopt,GetoptError
31 31 from pprint import pformat
32 32 from xmlrpclib import ServerProxy
33 33
34 34 # cProfile was added in Python2.5
35 35 try:
36 36 import cProfile as profile
37 37 import pstats
38 38 except ImportError:
39 39 # profile isn't bundled by default in Debian for license reasons
40 40 try:
41 41 import profile,pstats
42 42 except ImportError:
43 43 profile = pstats = None
44 44
45 45 import IPython
46 46 from IPython.core import debugger, oinspect
47 47 from IPython.core.error import TryNext
48 48 from IPython.core.error import UsageError
49 49 from IPython.core.error import StdinNotImplementedError
50 50 from IPython.core.fakemodule import FakeModule
51 51 from IPython.core.profiledir import ProfileDir
52 52 from IPython.core.macro import Macro
53 53 from IPython.core import magic_arguments, page
54 54 from IPython.core.prefilter import ESC_MAGIC
55 55 from IPython.core.pylabtools import mpl_runner
56 56 from IPython.testing.skipdoctest import skip_doctest
57 57 from IPython.utils import py3compat
58 58 from IPython.utils.io import file_read, nlprint
59 59 from IPython.utils.module_paths import find_mod
60 60 from IPython.utils.path import get_py_filename, unquote_filename
61 61 from IPython.utils.process import arg_split, abbrev_cwd
62 62 from IPython.utils.terminal import set_term_title
63 63 from IPython.utils.text import LSString, SList, format_screen
64 64 from IPython.utils.timing import clock, clock2
65 65 from IPython.utils.warn import warn, error
66 66 from IPython.utils.ipstruct import Struct
67 67 from IPython.config.application import Application
68 68
69 69 #-----------------------------------------------------------------------------
70 70 # Utility functions
71 71 #-----------------------------------------------------------------------------
72 72
73 73 def on_off(tag):
74 74 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
75 75 return ['OFF','ON'][tag]
76 76
77 77 class Bunch: pass
78 78
79 79 def compress_dhist(dh):
80 80 head, tail = dh[:-10], dh[-10:]
81 81
82 82 newhead = []
83 83 done = set()
84 84 for h in head:
85 85 if h in done:
86 86 continue
87 87 newhead.append(h)
88 88 done.add(h)
89 89
90 90 return newhead + tail
91 91
92 92 def needs_local_scope(func):
93 93 """Decorator to mark magic functions which need to local scope to run."""
94 94 func.needs_local_scope = True
95 95 return func
96 96
97 97
98 98 # Used for exception handling in magic_edit
99 99 class MacroToEdit(ValueError): pass
100 100
101 101 # Taken from PEP 263, this is the official encoding regexp.
102 102 _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)")
103 103
104 104 #***************************************************************************
105 105 # Main class implementing Magic functionality
106 106
107 107 # XXX - for some odd reason, if Magic is made a new-style class, we get errors
108 108 # on construction of the main InteractiveShell object. Something odd is going
109 109 # on with super() calls, Configurable and the MRO... For now leave it as-is, but
110 110 # eventually this needs to be clarified.
111 111 # BG: This is because InteractiveShell inherits from this, but is itself a
112 112 # Configurable. This messes up the MRO in some way. The fix is that we need to
113 113 # make Magic a configurable that InteractiveShell does not subclass.
114 114
115 115 class Magic:
116 116 """Magic functions for InteractiveShell.
117 117
118 118 Shell functions which can be reached as %function_name. All magic
119 119 functions should accept a string, which they can parse for their own
120 120 needs. This can make some functions easier to type, eg `%cd ../`
121 121 vs. `%cd("../")`
122 122
123 123 ALL definitions MUST begin with the prefix magic_. The user won't need it
124 124 at the command line, but it is is needed in the definition. """
125 125
126 126 # class globals
127 127 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
128 128 'Automagic is ON, % prefix NOT needed for magic functions.']
129 129
130 130
131 131 configurables = None
132 132 #......................................................................
133 133 # some utility functions
134 134
135 135 def __init__(self,shell):
136 136
137 137 self.options_table = {}
138 138 if profile is None:
139 139 self.magic_prun = self.profile_missing_notice
140 140 self.shell = shell
141 141 if self.configurables is None:
142 142 self.configurables = []
143 143
144 144 # namespace for holding state we may need
145 145 self._magic_state = Bunch()
146 146
147 147 def profile_missing_notice(self, *args, **kwargs):
148 148 error("""\
149 149 The profile module could not be found. It has been removed from the standard
150 150 python packages because of its non-free license. To use profiling, install the
151 151 python-profiler package from non-free.""")
152 152
153 153 def default_option(self,fn,optstr):
154 154 """Make an entry in the options_table for fn, with value optstr"""
155 155
156 156 if fn not in self.lsmagic():
157 157 error("%s is not a magic function" % fn)
158 158 self.options_table[fn] = optstr
159 159
160 160 def lsmagic(self):
161 161 """Return a list of currently available magic functions.
162 162
163 163 Gives a list of the bare names after mangling (['ls','cd', ...], not
164 164 ['magic_ls','magic_cd',...]"""
165 165
166 166 # FIXME. This needs a cleanup, in the way the magics list is built.
167 167
168 168 # magics in class definition
169 169 class_magic = lambda fn: fn.startswith('magic_') and \
170 170 callable(Magic.__dict__[fn])
171 171 # in instance namespace (run-time user additions)
172 172 inst_magic = lambda fn: fn.startswith('magic_') and \
173 173 callable(self.__dict__[fn])
174 174 # and bound magics by user (so they can access self):
175 175 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
176 176 callable(self.__class__.__dict__[fn])
177 177 magics = filter(class_magic,Magic.__dict__.keys()) + \
178 178 filter(inst_magic,self.__dict__.keys()) + \
179 179 filter(inst_bound_magic,self.__class__.__dict__.keys())
180 180 out = []
181 181 for fn in set(magics):
182 182 out.append(fn.replace('magic_','',1))
183 183 out.sort()
184 184 return out
185 185
186 186 def extract_input_lines(self, range_str, raw=False):
187 187 """Return as a string a set of input history slices.
188 188
189 189 Inputs:
190 190
191 191 - range_str: the set of slices is given as a string, like
192 192 "~5/6-~4/2 4:8 9", since this function is for use by magic functions
193 193 which get their arguments as strings. The number before the / is the
194 194 session number: ~n goes n back from the current session.
195 195
196 196 Optional inputs:
197 197
198 198 - raw(False): by default, the processed input is used. If this is
199 199 true, the raw input history is used instead.
200 200
201 201 Note that slices can be called with two notations:
202 202
203 203 N:M -> standard python form, means including items N...(M-1).
204 204
205 205 N-M -> include items N..M (closed endpoint)."""
206 206 lines = self.shell.history_manager.\
207 207 get_range_by_str(range_str, raw=raw)
208 208 return "\n".join(x for _, _, x in lines)
209 209
210 210 def arg_err(self,func):
211 211 """Print docstring if incorrect arguments were passed"""
212 212 print 'Error in arguments:'
213 213 print oinspect.getdoc(func)
214 214
215 215 def format_latex(self,strng):
216 216 """Format a string for latex inclusion."""
217 217
218 218 # Characters that need to be escaped for latex:
219 219 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
220 220 # Magic command names as headers:
221 221 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
222 222 re.MULTILINE)
223 223 # Magic commands
224 224 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
225 225 re.MULTILINE)
226 226 # Paragraph continue
227 227 par_re = re.compile(r'\\$',re.MULTILINE)
228 228
229 229 # The "\n" symbol
230 230 newline_re = re.compile(r'\\n')
231 231
232 232 # Now build the string for output:
233 233 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
234 234 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
235 235 strng)
236 236 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
237 237 strng = par_re.sub(r'\\\\',strng)
238 238 strng = escape_re.sub(r'\\\1',strng)
239 239 strng = newline_re.sub(r'\\textbackslash{}n',strng)
240 240 return strng
241 241
242 242 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
243 243 """Parse options passed to an argument string.
244 244
245 245 The interface is similar to that of getopt(), but it returns back a
246 246 Struct with the options as keys and the stripped argument string still
247 247 as a string.
248 248
249 249 arg_str is quoted as a true sys.argv vector by using shlex.split.
250 250 This allows us to easily expand variables, glob files, quote
251 251 arguments, etc.
252 252
253 253 Options:
254 254 -mode: default 'string'. If given as 'list', the argument string is
255 255 returned as a list (split on whitespace) instead of a string.
256 256
257 257 -list_all: put all option values in lists. Normally only options
258 258 appearing more than once are put in a list.
259 259
260 260 -posix (True): whether to split the input line in POSIX mode or not,
261 261 as per the conventions outlined in the shlex module from the
262 262 standard library."""
263 263
264 264 # inject default options at the beginning of the input line
265 265 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
266 266 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
267 267
268 268 mode = kw.get('mode','string')
269 269 if mode not in ['string','list']:
270 270 raise ValueError,'incorrect mode given: %s' % mode
271 271 # Get options
272 272 list_all = kw.get('list_all',0)
273 273 posix = kw.get('posix', os.name == 'posix')
274 274 strict = kw.get('strict', True)
275 275
276 276 # Check if we have more than one argument to warrant extra processing:
277 277 odict = {} # Dictionary with options
278 278 args = arg_str.split()
279 279 if len(args) >= 1:
280 280 # If the list of inputs only has 0 or 1 thing in it, there's no
281 281 # need to look for options
282 282 argv = arg_split(arg_str, posix, strict)
283 283 # Do regular option processing
284 284 try:
285 285 opts,args = getopt(argv,opt_str,*long_opts)
286 286 except GetoptError,e:
287 287 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
288 288 " ".join(long_opts)))
289 289 for o,a in opts:
290 290 if o.startswith('--'):
291 291 o = o[2:]
292 292 else:
293 293 o = o[1:]
294 294 try:
295 295 odict[o].append(a)
296 296 except AttributeError:
297 297 odict[o] = [odict[o],a]
298 298 except KeyError:
299 299 if list_all:
300 300 odict[o] = [a]
301 301 else:
302 302 odict[o] = a
303 303
304 304 # Prepare opts,args for return
305 305 opts = Struct(odict)
306 306 if mode == 'string':
307 307 args = ' '.join(args)
308 308
309 309 return opts,args
310 310
311 311 #......................................................................
312 312 # And now the actual magic functions
313 313
314 314 # Functions for IPython shell work (vars,funcs, config, etc)
315 315 def magic_lsmagic(self, parameter_s = ''):
316 316 """List currently available magic functions."""
317 317 mesc = ESC_MAGIC
318 318 print 'Available magic functions:\n'+mesc+\
319 319 (' '+mesc).join(self.lsmagic())
320 320 print '\n' + Magic.auto_status[self.shell.automagic]
321 321 return None
322 322
323 323 def magic_magic(self, parameter_s = ''):
324 324 """Print information about the magic function system.
325 325
326 326 Supported formats: -latex, -brief, -rest
327 327 """
328 328
329 329 mode = ''
330 330 try:
331 331 if parameter_s.split()[0] == '-latex':
332 332 mode = 'latex'
333 333 if parameter_s.split()[0] == '-brief':
334 334 mode = 'brief'
335 335 if parameter_s.split()[0] == '-rest':
336 336 mode = 'rest'
337 337 rest_docs = []
338 338 except:
339 339 pass
340 340
341 341 magic_docs = []
342 342 for fname in self.lsmagic():
343 343 mname = 'magic_' + fname
344 344 for space in (Magic,self,self.__class__):
345 345 try:
346 346 fn = space.__dict__[mname]
347 347 except KeyError:
348 348 pass
349 349 else:
350 350 break
351 351 if mode == 'brief':
352 352 # only first line
353 353 if fn.__doc__:
354 354 fndoc = fn.__doc__.split('\n',1)[0]
355 355 else:
356 356 fndoc = 'No documentation'
357 357 else:
358 358 if fn.__doc__:
359 359 fndoc = fn.__doc__.rstrip()
360 360 else:
361 361 fndoc = 'No documentation'
362 362
363 363
364 364 if mode == 'rest':
365 365 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC,
366 366 fname,fndoc))
367 367
368 368 else:
369 369 magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC,
370 370 fname,fndoc))
371 371
372 372 magic_docs = ''.join(magic_docs)
373 373
374 374 if mode == 'rest':
375 375 return "".join(rest_docs)
376 376
377 377 if mode == 'latex':
378 378 print self.format_latex(magic_docs)
379 379 return
380 380 else:
381 381 magic_docs = format_screen(magic_docs)
382 382 if mode == 'brief':
383 383 return magic_docs
384 384
385 385 outmsg = """
386 386 IPython's 'magic' functions
387 387 ===========================
388 388
389 389 The magic function system provides a series of functions which allow you to
390 390 control the behavior of IPython itself, plus a lot of system-type
391 391 features. All these functions are prefixed with a % character, but parameters
392 392 are given without parentheses or quotes.
393 393
394 394 NOTE: If you have 'automagic' enabled (via the command line option or with the
395 395 %automagic function), you don't need to type in the % explicitly. By default,
396 396 IPython ships with automagic on, so you should only rarely need the % escape.
397 397
398 398 Example: typing '%cd mydir' (without the quotes) changes you working directory
399 399 to 'mydir', if it exists.
400 400
401 401 For a list of the available magic functions, use %lsmagic. For a description
402 402 of any of them, type %magic_name?, e.g. '%cd?'.
403 403
404 404 Currently the magic system has the following functions:\n"""
405 405
406 406 mesc = ESC_MAGIC
407 407 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
408 408 "\n\n%s%s\n\n%s" % (outmsg,
409 409 magic_docs,mesc,mesc,
410 410 (' '+mesc).join(self.lsmagic()),
411 411 Magic.auto_status[self.shell.automagic] ) )
412 412 page.page(outmsg)
413 413
414 414 def magic_automagic(self, parameter_s = ''):
415 415 """Make magic functions callable without having to type the initial %.
416 416
417 417 Without argumentsl toggles on/off (when off, you must call it as
418 418 %automagic, of course). With arguments it sets the value, and you can
419 419 use any of (case insensitive):
420 420
421 421 - on,1,True: to activate
422 422
423 423 - off,0,False: to deactivate.
424 424
425 425 Note that magic functions have lowest priority, so if there's a
426 426 variable whose name collides with that of a magic fn, automagic won't
427 427 work for that function (you get the variable instead). However, if you
428 428 delete the variable (del var), the previously shadowed magic function
429 429 becomes visible to automagic again."""
430 430
431 431 arg = parameter_s.lower()
432 432 if parameter_s in ('on','1','true'):
433 433 self.shell.automagic = True
434 434 elif parameter_s in ('off','0','false'):
435 435 self.shell.automagic = False
436 436 else:
437 437 self.shell.automagic = not self.shell.automagic
438 438 print '\n' + Magic.auto_status[self.shell.automagic]
439 439
440 440 @skip_doctest
441 441 def magic_autocall(self, parameter_s = ''):
442 442 """Make functions callable without having to type parentheses.
443 443
444 444 Usage:
445 445
446 446 %autocall [mode]
447 447
448 448 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
449 449 value is toggled on and off (remembering the previous state).
450 450
451 451 In more detail, these values mean:
452 452
453 453 0 -> fully disabled
454 454
455 455 1 -> active, but do not apply if there are no arguments on the line.
456 456
457 457 In this mode, you get:
458 458
459 459 In [1]: callable
460 460 Out[1]: <built-in function callable>
461 461
462 462 In [2]: callable 'hello'
463 463 ------> callable('hello')
464 464 Out[2]: False
465 465
466 466 2 -> Active always. Even if no arguments are present, the callable
467 467 object is called:
468 468
469 469 In [2]: float
470 470 ------> float()
471 471 Out[2]: 0.0
472 472
473 473 Note that even with autocall off, you can still use '/' at the start of
474 474 a line to treat the first argument on the command line as a function
475 475 and add parentheses to it:
476 476
477 477 In [8]: /str 43
478 478 ------> str(43)
479 479 Out[8]: '43'
480 480
481 481 # all-random (note for auto-testing)
482 482 """
483 483
484 484 if parameter_s:
485 485 arg = int(parameter_s)
486 486 else:
487 487 arg = 'toggle'
488 488
489 489 if not arg in (0,1,2,'toggle'):
490 490 error('Valid modes: (0->Off, 1->Smart, 2->Full')
491 491 return
492 492
493 493 if arg in (0,1,2):
494 494 self.shell.autocall = arg
495 495 else: # toggle
496 496 if self.shell.autocall:
497 497 self._magic_state.autocall_save = self.shell.autocall
498 498 self.shell.autocall = 0
499 499 else:
500 500 try:
501 501 self.shell.autocall = self._magic_state.autocall_save
502 502 except AttributeError:
503 503 self.shell.autocall = self._magic_state.autocall_save = 1
504 504
505 505 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
506 506
507 507
508 508 def magic_page(self, parameter_s=''):
509 509 """Pretty print the object and display it through a pager.
510 510
511 511 %page [options] OBJECT
512 512
513 513 If no object is given, use _ (last output).
514 514
515 515 Options:
516 516
517 517 -r: page str(object), don't pretty-print it."""
518 518
519 519 # After a function contributed by Olivier Aubert, slightly modified.
520 520
521 521 # Process options/args
522 522 opts,args = self.parse_options(parameter_s,'r')
523 523 raw = 'r' in opts
524 524
525 525 oname = args and args or '_'
526 526 info = self._ofind(oname)
527 527 if info['found']:
528 528 txt = (raw and str or pformat)( info['obj'] )
529 529 page.page(txt)
530 530 else:
531 531 print 'Object `%s` not found' % oname
532 532
533 533 def magic_profile(self, parameter_s=''):
534 534 """Print your currently active IPython profile."""
535 535 from IPython.core.application import BaseIPythonApplication
536 536 if BaseIPythonApplication.initialized():
537 537 print BaseIPythonApplication.instance().profile
538 538 else:
539 539 error("profile is an application-level value, but you don't appear to be in an IPython application")
540 540
541 541 def magic_pinfo(self, parameter_s='', namespaces=None):
542 542 """Provide detailed information about an object.
543 543
544 544 '%pinfo object' is just a synonym for object? or ?object."""
545 545
546 546 #print 'pinfo par: <%s>' % parameter_s # dbg
547 547
548 548
549 549 # detail_level: 0 -> obj? , 1 -> obj??
550 550 detail_level = 0
551 551 # We need to detect if we got called as 'pinfo pinfo foo', which can
552 552 # happen if the user types 'pinfo foo?' at the cmd line.
553 553 pinfo,qmark1,oname,qmark2 = \
554 554 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
555 555 if pinfo or qmark1 or qmark2:
556 556 detail_level = 1
557 557 if "*" in oname:
558 558 self.magic_psearch(oname)
559 559 else:
560 560 self.shell._inspect('pinfo', oname, detail_level=detail_level,
561 561 namespaces=namespaces)
562 562
563 563 def magic_pinfo2(self, parameter_s='', namespaces=None):
564 564 """Provide extra detailed information about an object.
565 565
566 566 '%pinfo2 object' is just a synonym for object?? or ??object."""
567 567 self.shell._inspect('pinfo', parameter_s, detail_level=1,
568 568 namespaces=namespaces)
569 569
570 570 @skip_doctest
571 571 def magic_pdef(self, parameter_s='', namespaces=None):
572 572 """Print the definition header for any callable object.
573 573
574 574 If the object is a class, print the constructor information.
575 575
576 576 Examples
577 577 --------
578 578 ::
579 579
580 580 In [3]: %pdef urllib.urlopen
581 581 urllib.urlopen(url, data=None, proxies=None)
582 582 """
583 583 self._inspect('pdef',parameter_s, namespaces)
584 584
585 585 def magic_pdoc(self, parameter_s='', namespaces=None):
586 586 """Print the docstring for an object.
587 587
588 588 If the given object is a class, it will print both the class and the
589 589 constructor docstrings."""
590 590 self._inspect('pdoc',parameter_s, namespaces)
591 591
592 592 def magic_psource(self, parameter_s='', namespaces=None):
593 593 """Print (or run through pager) the source code for an object."""
594 594 self._inspect('psource',parameter_s, namespaces)
595 595
596 596 def magic_pfile(self, parameter_s=''):
597 597 """Print (or run through pager) the file where an object is defined.
598 598
599 599 The file opens at the line where the object definition begins. IPython
600 600 will honor the environment variable PAGER if set, and otherwise will
601 601 do its best to print the file in a convenient form.
602 602
603 603 If the given argument is not an object currently defined, IPython will
604 604 try to interpret it as a filename (automatically adding a .py extension
605 605 if needed). You can thus use %pfile as a syntax highlighting code
606 606 viewer."""
607 607
608 608 # first interpret argument as an object name
609 609 out = self._inspect('pfile',parameter_s)
610 610 # if not, try the input as a filename
611 611 if out == 'not found':
612 612 try:
613 613 filename = get_py_filename(parameter_s)
614 614 except IOError,msg:
615 615 print msg
616 616 return
617 617 page.page(self.shell.inspector.format(file(filename).read()))
618 618
619 619 def magic_psearch(self, parameter_s=''):
620 620 """Search for object in namespaces by wildcard.
621 621
622 622 %psearch [options] PATTERN [OBJECT TYPE]
623 623
624 624 Note: ? can be used as a synonym for %psearch, at the beginning or at
625 625 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
626 626 rest of the command line must be unchanged (options come first), so
627 627 for example the following forms are equivalent
628 628
629 629 %psearch -i a* function
630 630 -i a* function?
631 631 ?-i a* function
632 632
633 633 Arguments:
634 634
635 635 PATTERN
636 636
637 637 where PATTERN is a string containing * as a wildcard similar to its
638 638 use in a shell. The pattern is matched in all namespaces on the
639 639 search path. By default objects starting with a single _ are not
640 640 matched, many IPython generated objects have a single
641 641 underscore. The default is case insensitive matching. Matching is
642 642 also done on the attributes of objects and not only on the objects
643 643 in a module.
644 644
645 645 [OBJECT TYPE]
646 646
647 647 Is the name of a python type from the types module. The name is
648 648 given in lowercase without the ending type, ex. StringType is
649 649 written string. By adding a type here only objects matching the
650 650 given type are matched. Using all here makes the pattern match all
651 651 types (this is the default).
652 652
653 653 Options:
654 654
655 655 -a: makes the pattern match even objects whose names start with a
656 656 single underscore. These names are normally omitted from the
657 657 search.
658 658
659 659 -i/-c: make the pattern case insensitive/sensitive. If neither of
660 660 these options are given, the default is read from your configuration
661 661 file, with the option ``InteractiveShell.wildcards_case_sensitive``.
662 662 If this option is not specified in your configuration file, IPython's
663 663 internal default is to do a case sensitive search.
664 664
665 665 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
666 666 specify can be searched in any of the following namespaces:
667 667 'builtin', 'user', 'user_global','internal', 'alias', where
668 668 'builtin' and 'user' are the search defaults. Note that you should
669 669 not use quotes when specifying namespaces.
670 670
671 671 'Builtin' contains the python module builtin, 'user' contains all
672 672 user data, 'alias' only contain the shell aliases and no python
673 673 objects, 'internal' contains objects used by IPython. The
674 674 'user_global' namespace is only used by embedded IPython instances,
675 675 and it contains module-level globals. You can add namespaces to the
676 676 search with -s or exclude them with -e (these options can be given
677 677 more than once).
678 678
679 679 Examples:
680 680
681 681 %psearch a* -> objects beginning with an a
682 682 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
683 683 %psearch a* function -> all functions beginning with an a
684 684 %psearch re.e* -> objects beginning with an e in module re
685 685 %psearch r*.e* -> objects that start with e in modules starting in r
686 686 %psearch r*.* string -> all strings in modules beginning with r
687 687
688 688 Case sensitive search:
689 689
690 690 %psearch -c a* list all object beginning with lower case a
691 691
692 692 Show objects beginning with a single _:
693 693
694 694 %psearch -a _* list objects beginning with a single underscore"""
695 695 try:
696 696 parameter_s.encode('ascii')
697 697 except UnicodeEncodeError:
698 698 print 'Python identifiers can only contain ascii characters.'
699 699 return
700 700
701 701 # default namespaces to be searched
702 702 def_search = ['user_local', 'user_global', 'builtin']
703 703
704 704 # Process options/args
705 705 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
706 706 opt = opts.get
707 707 shell = self.shell
708 708 psearch = shell.inspector.psearch
709 709
710 710 # select case options
711 711 if opts.has_key('i'):
712 712 ignore_case = True
713 713 elif opts.has_key('c'):
714 714 ignore_case = False
715 715 else:
716 716 ignore_case = not shell.wildcards_case_sensitive
717 717
718 718 # Build list of namespaces to search from user options
719 719 def_search.extend(opt('s',[]))
720 720 ns_exclude = ns_exclude=opt('e',[])
721 721 ns_search = [nm for nm in def_search if nm not in ns_exclude]
722 722
723 723 # Call the actual search
724 724 try:
725 725 psearch(args,shell.ns_table,ns_search,
726 726 show_all=opt('a'),ignore_case=ignore_case)
727 727 except:
728 728 shell.showtraceback()
729 729
730 730 @skip_doctest
731 731 def magic_who_ls(self, parameter_s=''):
732 732 """Return a sorted list of all interactive variables.
733 733
734 734 If arguments are given, only variables of types matching these
735 735 arguments are returned.
736 736
737 737 Examples
738 738 --------
739 739
740 740 Define two variables and list them with who_ls::
741 741
742 742 In [1]: alpha = 123
743 743
744 744 In [2]: beta = 'test'
745 745
746 746 In [3]: %who_ls
747 747 Out[3]: ['alpha', 'beta']
748 748
749 749 In [4]: %who_ls int
750 750 Out[4]: ['alpha']
751 751
752 752 In [5]: %who_ls str
753 753 Out[5]: ['beta']
754 754 """
755 755
756 756 user_ns = self.shell.user_ns
757 757 user_ns_hidden = self.shell.user_ns_hidden
758 758 out = [ i for i in user_ns
759 759 if not i.startswith('_') \
760 760 and not i in user_ns_hidden ]
761 761
762 762 typelist = parameter_s.split()
763 763 if typelist:
764 764 typeset = set(typelist)
765 765 out = [i for i in out if type(user_ns[i]).__name__ in typeset]
766 766
767 767 out.sort()
768 768 return out
769 769
770 770 @skip_doctest
771 771 def magic_who(self, parameter_s=''):
772 772 """Print all interactive variables, with some minimal formatting.
773 773
774 774 If any arguments are given, only variables whose type matches one of
775 775 these are printed. For example:
776 776
777 777 %who function str
778 778
779 779 will only list functions and strings, excluding all other types of
780 780 variables. To find the proper type names, simply use type(var) at a
781 781 command line to see how python prints type names. For example:
782 782
783 783 In [1]: type('hello')\\
784 784 Out[1]: <type 'str'>
785 785
786 786 indicates that the type name for strings is 'str'.
787 787
788 788 %who always excludes executed names loaded through your configuration
789 789 file and things which are internal to IPython.
790 790
791 791 This is deliberate, as typically you may load many modules and the
792 792 purpose of %who is to show you only what you've manually defined.
793 793
794 794 Examples
795 795 --------
796 796
797 797 Define two variables and list them with who::
798 798
799 799 In [1]: alpha = 123
800 800
801 801 In [2]: beta = 'test'
802 802
803 803 In [3]: %who
804 804 alpha beta
805 805
806 806 In [4]: %who int
807 807 alpha
808 808
809 809 In [5]: %who str
810 810 beta
811 811 """
812 812
813 813 varlist = self.magic_who_ls(parameter_s)
814 814 if not varlist:
815 815 if parameter_s:
816 816 print 'No variables match your requested type.'
817 817 else:
818 818 print 'Interactive namespace is empty.'
819 819 return
820 820
821 821 # if we have variables, move on...
822 822 count = 0
823 823 for i in varlist:
824 824 print i+'\t',
825 825 count += 1
826 826 if count > 8:
827 827 count = 0
828 828 print
829 829 print
830 830
831 831 @skip_doctest
832 832 def magic_whos(self, parameter_s=''):
833 833 """Like %who, but gives some extra information about each variable.
834 834
835 835 The same type filtering of %who can be applied here.
836 836
837 837 For all variables, the type is printed. Additionally it prints:
838 838
839 839 - For {},[],(): their length.
840 840
841 841 - For numpy arrays, a summary with shape, number of
842 842 elements, typecode and size in memory.
843 843
844 844 - Everything else: a string representation, snipping their middle if
845 845 too long.
846 846
847 847 Examples
848 848 --------
849 849
850 850 Define two variables and list them with whos::
851 851
852 852 In [1]: alpha = 123
853 853
854 854 In [2]: beta = 'test'
855 855
856 856 In [3]: %whos
857 857 Variable Type Data/Info
858 858 --------------------------------
859 859 alpha int 123
860 860 beta str test
861 861 """
862 862
863 863 varnames = self.magic_who_ls(parameter_s)
864 864 if not varnames:
865 865 if parameter_s:
866 866 print 'No variables match your requested type.'
867 867 else:
868 868 print 'Interactive namespace is empty.'
869 869 return
870 870
871 871 # if we have variables, move on...
872 872
873 873 # for these types, show len() instead of data:
874 874 seq_types = ['dict', 'list', 'tuple']
875 875
876 876 # for numpy arrays, display summary info
877 877 ndarray_type = None
878 878 if 'numpy' in sys.modules:
879 879 try:
880 880 from numpy import ndarray
881 881 except ImportError:
882 882 pass
883 883 else:
884 884 ndarray_type = ndarray.__name__
885 885
886 886 # Find all variable names and types so we can figure out column sizes
887 887 def get_vars(i):
888 888 return self.shell.user_ns[i]
889 889
890 890 # some types are well known and can be shorter
891 891 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
892 892 def type_name(v):
893 893 tn = type(v).__name__
894 894 return abbrevs.get(tn,tn)
895 895
896 896 varlist = map(get_vars,varnames)
897 897
898 898 typelist = []
899 899 for vv in varlist:
900 900 tt = type_name(vv)
901 901
902 902 if tt=='instance':
903 903 typelist.append( abbrevs.get(str(vv.__class__),
904 904 str(vv.__class__)))
905 905 else:
906 906 typelist.append(tt)
907 907
908 908 # column labels and # of spaces as separator
909 909 varlabel = 'Variable'
910 910 typelabel = 'Type'
911 911 datalabel = 'Data/Info'
912 912 colsep = 3
913 913 # variable format strings
914 914 vformat = "{0:<{varwidth}}{1:<{typewidth}}"
915 915 aformat = "%s: %s elems, type `%s`, %s bytes"
916 916 # find the size of the columns to format the output nicely
917 917 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
918 918 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
919 919 # table header
920 920 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
921 921 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
922 922 # and the table itself
923 923 kb = 1024
924 924 Mb = 1048576 # kb**2
925 925 for vname,var,vtype in zip(varnames,varlist,typelist):
926 926 print vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth),
927 927 if vtype in seq_types:
928 928 print "n="+str(len(var))
929 929 elif vtype == ndarray_type:
930 930 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
931 931 if vtype==ndarray_type:
932 932 # numpy
933 933 vsize = var.size
934 934 vbytes = vsize*var.itemsize
935 935 vdtype = var.dtype
936 936 else:
937 937 # Numeric
938 938 vsize = Numeric.size(var)
939 939 vbytes = vsize*var.itemsize()
940 940 vdtype = var.typecode()
941 941
942 942 if vbytes < 100000:
943 943 print aformat % (vshape,vsize,vdtype,vbytes)
944 944 else:
945 945 print aformat % (vshape,vsize,vdtype,vbytes),
946 946 if vbytes < Mb:
947 947 print '(%s kb)' % (vbytes/kb,)
948 948 else:
949 949 print '(%s Mb)' % (vbytes/Mb,)
950 950 else:
951 951 try:
952 952 vstr = str(var)
953 953 except UnicodeEncodeError:
954 954 vstr = unicode(var).encode(sys.getdefaultencoding(),
955 955 'backslashreplace')
956 956 vstr = vstr.replace('\n','\\n')
957 957 if len(vstr) < 50:
958 958 print vstr
959 959 else:
960 960 print vstr[:25] + "<...>" + vstr[-25:]
961 961
962 962 def magic_reset(self, parameter_s=''):
963 """Resets the namespace by removing all names defined by the user.
963 """Resets the namespace by removing all names defined by the user, if
964 called without arguments, or by removing some types of objects, such
965 as everything currently in IPython's In[] and Out[] containers (see
966 the parameters for details).
964 967
965 968 Parameters
966 969 ----------
967 970 -f : force reset without asking for confirmation.
968 971
969 972 -s : 'Soft' reset: Only clears your namespace, leaving history intact.
970 973 References to objects may be kept. By default (without this option),
971 974 we do a 'hard' reset, giving you a new session and removing all
972 975 references to objects from the current session.
973 976
977 in : reset input history
978
979 out : reset output history
980
981 dhist : reset directory history
982
983 array : reset only variables that are NumPy arrays
984
985 See Also
986 --------
987 %reset_selective
988
974 989 Examples
975 990 --------
976 991 In [6]: a = 1
977 992
978 993 In [7]: a
979 994 Out[7]: 1
980 995
981 996 In [8]: 'a' in _ip.user_ns
982 997 Out[8]: True
983 998
984 999 In [9]: %reset -f
985 1000
986 1001 In [1]: 'a' in _ip.user_ns
987 1002 Out[1]: False
988 1003
1004 In [2]: %reset -f in
1005 Flushing input history
1006
1007 In [3]: %reset -f dhist in
1008 Flushing directory history
1009 Flushing input history
1010
989 1011 Notes
990 1012 -----
991 1013 Calling this magic from clients that do not implement standard input,
992 1014 such as the ipython notebook interface, will reset the namespace
993 1015 without confirmation.
994 1016 """
995 opts, args = self.parse_options(parameter_s,'sf')
1017 opts, args = self.parse_options(parameter_s,'sf', mode='list')
996 1018 if 'f' in opts:
997 1019 ans = True
998 1020 else:
999 1021 try:
1000 1022 ans = self.shell.ask_yes_no(
1001 1023 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ", default='n')
1002 1024 except StdinNotImplementedError:
1003 1025 ans = True
1004 1026 if not ans:
1005 1027 print 'Nothing done.'
1006 1028 return
1007 1029
1008 1030 if 's' in opts: # Soft reset
1009 1031 user_ns = self.shell.user_ns
1010 1032 for i in self.magic_who_ls():
1011 1033 del(user_ns[i])
1012
1013 else: # Hard reset
1034 elif len(args) == 0: # Hard reset
1014 1035 self.shell.reset(new_session = False)
1015 1036
1037 # reset in/out/dhist/array: previously extensinions/clearcmd.py
1038 ip = self.shell
1039 user_ns = self.user_ns # local lookup, heavily used
1040
1041 for target in args:
1042 if target == 'out':
1043 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
1044 self.displayhook.flush()
1045
1046 elif target == 'in':
1047 print "Flushing input history"
1048 pc = self.displayhook.prompt_count + 1
1049 for n in range(1, pc):
1050 key = '_i'+repr(n)
1051 user_ns.pop(key,None)
1052 user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
1053 hm = ip.history_manager
1054 # don't delete these, as %save and %macro depending on the length
1055 # of these lists to be preserved
1056 hm.input_hist_parsed[:] = [''] * pc
1057 hm.input_hist_raw[:] = [''] * pc
1058 # hm has internal machinery for _i,_ii,_iii, clear it out
1059 hm._i = hm._ii = hm._iii = hm._i00 = u''
1060
1061 elif target == 'array':
1062 # Support cleaning up numpy arrays
1063 try:
1064 from numpy import ndarray
1065 # This must be done with items and not iteritems because we're
1066 # going to modify the dict in-place.
1067 for x,val in user_ns.items():
1068 if isinstance(val,ndarray):
1069 del user_ns[x]
1070 except ImportError:
1071 print "reset array only works if Numpy is available."
1072
1073 elif target == 'dhist':
1074 print "Flushing directory history"
1075 del user_ns['_dh'][:]
1016 1076
1077 else:
1078 print "Don't know how to reset ",
1079 print target + ", please run `%reset?` for details"
1080
1081 gc.collect()
1017 1082
1018 1083 def magic_reset_selective(self, parameter_s=''):
1019 1084 """Resets the namespace by removing names defined by the user.
1020 1085
1021 1086 Input/Output history are left around in case you need them.
1022 1087
1023 1088 %reset_selective [-f] regex
1024 1089
1025 1090 No action is taken if regex is not included
1026 1091
1027 1092 Options
1028 1093 -f : force reset without asking for confirmation.
1029 1094
1095 See Also
1096 --------
1097 %reset
1098
1030 1099 Examples
1031 1100 --------
1032 1101
1033 1102 We first fully reset the namespace so your output looks identical to
1034 1103 this example for pedagogical reasons; in practice you do not need a
1035 1104 full reset.
1036 1105
1037 1106 In [1]: %reset -f
1038 1107
1039 1108 Now, with a clean namespace we can make a few variables and use
1040 1109 %reset_selective to only delete names that match our regexp:
1041 1110
1042 1111 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
1043 1112
1044 1113 In [3]: who_ls
1045 1114 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
1046 1115
1047 1116 In [4]: %reset_selective -f b[2-3]m
1048 1117
1049 1118 In [5]: who_ls
1050 1119 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1051 1120
1052 1121 In [6]: %reset_selective -f d
1053 1122
1054 1123 In [7]: who_ls
1055 1124 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1056 1125
1057 1126 In [8]: %reset_selective -f c
1058 1127
1059 1128 In [9]: who_ls
1060 1129 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
1061 1130
1062 1131 In [10]: %reset_selective -f b
1063 1132
1064 1133 In [11]: who_ls
1065 1134 Out[11]: ['a']
1066 1135
1067 1136 Notes
1068 1137 -----
1069 1138 Calling this magic from clients that do not implement standard input,
1070 1139 such as the ipython notebook interface, will reset the namespace
1071 1140 without confirmation.
1072 1141 """
1073 1142
1074 1143 opts, regex = self.parse_options(parameter_s,'f')
1075 1144
1076 1145 if opts.has_key('f'):
1077 1146 ans = True
1078 1147 else:
1079 1148 try:
1080 1149 ans = self.shell.ask_yes_no(
1081 1150 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ",
1082 1151 default='n')
1083 1152 except StdinNotImplementedError:
1084 1153 ans = True
1085 1154 if not ans:
1086 1155 print 'Nothing done.'
1087 1156 return
1088 1157 user_ns = self.shell.user_ns
1089 1158 if not regex:
1090 1159 print 'No regex pattern specified. Nothing done.'
1091 1160 return
1092 1161 else:
1093 1162 try:
1094 1163 m = re.compile(regex)
1095 1164 except TypeError:
1096 1165 raise TypeError('regex must be a string or compiled pattern')
1097 1166 for i in self.magic_who_ls():
1098 1167 if m.search(i):
1099 1168 del(user_ns[i])
1100 1169
1101 1170 def magic_xdel(self, parameter_s=''):
1102 1171 """Delete a variable, trying to clear it from anywhere that
1103 1172 IPython's machinery has references to it. By default, this uses
1104 1173 the identity of the named object in the user namespace to remove
1105 1174 references held under other names. The object is also removed
1106 1175 from the output history.
1107 1176
1108 1177 Options
1109 1178 -n : Delete the specified name from all namespaces, without
1110 1179 checking their identity.
1111 1180 """
1112 1181 opts, varname = self.parse_options(parameter_s,'n')
1113 1182 try:
1114 1183 self.shell.del_var(varname, ('n' in opts))
1115 1184 except (NameError, ValueError) as e:
1116 1185 print type(e).__name__ +": "+ str(e)
1117 1186
1118 1187 def magic_logstart(self,parameter_s=''):
1119 1188 """Start logging anywhere in a session.
1120 1189
1121 1190 %logstart [-o|-r|-t] [log_name [log_mode]]
1122 1191
1123 1192 If no name is given, it defaults to a file named 'ipython_log.py' in your
1124 1193 current directory, in 'rotate' mode (see below).
1125 1194
1126 1195 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1127 1196 history up to that point and then continues logging.
1128 1197
1129 1198 %logstart takes a second optional parameter: logging mode. This can be one
1130 1199 of (note that the modes are given unquoted):\\
1131 1200 append: well, that says it.\\
1132 1201 backup: rename (if exists) to name~ and start name.\\
1133 1202 global: single logfile in your home dir, appended to.\\
1134 1203 over : overwrite existing log.\\
1135 1204 rotate: create rotating logs name.1~, name.2~, etc.
1136 1205
1137 1206 Options:
1138 1207
1139 1208 -o: log also IPython's output. In this mode, all commands which
1140 1209 generate an Out[NN] prompt are recorded to the logfile, right after
1141 1210 their corresponding input line. The output lines are always
1142 1211 prepended with a '#[Out]# ' marker, so that the log remains valid
1143 1212 Python code.
1144 1213
1145 1214 Since this marker is always the same, filtering only the output from
1146 1215 a log is very easy, using for example a simple awk call:
1147 1216
1148 1217 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1149 1218
1150 1219 -r: log 'raw' input. Normally, IPython's logs contain the processed
1151 1220 input, so that user lines are logged in their final form, converted
1152 1221 into valid Python. For example, %Exit is logged as
1153 1222 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1154 1223 exactly as typed, with no transformations applied.
1155 1224
1156 1225 -t: put timestamps before each input line logged (these are put in
1157 1226 comments)."""
1158 1227
1159 1228 opts,par = self.parse_options(parameter_s,'ort')
1160 1229 log_output = 'o' in opts
1161 1230 log_raw_input = 'r' in opts
1162 1231 timestamp = 't' in opts
1163 1232
1164 1233 logger = self.shell.logger
1165 1234
1166 1235 # if no args are given, the defaults set in the logger constructor by
1167 1236 # ipython remain valid
1168 1237 if par:
1169 1238 try:
1170 1239 logfname,logmode = par.split()
1171 1240 except:
1172 1241 logfname = par
1173 1242 logmode = 'backup'
1174 1243 else:
1175 1244 logfname = logger.logfname
1176 1245 logmode = logger.logmode
1177 1246 # put logfname into rc struct as if it had been called on the command
1178 1247 # line, so it ends up saved in the log header Save it in case we need
1179 1248 # to restore it...
1180 1249 old_logfile = self.shell.logfile
1181 1250 if logfname:
1182 1251 logfname = os.path.expanduser(logfname)
1183 1252 self.shell.logfile = logfname
1184 1253
1185 1254 loghead = '# IPython log file\n\n'
1186 1255 try:
1187 1256 started = logger.logstart(logfname,loghead,logmode,
1188 1257 log_output,timestamp,log_raw_input)
1189 1258 except:
1190 1259 self.shell.logfile = old_logfile
1191 1260 warn("Couldn't start log: %s" % sys.exc_info()[1])
1192 1261 else:
1193 1262 # log input history up to this point, optionally interleaving
1194 1263 # output if requested
1195 1264
1196 1265 if timestamp:
1197 1266 # disable timestamping for the previous history, since we've
1198 1267 # lost those already (no time machine here).
1199 1268 logger.timestamp = False
1200 1269
1201 1270 if log_raw_input:
1202 1271 input_hist = self.shell.history_manager.input_hist_raw
1203 1272 else:
1204 1273 input_hist = self.shell.history_manager.input_hist_parsed
1205 1274
1206 1275 if log_output:
1207 1276 log_write = logger.log_write
1208 1277 output_hist = self.shell.history_manager.output_hist
1209 1278 for n in range(1,len(input_hist)-1):
1210 1279 log_write(input_hist[n].rstrip() + '\n')
1211 1280 if n in output_hist:
1212 1281 log_write(repr(output_hist[n]),'output')
1213 1282 else:
1214 1283 logger.log_write('\n'.join(input_hist[1:]))
1215 1284 logger.log_write('\n')
1216 1285 if timestamp:
1217 1286 # re-enable timestamping
1218 1287 logger.timestamp = True
1219 1288
1220 1289 print ('Activating auto-logging. '
1221 1290 'Current session state plus future input saved.')
1222 1291 logger.logstate()
1223 1292
1224 1293 def magic_logstop(self,parameter_s=''):
1225 1294 """Fully stop logging and close log file.
1226 1295
1227 1296 In order to start logging again, a new %logstart call needs to be made,
1228 1297 possibly (though not necessarily) with a new filename, mode and other
1229 1298 options."""
1230 1299 self.logger.logstop()
1231 1300
1232 1301 def magic_logoff(self,parameter_s=''):
1233 1302 """Temporarily stop logging.
1234 1303
1235 1304 You must have previously started logging."""
1236 1305 self.shell.logger.switch_log(0)
1237 1306
1238 1307 def magic_logon(self,parameter_s=''):
1239 1308 """Restart logging.
1240 1309
1241 1310 This function is for restarting logging which you've temporarily
1242 1311 stopped with %logoff. For starting logging for the first time, you
1243 1312 must use the %logstart function, which allows you to specify an
1244 1313 optional log filename."""
1245 1314
1246 1315 self.shell.logger.switch_log(1)
1247 1316
1248 1317 def magic_logstate(self,parameter_s=''):
1249 1318 """Print the status of the logging system."""
1250 1319
1251 1320 self.shell.logger.logstate()
1252 1321
1253 1322 def magic_pdb(self, parameter_s=''):
1254 1323 """Control the automatic calling of the pdb interactive debugger.
1255 1324
1256 1325 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1257 1326 argument it works as a toggle.
1258 1327
1259 1328 When an exception is triggered, IPython can optionally call the
1260 1329 interactive pdb debugger after the traceback printout. %pdb toggles
1261 1330 this feature on and off.
1262 1331
1263 1332 The initial state of this feature is set in your configuration
1264 1333 file (the option is ``InteractiveShell.pdb``).
1265 1334
1266 1335 If you want to just activate the debugger AFTER an exception has fired,
1267 1336 without having to type '%pdb on' and rerunning your code, you can use
1268 1337 the %debug magic."""
1269 1338
1270 1339 par = parameter_s.strip().lower()
1271 1340
1272 1341 if par:
1273 1342 try:
1274 1343 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1275 1344 except KeyError:
1276 1345 print ('Incorrect argument. Use on/1, off/0, '
1277 1346 'or nothing for a toggle.')
1278 1347 return
1279 1348 else:
1280 1349 # toggle
1281 1350 new_pdb = not self.shell.call_pdb
1282 1351
1283 1352 # set on the shell
1284 1353 self.shell.call_pdb = new_pdb
1285 1354 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1286 1355
1287 1356 def magic_debug(self, parameter_s=''):
1288 1357 """Activate the interactive debugger in post-mortem mode.
1289 1358
1290 1359 If an exception has just occurred, this lets you inspect its stack
1291 1360 frames interactively. Note that this will always work only on the last
1292 1361 traceback that occurred, so you must call this quickly after an
1293 1362 exception that you wish to inspect has fired, because if another one
1294 1363 occurs, it clobbers the previous one.
1295 1364
1296 1365 If you want IPython to automatically do this on every exception, see
1297 1366 the %pdb magic for more details.
1298 1367 """
1299 1368 self.shell.debugger(force=True)
1300 1369
1301 1370 @skip_doctest
1302 1371 def magic_prun(self, parameter_s ='',user_mode=1,
1303 1372 opts=None,arg_lst=None,prog_ns=None):
1304 1373
1305 1374 """Run a statement through the python code profiler.
1306 1375
1307 1376 Usage:
1308 1377 %prun [options] statement
1309 1378
1310 1379 The given statement (which doesn't require quote marks) is run via the
1311 1380 python profiler in a manner similar to the profile.run() function.
1312 1381 Namespaces are internally managed to work correctly; profile.run
1313 1382 cannot be used in IPython because it makes certain assumptions about
1314 1383 namespaces which do not hold under IPython.
1315 1384
1316 1385 Options:
1317 1386
1318 1387 -l <limit>: you can place restrictions on what or how much of the
1319 1388 profile gets printed. The limit value can be:
1320 1389
1321 1390 * A string: only information for function names containing this string
1322 1391 is printed.
1323 1392
1324 1393 * An integer: only these many lines are printed.
1325 1394
1326 1395 * A float (between 0 and 1): this fraction of the report is printed
1327 1396 (for example, use a limit of 0.4 to see the topmost 40% only).
1328 1397
1329 1398 You can combine several limits with repeated use of the option. For
1330 1399 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1331 1400 information about class constructors.
1332 1401
1333 1402 -r: return the pstats.Stats object generated by the profiling. This
1334 1403 object has all the information about the profile in it, and you can
1335 1404 later use it for further analysis or in other functions.
1336 1405
1337 1406 -s <key>: sort profile by given key. You can provide more than one key
1338 1407 by using the option several times: '-s key1 -s key2 -s key3...'. The
1339 1408 default sorting key is 'time'.
1340 1409
1341 1410 The following is copied verbatim from the profile documentation
1342 1411 referenced below:
1343 1412
1344 1413 When more than one key is provided, additional keys are used as
1345 1414 secondary criteria when the there is equality in all keys selected
1346 1415 before them.
1347 1416
1348 1417 Abbreviations can be used for any key names, as long as the
1349 1418 abbreviation is unambiguous. The following are the keys currently
1350 1419 defined:
1351 1420
1352 1421 Valid Arg Meaning
1353 1422 "calls" call count
1354 1423 "cumulative" cumulative time
1355 1424 "file" file name
1356 1425 "module" file name
1357 1426 "pcalls" primitive call count
1358 1427 "line" line number
1359 1428 "name" function name
1360 1429 "nfl" name/file/line
1361 1430 "stdname" standard name
1362 1431 "time" internal time
1363 1432
1364 1433 Note that all sorts on statistics are in descending order (placing
1365 1434 most time consuming items first), where as name, file, and line number
1366 1435 searches are in ascending order (i.e., alphabetical). The subtle
1367 1436 distinction between "nfl" and "stdname" is that the standard name is a
1368 1437 sort of the name as printed, which means that the embedded line
1369 1438 numbers get compared in an odd way. For example, lines 3, 20, and 40
1370 1439 would (if the file names were the same) appear in the string order
1371 1440 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1372 1441 line numbers. In fact, sort_stats("nfl") is the same as
1373 1442 sort_stats("name", "file", "line").
1374 1443
1375 1444 -T <filename>: save profile results as shown on screen to a text
1376 1445 file. The profile is still shown on screen.
1377 1446
1378 1447 -D <filename>: save (via dump_stats) profile statistics to given
1379 1448 filename. This data is in a format understood by the pstats module, and
1380 1449 is generated by a call to the dump_stats() method of profile
1381 1450 objects. The profile is still shown on screen.
1382 1451
1383 1452 -q: suppress output to the pager. Best used with -T and/or -D above.
1384 1453
1385 1454 If you want to run complete programs under the profiler's control, use
1386 1455 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1387 1456 contains profiler specific options as described here.
1388 1457
1389 1458 You can read the complete documentation for the profile module with::
1390 1459
1391 1460 In [1]: import profile; profile.help()
1392 1461 """
1393 1462
1394 1463 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1395 1464 # protect user quote marks
1396 1465 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1397 1466
1398 1467 if user_mode: # regular user call
1399 1468 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
1400 1469 list_all=1)
1401 1470 namespace = self.shell.user_ns
1402 1471 else: # called to run a program by %run -p
1403 1472 try:
1404 1473 filename = get_py_filename(arg_lst[0])
1405 1474 except IOError as e:
1406 1475 try:
1407 1476 msg = str(e)
1408 1477 except UnicodeError:
1409 1478 msg = e.message
1410 1479 error(msg)
1411 1480 return
1412 1481
1413 1482 arg_str = 'execfile(filename,prog_ns)'
1414 1483 namespace = {
1415 1484 'execfile': self.shell.safe_execfile,
1416 1485 'prog_ns': prog_ns,
1417 1486 'filename': filename
1418 1487 }
1419 1488
1420 1489 opts.merge(opts_def)
1421 1490
1422 1491 prof = profile.Profile()
1423 1492 try:
1424 1493 prof = prof.runctx(arg_str,namespace,namespace)
1425 1494 sys_exit = ''
1426 1495 except SystemExit:
1427 1496 sys_exit = """*** SystemExit exception caught in code being profiled."""
1428 1497
1429 1498 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1430 1499
1431 1500 lims = opts.l
1432 1501 if lims:
1433 1502 lims = [] # rebuild lims with ints/floats/strings
1434 1503 for lim in opts.l:
1435 1504 try:
1436 1505 lims.append(int(lim))
1437 1506 except ValueError:
1438 1507 try:
1439 1508 lims.append(float(lim))
1440 1509 except ValueError:
1441 1510 lims.append(lim)
1442 1511
1443 1512 # Trap output.
1444 1513 stdout_trap = StringIO()
1445 1514
1446 1515 if hasattr(stats,'stream'):
1447 1516 # In newer versions of python, the stats object has a 'stream'
1448 1517 # attribute to write into.
1449 1518 stats.stream = stdout_trap
1450 1519 stats.print_stats(*lims)
1451 1520 else:
1452 1521 # For older versions, we manually redirect stdout during printing
1453 1522 sys_stdout = sys.stdout
1454 1523 try:
1455 1524 sys.stdout = stdout_trap
1456 1525 stats.print_stats(*lims)
1457 1526 finally:
1458 1527 sys.stdout = sys_stdout
1459 1528
1460 1529 output = stdout_trap.getvalue()
1461 1530 output = output.rstrip()
1462 1531
1463 1532 if 'q' not in opts:
1464 1533 page.page(output)
1465 1534 print sys_exit,
1466 1535
1467 1536 dump_file = opts.D[0]
1468 1537 text_file = opts.T[0]
1469 1538 if dump_file:
1470 1539 dump_file = unquote_filename(dump_file)
1471 1540 prof.dump_stats(dump_file)
1472 1541 print '\n*** Profile stats marshalled to file',\
1473 1542 `dump_file`+'.',sys_exit
1474 1543 if text_file:
1475 1544 text_file = unquote_filename(text_file)
1476 1545 pfile = file(text_file,'w')
1477 1546 pfile.write(output)
1478 1547 pfile.close()
1479 1548 print '\n*** Profile printout saved to text file',\
1480 1549 `text_file`+'.',sys_exit
1481 1550
1482 1551 if opts.has_key('r'):
1483 1552 return stats
1484 1553 else:
1485 1554 return None
1486 1555
1487 1556 @skip_doctest
1488 1557 def magic_run(self, parameter_s ='', runner=None,
1489 1558 file_finder=get_py_filename):
1490 1559 """Run the named file inside IPython as a program.
1491 1560
1492 1561 Usage:\\
1493 1562 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1494 1563
1495 1564 Parameters after the filename are passed as command-line arguments to
1496 1565 the program (put in sys.argv). Then, control returns to IPython's
1497 1566 prompt.
1498 1567
1499 1568 This is similar to running at a system prompt:\\
1500 1569 $ python file args\\
1501 1570 but with the advantage of giving you IPython's tracebacks, and of
1502 1571 loading all variables into your interactive namespace for further use
1503 1572 (unless -p is used, see below).
1504 1573
1505 1574 The file is executed in a namespace initially consisting only of
1506 1575 __name__=='__main__' and sys.argv constructed as indicated. It thus
1507 1576 sees its environment as if it were being run as a stand-alone program
1508 1577 (except for sharing global objects such as previously imported
1509 1578 modules). But after execution, the IPython interactive namespace gets
1510 1579 updated with all variables defined in the program (except for __name__
1511 1580 and sys.argv). This allows for very convenient loading of code for
1512 1581 interactive work, while giving each program a 'clean sheet' to run in.
1513 1582
1514 1583 Options:
1515 1584
1516 1585 -n: __name__ is NOT set to '__main__', but to the running file's name
1517 1586 without extension (as python does under import). This allows running
1518 1587 scripts and reloading the definitions in them without calling code
1519 1588 protected by an ' if __name__ == "__main__" ' clause.
1520 1589
1521 1590 -i: run the file in IPython's namespace instead of an empty one. This
1522 1591 is useful if you are experimenting with code written in a text editor
1523 1592 which depends on variables defined interactively.
1524 1593
1525 1594 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1526 1595 being run. This is particularly useful if IPython is being used to
1527 1596 run unittests, which always exit with a sys.exit() call. In such
1528 1597 cases you are interested in the output of the test results, not in
1529 1598 seeing a traceback of the unittest module.
1530 1599
1531 1600 -t: print timing information at the end of the run. IPython will give
1532 1601 you an estimated CPU time consumption for your script, which under
1533 1602 Unix uses the resource module to avoid the wraparound problems of
1534 1603 time.clock(). Under Unix, an estimate of time spent on system tasks
1535 1604 is also given (for Windows platforms this is reported as 0.0).
1536 1605
1537 1606 If -t is given, an additional -N<N> option can be given, where <N>
1538 1607 must be an integer indicating how many times you want the script to
1539 1608 run. The final timing report will include total and per run results.
1540 1609
1541 1610 For example (testing the script uniq_stable.py):
1542 1611
1543 1612 In [1]: run -t uniq_stable
1544 1613
1545 1614 IPython CPU timings (estimated):\\
1546 1615 User : 0.19597 s.\\
1547 1616 System: 0.0 s.\\
1548 1617
1549 1618 In [2]: run -t -N5 uniq_stable
1550 1619
1551 1620 IPython CPU timings (estimated):\\
1552 1621 Total runs performed: 5\\
1553 1622 Times : Total Per run\\
1554 1623 User : 0.910862 s, 0.1821724 s.\\
1555 1624 System: 0.0 s, 0.0 s.
1556 1625
1557 1626 -d: run your program under the control of pdb, the Python debugger.
1558 1627 This allows you to execute your program step by step, watch variables,
1559 1628 etc. Internally, what IPython does is similar to calling:
1560 1629
1561 1630 pdb.run('execfile("YOURFILENAME")')
1562 1631
1563 1632 with a breakpoint set on line 1 of your file. You can change the line
1564 1633 number for this automatic breakpoint to be <N> by using the -bN option
1565 1634 (where N must be an integer). For example:
1566 1635
1567 1636 %run -d -b40 myscript
1568 1637
1569 1638 will set the first breakpoint at line 40 in myscript.py. Note that
1570 1639 the first breakpoint must be set on a line which actually does
1571 1640 something (not a comment or docstring) for it to stop execution.
1572 1641
1573 1642 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1574 1643 first enter 'c' (without quotes) to start execution up to the first
1575 1644 breakpoint.
1576 1645
1577 1646 Entering 'help' gives information about the use of the debugger. You
1578 1647 can easily see pdb's full documentation with "import pdb;pdb.help()"
1579 1648 at a prompt.
1580 1649
1581 1650 -p: run program under the control of the Python profiler module (which
1582 1651 prints a detailed report of execution times, function calls, etc).
1583 1652
1584 1653 You can pass other options after -p which affect the behavior of the
1585 1654 profiler itself. See the docs for %prun for details.
1586 1655
1587 1656 In this mode, the program's variables do NOT propagate back to the
1588 1657 IPython interactive namespace (because they remain in the namespace
1589 1658 where the profiler executes them).
1590 1659
1591 1660 Internally this triggers a call to %prun, see its documentation for
1592 1661 details on the options available specifically for profiling.
1593 1662
1594 1663 There is one special usage for which the text above doesn't apply:
1595 1664 if the filename ends with .ipy, the file is run as ipython script,
1596 1665 just as if the commands were written on IPython prompt.
1597 1666
1598 1667 -m: specify module name to load instead of script path. Similar to
1599 1668 the -m option for the python interpreter. Use this option last if you
1600 1669 want to combine with other %run options. Unlike the python interpreter
1601 1670 only source modules are allowed no .pyc or .pyo files.
1602 1671 For example:
1603 1672
1604 1673 %run -m example
1605 1674
1606 1675 will run the example module.
1607 1676
1608 1677 """
1609 1678
1610 1679 # get arguments and set sys.argv for program to be run.
1611 1680 opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:',
1612 1681 mode='list', list_all=1)
1613 1682 if "m" in opts:
1614 1683 modulename = opts["m"][0]
1615 1684 modpath = find_mod(modulename)
1616 1685 if modpath is None:
1617 1686 warn('%r is not a valid modulename on sys.path'%modulename)
1618 1687 return
1619 1688 arg_lst = [modpath] + arg_lst
1620 1689 try:
1621 1690 filename = file_finder(arg_lst[0])
1622 1691 except IndexError:
1623 1692 warn('you must provide at least a filename.')
1624 1693 print '\n%run:\n', oinspect.getdoc(self.magic_run)
1625 1694 return
1626 1695 except IOError as e:
1627 1696 try:
1628 1697 msg = str(e)
1629 1698 except UnicodeError:
1630 1699 msg = e.message
1631 1700 error(msg)
1632 1701 return
1633 1702
1634 1703 if filename.lower().endswith('.ipy'):
1635 1704 self.shell.safe_execfile_ipy(filename)
1636 1705 return
1637 1706
1638 1707 # Control the response to exit() calls made by the script being run
1639 1708 exit_ignore = 'e' in opts
1640 1709
1641 1710 # Make sure that the running script gets a proper sys.argv as if it
1642 1711 # were run from a system shell.
1643 1712 save_argv = sys.argv # save it for later restoring
1644 1713
1645 1714 # simulate shell expansion on arguments, at least tilde expansion
1646 1715 args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
1647 1716
1648 1717 sys.argv = [filename] + args # put in the proper filename
1649 1718 # protect sys.argv from potential unicode strings on Python 2:
1650 1719 if not py3compat.PY3:
1651 1720 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
1652 1721
1653 1722 if 'i' in opts:
1654 1723 # Run in user's interactive namespace
1655 1724 prog_ns = self.shell.user_ns
1656 1725 __name__save = self.shell.user_ns['__name__']
1657 1726 prog_ns['__name__'] = '__main__'
1658 1727 main_mod = self.shell.new_main_mod(prog_ns)
1659 1728 else:
1660 1729 # Run in a fresh, empty namespace
1661 1730 if 'n' in opts:
1662 1731 name = os.path.splitext(os.path.basename(filename))[0]
1663 1732 else:
1664 1733 name = '__main__'
1665 1734
1666 1735 main_mod = self.shell.new_main_mod()
1667 1736 prog_ns = main_mod.__dict__
1668 1737 prog_ns['__name__'] = name
1669 1738
1670 1739 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1671 1740 # set the __file__ global in the script's namespace
1672 1741 prog_ns['__file__'] = filename
1673 1742
1674 1743 # pickle fix. See interactiveshell for an explanation. But we need to make sure
1675 1744 # that, if we overwrite __main__, we replace it at the end
1676 1745 main_mod_name = prog_ns['__name__']
1677 1746
1678 1747 if main_mod_name == '__main__':
1679 1748 restore_main = sys.modules['__main__']
1680 1749 else:
1681 1750 restore_main = False
1682 1751
1683 1752 # This needs to be undone at the end to prevent holding references to
1684 1753 # every single object ever created.
1685 1754 sys.modules[main_mod_name] = main_mod
1686 1755
1687 1756 try:
1688 1757 stats = None
1689 1758 with self.readline_no_record:
1690 1759 if 'p' in opts:
1691 1760 stats = self.magic_prun('', 0, opts, arg_lst, prog_ns)
1692 1761 else:
1693 1762 if 'd' in opts:
1694 1763 deb = debugger.Pdb(self.shell.colors)
1695 1764 # reset Breakpoint state, which is moronically kept
1696 1765 # in a class
1697 1766 bdb.Breakpoint.next = 1
1698 1767 bdb.Breakpoint.bplist = {}
1699 1768 bdb.Breakpoint.bpbynumber = [None]
1700 1769 # Set an initial breakpoint to stop execution
1701 1770 maxtries = 10
1702 1771 bp = int(opts.get('b', [1])[0])
1703 1772 checkline = deb.checkline(filename, bp)
1704 1773 if not checkline:
1705 1774 for bp in range(bp + 1, bp + maxtries + 1):
1706 1775 if deb.checkline(filename, bp):
1707 1776 break
1708 1777 else:
1709 1778 msg = ("\nI failed to find a valid line to set "
1710 1779 "a breakpoint\n"
1711 1780 "after trying up to line: %s.\n"
1712 1781 "Please set a valid breakpoint manually "
1713 1782 "with the -b option." % bp)
1714 1783 error(msg)
1715 1784 return
1716 1785 # if we find a good linenumber, set the breakpoint
1717 1786 deb.do_break('%s:%s' % (filename, bp))
1718 1787 # Start file run
1719 1788 print "NOTE: Enter 'c' at the",
1720 1789 print "%s prompt to start your script." % deb.prompt
1721 1790 try:
1722 1791 deb.run('execfile("%s")' % filename, prog_ns)
1723 1792
1724 1793 except:
1725 1794 etype, value, tb = sys.exc_info()
1726 1795 # Skip three frames in the traceback: the %run one,
1727 1796 # one inside bdb.py, and the command-line typed by the
1728 1797 # user (run by exec in pdb itself).
1729 1798 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
1730 1799 else:
1731 1800 if runner is None:
1732 1801 runner = self.shell.safe_execfile
1733 1802 if 't' in opts:
1734 1803 # timed execution
1735 1804 try:
1736 1805 nruns = int(opts['N'][0])
1737 1806 if nruns < 1:
1738 1807 error('Number of runs must be >=1')
1739 1808 return
1740 1809 except (KeyError):
1741 1810 nruns = 1
1742 1811 twall0 = time.time()
1743 1812 if nruns == 1:
1744 1813 t0 = clock2()
1745 1814 runner(filename, prog_ns, prog_ns,
1746 1815 exit_ignore=exit_ignore)
1747 1816 t1 = clock2()
1748 1817 t_usr = t1[0] - t0[0]
1749 1818 t_sys = t1[1] - t0[1]
1750 1819 print "\nIPython CPU timings (estimated):"
1751 1820 print " User : %10.2f s." % t_usr
1752 1821 print " System : %10.2f s." % t_sys
1753 1822 else:
1754 1823 runs = range(nruns)
1755 1824 t0 = clock2()
1756 1825 for nr in runs:
1757 1826 runner(filename, prog_ns, prog_ns,
1758 1827 exit_ignore=exit_ignore)
1759 1828 t1 = clock2()
1760 1829 t_usr = t1[0] - t0[0]
1761 1830 t_sys = t1[1] - t0[1]
1762 1831 print "\nIPython CPU timings (estimated):"
1763 1832 print "Total runs performed:", nruns
1764 1833 print " Times : %10.2f %10.2f" % ('Total', 'Per run')
1765 1834 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
1766 1835 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
1767 1836 twall1 = time.time()
1768 1837 print "Wall time: %10.2f s." % (twall1 - twall0)
1769 1838
1770 1839 else:
1771 1840 # regular execution
1772 1841 runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore)
1773 1842
1774 1843 if 'i' in opts:
1775 1844 self.shell.user_ns['__name__'] = __name__save
1776 1845 else:
1777 1846 # The shell MUST hold a reference to prog_ns so after %run
1778 1847 # exits, the python deletion mechanism doesn't zero it out
1779 1848 # (leaving dangling references).
1780 1849 self.shell.cache_main_mod(prog_ns, filename)
1781 1850 # update IPython interactive namespace
1782 1851
1783 1852 # Some forms of read errors on the file may mean the
1784 1853 # __name__ key was never set; using pop we don't have to
1785 1854 # worry about a possible KeyError.
1786 1855 prog_ns.pop('__name__', None)
1787 1856
1788 1857 self.shell.user_ns.update(prog_ns)
1789 1858 finally:
1790 1859 # It's a bit of a mystery why, but __builtins__ can change from
1791 1860 # being a module to becoming a dict missing some key data after
1792 1861 # %run. As best I can see, this is NOT something IPython is doing
1793 1862 # at all, and similar problems have been reported before:
1794 1863 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1795 1864 # Since this seems to be done by the interpreter itself, the best
1796 1865 # we can do is to at least restore __builtins__ for the user on
1797 1866 # exit.
1798 1867 self.shell.user_ns['__builtins__'] = builtin_mod
1799 1868
1800 1869 # Ensure key global structures are restored
1801 1870 sys.argv = save_argv
1802 1871 if restore_main:
1803 1872 sys.modules['__main__'] = restore_main
1804 1873 else:
1805 1874 # Remove from sys.modules the reference to main_mod we'd
1806 1875 # added. Otherwise it will trap references to objects
1807 1876 # contained therein.
1808 1877 del sys.modules[main_mod_name]
1809 1878
1810 1879 return stats
1811 1880
1812 1881 @skip_doctest
1813 1882 def magic_timeit(self, parameter_s =''):
1814 1883 """Time execution of a Python statement or expression
1815 1884
1816 1885 Usage:\\
1817 1886 %timeit [-n<N> -r<R> [-t|-c]] statement
1818 1887
1819 1888 Time execution of a Python statement or expression using the timeit
1820 1889 module.
1821 1890
1822 1891 Options:
1823 1892 -n<N>: execute the given statement <N> times in a loop. If this value
1824 1893 is not given, a fitting value is chosen.
1825 1894
1826 1895 -r<R>: repeat the loop iteration <R> times and take the best result.
1827 1896 Default: 3
1828 1897
1829 1898 -t: use time.time to measure the time, which is the default on Unix.
1830 1899 This function measures wall time.
1831 1900
1832 1901 -c: use time.clock to measure the time, which is the default on
1833 1902 Windows and measures wall time. On Unix, resource.getrusage is used
1834 1903 instead and returns the CPU user time.
1835 1904
1836 1905 -p<P>: use a precision of <P> digits to display the timing result.
1837 1906 Default: 3
1838 1907
1839 1908
1840 1909 Examples:
1841 1910
1842 1911 In [1]: %timeit pass
1843 1912 10000000 loops, best of 3: 53.3 ns per loop
1844 1913
1845 1914 In [2]: u = None
1846 1915
1847 1916 In [3]: %timeit u is None
1848 1917 10000000 loops, best of 3: 184 ns per loop
1849 1918
1850 1919 In [4]: %timeit -r 4 u == None
1851 1920 1000000 loops, best of 4: 242 ns per loop
1852 1921
1853 1922 In [5]: import time
1854 1923
1855 1924 In [6]: %timeit -n1 time.sleep(2)
1856 1925 1 loops, best of 3: 2 s per loop
1857 1926
1858 1927
1859 1928 The times reported by %timeit will be slightly higher than those
1860 1929 reported by the timeit.py script when variables are accessed. This is
1861 1930 due to the fact that %timeit executes the statement in the namespace
1862 1931 of the shell, compared with timeit.py, which uses a single setup
1863 1932 statement to import function or create variables. Generally, the bias
1864 1933 does not matter as long as results from timeit.py are not mixed with
1865 1934 those from %timeit."""
1866 1935
1867 1936 import timeit
1868 1937 import math
1869 1938
1870 1939 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1871 1940 # certain terminals. Until we figure out a robust way of
1872 1941 # auto-detecting if the terminal can deal with it, use plain 'us' for
1873 1942 # microseconds. I am really NOT happy about disabling the proper
1874 1943 # 'micro' prefix, but crashing is worse... If anyone knows what the
1875 1944 # right solution for this is, I'm all ears...
1876 1945 #
1877 1946 # Note: using
1878 1947 #
1879 1948 # s = u'\xb5'
1880 1949 # s.encode(sys.getdefaultencoding())
1881 1950 #
1882 1951 # is not sufficient, as I've seen terminals where that fails but
1883 1952 # print s
1884 1953 #
1885 1954 # succeeds
1886 1955 #
1887 1956 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1888 1957
1889 1958 #units = [u"s", u"ms",u'\xb5',"ns"]
1890 1959 units = [u"s", u"ms",u'us',"ns"]
1891 1960
1892 1961 scaling = [1, 1e3, 1e6, 1e9]
1893 1962
1894 1963 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1895 1964 posix=False, strict=False)
1896 1965 if stmt == "":
1897 1966 return
1898 1967 timefunc = timeit.default_timer
1899 1968 number = int(getattr(opts, "n", 0))
1900 1969 repeat = int(getattr(opts, "r", timeit.default_repeat))
1901 1970 precision = int(getattr(opts, "p", 3))
1902 1971 if hasattr(opts, "t"):
1903 1972 timefunc = time.time
1904 1973 if hasattr(opts, "c"):
1905 1974 timefunc = clock
1906 1975
1907 1976 timer = timeit.Timer(timer=timefunc)
1908 1977 # this code has tight coupling to the inner workings of timeit.Timer,
1909 1978 # but is there a better way to achieve that the code stmt has access
1910 1979 # to the shell namespace?
1911 1980
1912 1981 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1913 1982 'setup': "pass"}
1914 1983 # Track compilation time so it can be reported if too long
1915 1984 # Minimum time above which compilation time will be reported
1916 1985 tc_min = 0.1
1917 1986
1918 1987 t0 = clock()
1919 1988 code = compile(src, "<magic-timeit>", "exec")
1920 1989 tc = clock()-t0
1921 1990
1922 1991 ns = {}
1923 1992 exec code in self.shell.user_ns, ns
1924 1993 timer.inner = ns["inner"]
1925 1994
1926 1995 if number == 0:
1927 1996 # determine number so that 0.2 <= total time < 2.0
1928 1997 number = 1
1929 1998 for i in range(1, 10):
1930 1999 if timer.timeit(number) >= 0.2:
1931 2000 break
1932 2001 number *= 10
1933 2002
1934 2003 best = min(timer.repeat(repeat, number)) / number
1935 2004
1936 2005 if best > 0.0 and best < 1000.0:
1937 2006 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1938 2007 elif best >= 1000.0:
1939 2008 order = 0
1940 2009 else:
1941 2010 order = 3
1942 2011 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1943 2012 precision,
1944 2013 best * scaling[order],
1945 2014 units[order])
1946 2015 if tc > tc_min:
1947 2016 print "Compiler time: %.2f s" % tc
1948 2017
1949 2018 @skip_doctest
1950 2019 @needs_local_scope
1951 2020 def magic_time(self,parameter_s = ''):
1952 2021 """Time execution of a Python statement or expression.
1953 2022
1954 2023 The CPU and wall clock times are printed, and the value of the
1955 2024 expression (if any) is returned. Note that under Win32, system time
1956 2025 is always reported as 0, since it can not be measured.
1957 2026
1958 2027 This function provides very basic timing functionality. In Python
1959 2028 2.3, the timeit module offers more control and sophistication, so this
1960 2029 could be rewritten to use it (patches welcome).
1961 2030
1962 2031 Some examples:
1963 2032
1964 2033 In [1]: time 2**128
1965 2034 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1966 2035 Wall time: 0.00
1967 2036 Out[1]: 340282366920938463463374607431768211456L
1968 2037
1969 2038 In [2]: n = 1000000
1970 2039
1971 2040 In [3]: time sum(range(n))
1972 2041 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1973 2042 Wall time: 1.37
1974 2043 Out[3]: 499999500000L
1975 2044
1976 2045 In [4]: time print 'hello world'
1977 2046 hello world
1978 2047 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1979 2048 Wall time: 0.00
1980 2049
1981 2050 Note that the time needed by Python to compile the given expression
1982 2051 will be reported if it is more than 0.1s. In this example, the
1983 2052 actual exponentiation is done by Python at compilation time, so while
1984 2053 the expression can take a noticeable amount of time to compute, that
1985 2054 time is purely due to the compilation:
1986 2055
1987 2056 In [5]: time 3**9999;
1988 2057 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1989 2058 Wall time: 0.00 s
1990 2059
1991 2060 In [6]: time 3**999999;
1992 2061 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1993 2062 Wall time: 0.00 s
1994 2063 Compiler : 0.78 s
1995 2064 """
1996 2065
1997 2066 # fail immediately if the given expression can't be compiled
1998 2067
1999 2068 expr = self.shell.prefilter(parameter_s,False)
2000 2069
2001 2070 # Minimum time above which compilation time will be reported
2002 2071 tc_min = 0.1
2003 2072
2004 2073 try:
2005 2074 mode = 'eval'
2006 2075 t0 = clock()
2007 2076 code = compile(expr,'<timed eval>',mode)
2008 2077 tc = clock()-t0
2009 2078 except SyntaxError:
2010 2079 mode = 'exec'
2011 2080 t0 = clock()
2012 2081 code = compile(expr,'<timed exec>',mode)
2013 2082 tc = clock()-t0
2014 2083 # skew measurement as little as possible
2015 2084 glob = self.shell.user_ns
2016 2085 locs = self._magic_locals
2017 2086 clk = clock2
2018 2087 wtime = time.time
2019 2088 # time execution
2020 2089 wall_st = wtime()
2021 2090 if mode=='eval':
2022 2091 st = clk()
2023 2092 out = eval(code, glob, locs)
2024 2093 end = clk()
2025 2094 else:
2026 2095 st = clk()
2027 2096 exec code in glob, locs
2028 2097 end = clk()
2029 2098 out = None
2030 2099 wall_end = wtime()
2031 2100 # Compute actual times and report
2032 2101 wall_time = wall_end-wall_st
2033 2102 cpu_user = end[0]-st[0]
2034 2103 cpu_sys = end[1]-st[1]
2035 2104 cpu_tot = cpu_user+cpu_sys
2036 2105 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
2037 2106 (cpu_user,cpu_sys,cpu_tot)
2038 2107 print "Wall time: %.2f s" % wall_time
2039 2108 if tc > tc_min:
2040 2109 print "Compiler : %.2f s" % tc
2041 2110 return out
2042 2111
2043 2112 @skip_doctest
2044 2113 def magic_macro(self,parameter_s = ''):
2045 2114 """Define a macro for future re-execution. It accepts ranges of history,
2046 2115 filenames or string objects.
2047 2116
2048 2117 Usage:\\
2049 2118 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
2050 2119
2051 2120 Options:
2052 2121
2053 2122 -r: use 'raw' input. By default, the 'processed' history is used,
2054 2123 so that magics are loaded in their transformed version to valid
2055 2124 Python. If this option is given, the raw input as typed as the
2056 2125 command line is used instead.
2057 2126
2058 2127 This will define a global variable called `name` which is a string
2059 2128 made of joining the slices and lines you specify (n1,n2,... numbers
2060 2129 above) from your input history into a single string. This variable
2061 2130 acts like an automatic function which re-executes those lines as if
2062 2131 you had typed them. You just type 'name' at the prompt and the code
2063 2132 executes.
2064 2133
2065 2134 The syntax for indicating input ranges is described in %history.
2066 2135
2067 2136 Note: as a 'hidden' feature, you can also use traditional python slice
2068 2137 notation, where N:M means numbers N through M-1.
2069 2138
2070 2139 For example, if your history contains (%hist prints it):
2071 2140
2072 2141 44: x=1
2073 2142 45: y=3
2074 2143 46: z=x+y
2075 2144 47: print x
2076 2145 48: a=5
2077 2146 49: print 'x',x,'y',y
2078 2147
2079 2148 you can create a macro with lines 44 through 47 (included) and line 49
2080 2149 called my_macro with:
2081 2150
2082 2151 In [55]: %macro my_macro 44-47 49
2083 2152
2084 2153 Now, typing `my_macro` (without quotes) will re-execute all this code
2085 2154 in one pass.
2086 2155
2087 2156 You don't need to give the line-numbers in order, and any given line
2088 2157 number can appear multiple times. You can assemble macros with any
2089 2158 lines from your input history in any order.
2090 2159
2091 2160 The macro is a simple object which holds its value in an attribute,
2092 2161 but IPython's display system checks for macros and executes them as
2093 2162 code instead of printing them when you type their name.
2094 2163
2095 2164 You can view a macro's contents by explicitly printing it with:
2096 2165
2097 2166 'print macro_name'.
2098 2167
2099 2168 """
2100 2169 opts,args = self.parse_options(parameter_s,'r',mode='list')
2101 2170 if not args: # List existing macros
2102 2171 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
2103 2172 isinstance(v, Macro))
2104 2173 if len(args) == 1:
2105 2174 raise UsageError(
2106 2175 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2107 2176 name, codefrom = args[0], " ".join(args[1:])
2108 2177
2109 2178 #print 'rng',ranges # dbg
2110 2179 try:
2111 2180 lines = self.shell.find_user_code(codefrom, 'r' in opts)
2112 2181 except (ValueError, TypeError) as e:
2113 2182 print e.args[0]
2114 2183 return
2115 2184 macro = Macro(lines)
2116 2185 self.shell.define_macro(name, macro)
2117 2186 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2118 2187 print '=== Macro contents: ==='
2119 2188 print macro,
2120 2189
2121 2190 def magic_save(self,parameter_s = ''):
2122 2191 """Save a set of lines or a macro to a given filename.
2123 2192
2124 2193 Usage:\\
2125 2194 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2126 2195
2127 2196 Options:
2128 2197
2129 2198 -r: use 'raw' input. By default, the 'processed' history is used,
2130 2199 so that magics are loaded in their transformed version to valid
2131 2200 Python. If this option is given, the raw input as typed as the
2132 2201 command line is used instead.
2133 2202
2134 2203 This function uses the same syntax as %history for input ranges,
2135 2204 then saves the lines to the filename you specify.
2136 2205
2137 2206 It adds a '.py' extension to the file if you don't do so yourself, and
2138 2207 it asks for confirmation before overwriting existing files."""
2139 2208
2140 2209 opts,args = self.parse_options(parameter_s,'r',mode='list')
2141 2210 fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
2142 2211 if not fname.endswith('.py'):
2143 2212 fname += '.py'
2144 2213 if os.path.isfile(fname):
2145 2214 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2146 2215 if ans.lower() not in ['y','yes']:
2147 2216 print 'Operation cancelled.'
2148 2217 return
2149 2218 try:
2150 2219 cmds = self.shell.find_user_code(codefrom, 'r' in opts)
2151 2220 except (TypeError, ValueError) as e:
2152 2221 print e.args[0]
2153 2222 return
2154 2223 with py3compat.open(fname,'w', encoding="utf-8") as f:
2155 2224 f.write(u"# coding: utf-8\n")
2156 2225 f.write(py3compat.cast_unicode(cmds))
2157 2226 print 'The following commands were written to file `%s`:' % fname
2158 2227 print cmds
2159 2228
2160 2229 def magic_pastebin(self, parameter_s = ''):
2161 2230 """Upload code to the 'Lodge it' paste bin, returning the URL."""
2162 2231 try:
2163 2232 code = self.shell.find_user_code(parameter_s)
2164 2233 except (ValueError, TypeError) as e:
2165 2234 print e.args[0]
2166 2235 return
2167 2236 pbserver = ServerProxy('http://paste.pocoo.org/xmlrpc/')
2168 2237 id = pbserver.pastes.newPaste("python", code)
2169 2238 return "http://paste.pocoo.org/show/" + id
2170 2239
2171 2240 def magic_loadpy(self, arg_s):
2172 2241 """Load a .py python script into the GUI console.
2173 2242
2174 2243 This magic command can either take a local filename or a url::
2175 2244
2176 2245 %loadpy myscript.py
2177 2246 %loadpy http://www.example.com/myscript.py
2178 2247 """
2179 2248 arg_s = unquote_filename(arg_s)
2180 2249 remote_url = arg_s.startswith(('http://', 'https://'))
2181 2250 local_url = not remote_url
2182 2251 if local_url and not arg_s.endswith('.py'):
2183 2252 # Local files must be .py; for remote URLs it's possible that the
2184 2253 # fetch URL doesn't have a .py in it (many servers have an opaque
2185 2254 # URL, such as scipy-central.org).
2186 2255 raise ValueError('%%load only works with .py files: %s' % arg_s)
2187 2256 if remote_url:
2188 2257 import urllib2
2189 2258 fileobj = urllib2.urlopen(arg_s)
2190 2259 # While responses have a .info().getencoding() way of asking for
2191 2260 # their encoding, in *many* cases the return value is bogus. In
2192 2261 # the wild, servers serving utf-8 but declaring latin-1 are
2193 2262 # extremely common, as the old HTTP standards specify latin-1 as
2194 2263 # the default but many modern filesystems use utf-8. So we can NOT
2195 2264 # rely on the headers. Short of building complex encoding-guessing
2196 2265 # logic, going with utf-8 is a simple solution likely to be right
2197 2266 # in most real-world cases.
2198 2267 linesource = fileobj.read().decode('utf-8', 'replace').splitlines()
2199 2268 fileobj.close()
2200 2269 else:
2201 2270 with open(arg_s) as fileobj:
2202 2271 linesource = fileobj.read().splitlines()
2203 2272
2204 2273 # Strip out encoding declarations
2205 2274 lines = [l for l in linesource if not _encoding_declaration_re.match(l)]
2206 2275
2207 2276 self.set_next_input(os.linesep.join(lines))
2208 2277
2209 2278 def _find_edit_target(self, args, opts, last_call):
2210 2279 """Utility method used by magic_edit to find what to edit."""
2211 2280
2212 2281 def make_filename(arg):
2213 2282 "Make a filename from the given args"
2214 2283 arg = unquote_filename(arg)
2215 2284 try:
2216 2285 filename = get_py_filename(arg)
2217 2286 except IOError:
2218 2287 # If it ends with .py but doesn't already exist, assume we want
2219 2288 # a new file.
2220 2289 if arg.endswith('.py'):
2221 2290 filename = arg
2222 2291 else:
2223 2292 filename = None
2224 2293 return filename
2225 2294
2226 2295 # Set a few locals from the options for convenience:
2227 2296 opts_prev = 'p' in opts
2228 2297 opts_raw = 'r' in opts
2229 2298
2230 2299 # custom exceptions
2231 2300 class DataIsObject(Exception): pass
2232 2301
2233 2302 # Default line number value
2234 2303 lineno = opts.get('n',None)
2235 2304
2236 2305 if opts_prev:
2237 2306 args = '_%s' % last_call[0]
2238 2307 if not self.shell.user_ns.has_key(args):
2239 2308 args = last_call[1]
2240 2309
2241 2310 # use last_call to remember the state of the previous call, but don't
2242 2311 # let it be clobbered by successive '-p' calls.
2243 2312 try:
2244 2313 last_call[0] = self.shell.displayhook.prompt_count
2245 2314 if not opts_prev:
2246 2315 last_call[1] = parameter_s
2247 2316 except:
2248 2317 pass
2249 2318
2250 2319 # by default this is done with temp files, except when the given
2251 2320 # arg is a filename
2252 2321 use_temp = True
2253 2322
2254 2323 data = ''
2255 2324
2256 2325 # First, see if the arguments should be a filename.
2257 2326 filename = make_filename(args)
2258 2327 if filename:
2259 2328 use_temp = False
2260 2329 elif args:
2261 2330 # Mode where user specifies ranges of lines, like in %macro.
2262 2331 data = self.extract_input_lines(args, opts_raw)
2263 2332 if not data:
2264 2333 try:
2265 2334 # Load the parameter given as a variable. If not a string,
2266 2335 # process it as an object instead (below)
2267 2336
2268 2337 #print '*** args',args,'type',type(args) # dbg
2269 2338 data = eval(args, self.shell.user_ns)
2270 2339 if not isinstance(data, basestring):
2271 2340 raise DataIsObject
2272 2341
2273 2342 except (NameError,SyntaxError):
2274 2343 # given argument is not a variable, try as a filename
2275 2344 filename = make_filename(args)
2276 2345 if filename is None:
2277 2346 warn("Argument given (%s) can't be found as a variable "
2278 2347 "or as a filename." % args)
2279 2348 return
2280 2349 use_temp = False
2281 2350
2282 2351 except DataIsObject:
2283 2352 # macros have a special edit function
2284 2353 if isinstance(data, Macro):
2285 2354 raise MacroToEdit(data)
2286 2355
2287 2356 # For objects, try to edit the file where they are defined
2288 2357 try:
2289 2358 filename = inspect.getabsfile(data)
2290 2359 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2291 2360 # class created by %edit? Try to find source
2292 2361 # by looking for method definitions instead, the
2293 2362 # __module__ in those classes is FakeModule.
2294 2363 attrs = [getattr(data, aname) for aname in dir(data)]
2295 2364 for attr in attrs:
2296 2365 if not inspect.ismethod(attr):
2297 2366 continue
2298 2367 filename = inspect.getabsfile(attr)
2299 2368 if filename and 'fakemodule' not in filename.lower():
2300 2369 # change the attribute to be the edit target instead
2301 2370 data = attr
2302 2371 break
2303 2372
2304 2373 datafile = 1
2305 2374 except TypeError:
2306 2375 filename = make_filename(args)
2307 2376 datafile = 1
2308 2377 warn('Could not find file where `%s` is defined.\n'
2309 2378 'Opening a file named `%s`' % (args,filename))
2310 2379 # Now, make sure we can actually read the source (if it was in
2311 2380 # a temp file it's gone by now).
2312 2381 if datafile:
2313 2382 try:
2314 2383 if lineno is None:
2315 2384 lineno = inspect.getsourcelines(data)[1]
2316 2385 except IOError:
2317 2386 filename = make_filename(args)
2318 2387 if filename is None:
2319 2388 warn('The file `%s` where `%s` was defined cannot '
2320 2389 'be read.' % (filename,data))
2321 2390 return
2322 2391 use_temp = False
2323 2392
2324 2393 if use_temp:
2325 2394 filename = self.shell.mktempfile(data)
2326 2395 print 'IPython will make a temporary file named:',filename
2327 2396
2328 2397 return filename, lineno, use_temp
2329 2398
2330 2399 def _edit_macro(self,mname,macro):
2331 2400 """open an editor with the macro data in a file"""
2332 2401 filename = self.shell.mktempfile(macro.value)
2333 2402 self.shell.hooks.editor(filename)
2334 2403
2335 2404 # and make a new macro object, to replace the old one
2336 2405 mfile = open(filename)
2337 2406 mvalue = mfile.read()
2338 2407 mfile.close()
2339 2408 self.shell.user_ns[mname] = Macro(mvalue)
2340 2409
2341 2410 def magic_ed(self,parameter_s=''):
2342 2411 """Alias to %edit."""
2343 2412 return self.magic_edit(parameter_s)
2344 2413
2345 2414 @skip_doctest
2346 2415 def magic_edit(self,parameter_s='',last_call=['','']):
2347 2416 """Bring up an editor and execute the resulting code.
2348 2417
2349 2418 Usage:
2350 2419 %edit [options] [args]
2351 2420
2352 2421 %edit runs IPython's editor hook. The default version of this hook is
2353 2422 set to call the editor specified by your $EDITOR environment variable.
2354 2423 If this isn't found, it will default to vi under Linux/Unix and to
2355 2424 notepad under Windows. See the end of this docstring for how to change
2356 2425 the editor hook.
2357 2426
2358 2427 You can also set the value of this editor via the
2359 2428 ``TerminalInteractiveShell.editor`` option in your configuration file.
2360 2429 This is useful if you wish to use a different editor from your typical
2361 2430 default with IPython (and for Windows users who typically don't set
2362 2431 environment variables).
2363 2432
2364 2433 This command allows you to conveniently edit multi-line code right in
2365 2434 your IPython session.
2366 2435
2367 2436 If called without arguments, %edit opens up an empty editor with a
2368 2437 temporary file and will execute the contents of this file when you
2369 2438 close it (don't forget to save it!).
2370 2439
2371 2440
2372 2441 Options:
2373 2442
2374 2443 -n <number>: open the editor at a specified line number. By default,
2375 2444 the IPython editor hook uses the unix syntax 'editor +N filename', but
2376 2445 you can configure this by providing your own modified hook if your
2377 2446 favorite editor supports line-number specifications with a different
2378 2447 syntax.
2379 2448
2380 2449 -p: this will call the editor with the same data as the previous time
2381 2450 it was used, regardless of how long ago (in your current session) it
2382 2451 was.
2383 2452
2384 2453 -r: use 'raw' input. This option only applies to input taken from the
2385 2454 user's history. By default, the 'processed' history is used, so that
2386 2455 magics are loaded in their transformed version to valid Python. If
2387 2456 this option is given, the raw input as typed as the command line is
2388 2457 used instead. When you exit the editor, it will be executed by
2389 2458 IPython's own processor.
2390 2459
2391 2460 -x: do not execute the edited code immediately upon exit. This is
2392 2461 mainly useful if you are editing programs which need to be called with
2393 2462 command line arguments, which you can then do using %run.
2394 2463
2395 2464
2396 2465 Arguments:
2397 2466
2398 2467 If arguments are given, the following possibilities exist:
2399 2468
2400 2469 - If the argument is a filename, IPython will load that into the
2401 2470 editor. It will execute its contents with execfile() when you exit,
2402 2471 loading any code in the file into your interactive namespace.
2403 2472
2404 2473 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
2405 2474 The syntax is the same as in the %history magic.
2406 2475
2407 2476 - If the argument is a string variable, its contents are loaded
2408 2477 into the editor. You can thus edit any string which contains
2409 2478 python code (including the result of previous edits).
2410 2479
2411 2480 - If the argument is the name of an object (other than a string),
2412 2481 IPython will try to locate the file where it was defined and open the
2413 2482 editor at the point where it is defined. You can use `%edit function`
2414 2483 to load an editor exactly at the point where 'function' is defined,
2415 2484 edit it and have the file be executed automatically.
2416 2485
2417 2486 - If the object is a macro (see %macro for details), this opens up your
2418 2487 specified editor with a temporary file containing the macro's data.
2419 2488 Upon exit, the macro is reloaded with the contents of the file.
2420 2489
2421 2490 Note: opening at an exact line is only supported under Unix, and some
2422 2491 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2423 2492 '+NUMBER' parameter necessary for this feature. Good editors like
2424 2493 (X)Emacs, vi, jed, pico and joe all do.
2425 2494
2426 2495 After executing your code, %edit will return as output the code you
2427 2496 typed in the editor (except when it was an existing file). This way
2428 2497 you can reload the code in further invocations of %edit as a variable,
2429 2498 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2430 2499 the output.
2431 2500
2432 2501 Note that %edit is also available through the alias %ed.
2433 2502
2434 2503 This is an example of creating a simple function inside the editor and
2435 2504 then modifying it. First, start up the editor:
2436 2505
2437 2506 In [1]: ed
2438 2507 Editing... done. Executing edited code...
2439 2508 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2440 2509
2441 2510 We can then call the function foo():
2442 2511
2443 2512 In [2]: foo()
2444 2513 foo() was defined in an editing session
2445 2514
2446 2515 Now we edit foo. IPython automatically loads the editor with the
2447 2516 (temporary) file where foo() was previously defined:
2448 2517
2449 2518 In [3]: ed foo
2450 2519 Editing... done. Executing edited code...
2451 2520
2452 2521 And if we call foo() again we get the modified version:
2453 2522
2454 2523 In [4]: foo()
2455 2524 foo() has now been changed!
2456 2525
2457 2526 Here is an example of how to edit a code snippet successive
2458 2527 times. First we call the editor:
2459 2528
2460 2529 In [5]: ed
2461 2530 Editing... done. Executing edited code...
2462 2531 hello
2463 2532 Out[5]: "print 'hello'n"
2464 2533
2465 2534 Now we call it again with the previous output (stored in _):
2466 2535
2467 2536 In [6]: ed _
2468 2537 Editing... done. Executing edited code...
2469 2538 hello world
2470 2539 Out[6]: "print 'hello world'n"
2471 2540
2472 2541 Now we call it with the output #8 (stored in _8, also as Out[8]):
2473 2542
2474 2543 In [7]: ed _8
2475 2544 Editing... done. Executing edited code...
2476 2545 hello again
2477 2546 Out[7]: "print 'hello again'n"
2478 2547
2479 2548
2480 2549 Changing the default editor hook:
2481 2550
2482 2551 If you wish to write your own editor hook, you can put it in a
2483 2552 configuration file which you load at startup time. The default hook
2484 2553 is defined in the IPython.core.hooks module, and you can use that as a
2485 2554 starting example for further modifications. That file also has
2486 2555 general instructions on how to set a new hook for use once you've
2487 2556 defined it."""
2488 2557 opts,args = self.parse_options(parameter_s,'prxn:')
2489 2558
2490 2559 try:
2491 2560 filename, lineno, is_temp = self._find_edit_target(args, opts, last_call)
2492 2561 except MacroToEdit as e:
2493 2562 self._edit_macro(args, e.args[0])
2494 2563 return
2495 2564
2496 2565 # do actual editing here
2497 2566 print 'Editing...',
2498 2567 sys.stdout.flush()
2499 2568 try:
2500 2569 # Quote filenames that may have spaces in them
2501 2570 if ' ' in filename:
2502 2571 filename = "'%s'" % filename
2503 2572 self.shell.hooks.editor(filename,lineno)
2504 2573 except TryNext:
2505 2574 warn('Could not open editor')
2506 2575 return
2507 2576
2508 2577 # XXX TODO: should this be generalized for all string vars?
2509 2578 # For now, this is special-cased to blocks created by cpaste
2510 2579 if args.strip() == 'pasted_block':
2511 2580 self.shell.user_ns['pasted_block'] = file_read(filename)
2512 2581
2513 2582 if 'x' in opts: # -x prevents actual execution
2514 2583 print
2515 2584 else:
2516 2585 print 'done. Executing edited code...'
2517 2586 if 'r' in opts: # Untranslated IPython code
2518 2587 self.shell.run_cell(file_read(filename),
2519 2588 store_history=False)
2520 2589 else:
2521 2590 self.shell.safe_execfile(filename,self.shell.user_ns,
2522 2591 self.shell.user_ns)
2523 2592
2524 2593 if is_temp:
2525 2594 try:
2526 2595 return open(filename).read()
2527 2596 except IOError,msg:
2528 2597 if msg.filename == filename:
2529 2598 warn('File not found. Did you forget to save?')
2530 2599 return
2531 2600 else:
2532 2601 self.shell.showtraceback()
2533 2602
2534 2603 def magic_xmode(self,parameter_s = ''):
2535 2604 """Switch modes for the exception handlers.
2536 2605
2537 2606 Valid modes: Plain, Context and Verbose.
2538 2607
2539 2608 If called without arguments, acts as a toggle."""
2540 2609
2541 2610 def xmode_switch_err(name):
2542 2611 warn('Error changing %s exception modes.\n%s' %
2543 2612 (name,sys.exc_info()[1]))
2544 2613
2545 2614 shell = self.shell
2546 2615 new_mode = parameter_s.strip().capitalize()
2547 2616 try:
2548 2617 shell.InteractiveTB.set_mode(mode=new_mode)
2549 2618 print 'Exception reporting mode:',shell.InteractiveTB.mode
2550 2619 except:
2551 2620 xmode_switch_err('user')
2552 2621
2553 2622 def magic_colors(self,parameter_s = ''):
2554 2623 """Switch color scheme for prompts, info system and exception handlers.
2555 2624
2556 2625 Currently implemented schemes: NoColor, Linux, LightBG.
2557 2626
2558 2627 Color scheme names are not case-sensitive.
2559 2628
2560 2629 Examples
2561 2630 --------
2562 2631 To get a plain black and white terminal::
2563 2632
2564 2633 %colors nocolor
2565 2634 """
2566 2635
2567 2636 def color_switch_err(name):
2568 2637 warn('Error changing %s color schemes.\n%s' %
2569 2638 (name,sys.exc_info()[1]))
2570 2639
2571 2640
2572 2641 new_scheme = parameter_s.strip()
2573 2642 if not new_scheme:
2574 2643 raise UsageError(
2575 2644 "%colors: you must specify a color scheme. See '%colors?'")
2576 2645 return
2577 2646 # local shortcut
2578 2647 shell = self.shell
2579 2648
2580 2649 import IPython.utils.rlineimpl as readline
2581 2650
2582 2651 if not shell.colors_force and \
2583 2652 not readline.have_readline and sys.platform == "win32":
2584 2653 msg = """\
2585 2654 Proper color support under MS Windows requires the pyreadline library.
2586 2655 You can find it at:
2587 2656 http://ipython.org/pyreadline.html
2588 2657 Gary's readline needs the ctypes module, from:
2589 2658 http://starship.python.net/crew/theller/ctypes
2590 2659 (Note that ctypes is already part of Python versions 2.5 and newer).
2591 2660
2592 2661 Defaulting color scheme to 'NoColor'"""
2593 2662 new_scheme = 'NoColor'
2594 2663 warn(msg)
2595 2664
2596 2665 # readline option is 0
2597 2666 if not shell.colors_force and not shell.has_readline:
2598 2667 new_scheme = 'NoColor'
2599 2668
2600 2669 # Set prompt colors
2601 2670 try:
2602 2671 shell.prompt_manager.color_scheme = new_scheme
2603 2672 except:
2604 2673 color_switch_err('prompt')
2605 2674 else:
2606 2675 shell.colors = \
2607 2676 shell.prompt_manager.color_scheme_table.active_scheme_name
2608 2677 # Set exception colors
2609 2678 try:
2610 2679 shell.InteractiveTB.set_colors(scheme = new_scheme)
2611 2680 shell.SyntaxTB.set_colors(scheme = new_scheme)
2612 2681 except:
2613 2682 color_switch_err('exception')
2614 2683
2615 2684 # Set info (for 'object?') colors
2616 2685 if shell.color_info:
2617 2686 try:
2618 2687 shell.inspector.set_active_scheme(new_scheme)
2619 2688 except:
2620 2689 color_switch_err('object inspector')
2621 2690 else:
2622 2691 shell.inspector.set_active_scheme('NoColor')
2623 2692
2624 2693 def magic_pprint(self, parameter_s=''):
2625 2694 """Toggle pretty printing on/off."""
2626 2695 ptformatter = self.shell.display_formatter.formatters['text/plain']
2627 2696 ptformatter.pprint = bool(1 - ptformatter.pprint)
2628 2697 print 'Pretty printing has been turned', \
2629 2698 ['OFF','ON'][ptformatter.pprint]
2630 2699
2631 2700 #......................................................................
2632 2701 # Functions to implement unix shell-type things
2633 2702
2634 2703 @skip_doctest
2635 2704 def magic_alias(self, parameter_s = ''):
2636 2705 """Define an alias for a system command.
2637 2706
2638 2707 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2639 2708
2640 2709 Then, typing 'alias_name params' will execute the system command 'cmd
2641 2710 params' (from your underlying operating system).
2642 2711
2643 2712 Aliases have lower precedence than magic functions and Python normal
2644 2713 variables, so if 'foo' is both a Python variable and an alias, the
2645 2714 alias can not be executed until 'del foo' removes the Python variable.
2646 2715
2647 2716 You can use the %l specifier in an alias definition to represent the
2648 2717 whole line when the alias is called. For example:
2649 2718
2650 2719 In [2]: alias bracket echo "Input in brackets: <%l>"
2651 2720 In [3]: bracket hello world
2652 2721 Input in brackets: <hello world>
2653 2722
2654 2723 You can also define aliases with parameters using %s specifiers (one
2655 2724 per parameter):
2656 2725
2657 2726 In [1]: alias parts echo first %s second %s
2658 2727 In [2]: %parts A B
2659 2728 first A second B
2660 2729 In [3]: %parts A
2661 2730 Incorrect number of arguments: 2 expected.
2662 2731 parts is an alias to: 'echo first %s second %s'
2663 2732
2664 2733 Note that %l and %s are mutually exclusive. You can only use one or
2665 2734 the other in your aliases.
2666 2735
2667 2736 Aliases expand Python variables just like system calls using ! or !!
2668 2737 do: all expressions prefixed with '$' get expanded. For details of
2669 2738 the semantic rules, see PEP-215:
2670 2739 http://www.python.org/peps/pep-0215.html. This is the library used by
2671 2740 IPython for variable expansion. If you want to access a true shell
2672 2741 variable, an extra $ is necessary to prevent its expansion by IPython:
2673 2742
2674 2743 In [6]: alias show echo
2675 2744 In [7]: PATH='A Python string'
2676 2745 In [8]: show $PATH
2677 2746 A Python string
2678 2747 In [9]: show $$PATH
2679 2748 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2680 2749
2681 2750 You can use the alias facility to acess all of $PATH. See the %rehash
2682 2751 and %rehashx functions, which automatically create aliases for the
2683 2752 contents of your $PATH.
2684 2753
2685 2754 If called with no parameters, %alias prints the current alias table."""
2686 2755
2687 2756 par = parameter_s.strip()
2688 2757 if not par:
2689 2758 stored = self.db.get('stored_aliases', {} )
2690 2759 aliases = sorted(self.shell.alias_manager.aliases)
2691 2760 # for k, v in stored:
2692 2761 # atab.append(k, v[0])
2693 2762
2694 2763 print "Total number of aliases:", len(aliases)
2695 2764 sys.stdout.flush()
2696 2765 return aliases
2697 2766
2698 2767 # Now try to define a new one
2699 2768 try:
2700 2769 alias,cmd = par.split(None, 1)
2701 2770 except:
2702 2771 print oinspect.getdoc(self.magic_alias)
2703 2772 else:
2704 2773 self.shell.alias_manager.soft_define_alias(alias, cmd)
2705 2774 # end magic_alias
2706 2775
2707 2776 def magic_unalias(self, parameter_s = ''):
2708 2777 """Remove an alias"""
2709 2778
2710 2779 aname = parameter_s.strip()
2711 2780 self.shell.alias_manager.undefine_alias(aname)
2712 2781 stored = self.db.get('stored_aliases', {} )
2713 2782 if aname in stored:
2714 2783 print "Removing %stored alias",aname
2715 2784 del stored[aname]
2716 2785 self.db['stored_aliases'] = stored
2717 2786
2718 2787 def magic_rehashx(self, parameter_s = ''):
2719 2788 """Update the alias table with all executable files in $PATH.
2720 2789
2721 2790 This version explicitly checks that every entry in $PATH is a file
2722 2791 with execute access (os.X_OK), so it is much slower than %rehash.
2723 2792
2724 2793 Under Windows, it checks executability as a match against a
2725 2794 '|'-separated string of extensions, stored in the IPython config
2726 2795 variable win_exec_ext. This defaults to 'exe|com|bat'.
2727 2796
2728 2797 This function also resets the root module cache of module completer,
2729 2798 used on slow filesystems.
2730 2799 """
2731 2800 from IPython.core.alias import InvalidAliasError
2732 2801
2733 2802 # for the benefit of module completer in ipy_completers.py
2734 2803 del self.shell.db['rootmodules']
2735 2804
2736 2805 path = [os.path.abspath(os.path.expanduser(p)) for p in
2737 2806 os.environ.get('PATH','').split(os.pathsep)]
2738 2807 path = filter(os.path.isdir,path)
2739 2808
2740 2809 syscmdlist = []
2741 2810 # Now define isexec in a cross platform manner.
2742 2811 if os.name == 'posix':
2743 2812 isexec = lambda fname:os.path.isfile(fname) and \
2744 2813 os.access(fname,os.X_OK)
2745 2814 else:
2746 2815 try:
2747 2816 winext = os.environ['pathext'].replace(';','|').replace('.','')
2748 2817 except KeyError:
2749 2818 winext = 'exe|com|bat|py'
2750 2819 if 'py' not in winext:
2751 2820 winext += '|py'
2752 2821 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2753 2822 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2754 2823 savedir = os.getcwdu()
2755 2824
2756 2825 # Now walk the paths looking for executables to alias.
2757 2826 try:
2758 2827 # write the whole loop for posix/Windows so we don't have an if in
2759 2828 # the innermost part
2760 2829 if os.name == 'posix':
2761 2830 for pdir in path:
2762 2831 os.chdir(pdir)
2763 2832 for ff in os.listdir(pdir):
2764 2833 if isexec(ff):
2765 2834 try:
2766 2835 # Removes dots from the name since ipython
2767 2836 # will assume names with dots to be python.
2768 2837 self.shell.alias_manager.define_alias(
2769 2838 ff.replace('.',''), ff)
2770 2839 except InvalidAliasError:
2771 2840 pass
2772 2841 else:
2773 2842 syscmdlist.append(ff)
2774 2843 else:
2775 2844 no_alias = self.shell.alias_manager.no_alias
2776 2845 for pdir in path:
2777 2846 os.chdir(pdir)
2778 2847 for ff in os.listdir(pdir):
2779 2848 base, ext = os.path.splitext(ff)
2780 2849 if isexec(ff) and base.lower() not in no_alias:
2781 2850 if ext.lower() == '.exe':
2782 2851 ff = base
2783 2852 try:
2784 2853 # Removes dots from the name since ipython
2785 2854 # will assume names with dots to be python.
2786 2855 self.shell.alias_manager.define_alias(
2787 2856 base.lower().replace('.',''), ff)
2788 2857 except InvalidAliasError:
2789 2858 pass
2790 2859 syscmdlist.append(ff)
2791 2860 self.shell.db['syscmdlist'] = syscmdlist
2792 2861 finally:
2793 2862 os.chdir(savedir)
2794 2863
2795 2864 @skip_doctest
2796 2865 def magic_pwd(self, parameter_s = ''):
2797 2866 """Return the current working directory path.
2798 2867
2799 2868 Examples
2800 2869 --------
2801 2870 ::
2802 2871
2803 2872 In [9]: pwd
2804 2873 Out[9]: '/home/tsuser/sprint/ipython'
2805 2874 """
2806 2875 return os.getcwdu()
2807 2876
2808 2877 @skip_doctest
2809 2878 def magic_cd(self, parameter_s=''):
2810 2879 """Change the current working directory.
2811 2880
2812 2881 This command automatically maintains an internal list of directories
2813 2882 you visit during your IPython session, in the variable _dh. The
2814 2883 command %dhist shows this history nicely formatted. You can also
2815 2884 do 'cd -<tab>' to see directory history conveniently.
2816 2885
2817 2886 Usage:
2818 2887
2819 2888 cd 'dir': changes to directory 'dir'.
2820 2889
2821 2890 cd -: changes to the last visited directory.
2822 2891
2823 2892 cd -<n>: changes to the n-th directory in the directory history.
2824 2893
2825 2894 cd --foo: change to directory that matches 'foo' in history
2826 2895
2827 2896 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2828 2897 (note: cd <bookmark_name> is enough if there is no
2829 2898 directory <bookmark_name>, but a bookmark with the name exists.)
2830 2899 'cd -b <tab>' allows you to tab-complete bookmark names.
2831 2900
2832 2901 Options:
2833 2902
2834 2903 -q: quiet. Do not print the working directory after the cd command is
2835 2904 executed. By default IPython's cd command does print this directory,
2836 2905 since the default prompts do not display path information.
2837 2906
2838 2907 Note that !cd doesn't work for this purpose because the shell where
2839 2908 !command runs is immediately discarded after executing 'command'.
2840 2909
2841 2910 Examples
2842 2911 --------
2843 2912 ::
2844 2913
2845 2914 In [10]: cd parent/child
2846 2915 /home/tsuser/parent/child
2847 2916 """
2848 2917
2849 2918 parameter_s = parameter_s.strip()
2850 2919 #bkms = self.shell.persist.get("bookmarks",{})
2851 2920
2852 2921 oldcwd = os.getcwdu()
2853 2922 numcd = re.match(r'(-)(\d+)$',parameter_s)
2854 2923 # jump in directory history by number
2855 2924 if numcd:
2856 2925 nn = int(numcd.group(2))
2857 2926 try:
2858 2927 ps = self.shell.user_ns['_dh'][nn]
2859 2928 except IndexError:
2860 2929 print 'The requested directory does not exist in history.'
2861 2930 return
2862 2931 else:
2863 2932 opts = {}
2864 2933 elif parameter_s.startswith('--'):
2865 2934 ps = None
2866 2935 fallback = None
2867 2936 pat = parameter_s[2:]
2868 2937 dh = self.shell.user_ns['_dh']
2869 2938 # first search only by basename (last component)
2870 2939 for ent in reversed(dh):
2871 2940 if pat in os.path.basename(ent) and os.path.isdir(ent):
2872 2941 ps = ent
2873 2942 break
2874 2943
2875 2944 if fallback is None and pat in ent and os.path.isdir(ent):
2876 2945 fallback = ent
2877 2946
2878 2947 # if we have no last part match, pick the first full path match
2879 2948 if ps is None:
2880 2949 ps = fallback
2881 2950
2882 2951 if ps is None:
2883 2952 print "No matching entry in directory history"
2884 2953 return
2885 2954 else:
2886 2955 opts = {}
2887 2956
2888 2957
2889 2958 else:
2890 2959 #turn all non-space-escaping backslashes to slashes,
2891 2960 # for c:\windows\directory\names\
2892 2961 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2893 2962 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2894 2963 # jump to previous
2895 2964 if ps == '-':
2896 2965 try:
2897 2966 ps = self.shell.user_ns['_dh'][-2]
2898 2967 except IndexError:
2899 2968 raise UsageError('%cd -: No previous directory to change to.')
2900 2969 # jump to bookmark if needed
2901 2970 else:
2902 2971 if not os.path.isdir(ps) or opts.has_key('b'):
2903 2972 bkms = self.db.get('bookmarks', {})
2904 2973
2905 2974 if bkms.has_key(ps):
2906 2975 target = bkms[ps]
2907 2976 print '(bookmark:%s) -> %s' % (ps,target)
2908 2977 ps = target
2909 2978 else:
2910 2979 if opts.has_key('b'):
2911 2980 raise UsageError("Bookmark '%s' not found. "
2912 2981 "Use '%%bookmark -l' to see your bookmarks." % ps)
2913 2982
2914 2983 # strip extra quotes on Windows, because os.chdir doesn't like them
2915 2984 ps = unquote_filename(ps)
2916 2985 # at this point ps should point to the target dir
2917 2986 if ps:
2918 2987 try:
2919 2988 os.chdir(os.path.expanduser(ps))
2920 2989 if hasattr(self.shell, 'term_title') and self.shell.term_title:
2921 2990 set_term_title('IPython: ' + abbrev_cwd())
2922 2991 except OSError:
2923 2992 print sys.exc_info()[1]
2924 2993 else:
2925 2994 cwd = os.getcwdu()
2926 2995 dhist = self.shell.user_ns['_dh']
2927 2996 if oldcwd != cwd:
2928 2997 dhist.append(cwd)
2929 2998 self.db['dhist'] = compress_dhist(dhist)[-100:]
2930 2999
2931 3000 else:
2932 3001 os.chdir(self.shell.home_dir)
2933 3002 if hasattr(self.shell, 'term_title') and self.shell.term_title:
2934 3003 set_term_title('IPython: ' + '~')
2935 3004 cwd = os.getcwdu()
2936 3005 dhist = self.shell.user_ns['_dh']
2937 3006
2938 3007 if oldcwd != cwd:
2939 3008 dhist.append(cwd)
2940 3009 self.db['dhist'] = compress_dhist(dhist)[-100:]
2941 3010 if not 'q' in opts and self.shell.user_ns['_dh']:
2942 3011 print self.shell.user_ns['_dh'][-1]
2943 3012
2944 3013
2945 3014 def magic_env(self, parameter_s=''):
2946 3015 """List environment variables."""
2947 3016
2948 3017 return os.environ.data
2949 3018
2950 3019 def magic_pushd(self, parameter_s=''):
2951 3020 """Place the current dir on stack and change directory.
2952 3021
2953 3022 Usage:\\
2954 3023 %pushd ['dirname']
2955 3024 """
2956 3025
2957 3026 dir_s = self.shell.dir_stack
2958 3027 tgt = os.path.expanduser(unquote_filename(parameter_s))
2959 3028 cwd = os.getcwdu().replace(self.home_dir,'~')
2960 3029 if tgt:
2961 3030 self.magic_cd(parameter_s)
2962 3031 dir_s.insert(0,cwd)
2963 3032 return self.magic_dirs()
2964 3033
2965 3034 def magic_popd(self, parameter_s=''):
2966 3035 """Change to directory popped off the top of the stack.
2967 3036 """
2968 3037 if not self.shell.dir_stack:
2969 3038 raise UsageError("%popd on empty stack")
2970 3039 top = self.shell.dir_stack.pop(0)
2971 3040 self.magic_cd(top)
2972 3041 print "popd ->",top
2973 3042
2974 3043 def magic_dirs(self, parameter_s=''):
2975 3044 """Return the current directory stack."""
2976 3045
2977 3046 return self.shell.dir_stack
2978 3047
2979 3048 def magic_dhist(self, parameter_s=''):
2980 3049 """Print your history of visited directories.
2981 3050
2982 3051 %dhist -> print full history\\
2983 3052 %dhist n -> print last n entries only\\
2984 3053 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2985 3054
2986 3055 This history is automatically maintained by the %cd command, and
2987 3056 always available as the global list variable _dh. You can use %cd -<n>
2988 3057 to go to directory number <n>.
2989 3058
2990 3059 Note that most of time, you should view directory history by entering
2991 3060 cd -<TAB>.
2992 3061
2993 3062 """
2994 3063
2995 3064 dh = self.shell.user_ns['_dh']
2996 3065 if parameter_s:
2997 3066 try:
2998 3067 args = map(int,parameter_s.split())
2999 3068 except:
3000 3069 self.arg_err(Magic.magic_dhist)
3001 3070 return
3002 3071 if len(args) == 1:
3003 3072 ini,fin = max(len(dh)-(args[0]),0),len(dh)
3004 3073 elif len(args) == 2:
3005 3074 ini,fin = args
3006 3075 else:
3007 3076 self.arg_err(Magic.magic_dhist)
3008 3077 return
3009 3078 else:
3010 3079 ini,fin = 0,len(dh)
3011 3080 nlprint(dh,
3012 3081 header = 'Directory history (kept in _dh)',
3013 3082 start=ini,stop=fin)
3014 3083
3015 3084 @skip_doctest
3016 3085 def magic_sc(self, parameter_s=''):
3017 3086 """Shell capture - execute a shell command and capture its output.
3018 3087
3019 3088 DEPRECATED. Suboptimal, retained for backwards compatibility.
3020 3089
3021 3090 You should use the form 'var = !command' instead. Example:
3022 3091
3023 3092 "%sc -l myfiles = ls ~" should now be written as
3024 3093
3025 3094 "myfiles = !ls ~"
3026 3095
3027 3096 myfiles.s, myfiles.l and myfiles.n still apply as documented
3028 3097 below.
3029 3098
3030 3099 --
3031 3100 %sc [options] varname=command
3032 3101
3033 3102 IPython will run the given command using commands.getoutput(), and
3034 3103 will then update the user's interactive namespace with a variable
3035 3104 called varname, containing the value of the call. Your command can
3036 3105 contain shell wildcards, pipes, etc.
3037 3106
3038 3107 The '=' sign in the syntax is mandatory, and the variable name you
3039 3108 supply must follow Python's standard conventions for valid names.
3040 3109
3041 3110 (A special format without variable name exists for internal use)
3042 3111
3043 3112 Options:
3044 3113
3045 3114 -l: list output. Split the output on newlines into a list before
3046 3115 assigning it to the given variable. By default the output is stored
3047 3116 as a single string.
3048 3117
3049 3118 -v: verbose. Print the contents of the variable.
3050 3119
3051 3120 In most cases you should not need to split as a list, because the
3052 3121 returned value is a special type of string which can automatically
3053 3122 provide its contents either as a list (split on newlines) or as a
3054 3123 space-separated string. These are convenient, respectively, either
3055 3124 for sequential processing or to be passed to a shell command.
3056 3125
3057 3126 For example:
3058 3127
3059 3128 # all-random
3060 3129
3061 3130 # Capture into variable a
3062 3131 In [1]: sc a=ls *py
3063 3132
3064 3133 # a is a string with embedded newlines
3065 3134 In [2]: a
3066 3135 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
3067 3136
3068 3137 # which can be seen as a list:
3069 3138 In [3]: a.l
3070 3139 Out[3]: ['setup.py', 'win32_manual_post_install.py']
3071 3140
3072 3141 # or as a whitespace-separated string:
3073 3142 In [4]: a.s
3074 3143 Out[4]: 'setup.py win32_manual_post_install.py'
3075 3144
3076 3145 # a.s is useful to pass as a single command line:
3077 3146 In [5]: !wc -l $a.s
3078 3147 146 setup.py
3079 3148 130 win32_manual_post_install.py
3080 3149 276 total
3081 3150
3082 3151 # while the list form is useful to loop over:
3083 3152 In [6]: for f in a.l:
3084 3153 ...: !wc -l $f
3085 3154 ...:
3086 3155 146 setup.py
3087 3156 130 win32_manual_post_install.py
3088 3157
3089 3158 Similarly, the lists returned by the -l option are also special, in
3090 3159 the sense that you can equally invoke the .s attribute on them to
3091 3160 automatically get a whitespace-separated string from their contents:
3092 3161
3093 3162 In [7]: sc -l b=ls *py
3094 3163
3095 3164 In [8]: b
3096 3165 Out[8]: ['setup.py', 'win32_manual_post_install.py']
3097 3166
3098 3167 In [9]: b.s
3099 3168 Out[9]: 'setup.py win32_manual_post_install.py'
3100 3169
3101 3170 In summary, both the lists and strings used for output capture have
3102 3171 the following special attributes:
3103 3172
3104 3173 .l (or .list) : value as list.
3105 3174 .n (or .nlstr): value as newline-separated string.
3106 3175 .s (or .spstr): value as space-separated string.
3107 3176 """
3108 3177
3109 3178 opts,args = self.parse_options(parameter_s,'lv')
3110 3179 # Try to get a variable name and command to run
3111 3180 try:
3112 3181 # the variable name must be obtained from the parse_options
3113 3182 # output, which uses shlex.split to strip options out.
3114 3183 var,_ = args.split('=',1)
3115 3184 var = var.strip()
3116 3185 # But the command has to be extracted from the original input
3117 3186 # parameter_s, not on what parse_options returns, to avoid the
3118 3187 # quote stripping which shlex.split performs on it.
3119 3188 _,cmd = parameter_s.split('=',1)
3120 3189 except ValueError:
3121 3190 var,cmd = '',''
3122 3191 # If all looks ok, proceed
3123 3192 split = 'l' in opts
3124 3193 out = self.shell.getoutput(cmd, split=split)
3125 3194 if opts.has_key('v'):
3126 3195 print '%s ==\n%s' % (var,pformat(out))
3127 3196 if var:
3128 3197 self.shell.user_ns.update({var:out})
3129 3198 else:
3130 3199 return out
3131 3200
3132 3201 def magic_sx(self, parameter_s=''):
3133 3202 """Shell execute - run a shell command and capture its output.
3134 3203
3135 3204 %sx command
3136 3205
3137 3206 IPython will run the given command using commands.getoutput(), and
3138 3207 return the result formatted as a list (split on '\\n'). Since the
3139 3208 output is _returned_, it will be stored in ipython's regular output
3140 3209 cache Out[N] and in the '_N' automatic variables.
3141 3210
3142 3211 Notes:
3143 3212
3144 3213 1) If an input line begins with '!!', then %sx is automatically
3145 3214 invoked. That is, while:
3146 3215 !ls
3147 3216 causes ipython to simply issue system('ls'), typing
3148 3217 !!ls
3149 3218 is a shorthand equivalent to:
3150 3219 %sx ls
3151 3220
3152 3221 2) %sx differs from %sc in that %sx automatically splits into a list,
3153 3222 like '%sc -l'. The reason for this is to make it as easy as possible
3154 3223 to process line-oriented shell output via further python commands.
3155 3224 %sc is meant to provide much finer control, but requires more
3156 3225 typing.
3157 3226
3158 3227 3) Just like %sc -l, this is a list with special attributes:
3159 3228
3160 3229 .l (or .list) : value as list.
3161 3230 .n (or .nlstr): value as newline-separated string.
3162 3231 .s (or .spstr): value as whitespace-separated string.
3163 3232
3164 3233 This is very useful when trying to use such lists as arguments to
3165 3234 system commands."""
3166 3235
3167 3236 if parameter_s:
3168 3237 return self.shell.getoutput(parameter_s)
3169 3238
3170 3239
3171 3240 def magic_bookmark(self, parameter_s=''):
3172 3241 """Manage IPython's bookmark system.
3173 3242
3174 3243 %bookmark <name> - set bookmark to current dir
3175 3244 %bookmark <name> <dir> - set bookmark to <dir>
3176 3245 %bookmark -l - list all bookmarks
3177 3246 %bookmark -d <name> - remove bookmark
3178 3247 %bookmark -r - remove all bookmarks
3179 3248
3180 3249 You can later on access a bookmarked folder with:
3181 3250 %cd -b <name>
3182 3251 or simply '%cd <name>' if there is no directory called <name> AND
3183 3252 there is such a bookmark defined.
3184 3253
3185 3254 Your bookmarks persist through IPython sessions, but they are
3186 3255 associated with each profile."""
3187 3256
3188 3257 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3189 3258 if len(args) > 2:
3190 3259 raise UsageError("%bookmark: too many arguments")
3191 3260
3192 3261 bkms = self.db.get('bookmarks',{})
3193 3262
3194 3263 if opts.has_key('d'):
3195 3264 try:
3196 3265 todel = args[0]
3197 3266 except IndexError:
3198 3267 raise UsageError(
3199 3268 "%bookmark -d: must provide a bookmark to delete")
3200 3269 else:
3201 3270 try:
3202 3271 del bkms[todel]
3203 3272 except KeyError:
3204 3273 raise UsageError(
3205 3274 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3206 3275
3207 3276 elif opts.has_key('r'):
3208 3277 bkms = {}
3209 3278 elif opts.has_key('l'):
3210 3279 bks = bkms.keys()
3211 3280 bks.sort()
3212 3281 if bks:
3213 3282 size = max(map(len,bks))
3214 3283 else:
3215 3284 size = 0
3216 3285 fmt = '%-'+str(size)+'s -> %s'
3217 3286 print 'Current bookmarks:'
3218 3287 for bk in bks:
3219 3288 print fmt % (bk,bkms[bk])
3220 3289 else:
3221 3290 if not args:
3222 3291 raise UsageError("%bookmark: You must specify the bookmark name")
3223 3292 elif len(args)==1:
3224 3293 bkms[args[0]] = os.getcwdu()
3225 3294 elif len(args)==2:
3226 3295 bkms[args[0]] = args[1]
3227 3296 self.db['bookmarks'] = bkms
3228 3297
3229 3298 def magic_pycat(self, parameter_s=''):
3230 3299 """Show a syntax-highlighted file through a pager.
3231 3300
3232 3301 This magic is similar to the cat utility, but it will assume the file
3233 3302 to be Python source and will show it with syntax highlighting. """
3234 3303
3235 3304 try:
3236 3305 filename = get_py_filename(parameter_s)
3237 3306 cont = file_read(filename)
3238 3307 except IOError:
3239 3308 try:
3240 3309 cont = eval(parameter_s,self.user_ns)
3241 3310 except NameError:
3242 3311 cont = None
3243 3312 if cont is None:
3244 3313 print "Error: no such file or variable"
3245 3314 return
3246 3315
3247 3316 page.page(self.shell.pycolorize(cont))
3248 3317
3249 3318 def magic_quickref(self,arg):
3250 3319 """ Show a quick reference sheet """
3251 3320 import IPython.core.usage
3252 3321 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3253 3322
3254 3323 page.page(qr)
3255 3324
3256 3325 def magic_doctest_mode(self,parameter_s=''):
3257 3326 """Toggle doctest mode on and off.
3258 3327
3259 3328 This mode is intended to make IPython behave as much as possible like a
3260 3329 plain Python shell, from the perspective of how its prompts, exceptions
3261 3330 and output look. This makes it easy to copy and paste parts of a
3262 3331 session into doctests. It does so by:
3263 3332
3264 3333 - Changing the prompts to the classic ``>>>`` ones.
3265 3334 - Changing the exception reporting mode to 'Plain'.
3266 3335 - Disabling pretty-printing of output.
3267 3336
3268 3337 Note that IPython also supports the pasting of code snippets that have
3269 3338 leading '>>>' and '...' prompts in them. This means that you can paste
3270 3339 doctests from files or docstrings (even if they have leading
3271 3340 whitespace), and the code will execute correctly. You can then use
3272 3341 '%history -t' to see the translated history; this will give you the
3273 3342 input after removal of all the leading prompts and whitespace, which
3274 3343 can be pasted back into an editor.
3275 3344
3276 3345 With these features, you can switch into this mode easily whenever you
3277 3346 need to do testing and changes to doctests, without having to leave
3278 3347 your existing IPython session.
3279 3348 """
3280 3349
3281 3350 from IPython.utils.ipstruct import Struct
3282 3351
3283 3352 # Shorthands
3284 3353 shell = self.shell
3285 3354 pm = shell.prompt_manager
3286 3355 meta = shell.meta
3287 3356 disp_formatter = self.shell.display_formatter
3288 3357 ptformatter = disp_formatter.formatters['text/plain']
3289 3358 # dstore is a data store kept in the instance metadata bag to track any
3290 3359 # changes we make, so we can undo them later.
3291 3360 dstore = meta.setdefault('doctest_mode',Struct())
3292 3361 save_dstore = dstore.setdefault
3293 3362
3294 3363 # save a few values we'll need to recover later
3295 3364 mode = save_dstore('mode',False)
3296 3365 save_dstore('rc_pprint',ptformatter.pprint)
3297 3366 save_dstore('xmode',shell.InteractiveTB.mode)
3298 3367 save_dstore('rc_separate_out',shell.separate_out)
3299 3368 save_dstore('rc_separate_out2',shell.separate_out2)
3300 3369 save_dstore('rc_prompts_pad_left',pm.justify)
3301 3370 save_dstore('rc_separate_in',shell.separate_in)
3302 3371 save_dstore('rc_plain_text_only',disp_formatter.plain_text_only)
3303 3372 save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template))
3304 3373
3305 3374 if mode == False:
3306 3375 # turn on
3307 3376 pm.in_template = '>>> '
3308 3377 pm.in2_template = '... '
3309 3378 pm.out_template = ''
3310 3379
3311 3380 # Prompt separators like plain python
3312 3381 shell.separate_in = ''
3313 3382 shell.separate_out = ''
3314 3383 shell.separate_out2 = ''
3315 3384
3316 3385 pm.justify = False
3317 3386
3318 3387 ptformatter.pprint = False
3319 3388 disp_formatter.plain_text_only = True
3320 3389
3321 3390 shell.magic_xmode('Plain')
3322 3391 else:
3323 3392 # turn off
3324 3393 pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates
3325 3394
3326 3395 shell.separate_in = dstore.rc_separate_in
3327 3396
3328 3397 shell.separate_out = dstore.rc_separate_out
3329 3398 shell.separate_out2 = dstore.rc_separate_out2
3330 3399
3331 3400 pm.justify = dstore.rc_prompts_pad_left
3332 3401
3333 3402 ptformatter.pprint = dstore.rc_pprint
3334 3403 disp_formatter.plain_text_only = dstore.rc_plain_text_only
3335 3404
3336 3405 shell.magic_xmode(dstore.xmode)
3337 3406
3338 3407 # Store new mode and inform
3339 3408 dstore.mode = bool(1-int(mode))
3340 3409 mode_label = ['OFF','ON'][dstore.mode]
3341 3410 print 'Doctest mode is:', mode_label
3342 3411
3343 3412 def magic_gui(self, parameter_s=''):
3344 3413 """Enable or disable IPython GUI event loop integration.
3345 3414
3346 3415 %gui [GUINAME]
3347 3416
3348 3417 This magic replaces IPython's threaded shells that were activated
3349 3418 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3350 3419 can now be enabled at runtime and keyboard
3351 3420 interrupts should work without any problems. The following toolkits
3352 3421 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
3353 3422
3354 3423 %gui wx # enable wxPython event loop integration
3355 3424 %gui qt4|qt # enable PyQt4 event loop integration
3356 3425 %gui gtk # enable PyGTK event loop integration
3357 3426 %gui tk # enable Tk event loop integration
3358 3427 %gui OSX # enable Cocoa event loop integration
3359 3428 # (requires %matplotlib 1.1)
3360 3429 %gui # disable all event loop integration
3361 3430
3362 3431 WARNING: after any of these has been called you can simply create
3363 3432 an application object, but DO NOT start the event loop yourself, as
3364 3433 we have already handled that.
3365 3434 """
3366 3435 opts, arg = self.parse_options(parameter_s, '')
3367 3436 if arg=='': arg = None
3368 3437 try:
3369 3438 return self.enable_gui(arg)
3370 3439 except Exception as e:
3371 3440 # print simple error message, rather than traceback if we can't
3372 3441 # hook up the GUI
3373 3442 error(str(e))
3374 3443
3375 3444 def magic_load_ext(self, module_str):
3376 3445 """Load an IPython extension by its module name."""
3377 3446 return self.extension_manager.load_extension(module_str)
3378 3447
3379 3448 def magic_unload_ext(self, module_str):
3380 3449 """Unload an IPython extension by its module name."""
3381 3450 self.extension_manager.unload_extension(module_str)
3382 3451
3383 3452 def magic_reload_ext(self, module_str):
3384 3453 """Reload an IPython extension by its module name."""
3385 3454 self.extension_manager.reload_extension(module_str)
3386 3455
3387 3456 def magic_install_profiles(self, s):
3388 3457 """%install_profiles has been deprecated."""
3389 3458 print '\n'.join([
3390 3459 "%install_profiles has been deprecated.",
3391 3460 "Use `ipython profile list` to view available profiles.",
3392 3461 "Requesting a profile with `ipython profile create <name>`",
3393 3462 "or `ipython --profile=<name>` will start with the bundled",
3394 3463 "profile of that name if it exists."
3395 3464 ])
3396 3465
3397 3466 def magic_install_default_config(self, s):
3398 3467 """%install_default_config has been deprecated."""
3399 3468 print '\n'.join([
3400 3469 "%install_default_config has been deprecated.",
3401 3470 "Use `ipython profile create <name>` to initialize a profile",
3402 3471 "with the default config files.",
3403 3472 "Add `--reset` to overwrite already existing config files with defaults."
3404 3473 ])
3405 3474
3406 3475 # Pylab support: simple wrappers that activate pylab, load gui input
3407 3476 # handling and modify slightly %run
3408 3477
3409 3478 @skip_doctest
3410 3479 def _pylab_magic_run(self, parameter_s=''):
3411 3480 Magic.magic_run(self, parameter_s,
3412 3481 runner=mpl_runner(self.shell.safe_execfile))
3413 3482
3414 3483 _pylab_magic_run.__doc__ = magic_run.__doc__
3415 3484
3416 3485 @skip_doctest
3417 3486 def magic_pylab(self, s):
3418 3487 """Load numpy and matplotlib to work interactively.
3419 3488
3420 3489 %pylab [GUINAME]
3421 3490
3422 3491 This function lets you activate pylab (matplotlib, numpy and
3423 3492 interactive support) at any point during an IPython session.
3424 3493
3425 3494 It will import at the top level numpy as np, pyplot as plt, matplotlib,
3426 3495 pylab and mlab, as well as all names from numpy and pylab.
3427 3496
3428 3497 If you are using the inline matplotlib backend for embedded figures,
3429 3498 you can adjust its behavior via the %config magic::
3430 3499
3431 3500 # enable SVG figures, necessary for SVG+XHTML export in the qtconsole
3432 3501 In [1]: %config InlineBackend.figure_format = 'svg'
3433 3502
3434 3503 # change the behavior of closing all figures at the end of each
3435 3504 # execution (cell), or allowing reuse of active figures across
3436 3505 # cells:
3437 3506 In [2]: %config InlineBackend.close_figures = False
3438 3507
3439 3508 Parameters
3440 3509 ----------
3441 3510 guiname : optional
3442 3511 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk',
3443 3512 'osx' or 'tk'). If given, the corresponding Matplotlib backend is
3444 3513 used, otherwise matplotlib's default (which you can override in your
3445 3514 matplotlib config file) is used.
3446 3515
3447 3516 Examples
3448 3517 --------
3449 3518 In this case, where the MPL default is TkAgg::
3450 3519
3451 3520 In [2]: %pylab
3452 3521
3453 3522 Welcome to pylab, a matplotlib-based Python environment.
3454 3523 Backend in use: TkAgg
3455 3524 For more information, type 'help(pylab)'.
3456 3525
3457 3526 But you can explicitly request a different backend::
3458 3527
3459 3528 In [3]: %pylab qt
3460 3529
3461 3530 Welcome to pylab, a matplotlib-based Python environment.
3462 3531 Backend in use: Qt4Agg
3463 3532 For more information, type 'help(pylab)'.
3464 3533 """
3465 3534
3466 3535 if Application.initialized():
3467 3536 app = Application.instance()
3468 3537 try:
3469 3538 import_all_status = app.pylab_import_all
3470 3539 except AttributeError:
3471 3540 import_all_status = True
3472 3541 else:
3473 3542 import_all_status = True
3474 3543
3475 3544 self.shell.enable_pylab(s, import_all=import_all_status)
3476 3545
3477 3546 def magic_tb(self, s):
3478 3547 """Print the last traceback with the currently active exception mode.
3479 3548
3480 3549 See %xmode for changing exception reporting modes."""
3481 3550 self.shell.showtraceback()
3482 3551
3483 3552 @skip_doctest
3484 3553 def magic_precision(self, s=''):
3485 3554 """Set floating point precision for pretty printing.
3486 3555
3487 3556 Can set either integer precision or a format string.
3488 3557
3489 3558 If numpy has been imported and precision is an int,
3490 3559 numpy display precision will also be set, via ``numpy.set_printoptions``.
3491 3560
3492 3561 If no argument is given, defaults will be restored.
3493 3562
3494 3563 Examples
3495 3564 --------
3496 3565 ::
3497 3566
3498 3567 In [1]: from math import pi
3499 3568
3500 3569 In [2]: %precision 3
3501 3570 Out[2]: u'%.3f'
3502 3571
3503 3572 In [3]: pi
3504 3573 Out[3]: 3.142
3505 3574
3506 3575 In [4]: %precision %i
3507 3576 Out[4]: u'%i'
3508 3577
3509 3578 In [5]: pi
3510 3579 Out[5]: 3
3511 3580
3512 3581 In [6]: %precision %e
3513 3582 Out[6]: u'%e'
3514 3583
3515 3584 In [7]: pi**10
3516 3585 Out[7]: 9.364805e+04
3517 3586
3518 3587 In [8]: %precision
3519 3588 Out[8]: u'%r'
3520 3589
3521 3590 In [9]: pi**10
3522 3591 Out[9]: 93648.047476082982
3523 3592
3524 3593 """
3525 3594
3526 3595 ptformatter = self.shell.display_formatter.formatters['text/plain']
3527 3596 ptformatter.float_precision = s
3528 3597 return ptformatter.float_format
3529 3598
3530 3599
3531 3600 @magic_arguments.magic_arguments()
3532 3601 @magic_arguments.argument(
3533 3602 '-e', '--export', action='store_true', default=False,
3534 3603 help='Export IPython history as a notebook. The filename argument '
3535 3604 'is used to specify the notebook name and format. For example '
3536 3605 'a filename of notebook.ipynb will result in a notebook name '
3537 3606 'of "notebook" and a format of "xml". Likewise using a ".json" '
3538 3607 'or ".py" file extension will write the notebook in the json '
3539 3608 'or py formats.'
3540 3609 )
3541 3610 @magic_arguments.argument(
3542 3611 '-f', '--format',
3543 3612 help='Convert an existing IPython notebook to a new format. This option '
3544 3613 'specifies the new format and can have the values: xml, json, py. '
3545 3614 'The target filename is chosen automatically based on the new '
3546 3615 'format. The filename argument gives the name of the source file.'
3547 3616 )
3548 3617 @magic_arguments.argument(
3549 3618 'filename', type=unicode,
3550 3619 help='Notebook name or filename'
3551 3620 )
3552 3621 def magic_notebook(self, s):
3553 3622 """Export and convert IPython notebooks.
3554 3623
3555 3624 This function can export the current IPython history to a notebook file
3556 3625 or can convert an existing notebook file into a different format. For
3557 3626 example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
3558 3627 To export the history to "foo.py" do "%notebook -e foo.py". To convert
3559 3628 "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
3560 3629 formats include (json/ipynb, py).
3561 3630 """
3562 3631 args = magic_arguments.parse_argstring(self.magic_notebook, s)
3563 3632
3564 3633 from IPython.nbformat import current
3565 3634 args.filename = unquote_filename(args.filename)
3566 3635 if args.export:
3567 3636 fname, name, format = current.parse_filename(args.filename)
3568 3637 cells = []
3569 3638 hist = list(self.history_manager.get_range())
3570 3639 for session, prompt_number, input in hist[:-1]:
3571 3640 cells.append(current.new_code_cell(prompt_number=prompt_number, input=input))
3572 3641 worksheet = current.new_worksheet(cells=cells)
3573 3642 nb = current.new_notebook(name=name,worksheets=[worksheet])
3574 3643 with open(fname, 'w') as f:
3575 3644 current.write(nb, f, format);
3576 3645 elif args.format is not None:
3577 3646 old_fname, old_name, old_format = current.parse_filename(args.filename)
3578 3647 new_format = args.format
3579 3648 if new_format == u'xml':
3580 3649 raise ValueError('Notebooks cannot be written as xml.')
3581 3650 elif new_format == u'ipynb' or new_format == u'json':
3582 3651 new_fname = old_name + u'.ipynb'
3583 3652 new_format = u'json'
3584 3653 elif new_format == u'py':
3585 3654 new_fname = old_name + u'.py'
3586 3655 else:
3587 3656 raise ValueError('Invalid notebook format: %s' % new_format)
3588 3657 with open(old_fname, 'r') as f:
3589 3658 s = f.read()
3590 3659 try:
3591 3660 nb = current.reads(s, old_format)
3592 3661 except:
3593 3662 nb = current.reads(s, u'xml')
3594 3663 with open(new_fname, 'w') as f:
3595 3664 current.write(nb, f, new_format)
3596 3665
3597 3666 def magic_config(self, s):
3598 3667 """configure IPython
3599 3668
3600 3669 %config Class[.trait=value]
3601 3670
3602 3671 This magic exposes most of the IPython config system. Any
3603 3672 Configurable class should be able to be configured with the simple
3604 3673 line::
3605 3674
3606 3675 %config Class.trait=value
3607 3676
3608 3677 Where `value` will be resolved in the user's namespace, if it is an
3609 3678 expression or variable name.
3610 3679
3611 3680 Examples
3612 3681 --------
3613 3682
3614 3683 To see what classes are available for config, pass no arguments::
3615 3684
3616 3685 In [1]: %config
3617 3686 Available objects for config:
3618 3687 TerminalInteractiveShell
3619 3688 HistoryManager
3620 3689 PrefilterManager
3621 3690 AliasManager
3622 3691 IPCompleter
3623 3692 PromptManager
3624 3693 DisplayFormatter
3625 3694
3626 3695 To view what is configurable on a given class, just pass the class name::
3627 3696
3628 3697 In [2]: %config IPCompleter
3629 3698 IPCompleter options
3630 3699 -----------------
3631 3700 IPCompleter.omit__names=<Enum>
3632 3701 Current: 2
3633 3702 Choices: (0, 1, 2)
3634 3703 Instruct the completer to omit private method names
3635 3704 Specifically, when completing on ``object.<tab>``.
3636 3705 When 2 [default]: all names that start with '_' will be excluded.
3637 3706 When 1: all 'magic' names (``__foo__``) will be excluded.
3638 3707 When 0: nothing will be excluded.
3639 3708 IPCompleter.merge_completions=<CBool>
3640 3709 Current: True
3641 3710 Whether to merge completion results into a single list
3642 3711 If False, only the completion results from the first non-empty completer
3643 3712 will be returned.
3644 3713 IPCompleter.greedy=<CBool>
3645 3714 Current: False
3646 3715 Activate greedy completion
3647 3716 This will enable completion on elements of lists, results of function calls,
3648 3717 etc., but can be unsafe because the code is actually evaluated on TAB.
3649 3718
3650 3719 but the real use is in setting values::
3651 3720
3652 3721 In [3]: %config IPCompleter.greedy = True
3653 3722
3654 3723 and these values are read from the user_ns if they are variables::
3655 3724
3656 3725 In [4]: feeling_greedy=False
3657 3726
3658 3727 In [5]: %config IPCompleter.greedy = feeling_greedy
3659 3728
3660 3729 """
3661 3730 from IPython.config.loader import Config
3662 3731 # some IPython objects are Configurable, but do not yet have
3663 3732 # any configurable traits. Exclude them from the effects of
3664 3733 # this magic, as their presence is just noise:
3665 3734 configurables = [ c for c in self.configurables if c.__class__.class_traits(config=True) ]
3666 3735 classnames = [ c.__class__.__name__ for c in configurables ]
3667 3736
3668 3737 line = s.strip()
3669 3738 if not line:
3670 3739 # print available configurable names
3671 3740 print "Available objects for config:"
3672 3741 for name in classnames:
3673 3742 print " ", name
3674 3743 return
3675 3744 elif line in classnames:
3676 3745 # `%config TerminalInteractiveShell` will print trait info for
3677 3746 # TerminalInteractiveShell
3678 3747 c = configurables[classnames.index(line)]
3679 3748 cls = c.__class__
3680 3749 help = cls.class_get_help(c)
3681 3750 # strip leading '--' from cl-args:
3682 3751 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
3683 3752 print help
3684 3753 return
3685 3754 elif '=' not in line:
3686 3755 raise UsageError("Invalid config statement: %r, should be Class.trait = value" % line)
3687 3756
3688 3757
3689 3758 # otherwise, assume we are setting configurables.
3690 3759 # leave quotes on args when splitting, because we want
3691 3760 # unquoted args to eval in user_ns
3692 3761 cfg = Config()
3693 3762 exec "cfg."+line in locals(), self.user_ns
3694 3763
3695 3764 for configurable in configurables:
3696 3765 try:
3697 3766 configurable.update_config(cfg)
3698 3767 except Exception as e:
3699 3768 error(e)
3700 3769
3701 def magic_clear(self, s):
3702 """Clear various data (e.g. stored history data)
3703
3704 %clear in - clear input history
3705 %clear out - clear output history
3706 %clear dhist - clear dir history
3707 %clear array - clear only variables that are NumPy arrays
3708
3709 Examples
3710 --------
3711 ::
3712
3713 In [1]: clear in
3714 Flushing input history
3715
3716 In [2]: clear dhist
3717 Clearing directory history
3718 """
3719 ip = self.shell
3720 user_ns = self.user_ns # local lookup, heavily used
3721
3722 for target in s.split():
3723 if target == 'out':
3724 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
3725 self.displayhook.flush()
3726
3727 elif target == 'in':
3728 print "Flushing input history"
3729 pc = self.displayhook.prompt_count + 1
3730 for n in range(1, pc):
3731 key = '_i'+repr(n)
3732 user_ns.pop(key,None)
3733 user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
3734 # don't delete these, as %save and %macro depending on the length
3735 # of these lists to be preserved
3736 self.history_manager.input_hist_parsed[:] = [''] * pc
3737 self.history_manager.input_hist_raw[:] = [''] * pc
3738
3739 elif target == 'array':
3740 # Support cleaning up numpy arrays
3741 try:
3742 from numpy import ndarray
3743 # This must be done with items and not iteritems because we're
3744 # going to modify the dict in-place.
3745 for x,val in user_ns.items():
3746 if isinstance(val,ndarray):
3747 del user_ns[x]
3748 except ImportError:
3749 print "Clear array only works if Numpy is available."
3750
3751 elif target == 'dhist':
3752 print "Clearing directory history"
3753 del user_ns['_dh'][:]
3754
3755 gc.collect()
3756
3757 3770 # end Magic
@@ -1,387 +1,387 b''
1 1 """Tests for various magic functions.
2 2
3 3 Needs to be run by nose (to make ipython session available).
4 4 """
5 5 from __future__ import absolute_import
6 6
7 7 #-----------------------------------------------------------------------------
8 8 # Imports
9 9 #-----------------------------------------------------------------------------
10 10
11 11 import os
12 12
13 13 import nose.tools as nt
14 14
15 15 from IPython.testing import decorators as dec
16 16 from IPython.testing import tools as tt
17 17 from IPython.utils import py3compat
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Test functions begin
21 21 #-----------------------------------------------------------------------------
22 22
23 23 def test_rehashx():
24 24 # clear up everything
25 25 _ip = get_ipython()
26 26 _ip.alias_manager.alias_table.clear()
27 27 del _ip.db['syscmdlist']
28 28
29 29 _ip.magic('rehashx')
30 30 # Practically ALL ipython development systems will have more than 10 aliases
31 31
32 32 yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
33 33 for key, val in _ip.alias_manager.alias_table.iteritems():
34 34 # we must strip dots from alias names
35 35 nt.assert_true('.' not in key)
36 36
37 37 # rehashx must fill up syscmdlist
38 38 scoms = _ip.db['syscmdlist']
39 39 yield (nt.assert_true, len(scoms) > 10)
40 40
41 41
42 42 def test_magic_parse_options():
43 43 """Test that we don't mangle paths when parsing magic options."""
44 44 ip = get_ipython()
45 45 path = 'c:\\x'
46 46 opts = ip.parse_options('-f %s' % path,'f:')[0]
47 47 # argv splitting is os-dependent
48 48 if os.name == 'posix':
49 49 expected = 'c:x'
50 50 else:
51 51 expected = path
52 52 nt.assert_equals(opts['f'], expected)
53 53
54 54
55 55 @dec.skip_without('sqlite3')
56 56 def doctest_hist_f():
57 57 """Test %hist -f with temporary filename.
58 58
59 59 In [9]: import tempfile
60 60
61 61 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
62 62
63 63 In [11]: %hist -nl -f $tfile 3
64 64
65 65 In [13]: import os; os.unlink(tfile)
66 66 """
67 67
68 68
69 69 @dec.skip_without('sqlite3')
70 70 def doctest_hist_r():
71 71 """Test %hist -r
72 72
73 73 XXX - This test is not recording the output correctly. For some reason, in
74 74 testing mode the raw history isn't getting populated. No idea why.
75 75 Disabling the output checking for now, though at least we do run it.
76 76
77 77 In [1]: 'hist' in _ip.lsmagic()
78 78 Out[1]: True
79 79
80 80 In [2]: x=1
81 81
82 82 In [3]: %hist -rl 2
83 83 x=1 # random
84 84 %hist -r 2
85 85 """
86 86
87 87
88 88 @dec.skip_without('sqlite3')
89 89 def doctest_hist_op():
90 90 """Test %hist -op
91 91
92 92 In [1]: class b(float):
93 93 ...: pass
94 94 ...:
95 95
96 96 In [2]: class s(object):
97 97 ...: def __str__(self):
98 98 ...: return 's'
99 99 ...:
100 100
101 101 In [3]:
102 102
103 103 In [4]: class r(b):
104 104 ...: def __repr__(self):
105 105 ...: return 'r'
106 106 ...:
107 107
108 108 In [5]: class sr(s,r): pass
109 109 ...:
110 110
111 111 In [6]:
112 112
113 113 In [7]: bb=b()
114 114
115 115 In [8]: ss=s()
116 116
117 117 In [9]: rr=r()
118 118
119 119 In [10]: ssrr=sr()
120 120
121 121 In [11]: 4.5
122 122 Out[11]: 4.5
123 123
124 124 In [12]: str(ss)
125 125 Out[12]: 's'
126 126
127 127 In [13]:
128 128
129 129 In [14]: %hist -op
130 130 >>> class b:
131 131 ... pass
132 132 ...
133 133 >>> class s(b):
134 134 ... def __str__(self):
135 135 ... return 's'
136 136 ...
137 137 >>>
138 138 >>> class r(b):
139 139 ... def __repr__(self):
140 140 ... return 'r'
141 141 ...
142 142 >>> class sr(s,r): pass
143 143 >>>
144 144 >>> bb=b()
145 145 >>> ss=s()
146 146 >>> rr=r()
147 147 >>> ssrr=sr()
148 148 >>> 4.5
149 149 4.5
150 150 >>> str(ss)
151 151 's'
152 152 >>>
153 153 """
154 154
155 155
156 156 @dec.skip_without('sqlite3')
157 157 def test_macro():
158 158 ip = get_ipython()
159 159 ip.history_manager.reset() # Clear any existing history.
160 160 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
161 161 for i, cmd in enumerate(cmds, start=1):
162 162 ip.history_manager.store_inputs(i, cmd)
163 163 ip.magic("macro test 1-3")
164 164 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
165 165
166 166 # List macros.
167 167 assert "test" in ip.magic("macro")
168 168
169 169
170 170 @dec.skip_without('sqlite3')
171 171 def test_macro_run():
172 172 """Test that we can run a multi-line macro successfully."""
173 173 ip = get_ipython()
174 174 ip.history_manager.reset()
175 175 cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"),
176 176 "%macro test 2-3"]
177 177 for cmd in cmds:
178 178 ip.run_cell(cmd, store_history=True)
179 179 nt.assert_equal(ip.user_ns["test"].value,
180 180 py3compat.doctest_refactor_print("a+=1\nprint a\n"))
181 181 with tt.AssertPrints("12"):
182 182 ip.run_cell("test")
183 183 with tt.AssertPrints("13"):
184 184 ip.run_cell("test")
185 185
186 186
187 187 @dec.skipif_not_numpy
188 def test_numpy_clear_array_undec():
189 "Test '%clear array' functionality"
188 def test_numpy_reset_array_undec():
189 "Test '%reset array' functionality"
190 190 _ip.ex('import numpy as np')
191 191 _ip.ex('a = np.empty(2)')
192 192 yield (nt.assert_true, 'a' in _ip.user_ns)
193 _ip.magic('clear array')
193 _ip.magic('reset -f array')
194 194 yield (nt.assert_false, 'a' in _ip.user_ns)
195 195
196 def test_clear():
197 "Test '%clear' magic provided by IPython.extensions.clearcmd"
196 def test_reset_args():
197 "Test '%reset' magic with args which used to be extensions.clearcmd"
198 198 _ip = get_ipython()
199 199 _ip.run_cell("parrot = 'dead'", store_history=True)
200 # test '%clear out', make an Out prompt
200 # test '%reset -f out', make an Out prompt
201 201 _ip.run_cell("parrot", store_history=True)
202 202 nt.assert_true('dead' in [_ip.user_ns[x] for x in '_','__','___'])
203 _ip.magic('clear out')
203 _ip.magic('reset -f out')
204 204 nt.assert_false('dead' in [_ip.user_ns[x] for x in '_','__','___'])
205 205 nt.assert_true(len(_ip.user_ns['Out']) == 0)
206 206
207 # test '%clear in'
207 # test '%reset -f in'
208 208 _ip.run_cell("parrot", store_history=True)
209 209 nt.assert_true('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
210 _ip.magic('%clear in')
210 _ip.magic('%reset -f in')
211 211 nt.assert_false('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
212 212 nt.assert_true(len(set(_ip.user_ns['In'])) == 1)
213 213
214 # test '%clear dhist'
214 # test '%reset -f dhist'
215 215 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
216 216 _ip.magic('cd')
217 217 _ip.magic('cd -')
218 218 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
219 _ip.magic('clear dhist')
219 _ip.magic('reset -f dhist')
220 220 nt.assert_true(len(_ip.user_ns['_dh']) == 0)
221 221 _ip.run_cell("_dh = [d for d in tmp]") #restore
222 222
223 223 # test that In length is preserved for %macro
224 224 _ip.run_cell("print 'foo'")
225 _ip.run_cell("clear in")
225 _ip.run_cell("reset -f in")
226 226 nt.assert_true(len(_ip.user_ns['In']) == _ip.displayhook.prompt_count+1)
227 227
228 228 def test_time():
229 229 _ip.magic('time None')
230 230
231 231
232 232 @py3compat.doctest_refactor_print
233 233 def doctest_time():
234 234 """
235 235 In [10]: %time None
236 236 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
237 237 Wall time: 0.00 s
238 238
239 239 In [11]: def f(kmjy):
240 240 ....: %time print 2*kmjy
241 241
242 242 In [12]: f(3)
243 243 6
244 244 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
245 245 Wall time: 0.00 s
246 246 """
247 247
248 248
249 249 def test_doctest_mode():
250 250 "Toggle doctest_mode twice, it should be a no-op and run without error"
251 251 _ip.magic('doctest_mode')
252 252 _ip.magic('doctest_mode')
253 253
254 254
255 255 def test_parse_options():
256 256 """Tests for basic options parsing in magics."""
257 257 # These are only the most minimal of tests, more should be added later. At
258 258 # the very least we check that basic text/unicode calls work OK.
259 259 nt.assert_equal(_ip.parse_options('foo', '')[1], 'foo')
260 260 nt.assert_equal(_ip.parse_options(u'foo', '')[1], u'foo')
261 261
262 262
263 263 def test_dirops():
264 264 """Test various directory handling operations."""
265 265 # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/')
266 266 curpath = os.getcwdu
267 267 startdir = os.getcwdu()
268 268 ipdir = os.path.realpath(_ip.ipython_dir)
269 269 try:
270 270 _ip.magic('cd "%s"' % ipdir)
271 271 nt.assert_equal(curpath(), ipdir)
272 272 _ip.magic('cd -')
273 273 nt.assert_equal(curpath(), startdir)
274 274 _ip.magic('pushd "%s"' % ipdir)
275 275 nt.assert_equal(curpath(), ipdir)
276 276 _ip.magic('popd')
277 277 nt.assert_equal(curpath(), startdir)
278 278 finally:
279 279 os.chdir(startdir)
280 280
281 281
282 282 def test_xmode():
283 283 # Calling xmode three times should be a no-op
284 284 xmode = _ip.InteractiveTB.mode
285 285 for i in range(3):
286 286 _ip.magic("xmode")
287 287 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
288 288
289 289 def test_reset_hard():
290 290 monitor = []
291 291 class A(object):
292 292 def __del__(self):
293 293 monitor.append(1)
294 294 def __repr__(self):
295 295 return "<A instance>"
296 296
297 297 _ip.user_ns["a"] = A()
298 298 _ip.run_cell("a")
299 299
300 300 nt.assert_equal(monitor, [])
301 301 _ip.magic_reset("-f")
302 302 nt.assert_equal(monitor, [1])
303 303
304 304 class TestXdel(tt.TempFileMixin):
305 305 def test_xdel(self):
306 306 """Test that references from %run are cleared by xdel."""
307 307 src = ("class A(object):\n"
308 308 " monitor = []\n"
309 309 " def __del__(self):\n"
310 310 " self.monitor.append(1)\n"
311 311 "a = A()\n")
312 312 self.mktmp(src)
313 313 # %run creates some hidden references...
314 314 _ip.magic("run %s" % self.fname)
315 315 # ... as does the displayhook.
316 316 _ip.run_cell("a")
317 317
318 318 monitor = _ip.user_ns["A"].monitor
319 319 nt.assert_equal(monitor, [])
320 320
321 321 _ip.magic("xdel a")
322 322
323 323 # Check that a's __del__ method has been called.
324 324 nt.assert_equal(monitor, [1])
325 325
326 326 def doctest_who():
327 327 """doctest for %who
328 328
329 329 In [1]: %reset -f
330 330
331 331 In [2]: alpha = 123
332 332
333 333 In [3]: beta = 'beta'
334 334
335 335 In [4]: %who int
336 336 alpha
337 337
338 338 In [5]: %who str
339 339 beta
340 340
341 341 In [6]: %whos
342 342 Variable Type Data/Info
343 343 ----------------------------
344 344 alpha int 123
345 345 beta str beta
346 346
347 347 In [7]: %who_ls
348 348 Out[7]: ['alpha', 'beta']
349 349 """
350 350
351 351 @py3compat.u_format
352 352 def doctest_precision():
353 353 """doctest for %precision
354 354
355 355 In [1]: f = get_ipython().shell.display_formatter.formatters['text/plain']
356 356
357 357 In [2]: %precision 5
358 358 Out[2]: {u}'%.5f'
359 359
360 360 In [3]: f.float_format
361 361 Out[3]: {u}'%.5f'
362 362
363 363 In [4]: %precision %e
364 364 Out[4]: {u}'%e'
365 365
366 366 In [5]: f(3.1415927)
367 367 Out[5]: {u}'3.141593e+00'
368 368 """
369 369
370 370 def test_psearch():
371 371 with tt.AssertPrints("dict.fromkeys"):
372 372 _ip.run_cell("dict.fr*?")
373 373
374 374 def test_timeit_shlex():
375 375 """test shlex issues with timeit (#1109)"""
376 376 _ip.ex("def f(*a,**kw): pass")
377 377 _ip.magic('timeit -n1 "this is a bug".count(" ")')
378 378 _ip.magic('timeit -r1 -n1 f(" ", 1)')
379 379 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
380 380 _ip.magic('timeit -r1 -n1 ("a " + "b")')
381 381 _ip.magic('timeit -r1 -n1 f("a " + "b")')
382 382 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
383 383
384 384
385 385 def test_timeit_arguments():
386 386 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
387 387 _ip.magic("timeit ('#')")
General Comments 0
You need to be logged in to leave comments. Login now