##// END OF EJS Templates
Merge pull request #11896 from minrk/secretssss...
Matthias Bussonnier -
r25204:cdb6252e merge
parent child Browse files
Show More
@@ -1,843 +1,849 b''
1 """Implementation of magic functions for interaction with the OS.
1 """Implementation of magic functions for interaction with the OS.
2
2
3 Note: this module is named 'osm' instead of 'os' to avoid a collision with the
3 Note: this module is named 'osm' instead of 'os' to avoid a collision with the
4 builtin.
4 builtin.
5 """
5 """
6 # Copyright (c) IPython Development Team.
6 # Copyright (c) IPython Development Team.
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8
8
9 import io
9 import io
10 import os
10 import os
11 import re
11 import re
12 import sys
12 import sys
13 from pprint import pformat
13 from pprint import pformat
14
14
15 from IPython.core import magic_arguments
15 from IPython.core import magic_arguments
16 from IPython.core import oinspect
16 from IPython.core import oinspect
17 from IPython.core import page
17 from IPython.core import page
18 from IPython.core.alias import AliasError, Alias
18 from IPython.core.alias import AliasError, Alias
19 from IPython.core.error import UsageError
19 from IPython.core.error import UsageError
20 from IPython.core.magic import (
20 from IPython.core.magic import (
21 Magics, compress_dhist, magics_class, line_magic, cell_magic, line_cell_magic
21 Magics, compress_dhist, magics_class, line_magic, cell_magic, line_cell_magic
22 )
22 )
23 from IPython.testing.skipdoctest import skip_doctest
23 from IPython.testing.skipdoctest import skip_doctest
24 from IPython.utils.openpy import source_to_unicode
24 from IPython.utils.openpy import source_to_unicode
25 from IPython.utils.process import abbrev_cwd
25 from IPython.utils.process import abbrev_cwd
26 from IPython.utils.terminal import set_term_title
26 from IPython.utils.terminal import set_term_title
27 from traitlets import Bool
27 from traitlets import Bool
28
28
29
29
30 @magics_class
30 @magics_class
31 class OSMagics(Magics):
31 class OSMagics(Magics):
32 """Magics to interact with the underlying OS (shell-type functionality).
32 """Magics to interact with the underlying OS (shell-type functionality).
33 """
33 """
34
34
35 cd_force_quiet = Bool(False,
35 cd_force_quiet = Bool(False,
36 help="Force %cd magic to be quiet even if -q is not passed."
36 help="Force %cd magic to be quiet even if -q is not passed."
37 ).tag(config=True)
37 ).tag(config=True)
38
38
39 def __init__(self, shell=None, **kwargs):
39 def __init__(self, shell=None, **kwargs):
40
40
41 # Now define isexec in a cross platform manner.
41 # Now define isexec in a cross platform manner.
42 self.is_posix = False
42 self.is_posix = False
43 self.execre = None
43 self.execre = None
44 if os.name == 'posix':
44 if os.name == 'posix':
45 self.is_posix = True
45 self.is_posix = True
46 else:
46 else:
47 try:
47 try:
48 winext = os.environ['pathext'].replace(';','|').replace('.','')
48 winext = os.environ['pathext'].replace(';','|').replace('.','')
49 except KeyError:
49 except KeyError:
50 winext = 'exe|com|bat|py'
50 winext = 'exe|com|bat|py'
51
51
52 self.execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
52 self.execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
53
53
54 # call up the chain
54 # call up the chain
55 super().__init__(shell=shell, **kwargs)
55 super().__init__(shell=shell, **kwargs)
56
56
57
57
58 @skip_doctest
58 @skip_doctest
59 def _isexec_POSIX(self, file):
59 def _isexec_POSIX(self, file):
60 """
60 """
61 Test for executable on a POSIX system
61 Test for executable on a POSIX system
62 """
62 """
63 if os.access(file.path, os.X_OK):
63 if os.access(file.path, os.X_OK):
64 # will fail on maxOS if access is not X_OK
64 # will fail on maxOS if access is not X_OK
65 return file.is_file()
65 return file.is_file()
66 return False
66 return False
67
67
68
68
69
69
70 @skip_doctest
70 @skip_doctest
71 def _isexec_WIN(self, file):
71 def _isexec_WIN(self, file):
72 """
72 """
73 Test for executable file on non POSIX system
73 Test for executable file on non POSIX system
74 """
74 """
75 return file.is_file() and self.execre.match(file.name) is not None
75 return file.is_file() and self.execre.match(file.name) is not None
76
76
77 @skip_doctest
77 @skip_doctest
78 def isexec(self, file):
78 def isexec(self, file):
79 """
79 """
80 Test for executable file on non POSIX system
80 Test for executable file on non POSIX system
81 """
81 """
82 if self.is_posix:
82 if self.is_posix:
83 return self._isexec_POSIX(file)
83 return self._isexec_POSIX(file)
84 else:
84 else:
85 return self._isexec_WIN(file)
85 return self._isexec_WIN(file)
86
86
87
87
88 @skip_doctest
88 @skip_doctest
89 @line_magic
89 @line_magic
90 def alias(self, parameter_s=''):
90 def alias(self, parameter_s=''):
91 """Define an alias for a system command.
91 """Define an alias for a system command.
92
92
93 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
93 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
94
94
95 Then, typing 'alias_name params' will execute the system command 'cmd
95 Then, typing 'alias_name params' will execute the system command 'cmd
96 params' (from your underlying operating system).
96 params' (from your underlying operating system).
97
97
98 Aliases have lower precedence than magic functions and Python normal
98 Aliases have lower precedence than magic functions and Python normal
99 variables, so if 'foo' is both a Python variable and an alias, the
99 variables, so if 'foo' is both a Python variable and an alias, the
100 alias can not be executed until 'del foo' removes the Python variable.
100 alias can not be executed until 'del foo' removes the Python variable.
101
101
102 You can use the %l specifier in an alias definition to represent the
102 You can use the %l specifier in an alias definition to represent the
103 whole line when the alias is called. For example::
103 whole line when the alias is called. For example::
104
104
105 In [2]: alias bracket echo "Input in brackets: <%l>"
105 In [2]: alias bracket echo "Input in brackets: <%l>"
106 In [3]: bracket hello world
106 In [3]: bracket hello world
107 Input in brackets: <hello world>
107 Input in brackets: <hello world>
108
108
109 You can also define aliases with parameters using %s specifiers (one
109 You can also define aliases with parameters using %s specifiers (one
110 per parameter)::
110 per parameter)::
111
111
112 In [1]: alias parts echo first %s second %s
112 In [1]: alias parts echo first %s second %s
113 In [2]: %parts A B
113 In [2]: %parts A B
114 first A second B
114 first A second B
115 In [3]: %parts A
115 In [3]: %parts A
116 Incorrect number of arguments: 2 expected.
116 Incorrect number of arguments: 2 expected.
117 parts is an alias to: 'echo first %s second %s'
117 parts is an alias to: 'echo first %s second %s'
118
118
119 Note that %l and %s are mutually exclusive. You can only use one or
119 Note that %l and %s are mutually exclusive. You can only use one or
120 the other in your aliases.
120 the other in your aliases.
121
121
122 Aliases expand Python variables just like system calls using ! or !!
122 Aliases expand Python variables just like system calls using ! or !!
123 do: all expressions prefixed with '$' get expanded. For details of
123 do: all expressions prefixed with '$' get expanded. For details of
124 the semantic rules, see PEP-215:
124 the semantic rules, see PEP-215:
125 http://www.python.org/peps/pep-0215.html. This is the library used by
125 http://www.python.org/peps/pep-0215.html. This is the library used by
126 IPython for variable expansion. If you want to access a true shell
126 IPython for variable expansion. If you want to access a true shell
127 variable, an extra $ is necessary to prevent its expansion by
127 variable, an extra $ is necessary to prevent its expansion by
128 IPython::
128 IPython::
129
129
130 In [6]: alias show echo
130 In [6]: alias show echo
131 In [7]: PATH='A Python string'
131 In [7]: PATH='A Python string'
132 In [8]: show $PATH
132 In [8]: show $PATH
133 A Python string
133 A Python string
134 In [9]: show $$PATH
134 In [9]: show $$PATH
135 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
135 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
136
136
137 You can use the alias facility to access all of $PATH. See the %rehashx
137 You can use the alias facility to access all of $PATH. See the %rehashx
138 function, which automatically creates aliases for the contents of your
138 function, which automatically creates aliases for the contents of your
139 $PATH.
139 $PATH.
140
140
141 If called with no parameters, %alias prints the current alias table
141 If called with no parameters, %alias prints the current alias table
142 for your system. For posix systems, the default aliases are 'cat',
142 for your system. For posix systems, the default aliases are 'cat',
143 'cp', 'mv', 'rm', 'rmdir', and 'mkdir', and other platform-specific
143 'cp', 'mv', 'rm', 'rmdir', and 'mkdir', and other platform-specific
144 aliases are added. For windows-based systems, the default aliases are
144 aliases are added. For windows-based systems, the default aliases are
145 'copy', 'ddir', 'echo', 'ls', 'ldir', 'mkdir', 'ren', and 'rmdir'.
145 'copy', 'ddir', 'echo', 'ls', 'ldir', 'mkdir', 'ren', and 'rmdir'.
146
146
147 You can see the definition of alias by adding a question mark in the
147 You can see the definition of alias by adding a question mark in the
148 end::
148 end::
149
149
150 In [1]: cat?
150 In [1]: cat?
151 Repr: <alias cat for 'cat'>"""
151 Repr: <alias cat for 'cat'>"""
152
152
153 par = parameter_s.strip()
153 par = parameter_s.strip()
154 if not par:
154 if not par:
155 aliases = sorted(self.shell.alias_manager.aliases)
155 aliases = sorted(self.shell.alias_manager.aliases)
156 # stored = self.shell.db.get('stored_aliases', {} )
156 # stored = self.shell.db.get('stored_aliases', {} )
157 # for k, v in stored:
157 # for k, v in stored:
158 # atab.append(k, v[0])
158 # atab.append(k, v[0])
159
159
160 print("Total number of aliases:", len(aliases))
160 print("Total number of aliases:", len(aliases))
161 sys.stdout.flush()
161 sys.stdout.flush()
162 return aliases
162 return aliases
163
163
164 # Now try to define a new one
164 # Now try to define a new one
165 try:
165 try:
166 alias,cmd = par.split(None, 1)
166 alias,cmd = par.split(None, 1)
167 except TypeError:
167 except TypeError:
168 print(oinspect.getdoc(self.alias))
168 print(oinspect.getdoc(self.alias))
169 return
169 return
170
170
171 try:
171 try:
172 self.shell.alias_manager.define_alias(alias, cmd)
172 self.shell.alias_manager.define_alias(alias, cmd)
173 except AliasError as e:
173 except AliasError as e:
174 print(e)
174 print(e)
175 # end magic_alias
175 # end magic_alias
176
176
177 @line_magic
177 @line_magic
178 def unalias(self, parameter_s=''):
178 def unalias(self, parameter_s=''):
179 """Remove an alias"""
179 """Remove an alias"""
180
180
181 aname = parameter_s.strip()
181 aname = parameter_s.strip()
182 try:
182 try:
183 self.shell.alias_manager.undefine_alias(aname)
183 self.shell.alias_manager.undefine_alias(aname)
184 except ValueError as e:
184 except ValueError as e:
185 print(e)
185 print(e)
186 return
186 return
187
187
188 stored = self.shell.db.get('stored_aliases', {} )
188 stored = self.shell.db.get('stored_aliases', {} )
189 if aname in stored:
189 if aname in stored:
190 print("Removing %stored alias",aname)
190 print("Removing %stored alias",aname)
191 del stored[aname]
191 del stored[aname]
192 self.shell.db['stored_aliases'] = stored
192 self.shell.db['stored_aliases'] = stored
193
193
194 @line_magic
194 @line_magic
195 def rehashx(self, parameter_s=''):
195 def rehashx(self, parameter_s=''):
196 """Update the alias table with all executable files in $PATH.
196 """Update the alias table with all executable files in $PATH.
197
197
198 rehashx explicitly checks that every entry in $PATH is a file
198 rehashx explicitly checks that every entry in $PATH is a file
199 with execute access (os.X_OK).
199 with execute access (os.X_OK).
200
200
201 Under Windows, it checks executability as a match against a
201 Under Windows, it checks executability as a match against a
202 '|'-separated string of extensions, stored in the IPython config
202 '|'-separated string of extensions, stored in the IPython config
203 variable win_exec_ext. This defaults to 'exe|com|bat'.
203 variable win_exec_ext. This defaults to 'exe|com|bat'.
204
204
205 This function also resets the root module cache of module completer,
205 This function also resets the root module cache of module completer,
206 used on slow filesystems.
206 used on slow filesystems.
207 """
207 """
208 from IPython.core.alias import InvalidAliasError
208 from IPython.core.alias import InvalidAliasError
209
209
210 # for the benefit of module completer in ipy_completers.py
210 # for the benefit of module completer in ipy_completers.py
211 del self.shell.db['rootmodules_cache']
211 del self.shell.db['rootmodules_cache']
212
212
213 path = [os.path.abspath(os.path.expanduser(p)) for p in
213 path = [os.path.abspath(os.path.expanduser(p)) for p in
214 os.environ.get('PATH','').split(os.pathsep)]
214 os.environ.get('PATH','').split(os.pathsep)]
215
215
216 syscmdlist = []
216 syscmdlist = []
217 savedir = os.getcwd()
217 savedir = os.getcwd()
218
218
219 # Now walk the paths looking for executables to alias.
219 # Now walk the paths looking for executables to alias.
220 try:
220 try:
221 # write the whole loop for posix/Windows so we don't have an if in
221 # write the whole loop for posix/Windows so we don't have an if in
222 # the innermost part
222 # the innermost part
223 if self.is_posix:
223 if self.is_posix:
224 for pdir in path:
224 for pdir in path:
225 try:
225 try:
226 os.chdir(pdir)
226 os.chdir(pdir)
227 except OSError:
227 except OSError:
228 continue
228 continue
229
229
230 # for python 3.6+ rewrite to: with os.scandir(pdir) as dirlist:
230 # for python 3.6+ rewrite to: with os.scandir(pdir) as dirlist:
231 dirlist = os.scandir(path=pdir)
231 dirlist = os.scandir(path=pdir)
232 for ff in dirlist:
232 for ff in dirlist:
233 if self.isexec(ff):
233 if self.isexec(ff):
234 fname = ff.name
234 fname = ff.name
235 try:
235 try:
236 # Removes dots from the name since ipython
236 # Removes dots from the name since ipython
237 # will assume names with dots to be python.
237 # will assume names with dots to be python.
238 if not self.shell.alias_manager.is_alias(fname):
238 if not self.shell.alias_manager.is_alias(fname):
239 self.shell.alias_manager.define_alias(
239 self.shell.alias_manager.define_alias(
240 fname.replace('.',''), fname)
240 fname.replace('.',''), fname)
241 except InvalidAliasError:
241 except InvalidAliasError:
242 pass
242 pass
243 else:
243 else:
244 syscmdlist.append(fname)
244 syscmdlist.append(fname)
245 else:
245 else:
246 no_alias = Alias.blacklist
246 no_alias = Alias.blacklist
247 for pdir in path:
247 for pdir in path:
248 try:
248 try:
249 os.chdir(pdir)
249 os.chdir(pdir)
250 except OSError:
250 except OSError:
251 continue
251 continue
252
252
253 # for python 3.6+ rewrite to: with os.scandir(pdir) as dirlist:
253 # for python 3.6+ rewrite to: with os.scandir(pdir) as dirlist:
254 dirlist = os.scandir(pdir)
254 dirlist = os.scandir(pdir)
255 for ff in dirlist:
255 for ff in dirlist:
256 fname = ff.name
256 fname = ff.name
257 base, ext = os.path.splitext(fname)
257 base, ext = os.path.splitext(fname)
258 if self.isexec(ff) and base.lower() not in no_alias:
258 if self.isexec(ff) and base.lower() not in no_alias:
259 if ext.lower() == '.exe':
259 if ext.lower() == '.exe':
260 fname = base
260 fname = base
261 try:
261 try:
262 # Removes dots from the name since ipython
262 # Removes dots from the name since ipython
263 # will assume names with dots to be python.
263 # will assume names with dots to be python.
264 self.shell.alias_manager.define_alias(
264 self.shell.alias_manager.define_alias(
265 base.lower().replace('.',''), fname)
265 base.lower().replace('.',''), fname)
266 except InvalidAliasError:
266 except InvalidAliasError:
267 pass
267 pass
268 syscmdlist.append(fname)
268 syscmdlist.append(fname)
269
269
270 self.shell.db['syscmdlist'] = syscmdlist
270 self.shell.db['syscmdlist'] = syscmdlist
271 finally:
271 finally:
272 os.chdir(savedir)
272 os.chdir(savedir)
273
273
274 @skip_doctest
274 @skip_doctest
275 @line_magic
275 @line_magic
276 def pwd(self, parameter_s=''):
276 def pwd(self, parameter_s=''):
277 """Return the current working directory path.
277 """Return the current working directory path.
278
278
279 Examples
279 Examples
280 --------
280 --------
281 ::
281 ::
282
282
283 In [9]: pwd
283 In [9]: pwd
284 Out[9]: '/home/tsuser/sprint/ipython'
284 Out[9]: '/home/tsuser/sprint/ipython'
285 """
285 """
286 try:
286 try:
287 return os.getcwd()
287 return os.getcwd()
288 except FileNotFoundError:
288 except FileNotFoundError:
289 raise UsageError("CWD no longer exists - please use %cd to change directory.")
289 raise UsageError("CWD no longer exists - please use %cd to change directory.")
290
290
291 @skip_doctest
291 @skip_doctest
292 @line_magic
292 @line_magic
293 def cd(self, parameter_s=''):
293 def cd(self, parameter_s=''):
294 """Change the current working directory.
294 """Change the current working directory.
295
295
296 This command automatically maintains an internal list of directories
296 This command automatically maintains an internal list of directories
297 you visit during your IPython session, in the variable _dh. The
297 you visit during your IPython session, in the variable _dh. The
298 command %dhist shows this history nicely formatted. You can also
298 command %dhist shows this history nicely formatted. You can also
299 do 'cd -<tab>' to see directory history conveniently.
299 do 'cd -<tab>' to see directory history conveniently.
300
300
301 Usage:
301 Usage:
302
302
303 cd 'dir': changes to directory 'dir'.
303 cd 'dir': changes to directory 'dir'.
304
304
305 cd -: changes to the last visited directory.
305 cd -: changes to the last visited directory.
306
306
307 cd -<n>: changes to the n-th directory in the directory history.
307 cd -<n>: changes to the n-th directory in the directory history.
308
308
309 cd --foo: change to directory that matches 'foo' in history
309 cd --foo: change to directory that matches 'foo' in history
310
310
311 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
311 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
312 (note: cd <bookmark_name> is enough if there is no
312 (note: cd <bookmark_name> is enough if there is no
313 directory <bookmark_name>, but a bookmark with the name exists.)
313 directory <bookmark_name>, but a bookmark with the name exists.)
314 'cd -b <tab>' allows you to tab-complete bookmark names.
314 'cd -b <tab>' allows you to tab-complete bookmark names.
315
315
316 Options:
316 Options:
317
317
318 -q: quiet. Do not print the working directory after the cd command is
318 -q: quiet. Do not print the working directory after the cd command is
319 executed. By default IPython's cd command does print this directory,
319 executed. By default IPython's cd command does print this directory,
320 since the default prompts do not display path information.
320 since the default prompts do not display path information.
321
321
322 Note that !cd doesn't work for this purpose because the shell where
322 Note that !cd doesn't work for this purpose because the shell where
323 !command runs is immediately discarded after executing 'command'.
323 !command runs is immediately discarded after executing 'command'.
324
324
325 Examples
325 Examples
326 --------
326 --------
327 ::
327 ::
328
328
329 In [10]: cd parent/child
329 In [10]: cd parent/child
330 /home/tsuser/parent/child
330 /home/tsuser/parent/child
331 """
331 """
332
332
333 try:
333 try:
334 oldcwd = os.getcwd()
334 oldcwd = os.getcwd()
335 except FileNotFoundError:
335 except FileNotFoundError:
336 # Happens if the CWD has been deleted.
336 # Happens if the CWD has been deleted.
337 oldcwd = None
337 oldcwd = None
338
338
339 numcd = re.match(r'(-)(\d+)$',parameter_s)
339 numcd = re.match(r'(-)(\d+)$',parameter_s)
340 # jump in directory history by number
340 # jump in directory history by number
341 if numcd:
341 if numcd:
342 nn = int(numcd.group(2))
342 nn = int(numcd.group(2))
343 try:
343 try:
344 ps = self.shell.user_ns['_dh'][nn]
344 ps = self.shell.user_ns['_dh'][nn]
345 except IndexError:
345 except IndexError:
346 print('The requested directory does not exist in history.')
346 print('The requested directory does not exist in history.')
347 return
347 return
348 else:
348 else:
349 opts = {}
349 opts = {}
350 elif parameter_s.startswith('--'):
350 elif parameter_s.startswith('--'):
351 ps = None
351 ps = None
352 fallback = None
352 fallback = None
353 pat = parameter_s[2:]
353 pat = parameter_s[2:]
354 dh = self.shell.user_ns['_dh']
354 dh = self.shell.user_ns['_dh']
355 # first search only by basename (last component)
355 # first search only by basename (last component)
356 for ent in reversed(dh):
356 for ent in reversed(dh):
357 if pat in os.path.basename(ent) and os.path.isdir(ent):
357 if pat in os.path.basename(ent) and os.path.isdir(ent):
358 ps = ent
358 ps = ent
359 break
359 break
360
360
361 if fallback is None and pat in ent and os.path.isdir(ent):
361 if fallback is None and pat in ent and os.path.isdir(ent):
362 fallback = ent
362 fallback = ent
363
363
364 # if we have no last part match, pick the first full path match
364 # if we have no last part match, pick the first full path match
365 if ps is None:
365 if ps is None:
366 ps = fallback
366 ps = fallback
367
367
368 if ps is None:
368 if ps is None:
369 print("No matching entry in directory history")
369 print("No matching entry in directory history")
370 return
370 return
371 else:
371 else:
372 opts = {}
372 opts = {}
373
373
374
374
375 else:
375 else:
376 opts, ps = self.parse_options(parameter_s, 'qb', mode='string')
376 opts, ps = self.parse_options(parameter_s, 'qb', mode='string')
377 # jump to previous
377 # jump to previous
378 if ps == '-':
378 if ps == '-':
379 try:
379 try:
380 ps = self.shell.user_ns['_dh'][-2]
380 ps = self.shell.user_ns['_dh'][-2]
381 except IndexError:
381 except IndexError:
382 raise UsageError('%cd -: No previous directory to change to.')
382 raise UsageError('%cd -: No previous directory to change to.')
383 # jump to bookmark if needed
383 # jump to bookmark if needed
384 else:
384 else:
385 if not os.path.isdir(ps) or 'b' in opts:
385 if not os.path.isdir(ps) or 'b' in opts:
386 bkms = self.shell.db.get('bookmarks', {})
386 bkms = self.shell.db.get('bookmarks', {})
387
387
388 if ps in bkms:
388 if ps in bkms:
389 target = bkms[ps]
389 target = bkms[ps]
390 print('(bookmark:%s) -> %s' % (ps, target))
390 print('(bookmark:%s) -> %s' % (ps, target))
391 ps = target
391 ps = target
392 else:
392 else:
393 if 'b' in opts:
393 if 'b' in opts:
394 raise UsageError("Bookmark '%s' not found. "
394 raise UsageError("Bookmark '%s' not found. "
395 "Use '%%bookmark -l' to see your bookmarks." % ps)
395 "Use '%%bookmark -l' to see your bookmarks." % ps)
396
396
397 # at this point ps should point to the target dir
397 # at this point ps should point to the target dir
398 if ps:
398 if ps:
399 try:
399 try:
400 os.chdir(os.path.expanduser(ps))
400 os.chdir(os.path.expanduser(ps))
401 if hasattr(self.shell, 'term_title') and self.shell.term_title:
401 if hasattr(self.shell, 'term_title') and self.shell.term_title:
402 set_term_title(self.shell.term_title_format.format(cwd=abbrev_cwd()))
402 set_term_title(self.shell.term_title_format.format(cwd=abbrev_cwd()))
403 except OSError:
403 except OSError:
404 print(sys.exc_info()[1])
404 print(sys.exc_info()[1])
405 else:
405 else:
406 cwd = os.getcwd()
406 cwd = os.getcwd()
407 dhist = self.shell.user_ns['_dh']
407 dhist = self.shell.user_ns['_dh']
408 if oldcwd != cwd:
408 if oldcwd != cwd:
409 dhist.append(cwd)
409 dhist.append(cwd)
410 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
410 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
411
411
412 else:
412 else:
413 os.chdir(self.shell.home_dir)
413 os.chdir(self.shell.home_dir)
414 if hasattr(self.shell, 'term_title') and self.shell.term_title:
414 if hasattr(self.shell, 'term_title') and self.shell.term_title:
415 set_term_title(self.shell.term_title_format.format(cwd="~"))
415 set_term_title(self.shell.term_title_format.format(cwd="~"))
416 cwd = os.getcwd()
416 cwd = os.getcwd()
417 dhist = self.shell.user_ns['_dh']
417 dhist = self.shell.user_ns['_dh']
418
418
419 if oldcwd != cwd:
419 if oldcwd != cwd:
420 dhist.append(cwd)
420 dhist.append(cwd)
421 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
421 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
422 if not 'q' in opts and not self.cd_force_quiet and self.shell.user_ns['_dh']:
422 if not 'q' in opts and not self.cd_force_quiet and self.shell.user_ns['_dh']:
423 print(self.shell.user_ns['_dh'][-1])
423 print(self.shell.user_ns['_dh'][-1])
424
424
425 @line_magic
425 @line_magic
426 def env(self, parameter_s=''):
426 def env(self, parameter_s=''):
427 """Get, set, or list environment variables.
427 """Get, set, or list environment variables.
428
428
429 Usage:\\
429 Usage:\\
430
430
431 %env: lists all environment variables/values
431 %env: lists all environment variables/values
432 %env var: get value for var
432 %env var: get value for var
433 %env var val: set value for var
433 %env var val: set value for var
434 %env var=val: set value for var
434 %env var=val: set value for var
435 %env var=$val: set value for var, using python expansion if possible
435 %env var=$val: set value for var, using python expansion if possible
436 """
436 """
437 if parameter_s.strip():
437 if parameter_s.strip():
438 split = '=' if '=' in parameter_s else ' '
438 split = '=' if '=' in parameter_s else ' '
439 bits = parameter_s.split(split)
439 bits = parameter_s.split(split)
440 if len(bits) == 1:
440 if len(bits) == 1:
441 key = parameter_s.strip()
441 key = parameter_s.strip()
442 if key in os.environ:
442 if key in os.environ:
443 return os.environ[key]
443 return os.environ[key]
444 else:
444 else:
445 err = "Environment does not have key: {0}".format(key)
445 err = "Environment does not have key: {0}".format(key)
446 raise UsageError(err)
446 raise UsageError(err)
447 if len(bits) > 1:
447 if len(bits) > 1:
448 return self.set_env(parameter_s)
448 return self.set_env(parameter_s)
449 return dict(os.environ)
449 env = dict(os.environ)
450 # hide likely secrets when printing the whole environment
451 for key in list(env):
452 if any(s in key.lower() for s in ('key', 'token', 'secret')):
453 env[key] = '<hidden>'
454
455 return env
450
456
451 @line_magic
457 @line_magic
452 def set_env(self, parameter_s):
458 def set_env(self, parameter_s):
453 """Set environment variables. Assumptions are that either "val" is a
459 """Set environment variables. Assumptions are that either "val" is a
454 name in the user namespace, or val is something that evaluates to a
460 name in the user namespace, or val is something that evaluates to a
455 string.
461 string.
456
462
457 Usage:\\
463 Usage:\\
458 %set_env var val: set value for var
464 %set_env var val: set value for var
459 %set_env var=val: set value for var
465 %set_env var=val: set value for var
460 %set_env var=$val: set value for var, using python expansion if possible
466 %set_env var=$val: set value for var, using python expansion if possible
461 """
467 """
462 split = '=' if '=' in parameter_s else ' '
468 split = '=' if '=' in parameter_s else ' '
463 bits = parameter_s.split(split, 1)
469 bits = parameter_s.split(split, 1)
464 if not parameter_s.strip() or len(bits)<2:
470 if not parameter_s.strip() or len(bits)<2:
465 raise UsageError("usage is 'set_env var=val'")
471 raise UsageError("usage is 'set_env var=val'")
466 var = bits[0].strip()
472 var = bits[0].strip()
467 val = bits[1].strip()
473 val = bits[1].strip()
468 if re.match(r'.*\s.*', var):
474 if re.match(r'.*\s.*', var):
469 # an environment variable with whitespace is almost certainly
475 # an environment variable with whitespace is almost certainly
470 # not what the user intended. what's more likely is the wrong
476 # not what the user intended. what's more likely is the wrong
471 # split was chosen, ie for "set_env cmd_args A=B", we chose
477 # split was chosen, ie for "set_env cmd_args A=B", we chose
472 # '=' for the split and should have chosen ' '. to get around
478 # '=' for the split and should have chosen ' '. to get around
473 # this, users should just assign directly to os.environ or use
479 # this, users should just assign directly to os.environ or use
474 # standard magic {var} expansion.
480 # standard magic {var} expansion.
475 err = "refusing to set env var with whitespace: '{0}'"
481 err = "refusing to set env var with whitespace: '{0}'"
476 err = err.format(val)
482 err = err.format(val)
477 raise UsageError(err)
483 raise UsageError(err)
478 os.environ[var] = val
484 os.environ[var] = val
479 print('env: {0}={1}'.format(var,val))
485 print('env: {0}={1}'.format(var,val))
480
486
481 @line_magic
487 @line_magic
482 def pushd(self, parameter_s=''):
488 def pushd(self, parameter_s=''):
483 """Place the current dir on stack and change directory.
489 """Place the current dir on stack and change directory.
484
490
485 Usage:\\
491 Usage:\\
486 %pushd ['dirname']
492 %pushd ['dirname']
487 """
493 """
488
494
489 dir_s = self.shell.dir_stack
495 dir_s = self.shell.dir_stack
490 tgt = os.path.expanduser(parameter_s)
496 tgt = os.path.expanduser(parameter_s)
491 cwd = os.getcwd().replace(self.shell.home_dir,'~')
497 cwd = os.getcwd().replace(self.shell.home_dir,'~')
492 if tgt:
498 if tgt:
493 self.cd(parameter_s)
499 self.cd(parameter_s)
494 dir_s.insert(0,cwd)
500 dir_s.insert(0,cwd)
495 return self.shell.magic('dirs')
501 return self.shell.magic('dirs')
496
502
497 @line_magic
503 @line_magic
498 def popd(self, parameter_s=''):
504 def popd(self, parameter_s=''):
499 """Change to directory popped off the top of the stack.
505 """Change to directory popped off the top of the stack.
500 """
506 """
501 if not self.shell.dir_stack:
507 if not self.shell.dir_stack:
502 raise UsageError("%popd on empty stack")
508 raise UsageError("%popd on empty stack")
503 top = self.shell.dir_stack.pop(0)
509 top = self.shell.dir_stack.pop(0)
504 self.cd(top)
510 self.cd(top)
505 print("popd ->",top)
511 print("popd ->",top)
506
512
507 @line_magic
513 @line_magic
508 def dirs(self, parameter_s=''):
514 def dirs(self, parameter_s=''):
509 """Return the current directory stack."""
515 """Return the current directory stack."""
510
516
511 return self.shell.dir_stack
517 return self.shell.dir_stack
512
518
513 @line_magic
519 @line_magic
514 def dhist(self, parameter_s=''):
520 def dhist(self, parameter_s=''):
515 """Print your history of visited directories.
521 """Print your history of visited directories.
516
522
517 %dhist -> print full history\\
523 %dhist -> print full history\\
518 %dhist n -> print last n entries only\\
524 %dhist n -> print last n entries only\\
519 %dhist n1 n2 -> print entries between n1 and n2 (n2 not included)\\
525 %dhist n1 n2 -> print entries between n1 and n2 (n2 not included)\\
520
526
521 This history is automatically maintained by the %cd command, and
527 This history is automatically maintained by the %cd command, and
522 always available as the global list variable _dh. You can use %cd -<n>
528 always available as the global list variable _dh. You can use %cd -<n>
523 to go to directory number <n>.
529 to go to directory number <n>.
524
530
525 Note that most of time, you should view directory history by entering
531 Note that most of time, you should view directory history by entering
526 cd -<TAB>.
532 cd -<TAB>.
527
533
528 """
534 """
529
535
530 dh = self.shell.user_ns['_dh']
536 dh = self.shell.user_ns['_dh']
531 if parameter_s:
537 if parameter_s:
532 try:
538 try:
533 args = map(int,parameter_s.split())
539 args = map(int,parameter_s.split())
534 except:
540 except:
535 self.arg_err(self.dhist)
541 self.arg_err(self.dhist)
536 return
542 return
537 if len(args) == 1:
543 if len(args) == 1:
538 ini,fin = max(len(dh)-(args[0]),0),len(dh)
544 ini,fin = max(len(dh)-(args[0]),0),len(dh)
539 elif len(args) == 2:
545 elif len(args) == 2:
540 ini,fin = args
546 ini,fin = args
541 fin = min(fin, len(dh))
547 fin = min(fin, len(dh))
542 else:
548 else:
543 self.arg_err(self.dhist)
549 self.arg_err(self.dhist)
544 return
550 return
545 else:
551 else:
546 ini,fin = 0,len(dh)
552 ini,fin = 0,len(dh)
547 print('Directory history (kept in _dh)')
553 print('Directory history (kept in _dh)')
548 for i in range(ini, fin):
554 for i in range(ini, fin):
549 print("%d: %s" % (i, dh[i]))
555 print("%d: %s" % (i, dh[i]))
550
556
551 @skip_doctest
557 @skip_doctest
552 @line_magic
558 @line_magic
553 def sc(self, parameter_s=''):
559 def sc(self, parameter_s=''):
554 """Shell capture - run shell command and capture output (DEPRECATED use !).
560 """Shell capture - run shell command and capture output (DEPRECATED use !).
555
561
556 DEPRECATED. Suboptimal, retained for backwards compatibility.
562 DEPRECATED. Suboptimal, retained for backwards compatibility.
557
563
558 You should use the form 'var = !command' instead. Example:
564 You should use the form 'var = !command' instead. Example:
559
565
560 "%sc -l myfiles = ls ~" should now be written as
566 "%sc -l myfiles = ls ~" should now be written as
561
567
562 "myfiles = !ls ~"
568 "myfiles = !ls ~"
563
569
564 myfiles.s, myfiles.l and myfiles.n still apply as documented
570 myfiles.s, myfiles.l and myfiles.n still apply as documented
565 below.
571 below.
566
572
567 --
573 --
568 %sc [options] varname=command
574 %sc [options] varname=command
569
575
570 IPython will run the given command using commands.getoutput(), and
576 IPython will run the given command using commands.getoutput(), and
571 will then update the user's interactive namespace with a variable
577 will then update the user's interactive namespace with a variable
572 called varname, containing the value of the call. Your command can
578 called varname, containing the value of the call. Your command can
573 contain shell wildcards, pipes, etc.
579 contain shell wildcards, pipes, etc.
574
580
575 The '=' sign in the syntax is mandatory, and the variable name you
581 The '=' sign in the syntax is mandatory, and the variable name you
576 supply must follow Python's standard conventions for valid names.
582 supply must follow Python's standard conventions for valid names.
577
583
578 (A special format without variable name exists for internal use)
584 (A special format without variable name exists for internal use)
579
585
580 Options:
586 Options:
581
587
582 -l: list output. Split the output on newlines into a list before
588 -l: list output. Split the output on newlines into a list before
583 assigning it to the given variable. By default the output is stored
589 assigning it to the given variable. By default the output is stored
584 as a single string.
590 as a single string.
585
591
586 -v: verbose. Print the contents of the variable.
592 -v: verbose. Print the contents of the variable.
587
593
588 In most cases you should not need to split as a list, because the
594 In most cases you should not need to split as a list, because the
589 returned value is a special type of string which can automatically
595 returned value is a special type of string which can automatically
590 provide its contents either as a list (split on newlines) or as a
596 provide its contents either as a list (split on newlines) or as a
591 space-separated string. These are convenient, respectively, either
597 space-separated string. These are convenient, respectively, either
592 for sequential processing or to be passed to a shell command.
598 for sequential processing or to be passed to a shell command.
593
599
594 For example::
600 For example::
595
601
596 # Capture into variable a
602 # Capture into variable a
597 In [1]: sc a=ls *py
603 In [1]: sc a=ls *py
598
604
599 # a is a string with embedded newlines
605 # a is a string with embedded newlines
600 In [2]: a
606 In [2]: a
601 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
607 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
602
608
603 # which can be seen as a list:
609 # which can be seen as a list:
604 In [3]: a.l
610 In [3]: a.l
605 Out[3]: ['setup.py', 'win32_manual_post_install.py']
611 Out[3]: ['setup.py', 'win32_manual_post_install.py']
606
612
607 # or as a whitespace-separated string:
613 # or as a whitespace-separated string:
608 In [4]: a.s
614 In [4]: a.s
609 Out[4]: 'setup.py win32_manual_post_install.py'
615 Out[4]: 'setup.py win32_manual_post_install.py'
610
616
611 # a.s is useful to pass as a single command line:
617 # a.s is useful to pass as a single command line:
612 In [5]: !wc -l $a.s
618 In [5]: !wc -l $a.s
613 146 setup.py
619 146 setup.py
614 130 win32_manual_post_install.py
620 130 win32_manual_post_install.py
615 276 total
621 276 total
616
622
617 # while the list form is useful to loop over:
623 # while the list form is useful to loop over:
618 In [6]: for f in a.l:
624 In [6]: for f in a.l:
619 ...: !wc -l $f
625 ...: !wc -l $f
620 ...:
626 ...:
621 146 setup.py
627 146 setup.py
622 130 win32_manual_post_install.py
628 130 win32_manual_post_install.py
623
629
624 Similarly, the lists returned by the -l option are also special, in
630 Similarly, the lists returned by the -l option are also special, in
625 the sense that you can equally invoke the .s attribute on them to
631 the sense that you can equally invoke the .s attribute on them to
626 automatically get a whitespace-separated string from their contents::
632 automatically get a whitespace-separated string from their contents::
627
633
628 In [7]: sc -l b=ls *py
634 In [7]: sc -l b=ls *py
629
635
630 In [8]: b
636 In [8]: b
631 Out[8]: ['setup.py', 'win32_manual_post_install.py']
637 Out[8]: ['setup.py', 'win32_manual_post_install.py']
632
638
633 In [9]: b.s
639 In [9]: b.s
634 Out[9]: 'setup.py win32_manual_post_install.py'
640 Out[9]: 'setup.py win32_manual_post_install.py'
635
641
636 In summary, both the lists and strings used for output capture have
642 In summary, both the lists and strings used for output capture have
637 the following special attributes::
643 the following special attributes::
638
644
639 .l (or .list) : value as list.
645 .l (or .list) : value as list.
640 .n (or .nlstr): value as newline-separated string.
646 .n (or .nlstr): value as newline-separated string.
641 .s (or .spstr): value as space-separated string.
647 .s (or .spstr): value as space-separated string.
642 """
648 """
643
649
644 opts,args = self.parse_options(parameter_s, 'lv')
650 opts,args = self.parse_options(parameter_s, 'lv')
645 # Try to get a variable name and command to run
651 # Try to get a variable name and command to run
646 try:
652 try:
647 # the variable name must be obtained from the parse_options
653 # the variable name must be obtained from the parse_options
648 # output, which uses shlex.split to strip options out.
654 # output, which uses shlex.split to strip options out.
649 var,_ = args.split('=', 1)
655 var,_ = args.split('=', 1)
650 var = var.strip()
656 var = var.strip()
651 # But the command has to be extracted from the original input
657 # But the command has to be extracted from the original input
652 # parameter_s, not on what parse_options returns, to avoid the
658 # parameter_s, not on what parse_options returns, to avoid the
653 # quote stripping which shlex.split performs on it.
659 # quote stripping which shlex.split performs on it.
654 _,cmd = parameter_s.split('=', 1)
660 _,cmd = parameter_s.split('=', 1)
655 except ValueError:
661 except ValueError:
656 var,cmd = '',''
662 var,cmd = '',''
657 # If all looks ok, proceed
663 # If all looks ok, proceed
658 split = 'l' in opts
664 split = 'l' in opts
659 out = self.shell.getoutput(cmd, split=split)
665 out = self.shell.getoutput(cmd, split=split)
660 if 'v' in opts:
666 if 'v' in opts:
661 print('%s ==\n%s' % (var, pformat(out)))
667 print('%s ==\n%s' % (var, pformat(out)))
662 if var:
668 if var:
663 self.shell.user_ns.update({var:out})
669 self.shell.user_ns.update({var:out})
664 else:
670 else:
665 return out
671 return out
666
672
667 @line_cell_magic
673 @line_cell_magic
668 def sx(self, line='', cell=None):
674 def sx(self, line='', cell=None):
669 """Shell execute - run shell command and capture output (!! is short-hand).
675 """Shell execute - run shell command and capture output (!! is short-hand).
670
676
671 %sx command
677 %sx command
672
678
673 IPython will run the given command using commands.getoutput(), and
679 IPython will run the given command using commands.getoutput(), and
674 return the result formatted as a list (split on '\\n'). Since the
680 return the result formatted as a list (split on '\\n'). Since the
675 output is _returned_, it will be stored in ipython's regular output
681 output is _returned_, it will be stored in ipython's regular output
676 cache Out[N] and in the '_N' automatic variables.
682 cache Out[N] and in the '_N' automatic variables.
677
683
678 Notes:
684 Notes:
679
685
680 1) If an input line begins with '!!', then %sx is automatically
686 1) If an input line begins with '!!', then %sx is automatically
681 invoked. That is, while::
687 invoked. That is, while::
682
688
683 !ls
689 !ls
684
690
685 causes ipython to simply issue system('ls'), typing::
691 causes ipython to simply issue system('ls'), typing::
686
692
687 !!ls
693 !!ls
688
694
689 is a shorthand equivalent to::
695 is a shorthand equivalent to::
690
696
691 %sx ls
697 %sx ls
692
698
693 2) %sx differs from %sc in that %sx automatically splits into a list,
699 2) %sx differs from %sc in that %sx automatically splits into a list,
694 like '%sc -l'. The reason for this is to make it as easy as possible
700 like '%sc -l'. The reason for this is to make it as easy as possible
695 to process line-oriented shell output via further python commands.
701 to process line-oriented shell output via further python commands.
696 %sc is meant to provide much finer control, but requires more
702 %sc is meant to provide much finer control, but requires more
697 typing.
703 typing.
698
704
699 3) Just like %sc -l, this is a list with special attributes:
705 3) Just like %sc -l, this is a list with special attributes:
700 ::
706 ::
701
707
702 .l (or .list) : value as list.
708 .l (or .list) : value as list.
703 .n (or .nlstr): value as newline-separated string.
709 .n (or .nlstr): value as newline-separated string.
704 .s (or .spstr): value as whitespace-separated string.
710 .s (or .spstr): value as whitespace-separated string.
705
711
706 This is very useful when trying to use such lists as arguments to
712 This is very useful when trying to use such lists as arguments to
707 system commands."""
713 system commands."""
708
714
709 if cell is None:
715 if cell is None:
710 # line magic
716 # line magic
711 return self.shell.getoutput(line)
717 return self.shell.getoutput(line)
712 else:
718 else:
713 opts,args = self.parse_options(line, '', 'out=')
719 opts,args = self.parse_options(line, '', 'out=')
714 output = self.shell.getoutput(cell)
720 output = self.shell.getoutput(cell)
715 out_name = opts.get('out', opts.get('o'))
721 out_name = opts.get('out', opts.get('o'))
716 if out_name:
722 if out_name:
717 self.shell.user_ns[out_name] = output
723 self.shell.user_ns[out_name] = output
718 else:
724 else:
719 return output
725 return output
720
726
721 system = line_cell_magic('system')(sx)
727 system = line_cell_magic('system')(sx)
722 bang = cell_magic('!')(sx)
728 bang = cell_magic('!')(sx)
723
729
724 @line_magic
730 @line_magic
725 def bookmark(self, parameter_s=''):
731 def bookmark(self, parameter_s=''):
726 """Manage IPython's bookmark system.
732 """Manage IPython's bookmark system.
727
733
728 %bookmark <name> - set bookmark to current dir
734 %bookmark <name> - set bookmark to current dir
729 %bookmark <name> <dir> - set bookmark to <dir>
735 %bookmark <name> <dir> - set bookmark to <dir>
730 %bookmark -l - list all bookmarks
736 %bookmark -l - list all bookmarks
731 %bookmark -d <name> - remove bookmark
737 %bookmark -d <name> - remove bookmark
732 %bookmark -r - remove all bookmarks
738 %bookmark -r - remove all bookmarks
733
739
734 You can later on access a bookmarked folder with::
740 You can later on access a bookmarked folder with::
735
741
736 %cd -b <name>
742 %cd -b <name>
737
743
738 or simply '%cd <name>' if there is no directory called <name> AND
744 or simply '%cd <name>' if there is no directory called <name> AND
739 there is such a bookmark defined.
745 there is such a bookmark defined.
740
746
741 Your bookmarks persist through IPython sessions, but they are
747 Your bookmarks persist through IPython sessions, but they are
742 associated with each profile."""
748 associated with each profile."""
743
749
744 opts,args = self.parse_options(parameter_s,'drl',mode='list')
750 opts,args = self.parse_options(parameter_s,'drl',mode='list')
745 if len(args) > 2:
751 if len(args) > 2:
746 raise UsageError("%bookmark: too many arguments")
752 raise UsageError("%bookmark: too many arguments")
747
753
748 bkms = self.shell.db.get('bookmarks',{})
754 bkms = self.shell.db.get('bookmarks',{})
749
755
750 if 'd' in opts:
756 if 'd' in opts:
751 try:
757 try:
752 todel = args[0]
758 todel = args[0]
753 except IndexError:
759 except IndexError:
754 raise UsageError(
760 raise UsageError(
755 "%bookmark -d: must provide a bookmark to delete")
761 "%bookmark -d: must provide a bookmark to delete")
756 else:
762 else:
757 try:
763 try:
758 del bkms[todel]
764 del bkms[todel]
759 except KeyError:
765 except KeyError:
760 raise UsageError(
766 raise UsageError(
761 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
767 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
762
768
763 elif 'r' in opts:
769 elif 'r' in opts:
764 bkms = {}
770 bkms = {}
765 elif 'l' in opts:
771 elif 'l' in opts:
766 bks = sorted(bkms)
772 bks = sorted(bkms)
767 if bks:
773 if bks:
768 size = max(map(len, bks))
774 size = max(map(len, bks))
769 else:
775 else:
770 size = 0
776 size = 0
771 fmt = '%-'+str(size)+'s -> %s'
777 fmt = '%-'+str(size)+'s -> %s'
772 print('Current bookmarks:')
778 print('Current bookmarks:')
773 for bk in bks:
779 for bk in bks:
774 print(fmt % (bk, bkms[bk]))
780 print(fmt % (bk, bkms[bk]))
775 else:
781 else:
776 if not args:
782 if not args:
777 raise UsageError("%bookmark: You must specify the bookmark name")
783 raise UsageError("%bookmark: You must specify the bookmark name")
778 elif len(args)==1:
784 elif len(args)==1:
779 bkms[args[0]] = os.getcwd()
785 bkms[args[0]] = os.getcwd()
780 elif len(args)==2:
786 elif len(args)==2:
781 bkms[args[0]] = args[1]
787 bkms[args[0]] = args[1]
782 self.shell.db['bookmarks'] = bkms
788 self.shell.db['bookmarks'] = bkms
783
789
784 @line_magic
790 @line_magic
785 def pycat(self, parameter_s=''):
791 def pycat(self, parameter_s=''):
786 """Show a syntax-highlighted file through a pager.
792 """Show a syntax-highlighted file through a pager.
787
793
788 This magic is similar to the cat utility, but it will assume the file
794 This magic is similar to the cat utility, but it will assume the file
789 to be Python source and will show it with syntax highlighting.
795 to be Python source and will show it with syntax highlighting.
790
796
791 This magic command can either take a local filename, an url,
797 This magic command can either take a local filename, an url,
792 an history range (see %history) or a macro as argument ::
798 an history range (see %history) or a macro as argument ::
793
799
794 %pycat myscript.py
800 %pycat myscript.py
795 %pycat 7-27
801 %pycat 7-27
796 %pycat myMacro
802 %pycat myMacro
797 %pycat http://www.example.com/myscript.py
803 %pycat http://www.example.com/myscript.py
798 """
804 """
799 if not parameter_s:
805 if not parameter_s:
800 raise UsageError('Missing filename, URL, input history range, '
806 raise UsageError('Missing filename, URL, input history range, '
801 'or macro.')
807 'or macro.')
802
808
803 try :
809 try :
804 cont = self.shell.find_user_code(parameter_s, skip_encoding_cookie=False)
810 cont = self.shell.find_user_code(parameter_s, skip_encoding_cookie=False)
805 except (ValueError, IOError):
811 except (ValueError, IOError):
806 print("Error: no such file, variable, URL, history range or macro")
812 print("Error: no such file, variable, URL, history range or macro")
807 return
813 return
808
814
809 page.page(self.shell.pycolorize(source_to_unicode(cont)))
815 page.page(self.shell.pycolorize(source_to_unicode(cont)))
810
816
811 @magic_arguments.magic_arguments()
817 @magic_arguments.magic_arguments()
812 @magic_arguments.argument(
818 @magic_arguments.argument(
813 '-a', '--append', action='store_true', default=False,
819 '-a', '--append', action='store_true', default=False,
814 help='Append contents of the cell to an existing file. '
820 help='Append contents of the cell to an existing file. '
815 'The file will be created if it does not exist.'
821 'The file will be created if it does not exist.'
816 )
822 )
817 @magic_arguments.argument(
823 @magic_arguments.argument(
818 'filename', type=str,
824 'filename', type=str,
819 help='file to write'
825 help='file to write'
820 )
826 )
821 @cell_magic
827 @cell_magic
822 def writefile(self, line, cell):
828 def writefile(self, line, cell):
823 """Write the contents of the cell to a file.
829 """Write the contents of the cell to a file.
824
830
825 The file will be overwritten unless the -a (--append) flag is specified.
831 The file will be overwritten unless the -a (--append) flag is specified.
826 """
832 """
827 args = magic_arguments.parse_argstring(self.writefile, line)
833 args = magic_arguments.parse_argstring(self.writefile, line)
828 if re.match(r'^(\'.*\')|(".*")$', args.filename):
834 if re.match(r'^(\'.*\')|(".*")$', args.filename):
829 filename = os.path.expanduser(args.filename[1:-1])
835 filename = os.path.expanduser(args.filename[1:-1])
830 else:
836 else:
831 filename = os.path.expanduser(args.filename)
837 filename = os.path.expanduser(args.filename)
832
838
833 if os.path.exists(filename):
839 if os.path.exists(filename):
834 if args.append:
840 if args.append:
835 print("Appending to %s" % filename)
841 print("Appending to %s" % filename)
836 else:
842 else:
837 print("Overwriting %s" % filename)
843 print("Overwriting %s" % filename)
838 else:
844 else:
839 print("Writing %s" % filename)
845 print("Writing %s" % filename)
840
846
841 mode = 'a' if args.append else 'w'
847 mode = 'a' if args.append else 'w'
842 with io.open(filename, mode, encoding='utf-8') as f:
848 with io.open(filename, mode, encoding='utf-8') as f:
843 f.write(cell)
849 f.write(cell)
@@ -1,1204 +1,1223 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tests for various magic functions.
2 """Tests for various magic functions.
3
3
4 Needs to be run by nose (to make ipython session available).
4 Needs to be run by nose (to make ipython session available).
5 """
5 """
6
6
7 import io
7 import io
8 import os
8 import os
9 import re
9 import re
10 import sys
10 import sys
11 import warnings
11 import warnings
12 from textwrap import dedent
12 from textwrap import dedent
13 from unittest import TestCase
13 from unittest import TestCase
14 from unittest import mock
14 from importlib import invalidate_caches
15 from importlib import invalidate_caches
15 from io import StringIO
16 from io import StringIO
16
17
17 import nose.tools as nt
18 import nose.tools as nt
18
19
19 import shlex
20 import shlex
20
21
21 from IPython import get_ipython
22 from IPython import get_ipython
22 from IPython.core import magic
23 from IPython.core import magic
23 from IPython.core.error import UsageError
24 from IPython.core.error import UsageError
24 from IPython.core.magic import (Magics, magics_class, line_magic,
25 from IPython.core.magic import (Magics, magics_class, line_magic,
25 cell_magic,
26 cell_magic,
26 register_line_magic, register_cell_magic)
27 register_line_magic, register_cell_magic)
27 from IPython.core.magics import execution, script, code, logging, osm
28 from IPython.core.magics import execution, script, code, logging, osm
28 from IPython.testing import decorators as dec
29 from IPython.testing import decorators as dec
29 from IPython.testing import tools as tt
30 from IPython.testing import tools as tt
30 from IPython.utils.io import capture_output
31 from IPython.utils.io import capture_output
31 from IPython.utils.tempdir import (TemporaryDirectory,
32 from IPython.utils.tempdir import (TemporaryDirectory,
32 TemporaryWorkingDirectory)
33 TemporaryWorkingDirectory)
33 from IPython.utils.process import find_cmd
34 from IPython.utils.process import find_cmd
34
35
35
36
36 @magic.magics_class
37 @magic.magics_class
37 class DummyMagics(magic.Magics): pass
38 class DummyMagics(magic.Magics): pass
38
39
39 def test_extract_code_ranges():
40 def test_extract_code_ranges():
40 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
41 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
41 expected = [(0, 1),
42 expected = [(0, 1),
42 (2, 3),
43 (2, 3),
43 (4, 6),
44 (4, 6),
44 (6, 9),
45 (6, 9),
45 (9, 14),
46 (9, 14),
46 (16, None),
47 (16, None),
47 (None, 9),
48 (None, 9),
48 (9, None),
49 (9, None),
49 (None, 13),
50 (None, 13),
50 (None, None)]
51 (None, None)]
51 actual = list(code.extract_code_ranges(instr))
52 actual = list(code.extract_code_ranges(instr))
52 nt.assert_equal(actual, expected)
53 nt.assert_equal(actual, expected)
53
54
54 def test_extract_symbols():
55 def test_extract_symbols():
55 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
56 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
56 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
57 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
57 expected = [([], ['a']),
58 expected = [([], ['a']),
58 (["def b():\n return 42\n"], []),
59 (["def b():\n return 42\n"], []),
59 (["class A: pass\n"], []),
60 (["class A: pass\n"], []),
60 (["class A: pass\n", "def b():\n return 42\n"], []),
61 (["class A: pass\n", "def b():\n return 42\n"], []),
61 (["class A: pass\n"], ['a']),
62 (["class A: pass\n"], ['a']),
62 ([], ['z'])]
63 ([], ['z'])]
63 for symbols, exp in zip(symbols_args, expected):
64 for symbols, exp in zip(symbols_args, expected):
64 nt.assert_equal(code.extract_symbols(source, symbols), exp)
65 nt.assert_equal(code.extract_symbols(source, symbols), exp)
65
66
66
67
67 def test_extract_symbols_raises_exception_with_non_python_code():
68 def test_extract_symbols_raises_exception_with_non_python_code():
68 source = ("=begin A Ruby program :)=end\n"
69 source = ("=begin A Ruby program :)=end\n"
69 "def hello\n"
70 "def hello\n"
70 "puts 'Hello world'\n"
71 "puts 'Hello world'\n"
71 "end")
72 "end")
72 with nt.assert_raises(SyntaxError):
73 with nt.assert_raises(SyntaxError):
73 code.extract_symbols(source, "hello")
74 code.extract_symbols(source, "hello")
74
75
75
76
76 def test_magic_not_found():
77 def test_magic_not_found():
77 # magic not found raises UsageError
78 # magic not found raises UsageError
78 with nt.assert_raises(UsageError):
79 with nt.assert_raises(UsageError):
79 _ip.magic('doesntexist')
80 _ip.magic('doesntexist')
80
81
81 # ensure result isn't success when a magic isn't found
82 # ensure result isn't success when a magic isn't found
82 result = _ip.run_cell('%doesntexist')
83 result = _ip.run_cell('%doesntexist')
83 assert isinstance(result.error_in_exec, UsageError)
84 assert isinstance(result.error_in_exec, UsageError)
84
85
85
86
86 def test_cell_magic_not_found():
87 def test_cell_magic_not_found():
87 # magic not found raises UsageError
88 # magic not found raises UsageError
88 with nt.assert_raises(UsageError):
89 with nt.assert_raises(UsageError):
89 _ip.run_cell_magic('doesntexist', 'line', 'cell')
90 _ip.run_cell_magic('doesntexist', 'line', 'cell')
90
91
91 # ensure result isn't success when a magic isn't found
92 # ensure result isn't success when a magic isn't found
92 result = _ip.run_cell('%%doesntexist')
93 result = _ip.run_cell('%%doesntexist')
93 assert isinstance(result.error_in_exec, UsageError)
94 assert isinstance(result.error_in_exec, UsageError)
94
95
95
96
96 def test_magic_error_status():
97 def test_magic_error_status():
97 def fail(shell):
98 def fail(shell):
98 1/0
99 1/0
99 _ip.register_magic_function(fail)
100 _ip.register_magic_function(fail)
100 result = _ip.run_cell('%fail')
101 result = _ip.run_cell('%fail')
101 assert isinstance(result.error_in_exec, ZeroDivisionError)
102 assert isinstance(result.error_in_exec, ZeroDivisionError)
102
103
103
104
104 def test_config():
105 def test_config():
105 """ test that config magic does not raise
106 """ test that config magic does not raise
106 can happen if Configurable init is moved too early into
107 can happen if Configurable init is moved too early into
107 Magics.__init__ as then a Config object will be registered as a
108 Magics.__init__ as then a Config object will be registered as a
108 magic.
109 magic.
109 """
110 """
110 ## should not raise.
111 ## should not raise.
111 _ip.magic('config')
112 _ip.magic('config')
112
113
113 def test_config_available_configs():
114 def test_config_available_configs():
114 """ test that config magic prints available configs in unique and
115 """ test that config magic prints available configs in unique and
115 sorted order. """
116 sorted order. """
116 with capture_output() as captured:
117 with capture_output() as captured:
117 _ip.magic('config')
118 _ip.magic('config')
118
119
119 stdout = captured.stdout
120 stdout = captured.stdout
120 config_classes = stdout.strip().split('\n')[1:]
121 config_classes = stdout.strip().split('\n')[1:]
121 nt.assert_list_equal(config_classes, sorted(set(config_classes)))
122 nt.assert_list_equal(config_classes, sorted(set(config_classes)))
122
123
123 def test_config_print_class():
124 def test_config_print_class():
124 """ test that config with a classname prints the class's options. """
125 """ test that config with a classname prints the class's options. """
125 with capture_output() as captured:
126 with capture_output() as captured:
126 _ip.magic('config TerminalInteractiveShell')
127 _ip.magic('config TerminalInteractiveShell')
127
128
128 stdout = captured.stdout
129 stdout = captured.stdout
129 if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]):
130 if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]):
130 print(stdout)
131 print(stdout)
131 raise AssertionError("1st line of stdout not like "
132 raise AssertionError("1st line of stdout not like "
132 "'TerminalInteractiveShell.* options'")
133 "'TerminalInteractiveShell.* options'")
133
134
134 def test_rehashx():
135 def test_rehashx():
135 # clear up everything
136 # clear up everything
136 _ip.alias_manager.clear_aliases()
137 _ip.alias_manager.clear_aliases()
137 del _ip.db['syscmdlist']
138 del _ip.db['syscmdlist']
138
139
139 _ip.magic('rehashx')
140 _ip.magic('rehashx')
140 # Practically ALL ipython development systems will have more than 10 aliases
141 # Practically ALL ipython development systems will have more than 10 aliases
141
142
142 nt.assert_true(len(_ip.alias_manager.aliases) > 10)
143 nt.assert_true(len(_ip.alias_manager.aliases) > 10)
143 for name, cmd in _ip.alias_manager.aliases:
144 for name, cmd in _ip.alias_manager.aliases:
144 # we must strip dots from alias names
145 # we must strip dots from alias names
145 nt.assert_not_in('.', name)
146 nt.assert_not_in('.', name)
146
147
147 # rehashx must fill up syscmdlist
148 # rehashx must fill up syscmdlist
148 scoms = _ip.db['syscmdlist']
149 scoms = _ip.db['syscmdlist']
149 nt.assert_true(len(scoms) > 10)
150 nt.assert_true(len(scoms) > 10)
150
151
151
152
152
153
153 def test_magic_parse_options():
154 def test_magic_parse_options():
154 """Test that we don't mangle paths when parsing magic options."""
155 """Test that we don't mangle paths when parsing magic options."""
155 ip = get_ipython()
156 ip = get_ipython()
156 path = 'c:\\x'
157 path = 'c:\\x'
157 m = DummyMagics(ip)
158 m = DummyMagics(ip)
158 opts = m.parse_options('-f %s' % path,'f:')[0]
159 opts = m.parse_options('-f %s' % path,'f:')[0]
159 # argv splitting is os-dependent
160 # argv splitting is os-dependent
160 if os.name == 'posix':
161 if os.name == 'posix':
161 expected = 'c:x'
162 expected = 'c:x'
162 else:
163 else:
163 expected = path
164 expected = path
164 nt.assert_equal(opts['f'], expected)
165 nt.assert_equal(opts['f'], expected)
165
166
166 def test_magic_parse_long_options():
167 def test_magic_parse_long_options():
167 """Magic.parse_options can handle --foo=bar long options"""
168 """Magic.parse_options can handle --foo=bar long options"""
168 ip = get_ipython()
169 ip = get_ipython()
169 m = DummyMagics(ip)
170 m = DummyMagics(ip)
170 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
171 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
171 nt.assert_in('foo', opts)
172 nt.assert_in('foo', opts)
172 nt.assert_in('bar', opts)
173 nt.assert_in('bar', opts)
173 nt.assert_equal(opts['bar'], "bubble")
174 nt.assert_equal(opts['bar'], "bubble")
174
175
175
176
176 @dec.skip_without('sqlite3')
177 @dec.skip_without('sqlite3')
177 def doctest_hist_f():
178 def doctest_hist_f():
178 """Test %hist -f with temporary filename.
179 """Test %hist -f with temporary filename.
179
180
180 In [9]: import tempfile
181 In [9]: import tempfile
181
182
182 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
183 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
183
184
184 In [11]: %hist -nl -f $tfile 3
185 In [11]: %hist -nl -f $tfile 3
185
186
186 In [13]: import os; os.unlink(tfile)
187 In [13]: import os; os.unlink(tfile)
187 """
188 """
188
189
189
190
190 @dec.skip_without('sqlite3')
191 @dec.skip_without('sqlite3')
191 def doctest_hist_r():
192 def doctest_hist_r():
192 """Test %hist -r
193 """Test %hist -r
193
194
194 XXX - This test is not recording the output correctly. For some reason, in
195 XXX - This test is not recording the output correctly. For some reason, in
195 testing mode the raw history isn't getting populated. No idea why.
196 testing mode the raw history isn't getting populated. No idea why.
196 Disabling the output checking for now, though at least we do run it.
197 Disabling the output checking for now, though at least we do run it.
197
198
198 In [1]: 'hist' in _ip.lsmagic()
199 In [1]: 'hist' in _ip.lsmagic()
199 Out[1]: True
200 Out[1]: True
200
201
201 In [2]: x=1
202 In [2]: x=1
202
203
203 In [3]: %hist -rl 2
204 In [3]: %hist -rl 2
204 x=1 # random
205 x=1 # random
205 %hist -r 2
206 %hist -r 2
206 """
207 """
207
208
208
209
209 @dec.skip_without('sqlite3')
210 @dec.skip_without('sqlite3')
210 def doctest_hist_op():
211 def doctest_hist_op():
211 """Test %hist -op
212 """Test %hist -op
212
213
213 In [1]: class b(float):
214 In [1]: class b(float):
214 ...: pass
215 ...: pass
215 ...:
216 ...:
216
217
217 In [2]: class s(object):
218 In [2]: class s(object):
218 ...: def __str__(self):
219 ...: def __str__(self):
219 ...: return 's'
220 ...: return 's'
220 ...:
221 ...:
221
222
222 In [3]:
223 In [3]:
223
224
224 In [4]: class r(b):
225 In [4]: class r(b):
225 ...: def __repr__(self):
226 ...: def __repr__(self):
226 ...: return 'r'
227 ...: return 'r'
227 ...:
228 ...:
228
229
229 In [5]: class sr(s,r): pass
230 In [5]: class sr(s,r): pass
230 ...:
231 ...:
231
232
232 In [6]:
233 In [6]:
233
234
234 In [7]: bb=b()
235 In [7]: bb=b()
235
236
236 In [8]: ss=s()
237 In [8]: ss=s()
237
238
238 In [9]: rr=r()
239 In [9]: rr=r()
239
240
240 In [10]: ssrr=sr()
241 In [10]: ssrr=sr()
241
242
242 In [11]: 4.5
243 In [11]: 4.5
243 Out[11]: 4.5
244 Out[11]: 4.5
244
245
245 In [12]: str(ss)
246 In [12]: str(ss)
246 Out[12]: 's'
247 Out[12]: 's'
247
248
248 In [13]:
249 In [13]:
249
250
250 In [14]: %hist -op
251 In [14]: %hist -op
251 >>> class b:
252 >>> class b:
252 ... pass
253 ... pass
253 ...
254 ...
254 >>> class s(b):
255 >>> class s(b):
255 ... def __str__(self):
256 ... def __str__(self):
256 ... return 's'
257 ... return 's'
257 ...
258 ...
258 >>>
259 >>>
259 >>> class r(b):
260 >>> class r(b):
260 ... def __repr__(self):
261 ... def __repr__(self):
261 ... return 'r'
262 ... return 'r'
262 ...
263 ...
263 >>> class sr(s,r): pass
264 >>> class sr(s,r): pass
264 >>>
265 >>>
265 >>> bb=b()
266 >>> bb=b()
266 >>> ss=s()
267 >>> ss=s()
267 >>> rr=r()
268 >>> rr=r()
268 >>> ssrr=sr()
269 >>> ssrr=sr()
269 >>> 4.5
270 >>> 4.5
270 4.5
271 4.5
271 >>> str(ss)
272 >>> str(ss)
272 's'
273 's'
273 >>>
274 >>>
274 """
275 """
275
276
276 def test_hist_pof():
277 def test_hist_pof():
277 ip = get_ipython()
278 ip = get_ipython()
278 ip.run_cell(u"1+2", store_history=True)
279 ip.run_cell(u"1+2", store_history=True)
279 #raise Exception(ip.history_manager.session_number)
280 #raise Exception(ip.history_manager.session_number)
280 #raise Exception(list(ip.history_manager._get_range_session()))
281 #raise Exception(list(ip.history_manager._get_range_session()))
281 with TemporaryDirectory() as td:
282 with TemporaryDirectory() as td:
282 tf = os.path.join(td, 'hist.py')
283 tf = os.path.join(td, 'hist.py')
283 ip.run_line_magic('history', '-pof %s' % tf)
284 ip.run_line_magic('history', '-pof %s' % tf)
284 assert os.path.isfile(tf)
285 assert os.path.isfile(tf)
285
286
286
287
287 @dec.skip_without('sqlite3')
288 @dec.skip_without('sqlite3')
288 def test_macro():
289 def test_macro():
289 ip = get_ipython()
290 ip = get_ipython()
290 ip.history_manager.reset() # Clear any existing history.
291 ip.history_manager.reset() # Clear any existing history.
291 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
292 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
292 for i, cmd in enumerate(cmds, start=1):
293 for i, cmd in enumerate(cmds, start=1):
293 ip.history_manager.store_inputs(i, cmd)
294 ip.history_manager.store_inputs(i, cmd)
294 ip.magic("macro test 1-3")
295 ip.magic("macro test 1-3")
295 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
296 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
296
297
297 # List macros
298 # List macros
298 nt.assert_in("test", ip.magic("macro"))
299 nt.assert_in("test", ip.magic("macro"))
299
300
300
301
301 @dec.skip_without('sqlite3')
302 @dec.skip_without('sqlite3')
302 def test_macro_run():
303 def test_macro_run():
303 """Test that we can run a multi-line macro successfully."""
304 """Test that we can run a multi-line macro successfully."""
304 ip = get_ipython()
305 ip = get_ipython()
305 ip.history_manager.reset()
306 ip.history_manager.reset()
306 cmds = ["a=10", "a+=1", "print(a)", "%macro test 2-3"]
307 cmds = ["a=10", "a+=1", "print(a)", "%macro test 2-3"]
307 for cmd in cmds:
308 for cmd in cmds:
308 ip.run_cell(cmd, store_history=True)
309 ip.run_cell(cmd, store_history=True)
309 nt.assert_equal(ip.user_ns["test"].value, "a+=1\nprint(a)\n")
310 nt.assert_equal(ip.user_ns["test"].value, "a+=1\nprint(a)\n")
310 with tt.AssertPrints("12"):
311 with tt.AssertPrints("12"):
311 ip.run_cell("test")
312 ip.run_cell("test")
312 with tt.AssertPrints("13"):
313 with tt.AssertPrints("13"):
313 ip.run_cell("test")
314 ip.run_cell("test")
314
315
315
316
316 def test_magic_magic():
317 def test_magic_magic():
317 """Test %magic"""
318 """Test %magic"""
318 ip = get_ipython()
319 ip = get_ipython()
319 with capture_output() as captured:
320 with capture_output() as captured:
320 ip.magic("magic")
321 ip.magic("magic")
321
322
322 stdout = captured.stdout
323 stdout = captured.stdout
323 nt.assert_in('%magic', stdout)
324 nt.assert_in('%magic', stdout)
324 nt.assert_in('IPython', stdout)
325 nt.assert_in('IPython', stdout)
325 nt.assert_in('Available', stdout)
326 nt.assert_in('Available', stdout)
326
327
327
328
328 @dec.skipif_not_numpy
329 @dec.skipif_not_numpy
329 def test_numpy_reset_array_undec():
330 def test_numpy_reset_array_undec():
330 "Test '%reset array' functionality"
331 "Test '%reset array' functionality"
331 _ip.ex('import numpy as np')
332 _ip.ex('import numpy as np')
332 _ip.ex('a = np.empty(2)')
333 _ip.ex('a = np.empty(2)')
333 nt.assert_in('a', _ip.user_ns)
334 nt.assert_in('a', _ip.user_ns)
334 _ip.magic('reset -f array')
335 _ip.magic('reset -f array')
335 nt.assert_not_in('a', _ip.user_ns)
336 nt.assert_not_in('a', _ip.user_ns)
336
337
337 def test_reset_out():
338 def test_reset_out():
338 "Test '%reset out' magic"
339 "Test '%reset out' magic"
339 _ip.run_cell("parrot = 'dead'", store_history=True)
340 _ip.run_cell("parrot = 'dead'", store_history=True)
340 # test '%reset -f out', make an Out prompt
341 # test '%reset -f out', make an Out prompt
341 _ip.run_cell("parrot", store_history=True)
342 _ip.run_cell("parrot", store_history=True)
342 nt.assert_true('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
343 nt.assert_true('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
343 _ip.magic('reset -f out')
344 _ip.magic('reset -f out')
344 nt.assert_false('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
345 nt.assert_false('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
345 nt.assert_equal(len(_ip.user_ns['Out']), 0)
346 nt.assert_equal(len(_ip.user_ns['Out']), 0)
346
347
347 def test_reset_in():
348 def test_reset_in():
348 "Test '%reset in' magic"
349 "Test '%reset in' magic"
349 # test '%reset -f in'
350 # test '%reset -f in'
350 _ip.run_cell("parrot", store_history=True)
351 _ip.run_cell("parrot", store_history=True)
351 nt.assert_true('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
352 nt.assert_true('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
352 _ip.magic('%reset -f in')
353 _ip.magic('%reset -f in')
353 nt.assert_false('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
354 nt.assert_false('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
354 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
355 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
355
356
356 def test_reset_dhist():
357 def test_reset_dhist():
357 "Test '%reset dhist' magic"
358 "Test '%reset dhist' magic"
358 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
359 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
359 _ip.magic('cd ' + os.path.dirname(nt.__file__))
360 _ip.magic('cd ' + os.path.dirname(nt.__file__))
360 _ip.magic('cd -')
361 _ip.magic('cd -')
361 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
362 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
362 _ip.magic('reset -f dhist')
363 _ip.magic('reset -f dhist')
363 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
364 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
364 _ip.run_cell("_dh = [d for d in tmp]") #restore
365 _ip.run_cell("_dh = [d for d in tmp]") #restore
365
366
366 def test_reset_in_length():
367 def test_reset_in_length():
367 "Test that '%reset in' preserves In[] length"
368 "Test that '%reset in' preserves In[] length"
368 _ip.run_cell("print 'foo'")
369 _ip.run_cell("print 'foo'")
369 _ip.run_cell("reset -f in")
370 _ip.run_cell("reset -f in")
370 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
371 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
371
372
372 class TestResetErrors(TestCase):
373 class TestResetErrors(TestCase):
373
374
374 def test_reset_redefine(self):
375 def test_reset_redefine(self):
375
376
376 @magics_class
377 @magics_class
377 class KernelMagics(Magics):
378 class KernelMagics(Magics):
378 @line_magic
379 @line_magic
379 def less(self, shell): pass
380 def less(self, shell): pass
380
381
381 _ip.register_magics(KernelMagics)
382 _ip.register_magics(KernelMagics)
382
383
383 with self.assertLogs() as cm:
384 with self.assertLogs() as cm:
384 # hack, we want to just capture logs, but assertLogs fails if not
385 # hack, we want to just capture logs, but assertLogs fails if not
385 # logs get produce.
386 # logs get produce.
386 # so log one things we ignore.
387 # so log one things we ignore.
387 import logging as log_mod
388 import logging as log_mod
388 log = log_mod.getLogger()
389 log = log_mod.getLogger()
389 log.info('Nothing')
390 log.info('Nothing')
390 # end hack.
391 # end hack.
391 _ip.run_cell("reset -f")
392 _ip.run_cell("reset -f")
392
393
393 assert len(cm.output) == 1
394 assert len(cm.output) == 1
394 for out in cm.output:
395 for out in cm.output:
395 assert "Invalid alias" not in out
396 assert "Invalid alias" not in out
396
397
397 def test_tb_syntaxerror():
398 def test_tb_syntaxerror():
398 """test %tb after a SyntaxError"""
399 """test %tb after a SyntaxError"""
399 ip = get_ipython()
400 ip = get_ipython()
400 ip.run_cell("for")
401 ip.run_cell("for")
401
402
402 # trap and validate stdout
403 # trap and validate stdout
403 save_stdout = sys.stdout
404 save_stdout = sys.stdout
404 try:
405 try:
405 sys.stdout = StringIO()
406 sys.stdout = StringIO()
406 ip.run_cell("%tb")
407 ip.run_cell("%tb")
407 out = sys.stdout.getvalue()
408 out = sys.stdout.getvalue()
408 finally:
409 finally:
409 sys.stdout = save_stdout
410 sys.stdout = save_stdout
410 # trim output, and only check the last line
411 # trim output, and only check the last line
411 last_line = out.rstrip().splitlines()[-1].strip()
412 last_line = out.rstrip().splitlines()[-1].strip()
412 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
413 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
413
414
414
415
415 def test_time():
416 def test_time():
416 ip = get_ipython()
417 ip = get_ipython()
417
418
418 with tt.AssertPrints("Wall time: "):
419 with tt.AssertPrints("Wall time: "):
419 ip.run_cell("%time None")
420 ip.run_cell("%time None")
420
421
421 ip.run_cell("def f(kmjy):\n"
422 ip.run_cell("def f(kmjy):\n"
422 " %time print (2*kmjy)")
423 " %time print (2*kmjy)")
423
424
424 with tt.AssertPrints("Wall time: "):
425 with tt.AssertPrints("Wall time: "):
425 with tt.AssertPrints("hihi", suppress=False):
426 with tt.AssertPrints("hihi", suppress=False):
426 ip.run_cell("f('hi')")
427 ip.run_cell("f('hi')")
427
428
428 def test_time_last_not_expression():
429 def test_time_last_not_expression():
429 ip.run_cell("%%time\n"
430 ip.run_cell("%%time\n"
430 "var_1 = 1\n"
431 "var_1 = 1\n"
431 "var_2 = 2\n")
432 "var_2 = 2\n")
432 assert ip.user_ns['var_1'] == 1
433 assert ip.user_ns['var_1'] == 1
433 del ip.user_ns['var_1']
434 del ip.user_ns['var_1']
434 assert ip.user_ns['var_2'] == 2
435 assert ip.user_ns['var_2'] == 2
435 del ip.user_ns['var_2']
436 del ip.user_ns['var_2']
436
437
437
438
438 @dec.skip_win32
439 @dec.skip_win32
439 def test_time2():
440 def test_time2():
440 ip = get_ipython()
441 ip = get_ipython()
441
442
442 with tt.AssertPrints("CPU times: user "):
443 with tt.AssertPrints("CPU times: user "):
443 ip.run_cell("%time None")
444 ip.run_cell("%time None")
444
445
445 def test_time3():
446 def test_time3():
446 """Erroneous magic function calls, issue gh-3334"""
447 """Erroneous magic function calls, issue gh-3334"""
447 ip = get_ipython()
448 ip = get_ipython()
448 ip.user_ns.pop('run', None)
449 ip.user_ns.pop('run', None)
449
450
450 with tt.AssertNotPrints("not found", channel='stderr'):
451 with tt.AssertNotPrints("not found", channel='stderr'):
451 ip.run_cell("%%time\n"
452 ip.run_cell("%%time\n"
452 "run = 0\n"
453 "run = 0\n"
453 "run += 1")
454 "run += 1")
454
455
455 def test_multiline_time():
456 def test_multiline_time():
456 """Make sure last statement from time return a value."""
457 """Make sure last statement from time return a value."""
457 ip = get_ipython()
458 ip = get_ipython()
458 ip.user_ns.pop('run', None)
459 ip.user_ns.pop('run', None)
459
460
460 ip.run_cell(dedent("""\
461 ip.run_cell(dedent("""\
461 %%time
462 %%time
462 a = "ho"
463 a = "ho"
463 b = "hey"
464 b = "hey"
464 a+b
465 a+b
465 """))
466 """))
466 nt.assert_equal(ip.user_ns_hidden['_'], 'hohey')
467 nt.assert_equal(ip.user_ns_hidden['_'], 'hohey')
467
468
468 def test_time_local_ns():
469 def test_time_local_ns():
469 """
470 """
470 Test that local_ns is actually global_ns when running a cell magic
471 Test that local_ns is actually global_ns when running a cell magic
471 """
472 """
472 ip = get_ipython()
473 ip = get_ipython()
473 ip.run_cell("%%time\n"
474 ip.run_cell("%%time\n"
474 "myvar = 1")
475 "myvar = 1")
475 nt.assert_equal(ip.user_ns['myvar'], 1)
476 nt.assert_equal(ip.user_ns['myvar'], 1)
476 del ip.user_ns['myvar']
477 del ip.user_ns['myvar']
477
478
478 def test_doctest_mode():
479 def test_doctest_mode():
479 "Toggle doctest_mode twice, it should be a no-op and run without error"
480 "Toggle doctest_mode twice, it should be a no-op and run without error"
480 _ip.magic('doctest_mode')
481 _ip.magic('doctest_mode')
481 _ip.magic('doctest_mode')
482 _ip.magic('doctest_mode')
482
483
483
484
484 def test_parse_options():
485 def test_parse_options():
485 """Tests for basic options parsing in magics."""
486 """Tests for basic options parsing in magics."""
486 # These are only the most minimal of tests, more should be added later. At
487 # These are only the most minimal of tests, more should be added later. At
487 # the very least we check that basic text/unicode calls work OK.
488 # the very least we check that basic text/unicode calls work OK.
488 m = DummyMagics(_ip)
489 m = DummyMagics(_ip)
489 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
490 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
490 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
491 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
491
492
492
493
493 def test_dirops():
494 def test_dirops():
494 """Test various directory handling operations."""
495 """Test various directory handling operations."""
495 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
496 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
496 curpath = os.getcwd
497 curpath = os.getcwd
497 startdir = os.getcwd()
498 startdir = os.getcwd()
498 ipdir = os.path.realpath(_ip.ipython_dir)
499 ipdir = os.path.realpath(_ip.ipython_dir)
499 try:
500 try:
500 _ip.magic('cd "%s"' % ipdir)
501 _ip.magic('cd "%s"' % ipdir)
501 nt.assert_equal(curpath(), ipdir)
502 nt.assert_equal(curpath(), ipdir)
502 _ip.magic('cd -')
503 _ip.magic('cd -')
503 nt.assert_equal(curpath(), startdir)
504 nt.assert_equal(curpath(), startdir)
504 _ip.magic('pushd "%s"' % ipdir)
505 _ip.magic('pushd "%s"' % ipdir)
505 nt.assert_equal(curpath(), ipdir)
506 nt.assert_equal(curpath(), ipdir)
506 _ip.magic('popd')
507 _ip.magic('popd')
507 nt.assert_equal(curpath(), startdir)
508 nt.assert_equal(curpath(), startdir)
508 finally:
509 finally:
509 os.chdir(startdir)
510 os.chdir(startdir)
510
511
511
512
512 def test_cd_force_quiet():
513 def test_cd_force_quiet():
513 """Test OSMagics.cd_force_quiet option"""
514 """Test OSMagics.cd_force_quiet option"""
514 _ip.config.OSMagics.cd_force_quiet = True
515 _ip.config.OSMagics.cd_force_quiet = True
515 osmagics = osm.OSMagics(shell=_ip)
516 osmagics = osm.OSMagics(shell=_ip)
516
517
517 startdir = os.getcwd()
518 startdir = os.getcwd()
518 ipdir = os.path.realpath(_ip.ipython_dir)
519 ipdir = os.path.realpath(_ip.ipython_dir)
519
520
520 try:
521 try:
521 with tt.AssertNotPrints(ipdir):
522 with tt.AssertNotPrints(ipdir):
522 osmagics.cd('"%s"' % ipdir)
523 osmagics.cd('"%s"' % ipdir)
523 with tt.AssertNotPrints(startdir):
524 with tt.AssertNotPrints(startdir):
524 osmagics.cd('-')
525 osmagics.cd('-')
525 finally:
526 finally:
526 os.chdir(startdir)
527 os.chdir(startdir)
527
528
528
529
529 def test_xmode():
530 def test_xmode():
530 # Calling xmode three times should be a no-op
531 # Calling xmode three times should be a no-op
531 xmode = _ip.InteractiveTB.mode
532 xmode = _ip.InteractiveTB.mode
532 for i in range(4):
533 for i in range(4):
533 _ip.magic("xmode")
534 _ip.magic("xmode")
534 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
535 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
535
536
536 def test_reset_hard():
537 def test_reset_hard():
537 monitor = []
538 monitor = []
538 class A(object):
539 class A(object):
539 def __del__(self):
540 def __del__(self):
540 monitor.append(1)
541 monitor.append(1)
541 def __repr__(self):
542 def __repr__(self):
542 return "<A instance>"
543 return "<A instance>"
543
544
544 _ip.user_ns["a"] = A()
545 _ip.user_ns["a"] = A()
545 _ip.run_cell("a")
546 _ip.run_cell("a")
546
547
547 nt.assert_equal(monitor, [])
548 nt.assert_equal(monitor, [])
548 _ip.magic("reset -f")
549 _ip.magic("reset -f")
549 nt.assert_equal(monitor, [1])
550 nt.assert_equal(monitor, [1])
550
551
551 class TestXdel(tt.TempFileMixin):
552 class TestXdel(tt.TempFileMixin):
552 def test_xdel(self):
553 def test_xdel(self):
553 """Test that references from %run are cleared by xdel."""
554 """Test that references from %run are cleared by xdel."""
554 src = ("class A(object):\n"
555 src = ("class A(object):\n"
555 " monitor = []\n"
556 " monitor = []\n"
556 " def __del__(self):\n"
557 " def __del__(self):\n"
557 " self.monitor.append(1)\n"
558 " self.monitor.append(1)\n"
558 "a = A()\n")
559 "a = A()\n")
559 self.mktmp(src)
560 self.mktmp(src)
560 # %run creates some hidden references...
561 # %run creates some hidden references...
561 _ip.magic("run %s" % self.fname)
562 _ip.magic("run %s" % self.fname)
562 # ... as does the displayhook.
563 # ... as does the displayhook.
563 _ip.run_cell("a")
564 _ip.run_cell("a")
564
565
565 monitor = _ip.user_ns["A"].monitor
566 monitor = _ip.user_ns["A"].monitor
566 nt.assert_equal(monitor, [])
567 nt.assert_equal(monitor, [])
567
568
568 _ip.magic("xdel a")
569 _ip.magic("xdel a")
569
570
570 # Check that a's __del__ method has been called.
571 # Check that a's __del__ method has been called.
571 nt.assert_equal(monitor, [1])
572 nt.assert_equal(monitor, [1])
572
573
573 def doctest_who():
574 def doctest_who():
574 """doctest for %who
575 """doctest for %who
575
576
576 In [1]: %reset -f
577 In [1]: %reset -f
577
578
578 In [2]: alpha = 123
579 In [2]: alpha = 123
579
580
580 In [3]: beta = 'beta'
581 In [3]: beta = 'beta'
581
582
582 In [4]: %who int
583 In [4]: %who int
583 alpha
584 alpha
584
585
585 In [5]: %who str
586 In [5]: %who str
586 beta
587 beta
587
588
588 In [6]: %whos
589 In [6]: %whos
589 Variable Type Data/Info
590 Variable Type Data/Info
590 ----------------------------
591 ----------------------------
591 alpha int 123
592 alpha int 123
592 beta str beta
593 beta str beta
593
594
594 In [7]: %who_ls
595 In [7]: %who_ls
595 Out[7]: ['alpha', 'beta']
596 Out[7]: ['alpha', 'beta']
596 """
597 """
597
598
598 def test_whos():
599 def test_whos():
599 """Check that whos is protected against objects where repr() fails."""
600 """Check that whos is protected against objects where repr() fails."""
600 class A(object):
601 class A(object):
601 def __repr__(self):
602 def __repr__(self):
602 raise Exception()
603 raise Exception()
603 _ip.user_ns['a'] = A()
604 _ip.user_ns['a'] = A()
604 _ip.magic("whos")
605 _ip.magic("whos")
605
606
606 def doctest_precision():
607 def doctest_precision():
607 """doctest for %precision
608 """doctest for %precision
608
609
609 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
610 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
610
611
611 In [2]: %precision 5
612 In [2]: %precision 5
612 Out[2]: '%.5f'
613 Out[2]: '%.5f'
613
614
614 In [3]: f.float_format
615 In [3]: f.float_format
615 Out[3]: '%.5f'
616 Out[3]: '%.5f'
616
617
617 In [4]: %precision %e
618 In [4]: %precision %e
618 Out[4]: '%e'
619 Out[4]: '%e'
619
620
620 In [5]: f(3.1415927)
621 In [5]: f(3.1415927)
621 Out[5]: '3.141593e+00'
622 Out[5]: '3.141593e+00'
622 """
623 """
623
624
624 def test_psearch():
625 def test_psearch():
625 with tt.AssertPrints("dict.fromkeys"):
626 with tt.AssertPrints("dict.fromkeys"):
626 _ip.run_cell("dict.fr*?")
627 _ip.run_cell("dict.fr*?")
627
628
628 def test_timeit_shlex():
629 def test_timeit_shlex():
629 """test shlex issues with timeit (#1109)"""
630 """test shlex issues with timeit (#1109)"""
630 _ip.ex("def f(*a,**kw): pass")
631 _ip.ex("def f(*a,**kw): pass")
631 _ip.magic('timeit -n1 "this is a bug".count(" ")')
632 _ip.magic('timeit -n1 "this is a bug".count(" ")')
632 _ip.magic('timeit -r1 -n1 f(" ", 1)')
633 _ip.magic('timeit -r1 -n1 f(" ", 1)')
633 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
634 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
634 _ip.magic('timeit -r1 -n1 ("a " + "b")')
635 _ip.magic('timeit -r1 -n1 ("a " + "b")')
635 _ip.magic('timeit -r1 -n1 f("a " + "b")')
636 _ip.magic('timeit -r1 -n1 f("a " + "b")')
636 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
637 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
637
638
638
639
639 def test_timeit_special_syntax():
640 def test_timeit_special_syntax():
640 "Test %%timeit with IPython special syntax"
641 "Test %%timeit with IPython special syntax"
641 @register_line_magic
642 @register_line_magic
642 def lmagic(line):
643 def lmagic(line):
643 ip = get_ipython()
644 ip = get_ipython()
644 ip.user_ns['lmagic_out'] = line
645 ip.user_ns['lmagic_out'] = line
645
646
646 # line mode test
647 # line mode test
647 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
648 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
648 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
649 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
649 # cell mode test
650 # cell mode test
650 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
651 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
651 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
652 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
652
653
653 def test_timeit_return():
654 def test_timeit_return():
654 """
655 """
655 test whether timeit -o return object
656 test whether timeit -o return object
656 """
657 """
657
658
658 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
659 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
659 assert(res is not None)
660 assert(res is not None)
660
661
661 def test_timeit_quiet():
662 def test_timeit_quiet():
662 """
663 """
663 test quiet option of timeit magic
664 test quiet option of timeit magic
664 """
665 """
665 with tt.AssertNotPrints("loops"):
666 with tt.AssertNotPrints("loops"):
666 _ip.run_cell("%timeit -n1 -r1 -q 1")
667 _ip.run_cell("%timeit -n1 -r1 -q 1")
667
668
668 def test_timeit_return_quiet():
669 def test_timeit_return_quiet():
669 with tt.AssertNotPrints("loops"):
670 with tt.AssertNotPrints("loops"):
670 res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1')
671 res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1')
671 assert (res is not None)
672 assert (res is not None)
672
673
673 def test_timeit_invalid_return():
674 def test_timeit_invalid_return():
674 with nt.assert_raises_regex(SyntaxError, "outside function"):
675 with nt.assert_raises_regex(SyntaxError, "outside function"):
675 _ip.run_line_magic('timeit', 'return')
676 _ip.run_line_magic('timeit', 'return')
676
677
677 @dec.skipif(execution.profile is None)
678 @dec.skipif(execution.profile is None)
678 def test_prun_special_syntax():
679 def test_prun_special_syntax():
679 "Test %%prun with IPython special syntax"
680 "Test %%prun with IPython special syntax"
680 @register_line_magic
681 @register_line_magic
681 def lmagic(line):
682 def lmagic(line):
682 ip = get_ipython()
683 ip = get_ipython()
683 ip.user_ns['lmagic_out'] = line
684 ip.user_ns['lmagic_out'] = line
684
685
685 # line mode test
686 # line mode test
686 _ip.run_line_magic('prun', '-q %lmagic my line')
687 _ip.run_line_magic('prun', '-q %lmagic my line')
687 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
688 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
688 # cell mode test
689 # cell mode test
689 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
690 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
690 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
691 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
691
692
692 @dec.skipif(execution.profile is None)
693 @dec.skipif(execution.profile is None)
693 def test_prun_quotes():
694 def test_prun_quotes():
694 "Test that prun does not clobber string escapes (GH #1302)"
695 "Test that prun does not clobber string escapes (GH #1302)"
695 _ip.magic(r"prun -q x = '\t'")
696 _ip.magic(r"prun -q x = '\t'")
696 nt.assert_equal(_ip.user_ns['x'], '\t')
697 nt.assert_equal(_ip.user_ns['x'], '\t')
697
698
698 def test_extension():
699 def test_extension():
699 # Debugging information for failures of this test
700 # Debugging information for failures of this test
700 print('sys.path:')
701 print('sys.path:')
701 for p in sys.path:
702 for p in sys.path:
702 print(' ', p)
703 print(' ', p)
703 print('CWD', os.getcwd())
704 print('CWD', os.getcwd())
704
705
705 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
706 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
706 daft_path = os.path.join(os.path.dirname(__file__), "daft_extension")
707 daft_path = os.path.join(os.path.dirname(__file__), "daft_extension")
707 sys.path.insert(0, daft_path)
708 sys.path.insert(0, daft_path)
708 try:
709 try:
709 _ip.user_ns.pop('arq', None)
710 _ip.user_ns.pop('arq', None)
710 invalidate_caches() # Clear import caches
711 invalidate_caches() # Clear import caches
711 _ip.magic("load_ext daft_extension")
712 _ip.magic("load_ext daft_extension")
712 nt.assert_equal(_ip.user_ns['arq'], 185)
713 nt.assert_equal(_ip.user_ns['arq'], 185)
713 _ip.magic("unload_ext daft_extension")
714 _ip.magic("unload_ext daft_extension")
714 assert 'arq' not in _ip.user_ns
715 assert 'arq' not in _ip.user_ns
715 finally:
716 finally:
716 sys.path.remove(daft_path)
717 sys.path.remove(daft_path)
717
718
718
719
719 def test_notebook_export_json():
720 def test_notebook_export_json():
720 _ip = get_ipython()
721 _ip = get_ipython()
721 _ip.history_manager.reset() # Clear any existing history.
722 _ip.history_manager.reset() # Clear any existing history.
722 cmds = [u"a=1", u"def b():\n return a**2", u"print('noΓ«l, Γ©tΓ©', b())"]
723 cmds = [u"a=1", u"def b():\n return a**2", u"print('noΓ«l, Γ©tΓ©', b())"]
723 for i, cmd in enumerate(cmds, start=1):
724 for i, cmd in enumerate(cmds, start=1):
724 _ip.history_manager.store_inputs(i, cmd)
725 _ip.history_manager.store_inputs(i, cmd)
725 with TemporaryDirectory() as td:
726 with TemporaryDirectory() as td:
726 outfile = os.path.join(td, "nb.ipynb")
727 outfile = os.path.join(td, "nb.ipynb")
727 _ip.magic("notebook -e %s" % outfile)
728 _ip.magic("notebook -e %s" % outfile)
728
729
729
730
730 class TestEnv(TestCase):
731 class TestEnv(TestCase):
731
732
732 def test_env(self):
733 def test_env(self):
733 env = _ip.magic("env")
734 env = _ip.magic("env")
734 self.assertTrue(isinstance(env, dict))
735 self.assertTrue(isinstance(env, dict))
735
736
737 def test_env_secret(self):
738 env = _ip.magic("env")
739 hidden = "<hidden>"
740 with mock.patch.dict(
741 os.environ,
742 {
743 "API_KEY": "abc123",
744 "SECRET_THING": "ssshhh",
745 "JUPYTER_TOKEN": "",
746 "VAR": "abc"
747 }
748 ):
749 env = _ip.magic("env")
750 assert env["API_KEY"] == hidden
751 assert env["SECRET_THING"] == hidden
752 assert env["JUPYTER_TOKEN"] == hidden
753 assert env["VAR"] == "abc"
754
736 def test_env_get_set_simple(self):
755 def test_env_get_set_simple(self):
737 env = _ip.magic("env var val1")
756 env = _ip.magic("env var val1")
738 self.assertEqual(env, None)
757 self.assertEqual(env, None)
739 self.assertEqual(os.environ['var'], 'val1')
758 self.assertEqual(os.environ['var'], 'val1')
740 self.assertEqual(_ip.magic("env var"), 'val1')
759 self.assertEqual(_ip.magic("env var"), 'val1')
741 env = _ip.magic("env var=val2")
760 env = _ip.magic("env var=val2")
742 self.assertEqual(env, None)
761 self.assertEqual(env, None)
743 self.assertEqual(os.environ['var'], 'val2')
762 self.assertEqual(os.environ['var'], 'val2')
744
763
745 def test_env_get_set_complex(self):
764 def test_env_get_set_complex(self):
746 env = _ip.magic("env var 'val1 '' 'val2")
765 env = _ip.magic("env var 'val1 '' 'val2")
747 self.assertEqual(env, None)
766 self.assertEqual(env, None)
748 self.assertEqual(os.environ['var'], "'val1 '' 'val2")
767 self.assertEqual(os.environ['var'], "'val1 '' 'val2")
749 self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2")
768 self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2")
750 env = _ip.magic('env var=val2 val3="val4')
769 env = _ip.magic('env var=val2 val3="val4')
751 self.assertEqual(env, None)
770 self.assertEqual(env, None)
752 self.assertEqual(os.environ['var'], 'val2 val3="val4')
771 self.assertEqual(os.environ['var'], 'val2 val3="val4')
753
772
754 def test_env_set_bad_input(self):
773 def test_env_set_bad_input(self):
755 self.assertRaises(UsageError, lambda: _ip.magic("set_env var"))
774 self.assertRaises(UsageError, lambda: _ip.magic("set_env var"))
756
775
757 def test_env_set_whitespace(self):
776 def test_env_set_whitespace(self):
758 self.assertRaises(UsageError, lambda: _ip.magic("env var A=B"))
777 self.assertRaises(UsageError, lambda: _ip.magic("env var A=B"))
759
778
760
779
761 class CellMagicTestCase(TestCase):
780 class CellMagicTestCase(TestCase):
762
781
763 def check_ident(self, magic):
782 def check_ident(self, magic):
764 # Manually called, we get the result
783 # Manually called, we get the result
765 out = _ip.run_cell_magic(magic, 'a', 'b')
784 out = _ip.run_cell_magic(magic, 'a', 'b')
766 nt.assert_equal(out, ('a','b'))
785 nt.assert_equal(out, ('a','b'))
767 # Via run_cell, it goes into the user's namespace via displayhook
786 # Via run_cell, it goes into the user's namespace via displayhook
768 _ip.run_cell('%%' + magic +' c\nd\n')
787 _ip.run_cell('%%' + magic +' c\nd\n')
769 nt.assert_equal(_ip.user_ns['_'], ('c','d\n'))
788 nt.assert_equal(_ip.user_ns['_'], ('c','d\n'))
770
789
771 def test_cell_magic_func_deco(self):
790 def test_cell_magic_func_deco(self):
772 "Cell magic using simple decorator"
791 "Cell magic using simple decorator"
773 @register_cell_magic
792 @register_cell_magic
774 def cellm(line, cell):
793 def cellm(line, cell):
775 return line, cell
794 return line, cell
776
795
777 self.check_ident('cellm')
796 self.check_ident('cellm')
778
797
779 def test_cell_magic_reg(self):
798 def test_cell_magic_reg(self):
780 "Cell magic manually registered"
799 "Cell magic manually registered"
781 def cellm(line, cell):
800 def cellm(line, cell):
782 return line, cell
801 return line, cell
783
802
784 _ip.register_magic_function(cellm, 'cell', 'cellm2')
803 _ip.register_magic_function(cellm, 'cell', 'cellm2')
785 self.check_ident('cellm2')
804 self.check_ident('cellm2')
786
805
787 def test_cell_magic_class(self):
806 def test_cell_magic_class(self):
788 "Cell magics declared via a class"
807 "Cell magics declared via a class"
789 @magics_class
808 @magics_class
790 class MyMagics(Magics):
809 class MyMagics(Magics):
791
810
792 @cell_magic
811 @cell_magic
793 def cellm3(self, line, cell):
812 def cellm3(self, line, cell):
794 return line, cell
813 return line, cell
795
814
796 _ip.register_magics(MyMagics)
815 _ip.register_magics(MyMagics)
797 self.check_ident('cellm3')
816 self.check_ident('cellm3')
798
817
799 def test_cell_magic_class2(self):
818 def test_cell_magic_class2(self):
800 "Cell magics declared via a class, #2"
819 "Cell magics declared via a class, #2"
801 @magics_class
820 @magics_class
802 class MyMagics2(Magics):
821 class MyMagics2(Magics):
803
822
804 @cell_magic('cellm4')
823 @cell_magic('cellm4')
805 def cellm33(self, line, cell):
824 def cellm33(self, line, cell):
806 return line, cell
825 return line, cell
807
826
808 _ip.register_magics(MyMagics2)
827 _ip.register_magics(MyMagics2)
809 self.check_ident('cellm4')
828 self.check_ident('cellm4')
810 # Check that nothing is registered as 'cellm33'
829 # Check that nothing is registered as 'cellm33'
811 c33 = _ip.find_cell_magic('cellm33')
830 c33 = _ip.find_cell_magic('cellm33')
812 nt.assert_equal(c33, None)
831 nt.assert_equal(c33, None)
813
832
814 def test_file():
833 def test_file():
815 """Basic %%writefile"""
834 """Basic %%writefile"""
816 ip = get_ipython()
835 ip = get_ipython()
817 with TemporaryDirectory() as td:
836 with TemporaryDirectory() as td:
818 fname = os.path.join(td, 'file1')
837 fname = os.path.join(td, 'file1')
819 ip.run_cell_magic("writefile", fname, u'\n'.join([
838 ip.run_cell_magic("writefile", fname, u'\n'.join([
820 'line1',
839 'line1',
821 'line2',
840 'line2',
822 ]))
841 ]))
823 with open(fname) as f:
842 with open(fname) as f:
824 s = f.read()
843 s = f.read()
825 nt.assert_in('line1\n', s)
844 nt.assert_in('line1\n', s)
826 nt.assert_in('line2', s)
845 nt.assert_in('line2', s)
827
846
828 @dec.skip_win32
847 @dec.skip_win32
829 def test_file_single_quote():
848 def test_file_single_quote():
830 """Basic %%writefile with embedded single quotes"""
849 """Basic %%writefile with embedded single quotes"""
831 ip = get_ipython()
850 ip = get_ipython()
832 with TemporaryDirectory() as td:
851 with TemporaryDirectory() as td:
833 fname = os.path.join(td, '\'file1\'')
852 fname = os.path.join(td, '\'file1\'')
834 ip.run_cell_magic("writefile", fname, u'\n'.join([
853 ip.run_cell_magic("writefile", fname, u'\n'.join([
835 'line1',
854 'line1',
836 'line2',
855 'line2',
837 ]))
856 ]))
838 with open(fname) as f:
857 with open(fname) as f:
839 s = f.read()
858 s = f.read()
840 nt.assert_in('line1\n', s)
859 nt.assert_in('line1\n', s)
841 nt.assert_in('line2', s)
860 nt.assert_in('line2', s)
842
861
843 @dec.skip_win32
862 @dec.skip_win32
844 def test_file_double_quote():
863 def test_file_double_quote():
845 """Basic %%writefile with embedded double quotes"""
864 """Basic %%writefile with embedded double quotes"""
846 ip = get_ipython()
865 ip = get_ipython()
847 with TemporaryDirectory() as td:
866 with TemporaryDirectory() as td:
848 fname = os.path.join(td, '"file1"')
867 fname = os.path.join(td, '"file1"')
849 ip.run_cell_magic("writefile", fname, u'\n'.join([
868 ip.run_cell_magic("writefile", fname, u'\n'.join([
850 'line1',
869 'line1',
851 'line2',
870 'line2',
852 ]))
871 ]))
853 with open(fname) as f:
872 with open(fname) as f:
854 s = f.read()
873 s = f.read()
855 nt.assert_in('line1\n', s)
874 nt.assert_in('line1\n', s)
856 nt.assert_in('line2', s)
875 nt.assert_in('line2', s)
857
876
858 def test_file_var_expand():
877 def test_file_var_expand():
859 """%%writefile $filename"""
878 """%%writefile $filename"""
860 ip = get_ipython()
879 ip = get_ipython()
861 with TemporaryDirectory() as td:
880 with TemporaryDirectory() as td:
862 fname = os.path.join(td, 'file1')
881 fname = os.path.join(td, 'file1')
863 ip.user_ns['filename'] = fname
882 ip.user_ns['filename'] = fname
864 ip.run_cell_magic("writefile", '$filename', u'\n'.join([
883 ip.run_cell_magic("writefile", '$filename', u'\n'.join([
865 'line1',
884 'line1',
866 'line2',
885 'line2',
867 ]))
886 ]))
868 with open(fname) as f:
887 with open(fname) as f:
869 s = f.read()
888 s = f.read()
870 nt.assert_in('line1\n', s)
889 nt.assert_in('line1\n', s)
871 nt.assert_in('line2', s)
890 nt.assert_in('line2', s)
872
891
873 def test_file_unicode():
892 def test_file_unicode():
874 """%%writefile with unicode cell"""
893 """%%writefile with unicode cell"""
875 ip = get_ipython()
894 ip = get_ipython()
876 with TemporaryDirectory() as td:
895 with TemporaryDirectory() as td:
877 fname = os.path.join(td, 'file1')
896 fname = os.path.join(td, 'file1')
878 ip.run_cell_magic("writefile", fname, u'\n'.join([
897 ip.run_cell_magic("writefile", fname, u'\n'.join([
879 u'linΓ©1',
898 u'linΓ©1',
880 u'linΓ©2',
899 u'linΓ©2',
881 ]))
900 ]))
882 with io.open(fname, encoding='utf-8') as f:
901 with io.open(fname, encoding='utf-8') as f:
883 s = f.read()
902 s = f.read()
884 nt.assert_in(u'linΓ©1\n', s)
903 nt.assert_in(u'linΓ©1\n', s)
885 nt.assert_in(u'linΓ©2', s)
904 nt.assert_in(u'linΓ©2', s)
886
905
887 def test_file_amend():
906 def test_file_amend():
888 """%%writefile -a amends files"""
907 """%%writefile -a amends files"""
889 ip = get_ipython()
908 ip = get_ipython()
890 with TemporaryDirectory() as td:
909 with TemporaryDirectory() as td:
891 fname = os.path.join(td, 'file2')
910 fname = os.path.join(td, 'file2')
892 ip.run_cell_magic("writefile", fname, u'\n'.join([
911 ip.run_cell_magic("writefile", fname, u'\n'.join([
893 'line1',
912 'line1',
894 'line2',
913 'line2',
895 ]))
914 ]))
896 ip.run_cell_magic("writefile", "-a %s" % fname, u'\n'.join([
915 ip.run_cell_magic("writefile", "-a %s" % fname, u'\n'.join([
897 'line3',
916 'line3',
898 'line4',
917 'line4',
899 ]))
918 ]))
900 with open(fname) as f:
919 with open(fname) as f:
901 s = f.read()
920 s = f.read()
902 nt.assert_in('line1\n', s)
921 nt.assert_in('line1\n', s)
903 nt.assert_in('line3\n', s)
922 nt.assert_in('line3\n', s)
904
923
905 def test_file_spaces():
924 def test_file_spaces():
906 """%%file with spaces in filename"""
925 """%%file with spaces in filename"""
907 ip = get_ipython()
926 ip = get_ipython()
908 with TemporaryWorkingDirectory() as td:
927 with TemporaryWorkingDirectory() as td:
909 fname = "file name"
928 fname = "file name"
910 ip.run_cell_magic("file", '"%s"'%fname, u'\n'.join([
929 ip.run_cell_magic("file", '"%s"'%fname, u'\n'.join([
911 'line1',
930 'line1',
912 'line2',
931 'line2',
913 ]))
932 ]))
914 with open(fname) as f:
933 with open(fname) as f:
915 s = f.read()
934 s = f.read()
916 nt.assert_in('line1\n', s)
935 nt.assert_in('line1\n', s)
917 nt.assert_in('line2', s)
936 nt.assert_in('line2', s)
918
937
919 def test_script_config():
938 def test_script_config():
920 ip = get_ipython()
939 ip = get_ipython()
921 ip.config.ScriptMagics.script_magics = ['whoda']
940 ip.config.ScriptMagics.script_magics = ['whoda']
922 sm = script.ScriptMagics(shell=ip)
941 sm = script.ScriptMagics(shell=ip)
923 nt.assert_in('whoda', sm.magics['cell'])
942 nt.assert_in('whoda', sm.magics['cell'])
924
943
925 @dec.skip_win32
944 @dec.skip_win32
926 def test_script_out():
945 def test_script_out():
927 ip = get_ipython()
946 ip = get_ipython()
928 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
947 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
929 nt.assert_equal(ip.user_ns['output'], 'hi\n')
948 nt.assert_equal(ip.user_ns['output'], 'hi\n')
930
949
931 @dec.skip_win32
950 @dec.skip_win32
932 def test_script_err():
951 def test_script_err():
933 ip = get_ipython()
952 ip = get_ipython()
934 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
953 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
935 nt.assert_equal(ip.user_ns['error'], 'hello\n')
954 nt.assert_equal(ip.user_ns['error'], 'hello\n')
936
955
937 @dec.skip_win32
956 @dec.skip_win32
938 def test_script_out_err():
957 def test_script_out_err():
939 ip = get_ipython()
958 ip = get_ipython()
940 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
959 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
941 nt.assert_equal(ip.user_ns['output'], 'hi\n')
960 nt.assert_equal(ip.user_ns['output'], 'hi\n')
942 nt.assert_equal(ip.user_ns['error'], 'hello\n')
961 nt.assert_equal(ip.user_ns['error'], 'hello\n')
943
962
944 @dec.skip_win32
963 @dec.skip_win32
945 def test_script_bg_out():
964 def test_script_bg_out():
946 ip = get_ipython()
965 ip = get_ipython()
947 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
966 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
948
967
949 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
968 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
950 ip.user_ns['output'].close()
969 ip.user_ns['output'].close()
951
970
952 @dec.skip_win32
971 @dec.skip_win32
953 def test_script_bg_err():
972 def test_script_bg_err():
954 ip = get_ipython()
973 ip = get_ipython()
955 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
974 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
956 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
975 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
957 ip.user_ns['error'].close()
976 ip.user_ns['error'].close()
958
977
959 @dec.skip_win32
978 @dec.skip_win32
960 def test_script_bg_out_err():
979 def test_script_bg_out_err():
961 ip = get_ipython()
980 ip = get_ipython()
962 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
981 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
963 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
982 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
964 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
983 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
965 ip.user_ns['output'].close()
984 ip.user_ns['output'].close()
966 ip.user_ns['error'].close()
985 ip.user_ns['error'].close()
967
986
968 def test_script_defaults():
987 def test_script_defaults():
969 ip = get_ipython()
988 ip = get_ipython()
970 for cmd in ['sh', 'bash', 'perl', 'ruby']:
989 for cmd in ['sh', 'bash', 'perl', 'ruby']:
971 try:
990 try:
972 find_cmd(cmd)
991 find_cmd(cmd)
973 except Exception:
992 except Exception:
974 pass
993 pass
975 else:
994 else:
976 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
995 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
977
996
978
997
979 @magics_class
998 @magics_class
980 class FooFoo(Magics):
999 class FooFoo(Magics):
981 """class with both %foo and %%foo magics"""
1000 """class with both %foo and %%foo magics"""
982 @line_magic('foo')
1001 @line_magic('foo')
983 def line_foo(self, line):
1002 def line_foo(self, line):
984 "I am line foo"
1003 "I am line foo"
985 pass
1004 pass
986
1005
987 @cell_magic("foo")
1006 @cell_magic("foo")
988 def cell_foo(self, line, cell):
1007 def cell_foo(self, line, cell):
989 "I am cell foo, not line foo"
1008 "I am cell foo, not line foo"
990 pass
1009 pass
991
1010
992 def test_line_cell_info():
1011 def test_line_cell_info():
993 """%%foo and %foo magics are distinguishable to inspect"""
1012 """%%foo and %foo magics are distinguishable to inspect"""
994 ip = get_ipython()
1013 ip = get_ipython()
995 ip.magics_manager.register(FooFoo)
1014 ip.magics_manager.register(FooFoo)
996 oinfo = ip.object_inspect('foo')
1015 oinfo = ip.object_inspect('foo')
997 nt.assert_true(oinfo['found'])
1016 nt.assert_true(oinfo['found'])
998 nt.assert_true(oinfo['ismagic'])
1017 nt.assert_true(oinfo['ismagic'])
999
1018
1000 oinfo = ip.object_inspect('%%foo')
1019 oinfo = ip.object_inspect('%%foo')
1001 nt.assert_true(oinfo['found'])
1020 nt.assert_true(oinfo['found'])
1002 nt.assert_true(oinfo['ismagic'])
1021 nt.assert_true(oinfo['ismagic'])
1003 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
1022 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
1004
1023
1005 oinfo = ip.object_inspect('%foo')
1024 oinfo = ip.object_inspect('%foo')
1006 nt.assert_true(oinfo['found'])
1025 nt.assert_true(oinfo['found'])
1007 nt.assert_true(oinfo['ismagic'])
1026 nt.assert_true(oinfo['ismagic'])
1008 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
1027 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
1009
1028
1010 def test_multiple_magics():
1029 def test_multiple_magics():
1011 ip = get_ipython()
1030 ip = get_ipython()
1012 foo1 = FooFoo(ip)
1031 foo1 = FooFoo(ip)
1013 foo2 = FooFoo(ip)
1032 foo2 = FooFoo(ip)
1014 mm = ip.magics_manager
1033 mm = ip.magics_manager
1015 mm.register(foo1)
1034 mm.register(foo1)
1016 nt.assert_true(mm.magics['line']['foo'].__self__ is foo1)
1035 nt.assert_true(mm.magics['line']['foo'].__self__ is foo1)
1017 mm.register(foo2)
1036 mm.register(foo2)
1018 nt.assert_true(mm.magics['line']['foo'].__self__ is foo2)
1037 nt.assert_true(mm.magics['line']['foo'].__self__ is foo2)
1019
1038
1020 def test_alias_magic():
1039 def test_alias_magic():
1021 """Test %alias_magic."""
1040 """Test %alias_magic."""
1022 ip = get_ipython()
1041 ip = get_ipython()
1023 mm = ip.magics_manager
1042 mm = ip.magics_manager
1024
1043
1025 # Basic operation: both cell and line magics are created, if possible.
1044 # Basic operation: both cell and line magics are created, if possible.
1026 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
1045 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
1027 nt.assert_in('timeit_alias', mm.magics['line'])
1046 nt.assert_in('timeit_alias', mm.magics['line'])
1028 nt.assert_in('timeit_alias', mm.magics['cell'])
1047 nt.assert_in('timeit_alias', mm.magics['cell'])
1029
1048
1030 # --cell is specified, line magic not created.
1049 # --cell is specified, line magic not created.
1031 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
1050 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
1032 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
1051 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
1033 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
1052 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
1034
1053
1035 # Test that line alias is created successfully.
1054 # Test that line alias is created successfully.
1036 ip.run_line_magic('alias_magic', '--line env_alias env')
1055 ip.run_line_magic('alias_magic', '--line env_alias env')
1037 nt.assert_equal(ip.run_line_magic('env', ''),
1056 nt.assert_equal(ip.run_line_magic('env', ''),
1038 ip.run_line_magic('env_alias', ''))
1057 ip.run_line_magic('env_alias', ''))
1039
1058
1040 # Test that line alias with parameters passed in is created successfully.
1059 # Test that line alias with parameters passed in is created successfully.
1041 ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3'))
1060 ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3'))
1042 nt.assert_in('history_alias', mm.magics['line'])
1061 nt.assert_in('history_alias', mm.magics['line'])
1043
1062
1044
1063
1045 def test_save():
1064 def test_save():
1046 """Test %save."""
1065 """Test %save."""
1047 ip = get_ipython()
1066 ip = get_ipython()
1048 ip.history_manager.reset() # Clear any existing history.
1067 ip.history_manager.reset() # Clear any existing history.
1049 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
1068 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
1050 for i, cmd in enumerate(cmds, start=1):
1069 for i, cmd in enumerate(cmds, start=1):
1051 ip.history_manager.store_inputs(i, cmd)
1070 ip.history_manager.store_inputs(i, cmd)
1052 with TemporaryDirectory() as tmpdir:
1071 with TemporaryDirectory() as tmpdir:
1053 file = os.path.join(tmpdir, "testsave.py")
1072 file = os.path.join(tmpdir, "testsave.py")
1054 ip.run_line_magic("save", "%s 1-10" % file)
1073 ip.run_line_magic("save", "%s 1-10" % file)
1055 with open(file) as f:
1074 with open(file) as f:
1056 content = f.read()
1075 content = f.read()
1057 nt.assert_equal(content.count(cmds[0]), 1)
1076 nt.assert_equal(content.count(cmds[0]), 1)
1058 nt.assert_in('coding: utf-8', content)
1077 nt.assert_in('coding: utf-8', content)
1059 ip.run_line_magic("save", "-a %s 1-10" % file)
1078 ip.run_line_magic("save", "-a %s 1-10" % file)
1060 with open(file) as f:
1079 with open(file) as f:
1061 content = f.read()
1080 content = f.read()
1062 nt.assert_equal(content.count(cmds[0]), 2)
1081 nt.assert_equal(content.count(cmds[0]), 2)
1063 nt.assert_in('coding: utf-8', content)
1082 nt.assert_in('coding: utf-8', content)
1064
1083
1065
1084
1066 def test_store():
1085 def test_store():
1067 """Test %store."""
1086 """Test %store."""
1068 ip = get_ipython()
1087 ip = get_ipython()
1069 ip.run_line_magic('load_ext', 'storemagic')
1088 ip.run_line_magic('load_ext', 'storemagic')
1070
1089
1071 # make sure the storage is empty
1090 # make sure the storage is empty
1072 ip.run_line_magic('store', '-z')
1091 ip.run_line_magic('store', '-z')
1073 ip.user_ns['var'] = 42
1092 ip.user_ns['var'] = 42
1074 ip.run_line_magic('store', 'var')
1093 ip.run_line_magic('store', 'var')
1075 ip.user_ns['var'] = 39
1094 ip.user_ns['var'] = 39
1076 ip.run_line_magic('store', '-r')
1095 ip.run_line_magic('store', '-r')
1077 nt.assert_equal(ip.user_ns['var'], 42)
1096 nt.assert_equal(ip.user_ns['var'], 42)
1078
1097
1079 ip.run_line_magic('store', '-d var')
1098 ip.run_line_magic('store', '-d var')
1080 ip.user_ns['var'] = 39
1099 ip.user_ns['var'] = 39
1081 ip.run_line_magic('store' , '-r')
1100 ip.run_line_magic('store' , '-r')
1082 nt.assert_equal(ip.user_ns['var'], 39)
1101 nt.assert_equal(ip.user_ns['var'], 39)
1083
1102
1084
1103
1085 def _run_edit_test(arg_s, exp_filename=None,
1104 def _run_edit_test(arg_s, exp_filename=None,
1086 exp_lineno=-1,
1105 exp_lineno=-1,
1087 exp_contents=None,
1106 exp_contents=None,
1088 exp_is_temp=None):
1107 exp_is_temp=None):
1089 ip = get_ipython()
1108 ip = get_ipython()
1090 M = code.CodeMagics(ip)
1109 M = code.CodeMagics(ip)
1091 last_call = ['','']
1110 last_call = ['','']
1092 opts,args = M.parse_options(arg_s,'prxn:')
1111 opts,args = M.parse_options(arg_s,'prxn:')
1093 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
1112 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
1094
1113
1095 if exp_filename is not None:
1114 if exp_filename is not None:
1096 nt.assert_equal(exp_filename, filename)
1115 nt.assert_equal(exp_filename, filename)
1097 if exp_contents is not None:
1116 if exp_contents is not None:
1098 with io.open(filename, 'r', encoding='utf-8') as f:
1117 with io.open(filename, 'r', encoding='utf-8') as f:
1099 contents = f.read()
1118 contents = f.read()
1100 nt.assert_equal(exp_contents, contents)
1119 nt.assert_equal(exp_contents, contents)
1101 if exp_lineno != -1:
1120 if exp_lineno != -1:
1102 nt.assert_equal(exp_lineno, lineno)
1121 nt.assert_equal(exp_lineno, lineno)
1103 if exp_is_temp is not None:
1122 if exp_is_temp is not None:
1104 nt.assert_equal(exp_is_temp, is_temp)
1123 nt.assert_equal(exp_is_temp, is_temp)
1105
1124
1106
1125
1107 def test_edit_interactive():
1126 def test_edit_interactive():
1108 """%edit on interactively defined objects"""
1127 """%edit on interactively defined objects"""
1109 ip = get_ipython()
1128 ip = get_ipython()
1110 n = ip.execution_count
1129 n = ip.execution_count
1111 ip.run_cell(u"def foo(): return 1", store_history=True)
1130 ip.run_cell(u"def foo(): return 1", store_history=True)
1112
1131
1113 try:
1132 try:
1114 _run_edit_test("foo")
1133 _run_edit_test("foo")
1115 except code.InteractivelyDefined as e:
1134 except code.InteractivelyDefined as e:
1116 nt.assert_equal(e.index, n)
1135 nt.assert_equal(e.index, n)
1117 else:
1136 else:
1118 raise AssertionError("Should have raised InteractivelyDefined")
1137 raise AssertionError("Should have raised InteractivelyDefined")
1119
1138
1120
1139
1121 def test_edit_cell():
1140 def test_edit_cell():
1122 """%edit [cell id]"""
1141 """%edit [cell id]"""
1123 ip = get_ipython()
1142 ip = get_ipython()
1124
1143
1125 ip.run_cell(u"def foo(): return 1", store_history=True)
1144 ip.run_cell(u"def foo(): return 1", store_history=True)
1126
1145
1127 # test
1146 # test
1128 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1147 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1129
1148
1130 def test_bookmark():
1149 def test_bookmark():
1131 ip = get_ipython()
1150 ip = get_ipython()
1132 ip.run_line_magic('bookmark', 'bmname')
1151 ip.run_line_magic('bookmark', 'bmname')
1133 with tt.AssertPrints('bmname'):
1152 with tt.AssertPrints('bmname'):
1134 ip.run_line_magic('bookmark', '-l')
1153 ip.run_line_magic('bookmark', '-l')
1135 ip.run_line_magic('bookmark', '-d bmname')
1154 ip.run_line_magic('bookmark', '-d bmname')
1136
1155
1137 def test_ls_magic():
1156 def test_ls_magic():
1138 ip = get_ipython()
1157 ip = get_ipython()
1139 json_formatter = ip.display_formatter.formatters['application/json']
1158 json_formatter = ip.display_formatter.formatters['application/json']
1140 json_formatter.enabled = True
1159 json_formatter.enabled = True
1141 lsmagic = ip.magic('lsmagic')
1160 lsmagic = ip.magic('lsmagic')
1142 with warnings.catch_warnings(record=True) as w:
1161 with warnings.catch_warnings(record=True) as w:
1143 j = json_formatter(lsmagic)
1162 j = json_formatter(lsmagic)
1144 nt.assert_equal(sorted(j), ['cell', 'line'])
1163 nt.assert_equal(sorted(j), ['cell', 'line'])
1145 nt.assert_equal(w, []) # no warnings
1164 nt.assert_equal(w, []) # no warnings
1146
1165
1147 def test_strip_initial_indent():
1166 def test_strip_initial_indent():
1148 def sii(s):
1167 def sii(s):
1149 lines = s.splitlines()
1168 lines = s.splitlines()
1150 return '\n'.join(code.strip_initial_indent(lines))
1169 return '\n'.join(code.strip_initial_indent(lines))
1151
1170
1152 nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2")
1171 nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2")
1153 nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc")
1172 nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc")
1154 nt.assert_equal(sii("a\n b"), "a\n b")
1173 nt.assert_equal(sii("a\n b"), "a\n b")
1155
1174
1156 def test_logging_magic_quiet_from_arg():
1175 def test_logging_magic_quiet_from_arg():
1157 _ip.config.LoggingMagics.quiet = False
1176 _ip.config.LoggingMagics.quiet = False
1158 lm = logging.LoggingMagics(shell=_ip)
1177 lm = logging.LoggingMagics(shell=_ip)
1159 with TemporaryDirectory() as td:
1178 with TemporaryDirectory() as td:
1160 try:
1179 try:
1161 with tt.AssertNotPrints(re.compile("Activating.*")):
1180 with tt.AssertNotPrints(re.compile("Activating.*")):
1162 lm.logstart('-q {}'.format(
1181 lm.logstart('-q {}'.format(
1163 os.path.join(td, "quiet_from_arg.log")))
1182 os.path.join(td, "quiet_from_arg.log")))
1164 finally:
1183 finally:
1165 _ip.logger.logstop()
1184 _ip.logger.logstop()
1166
1185
1167 def test_logging_magic_quiet_from_config():
1186 def test_logging_magic_quiet_from_config():
1168 _ip.config.LoggingMagics.quiet = True
1187 _ip.config.LoggingMagics.quiet = True
1169 lm = logging.LoggingMagics(shell=_ip)
1188 lm = logging.LoggingMagics(shell=_ip)
1170 with TemporaryDirectory() as td:
1189 with TemporaryDirectory() as td:
1171 try:
1190 try:
1172 with tt.AssertNotPrints(re.compile("Activating.*")):
1191 with tt.AssertNotPrints(re.compile("Activating.*")):
1173 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1192 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1174 finally:
1193 finally:
1175 _ip.logger.logstop()
1194 _ip.logger.logstop()
1176
1195
1177
1196
1178 def test_logging_magic_not_quiet():
1197 def test_logging_magic_not_quiet():
1179 _ip.config.LoggingMagics.quiet = False
1198 _ip.config.LoggingMagics.quiet = False
1180 lm = logging.LoggingMagics(shell=_ip)
1199 lm = logging.LoggingMagics(shell=_ip)
1181 with TemporaryDirectory() as td:
1200 with TemporaryDirectory() as td:
1182 try:
1201 try:
1183 with tt.AssertPrints(re.compile("Activating.*")):
1202 with tt.AssertPrints(re.compile("Activating.*")):
1184 lm.logstart(os.path.join(td, "not_quiet.log"))
1203 lm.logstart(os.path.join(td, "not_quiet.log"))
1185 finally:
1204 finally:
1186 _ip.logger.logstop()
1205 _ip.logger.logstop()
1187
1206
1188
1207
1189 def test_time_no_var_expand():
1208 def test_time_no_var_expand():
1190 _ip.user_ns['a'] = 5
1209 _ip.user_ns['a'] = 5
1191 _ip.user_ns['b'] = []
1210 _ip.user_ns['b'] = []
1192 _ip.magic('time b.append("{a}")')
1211 _ip.magic('time b.append("{a}")')
1193 assert _ip.user_ns['b'] == ['{a}']
1212 assert _ip.user_ns['b'] == ['{a}']
1194
1213
1195
1214
1196 # this is slow, put at the end for local testing.
1215 # this is slow, put at the end for local testing.
1197 def test_timeit_arguments():
1216 def test_timeit_arguments():
1198 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
1217 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
1199 if sys.version_info < (3,7):
1218 if sys.version_info < (3,7):
1200 _ip.magic("timeit -n1 -r1 ('#')")
1219 _ip.magic("timeit -n1 -r1 ('#')")
1201 else:
1220 else:
1202 # 3.7 optimize no-op statement like above out, and complain there is
1221 # 3.7 optimize no-op statement like above out, and complain there is
1203 # nothing in the for loop.
1222 # nothing in the for loop.
1204 _ip.magic("timeit -n1 -r1 a=('#')")
1223 _ip.magic("timeit -n1 -r1 a=('#')")
General Comments 0
You need to be logged in to leave comments. Login now