Skip to content
Browse files

BUG: Allow %magic argument filenames with spaces to be specified with…

… quotes under win32.
  • Loading branch information...
1 parent 0e2e358 commit de1bf8aa601096e591473a1083755109a6fd550d @rkern rkern committed
Showing with 59 additions and 14 deletions.
  1. +5 −5 IPython/core/magic.py
  2. +3 −3 IPython/testing/globalipapp.py
  3. +13 −1 IPython/testing/tools.py
  4. +9 −2 IPython/utils/path.py
  5. +29 −3 IPython/utils/tests/test_path.py
View
10 IPython/core/magic.py
@@ -593,7 +593,7 @@ def magic_pfile(self, parameter_s=''):
# if not, try the input as a filename
if out == 'not found':
try:
- filename = get_py_filename(parameter_s)
+ filename = get_py_filename(parameter_s, sys.platform == 'win32')
except IOError,msg:
print msg
return
@@ -1369,7 +1369,7 @@ def magic_prun(self, parameter_s ='',user_mode=1,
namespace = self.shell.user_ns
else: # called to run a program by %run -p
try:
- filename = get_py_filename(arg_lst[0])
+ filename = get_py_filename(arg_lst[0], sys.platform == 'win32')
except IOError,msg:
error(msg)
return
@@ -1558,7 +1558,7 @@ def magic_run(self, parameter_s ='',runner=None,
mode='list',list_all=1)
try:
- filename = file_finder(arg_lst[0])
+ filename = file_finder(arg_lst[0], sys.platform == 'win32')
except IndexError:
warn('you must provide at least a filename.')
print '\n%run:\n',oinspect.getdoc(self.magic_run)
@@ -2128,7 +2128,7 @@ def _find_edit_target(self, args, opts, last_call):
def make_filename(arg):
"Make a filename from the given args"
try:
- filename = get_py_filename(arg)
+ filename = get_py_filename(arg, win32=sys.platform == 'win32')
except IOError:
# If it ends with .py but doesn't already exist, assume we want
# a new file.
@@ -3148,7 +3148,7 @@ def magic_pycat(self, parameter_s=''):
to be Python source and will show it with syntax highlighting. """
try:
- filename = get_py_filename(parameter_s)
+ filename = get_py_filename(parameter_s, sys.platform == 'win32')
cont = file_read(filename)
except IOError:
try:
View
6 IPython/testing/globalipapp.py
@@ -63,14 +63,14 @@ class py_file_finder(object):
def __init__(self,test_filename):
self.test_filename = test_filename
- def __call__(self,name):
+ def __call__(self,name,win32=False):
from IPython.utils.path import get_py_filename
try:
- return get_py_filename(name)
+ return get_py_filename(name,win32=win32)
except IOError:
test_dir = os.path.dirname(self.test_filename)
new_path = os.path.join(test_dir,name)
- return get_py_filename(new_path)
+ return get_py_filename(new_path,win32=win32)
def _run_ns_sync(self,arg_s,runner=None):
View
14 IPython/testing/tools.py
@@ -330,4 +330,16 @@ def mute_warn():
try:
yield
finally:
- warn.warn = save_warn
+ warn.warn = save_warn
+
+@contextmanager
+def make_tempfile(name):
+ """ Create an empty, named, temporary file for the duration of the context.
+ """
+ f = open(name, 'w')
+ f.close()
+ try:
+ yield
+ finally:
+ os.unlink(name)
+
View
11 IPython/utils/path.py
@@ -82,13 +82,20 @@ def get_long_path_name(path):
return _get_long_path_name(path)
-def get_py_filename(name):
+def get_py_filename(name, win32=False):
"""Return a valid python filename in the current directory.
If the given name is not a file, it adds '.py' and searches again.
- Raises IOError with an informative message if the file isn't found."""
+ Raises IOError with an informative message if the file isn't found.
+
+ If the win32 argument is True, then apply Windows semantics to the filename.
+ In particular, remove any quoting that has been applied to it.
+ """
name = os.path.expanduser(name)
+ if win32:
+ if name.startswith(("'", '"')) and name.endswith(("'", '"')):
+ name = name[1:-1]
if not os.path.isfile(name) and not name.endswith('.py'):
name += '.py'
if os.path.isfile(name):
View
32 IPython/utils/tests/test_path.py
@@ -12,6 +12,8 @@
# Imports
#-----------------------------------------------------------------------------
+from __future__ import with_statement
+
import os
import shutil
import sys
@@ -27,6 +29,7 @@
import IPython
from IPython.testing import decorators as dec
from IPython.testing.decorators import skip_if_not_win32, skip_win32
+from IPython.testing.tools import make_tempfile
from IPython.utils import path, io
# Platform-dependent imports
@@ -83,7 +86,7 @@ def setup_environment():
each testfunction needs a pristine environment.
"""
global oldstuff, platformstuff
- oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__)
+ oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__, os.getcwd())
if os.name == 'nt':
platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
@@ -92,7 +95,8 @@ def setup_environment():
def teardown_environment():
"""Restore things that were remebered by the setup_environment function
"""
- (oldenv, os.name, path.get_home_dir, IPython.__file__,) = oldstuff
+ (oldenv, os.name, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
+ os.chdir(old_wd)
reload(path)
for key in env.keys():
@@ -401,4 +405,26 @@ def test_not_writable_ipdir():
io.stderr = stderr
nt.assert_true('WARNING' in pipe.getvalue())
env.pop('IPYTHON_DIR', None)
-
+
+@with_environment
+def test_get_py_filename():
+ os.chdir(TMP_TEST_DIR)
+ for win32 in (True, False):
+ with make_tempfile('foo.py'):
+ nt.assert_equals(path.get_py_filename('foo.py', win32=win32), 'foo.py')
+ nt.assert_equals(path.get_py_filename('foo', win32=win32), 'foo.py')
+ with make_tempfile('foo'):
+ nt.assert_equals(path.get_py_filename('foo', win32=win32), 'foo')
+ nt.assert_raises(IOError, path.get_py_filename, 'foo.py', win32=win32)
+ nt.assert_raises(IOError, path.get_py_filename, 'foo', win32=win32)
+ nt.assert_raises(IOError, path.get_py_filename, 'foo.py', win32=win32)
+ true_fn = 'foo with spaces.py'
+ with make_tempfile(true_fn):
+ nt.assert_equals(path.get_py_filename('foo with spaces', win32=win32), true_fn)
+ nt.assert_equals(path.get_py_filename('foo with spaces.py', win32=win32), true_fn)
+ if win32:
+ nt.assert_equals(path.get_py_filename('"foo with spaces.py"', win32=True), true_fn)
+ nt.assert_equals(path.get_py_filename("'foo with spaces.py'", win32=True), true_fn)
+ else:
+ nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', win32=False)
+ nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", win32=False)

0 comments on commit de1bf8a

Please sign in to comment.
Something went wrong with that request. Please try again.