##// END OF EJS Templates
BUG: Allow %magic argument filenames with spaces to be specified with quotes under win32.
Robert Kern -
r4688:de1bf8aa
parent child
Show More
@@ -593,7 +593,7 Currently the magic system has the following functions:\n"""
593 # if not, try the input as a filename
593 # if not, try the input as a filename
594 if out == 'not found':
594 if out == 'not found':
595 try:
595 try:
596 filename = get_py_filename(parameter_s)
596 filename = get_py_filename(parameter_s, sys.platform == 'win32')
597 except IOError,msg:
597 except IOError,msg:
598 print msg
598 print msg
599 return
599 return
@@ -1369,7 +1369,7 Currently the magic system has the following functions:\n"""
1369 namespace = self.shell.user_ns
1369 namespace = self.shell.user_ns
1370 else: # called to run a program by %run -p
1370 else: # called to run a program by %run -p
1371 try:
1371 try:
1372 filename = get_py_filename(arg_lst[0])
1372 filename = get_py_filename(arg_lst[0], sys.platform == 'win32')
1373 except IOError,msg:
1373 except IOError,msg:
1374 error(msg)
1374 error(msg)
1375 return
1375 return
@@ -1558,7 +1558,7 Currently the magic system has the following functions:\n"""
1558 mode='list',list_all=1)
1558 mode='list',list_all=1)
1559
1559
1560 try:
1560 try:
1561 filename = file_finder(arg_lst[0])
1561 filename = file_finder(arg_lst[0], sys.platform == 'win32')
1562 except IndexError:
1562 except IndexError:
1563 warn('you must provide at least a filename.')
1563 warn('you must provide at least a filename.')
1564 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1564 print '\n%run:\n',oinspect.getdoc(self.magic_run)
@@ -2128,7 +2128,7 Currently the magic system has the following functions:\n"""
2128 def make_filename(arg):
2128 def make_filename(arg):
2129 "Make a filename from the given args"
2129 "Make a filename from the given args"
2130 try:
2130 try:
2131 filename = get_py_filename(arg)
2131 filename = get_py_filename(arg, win32=sys.platform == 'win32')
2132 except IOError:
2132 except IOError:
2133 # If it ends with .py but doesn't already exist, assume we want
2133 # If it ends with .py but doesn't already exist, assume we want
2134 # a new file.
2134 # a new file.
@@ -3148,7 +3148,7 Defaulting color scheme to 'NoColor'"""
3148 to be Python source and will show it with syntax highlighting. """
3148 to be Python source and will show it with syntax highlighting. """
3149
3149
3150 try:
3150 try:
3151 filename = get_py_filename(parameter_s)
3151 filename = get_py_filename(parameter_s, sys.platform == 'win32')
3152 cont = file_read(filename)
3152 cont = file_read(filename)
3153 except IOError:
3153 except IOError:
3154 try:
3154 try:
@@ -63,14 +63,14 class py_file_finder(object):
63 def __init__(self,test_filename):
63 def __init__(self,test_filename):
64 self.test_filename = test_filename
64 self.test_filename = test_filename
65
65
66 def __call__(self,name):
66 def __call__(self,name,win32=False):
67 from IPython.utils.path import get_py_filename
67 from IPython.utils.path import get_py_filename
68 try:
68 try:
69 return get_py_filename(name)
69 return get_py_filename(name,win32=win32)
70 except IOError:
70 except IOError:
71 test_dir = os.path.dirname(self.test_filename)
71 test_dir = os.path.dirname(self.test_filename)
72 new_path = os.path.join(test_dir,name)
72 new_path = os.path.join(test_dir,name)
73 return get_py_filename(new_path)
73 return get_py_filename(new_path,win32=win32)
74
74
75
75
76 def _run_ns_sync(self,arg_s,runner=None):
76 def _run_ns_sync(self,arg_s,runner=None):
@@ -330,4 +330,16 def mute_warn():
330 try:
330 try:
331 yield
331 yield
332 finally:
332 finally:
333 warn.warn = save_warn No newline at end of file
333 warn.warn = save_warn
334
335 @contextmanager
336 def make_tempfile(name):
337 """ Create an empty, named, temporary file for the duration of the context.
338 """
339 f = open(name, 'w')
340 f.close()
341 try:
342 yield
343 finally:
344 os.unlink(name)
345
@@ -82,13 +82,20 def get_long_path_name(path):
82 return _get_long_path_name(path)
82 return _get_long_path_name(path)
83
83
84
84
85 def get_py_filename(name):
85 def get_py_filename(name, win32=False):
86 """Return a valid python filename in the current directory.
86 """Return a valid python filename in the current directory.
87
87
88 If the given name is not a file, it adds '.py' and searches again.
88 If the given name is not a file, it adds '.py' and searches again.
89 Raises IOError with an informative message if the file isn't found."""
89 Raises IOError with an informative message if the file isn't found.
90
91 If the win32 argument is True, then apply Windows semantics to the filename.
92 In particular, remove any quoting that has been applied to it.
93 """
90
94
91 name = os.path.expanduser(name)
95 name = os.path.expanduser(name)
96 if win32:
97 if name.startswith(("'", '"')) and name.endswith(("'", '"')):
98 name = name[1:-1]
92 if not os.path.isfile(name) and not name.endswith('.py'):
99 if not os.path.isfile(name) and not name.endswith('.py'):
93 name += '.py'
100 name += '.py'
94 if os.path.isfile(name):
101 if os.path.isfile(name):
@@ -12,6 +12,8
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 from __future__ import with_statement
16
15 import os
17 import os
16 import shutil
18 import shutil
17 import sys
19 import sys
@@ -27,6 +29,7 from nose import with_setup
27 import IPython
29 import IPython
28 from IPython.testing import decorators as dec
30 from IPython.testing import decorators as dec
29 from IPython.testing.decorators import skip_if_not_win32, skip_win32
31 from IPython.testing.decorators import skip_if_not_win32, skip_win32
32 from IPython.testing.tools import make_tempfile
30 from IPython.utils import path, io
33 from IPython.utils import path, io
31
34
32 # Platform-dependent imports
35 # Platform-dependent imports
@@ -83,7 +86,7 def setup_environment():
83 each testfunction needs a pristine environment.
86 each testfunction needs a pristine environment.
84 """
87 """
85 global oldstuff, platformstuff
88 global oldstuff, platformstuff
86 oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__)
89 oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__, os.getcwd())
87
90
88 if os.name == 'nt':
91 if os.name == 'nt':
89 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
92 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
@@ -92,7 +95,8 def setup_environment():
92 def teardown_environment():
95 def teardown_environment():
93 """Restore things that were remebered by the setup_environment function
96 """Restore things that were remebered by the setup_environment function
94 """
97 """
95 (oldenv, os.name, path.get_home_dir, IPython.__file__,) = oldstuff
98 (oldenv, os.name, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
99 os.chdir(old_wd)
96 reload(path)
100 reload(path)
97
101
98 for key in env.keys():
102 for key in env.keys():
@@ -401,4 +405,26 def test_not_writable_ipdir():
401 io.stderr = stderr
405 io.stderr = stderr
402 nt.assert_true('WARNING' in pipe.getvalue())
406 nt.assert_true('WARNING' in pipe.getvalue())
403 env.pop('IPYTHON_DIR', None)
407 env.pop('IPYTHON_DIR', None)
404 No newline at end of file
408
409 @with_environment
410 def test_get_py_filename():
411 os.chdir(TMP_TEST_DIR)
412 for win32 in (True, False):
413 with make_tempfile('foo.py'):
414 nt.assert_equals(path.get_py_filename('foo.py', win32=win32), 'foo.py')
415 nt.assert_equals(path.get_py_filename('foo', win32=win32), 'foo.py')
416 with make_tempfile('foo'):
417 nt.assert_equals(path.get_py_filename('foo', win32=win32), 'foo')
418 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', win32=win32)
419 nt.assert_raises(IOError, path.get_py_filename, 'foo', win32=win32)
420 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', win32=win32)
421 true_fn = 'foo with spaces.py'
422 with make_tempfile(true_fn):
423 nt.assert_equals(path.get_py_filename('foo with spaces', win32=win32), true_fn)
424 nt.assert_equals(path.get_py_filename('foo with spaces.py', win32=win32), true_fn)
425 if win32:
426 nt.assert_equals(path.get_py_filename('"foo with spaces.py"', win32=True), true_fn)
427 nt.assert_equals(path.get_py_filename("'foo with spaces.py'", win32=True), true_fn)
428 else:
429 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', win32=False)
430 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", win32=False)
General Comments 0
You need to be logged in to leave comments. Login now