##// END OF EJS Templates
Allow pwd and cd to work when CWD is deleted...
Thomas Kluyver -
Show More
@@ -3,26 +3,15 b''
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 #-----------------------------------------------------------------------------
6 # Copyright (c) IPython Development Team.
7 # Copyright (c) 2012 The IPython Development Team.
7 # Distributed under the terms of the Modified BSD License.
8 #
8
9 # Distributed under the terms of the Modified BSD License.
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17
18 # Stdlib
19 import io
9 import io
20 import os
10 import os
21 import re
11 import re
22 import sys
12 import sys
23 from pprint import pformat
13 from pprint import pformat
24
14
25 # Our own packages
26 from IPython.core import magic_arguments
15 from IPython.core import magic_arguments
27 from IPython.core import oinspect
16 from IPython.core import oinspect
28 from IPython.core import page
17 from IPython.core import page
@@ -37,9 +26,7 b' from IPython.utils.process import abbrev_cwd'
37 from IPython.utils import py3compat
26 from IPython.utils import py3compat
38 from IPython.utils.terminal import set_term_title
27 from IPython.utils.terminal import set_term_title
39
28
40 #-----------------------------------------------------------------------------
29
41 # Magic implementation classes
42 #-----------------------------------------------------------------------------
43 @magics_class
30 @magics_class
44 class OSMagics(Magics):
31 class OSMagics(Magics):
45 """Magics to interact with the underlying OS (shell-type functionality).
32 """Magics to interact with the underlying OS (shell-type functionality).
@@ -239,7 +226,10 b' class OSMagics(Magics):'
239 In [9]: pwd
226 In [9]: pwd
240 Out[9]: '/home/tsuser/sprint/ipython'
227 Out[9]: '/home/tsuser/sprint/ipython'
241 """
228 """
242 return os.getcwd()
229 try:
230 return os.getcwd()
231 except FileNotFoundError:
232 raise UsageError("CWD no longer exists - please use %cd to change directory.")
243
233
244 @skip_doctest
234 @skip_doctest
245 @line_magic
235 @line_magic
@@ -283,7 +273,12 b' class OSMagics(Magics):'
283 /home/tsuser/parent/child
273 /home/tsuser/parent/child
284 """
274 """
285
275
286 oldcwd = os.getcwd()
276 try:
277 oldcwd = os.getcwd()
278 except FileNotFoundError:
279 # Happens if the CWD has been deleted.
280 oldcwd = None
281
287 numcd = re.match(r'(-)(\d+)$',parameter_s)
282 numcd = re.match(r'(-)(\d+)$',parameter_s)
288 # jump in directory history by number
283 # jump in directory history by number
289 if numcd:
284 if numcd:
General Comments 0
You need to be logged in to leave comments. Login now