##// END OF EJS Templates
Merge pull request #9516 from anntzer/windows-spaces...
Thomas Kluyver -
r22439:10073b6f merge
parent child Browse files
Show More
@@ -117,9 +117,14 b' def has_open_quotes(s):'
117 117
118 118 def protect_filename(s):
119 119 """Escape a string to protect certain characters."""
120 if set(s) & set(PROTECTABLES):
121 if sys.platform == "win32":
122 return '"' + s + '"'
123 else:
124 return "".join(("\\" + c if c in PROTECTABLES else c) for c in s)
125 else:
126 return s
120 127
121 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
122 for ch in s])
123 128
124 129 def expand_user(path):
125 130 """Expand '~'-style usernames in strings.
@@ -67,7 +67,7 b' from IPython.utils.decorators import undoc'
67 67 from IPython.utils.io import ask_yes_no
68 68 from IPython.utils.ipstruct import Struct
69 69 from IPython.paths import get_ipython_dir
70 from IPython.utils.path import get_home_dir, get_py_filename, unquote_filename, ensure_dir_exists
70 from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists
71 71 from IPython.utils.process import system, getoutput
72 72 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
73 73 with_metaclass, iteritems)
@@ -3128,10 +3128,9 b' class InteractiveShell(SingletonConfigurable):'
3128 3128 code = self.extract_input_lines(target, raw=raw) # Grab history
3129 3129 if code:
3130 3130 return code
3131 utarget = unquote_filename(target)
3132 3131 try:
3133 if utarget.startswith(('http://', 'https://')):
3134 return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie)
3132 if target.startswith(('http://', 'https://')):
3133 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3135 3134 except UnicodeDecodeError:
3136 3135 if not py_only :
3137 3136 # Deferred import
@@ -3141,7 +3140,7 b' class InteractiveShell(SingletonConfigurable):'
3141 3140 from urllib import urlopen
3142 3141 response = urlopen(target)
3143 3142 return response.read().decode('latin1')
3144 raise ValueError(("'%s' seem to be unreadable.") % utarget)
3143 raise ValueError(("'%s' seem to be unreadable.") % target)
3145 3144
3146 3145 potential_target = [target]
3147 3146 try :
@@ -13,7 +13,6 b' from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes'
13 13 from IPython.utils.text import format_screen, dedent, indent
14 14 from IPython.testing.skipdoctest import skip_doctest
15 15 from IPython.utils.ipstruct import Struct
16 from IPython.utils.path import unquote_filename
17 16 from IPython.utils.py3compat import unicode_type
18 17 from warnings import warn
19 18 from logging import error
@@ -583,7 +582,6 b' Defaulting color scheme to \'NoColor\'"""'
583 582 args = magic_arguments.parse_argstring(self.notebook, s)
584 583
585 584 from nbformat import write, v4
586 args.filename = unquote_filename(args.filename)
587 585 if args.export:
588 586 cells = []
589 587 hist = list(self.shell.history_manager.get_range())
@@ -32,7 +32,7 b' from IPython.testing.skipdoctest import skip_doctest'
32 32 from IPython.utils import py3compat
33 33 from IPython.utils.py3compat import string_types
34 34 from IPython.utils.contexts import preserve_keys
35 from IPython.utils.path import get_py_filename, unquote_filename
35 from IPython.utils.path import get_py_filename
36 36 from warnings import warn
37 37 from logging import error
38 38 from IPython.utils.text import get_text_list
@@ -189,7 +189,7 b' class CodeMagics(Magics):'
189 189 append = 'a' in opts
190 190 mode = 'a' if append else 'w'
191 191 ext = u'.ipy' if raw else u'.py'
192 fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
192 fname, codefrom = args[0], " ".join(args[1:])
193 193 if not fname.endswith((u'.py',u'.ipy')):
194 194 fname += ext
195 195 file_exists = os.path.isfile(fname)
@@ -369,7 +369,6 b' class CodeMagics(Magics):'
369 369
370 370 def make_filename(arg):
371 371 "Make a filename from the given args"
372 arg = unquote_filename(arg)
373 372 try:
374 373 filename = get_py_filename(arg)
375 374 except IOError:
@@ -42,7 +42,7 b' from IPython.utils.contexts import preserve_keys'
42 42 from IPython.utils.capture import capture_output
43 43 from IPython.utils.ipstruct import Struct
44 44 from IPython.utils.module_paths import find_mod
45 from IPython.utils.path import get_py_filename, unquote_filename, shellglob
45 from IPython.utils.path import get_py_filename, shellglob
46 46 from IPython.utils.timing import clock, clock2
47 47 from warnings import warn
48 48 from logging import error
@@ -338,12 +338,10 b' python-profiler package from non-free.""")'
338 338 dump_file = opts.D[0]
339 339 text_file = opts.T[0]
340 340 if dump_file:
341 dump_file = unquote_filename(dump_file)
342 341 prof.dump_stats(dump_file)
343 342 print('\n*** Profile stats marshalled to file',\
344 343 repr(dump_file)+'.',sys_exit)
345 344 if text_file:
346 text_file = unquote_filename(text_file)
347 345 pfile = open(text_file,'w')
348 346 pfile.write(output)
349 347 pfile.close()
@@ -34,7 +34,6 b' from IPython.core.magic import ('
34 34 )
35 35 from IPython.testing.skipdoctest import skip_doctest
36 36 from IPython.utils.openpy import source_to_unicode
37 from IPython.utils.path import unquote_filename
38 37 from IPython.utils.process import abbrev_cwd
39 38 from IPython.utils import py3compat
40 39 from IPython.utils.py3compat import unicode_type
@@ -324,10 +323,7 b' class OSMagics(Magics):'
324 323
325 324
326 325 else:
327 #turn all non-space-escaping backslashes to slashes,
328 # for c:\windows\directory\names\
329 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
330 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
326 opts, ps = self.parse_options(parameter_s, 'qb', mode='string')
331 327 # jump to previous
332 328 if ps == '-':
333 329 try:
@@ -348,8 +344,6 b' class OSMagics(Magics):'
348 344 raise UsageError("Bookmark '%s' not found. "
349 345 "Use '%%bookmark -l' to see your bookmarks." % ps)
350 346
351 # strip extra quotes on Windows, because os.chdir doesn't like them
352 ps = unquote_filename(ps)
353 347 # at this point ps should point to the target dir
354 348 if ps:
355 349 try:
@@ -443,7 +437,7 b' class OSMagics(Magics):'
443 437 """
444 438
445 439 dir_s = self.shell.dir_stack
446 tgt = os.path.expanduser(unquote_filename(parameter_s))
440 tgt = os.path.expanduser(parameter_s)
447 441 cwd = py3compat.getcwd().replace(self.shell.home_dir,'~')
448 442 if tgt:
449 443 self.cd(parameter_s)
@@ -781,8 +775,8 b' class OSMagics(Magics):'
781 775 The file will be overwritten unless the -a (--append) flag is specified.
782 776 """
783 777 args = magic_arguments.parse_argstring(self.writefile, line)
784 filename = os.path.expanduser(unquote_filename(args.filename))
785
778 filename = os.path.expanduser(args.filename)
779
786 780 if os.path.exists(filename):
787 781 if args.append:
788 782 print("Appending to %s" % filename)
@@ -36,32 +36,38 b' def greedy_completion():'
36 36 ip.Completer.greedy = greedy_original
37 37
38 38 def test_protect_filename():
39 pairs = [ ('abc','abc'),
40 (' abc',r'\ abc'),
41 ('a bc',r'a\ bc'),
42 ('a bc',r'a\ \ bc'),
43 (' bc',r'\ \ bc'),
44 ]
45 # On posix, we also protect parens and other special characters
46 if sys.platform != 'win32':
47 pairs.extend( [('a(bc',r'a\(bc'),
48 ('a)bc',r'a\)bc'),
49 ('a( )bc',r'a\(\ \)bc'),
50 ('a[1]bc', r'a\[1\]bc'),
51 ('a{1}bc', r'a\{1\}bc'),
52 ('a#bc', r'a\#bc'),
53 ('a?bc', r'a\?bc'),
54 ('a=bc', r'a\=bc'),
55 ('a\\bc', r'a\\bc'),
56 ('a|bc', r'a\|bc'),
57 ('a;bc', r'a\;bc'),
58 ('a:bc', r'a\:bc'),
59 ("a'bc", r"a\'bc"),
60 ('a*bc', r'a\*bc'),
61 ('a"bc', r'a\"bc'),
62 ('a^bc', r'a\^bc'),
63 ('a&bc', r'a\&bc'),
64 ] )
39 if sys.platform == 'win32':
40 pairs = [ ('abc','abc'),
41 (' abc',"' abc'"),
42 ('a bc',"'a bc'"),
43 ('a bc',"'a bc'"),
44 (' bc',"' bc'"),
45 ]
46 else:
47 pairs = [ ('abc','abc'),
48 (' abc',r'\ abc'),
49 ('a bc',r'a\ bc'),
50 ('a bc',r'a\ \ bc'),
51 (' bc',r'\ \ bc'),
52 # On posix, we also protect parens and other special characters
53 ('a(bc',r'a\(bc'),
54 ('a)bc',r'a\)bc'),
55 ('a( )bc',r'a\(\ \)bc'),
56 ('a[1]bc', r'a\[1\]bc'),
57 ('a{1}bc', r'a\{1\}bc'),
58 ('a#bc', r'a\#bc'),
59 ('a?bc', r'a\?bc'),
60 ('a=bc', r'a\=bc'),
61 ('a\\bc', r'a\\bc'),
62 ('a|bc', r'a\|bc'),
63 ('a;bc', r'a\;bc'),
64 ('a:bc', r'a\:bc'),
65 ("a'bc", r"a\'bc"),
66 ('a*bc', r'a\*bc'),
67 ('a"bc', r'a\"bc'),
68 ('a^bc', r'a\^bc'),
69 ('a&bc', r'a\&bc'),
70 ]
65 71 # run the actual tests
66 72 for s1, s2 in pairs:
67 73 s1p = completer.protect_filename(s1)
@@ -73,15 +73,20 b' def get_long_path_name(path):'
73 73
74 74 def unquote_filename(name, win32=(sys.platform=='win32')):
75 75 """ On Windows, remove leading and trailing quotes from filenames.
76
77 This function has been deprecated and should not be used any more:
78 unquoting is now taken care of by :func:`IPython.utils.process.arg_split`.
76 79 """
80 warn("'unquote_filename' is deprecated", DeprecationWarning)
77 81 if win32:
78 82 if name.startswith(("'", '"')) and name.endswith(("'", '"')):
79 83 name = name[1:-1]
80 84 return name
81 85
86
82 87 def compress_user(path):
83 88 """Reverse of :func:`os.path.expanduser`
84 """
89 """
85 90 path = py3compat.unicode_to_str(path, sys.getfilesystemencoding())
86 91 home = os.path.expanduser('~')
87 92 if path.startswith(home):
@@ -302,19 +302,6 b' def test_not_writable_ipdir():'
302 302 ipdir = paths.get_ipython_dir()
303 303 env.pop('IPYTHON_DIR', None)
304 304
305 def test_unquote_filename():
306 for win32 in (True, False):
307 nt.assert_equal(path.unquote_filename('foo.py', win32=win32), 'foo.py')
308 nt.assert_equal(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
309 nt.assert_equal(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
310 nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
311 nt.assert_equal(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
312 nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
313 nt.assert_equal(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
314 nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
315 nt.assert_equal(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
316 nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
317
318 305 @with_environment
319 306 def test_get_py_filename():
320 307 os.chdir(TMP_TEST_DIR)
General Comments 0
You need to be logged in to leave comments. Login now