Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #11309 (handle quotes in magics arguments) #11330

Merged
merged 6 commits into from
Oct 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions IPython/core/magics/osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,11 @@ def writefile(self, line, cell):
The file will be overwritten unless the -a (--append) flag is specified.
"""
args = magic_arguments.parse_argstring(self.writefile, line)
filename = os.path.expanduser(args.filename)

if re.match(r'[\'*\']|["*"]', args.filename):
filename = os.path.expanduser(args.filename[1:-1])
else:
filename = os.path.expanduser(args.filename)

if os.path.exists(filename):
if args.append:
print("Appending to %s" % filename)
Expand Down
18 changes: 16 additions & 2 deletions IPython/core/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
from IPython.testing import decorators as dec
from IPython.testing import tools as tt
from IPython.utils.io import capture_output
from IPython.utils.tempdir import TemporaryDirectory
from IPython.utils.tempdir import (TemporaryDirectory,
TemporaryWorkingDirectory)
from IPython.utils.process import find_cmd


Expand Down Expand Up @@ -797,7 +798,20 @@ def test_file_amend():
s = f.read()
nt.assert_in('line1\n', s)
nt.assert_in('line3\n', s)


def test_file_spaces():
"""%%file with spaces in filename"""
ip = get_ipython()
with TemporaryWorkingDirectory() as td:
fname = "file name"
ip.run_cell_magic("file", '"%s"'%fname, u'\n'.join([
'line1',
'line2',
]))
with open(fname) as f:
s = f.read()
nt.assert_in('line1\n', s)
nt.assert_in('line2', s)

def test_script_config():
ip = get_ipython()
Expand Down