Show More
@@ -593,7 +593,7 b' Currently the magic system has the following functions:\\n"""' | |||
|
593 | 593 | # if not, try the input as a filename |
|
594 | 594 | if out == 'not found': |
|
595 | 595 | try: |
|
596 | filename = get_py_filename(parameter_s) | |
|
596 | filename = get_py_filename(parameter_s, sys.platform == 'win32') | |
|
597 | 597 | except IOError,msg: |
|
598 | 598 | print msg |
|
599 | 599 | return |
@@ -1369,7 +1369,7 b' Currently the magic system has the following functions:\\n"""' | |||
|
1369 | 1369 | namespace = self.shell.user_ns |
|
1370 | 1370 | else: # called to run a program by %run -p |
|
1371 | 1371 | try: |
|
1372 | filename = get_py_filename(arg_lst[0]) | |
|
1372 | filename = get_py_filename(arg_lst[0], sys.platform == 'win32') | |
|
1373 | 1373 | except IOError,msg: |
|
1374 | 1374 | error(msg) |
|
1375 | 1375 | return |
@@ -1558,7 +1558,7 b' Currently the magic system has the following functions:\\n"""' | |||
|
1558 | 1558 | mode='list',list_all=1) |
|
1559 | 1559 | |
|
1560 | 1560 | try: |
|
1561 | filename = file_finder(arg_lst[0]) | |
|
1561 | filename = file_finder(arg_lst[0], sys.platform == 'win32') | |
|
1562 | 1562 | except IndexError: |
|
1563 | 1563 | warn('you must provide at least a filename.') |
|
1564 | 1564 | print '\n%run:\n',oinspect.getdoc(self.magic_run) |
@@ -2128,7 +2128,7 b' Currently the magic system has the following functions:\\n"""' | |||
|
2128 | 2128 | def make_filename(arg): |
|
2129 | 2129 | "Make a filename from the given args" |
|
2130 | 2130 | try: |
|
2131 | filename = get_py_filename(arg) | |
|
2131 | filename = get_py_filename(arg, win32=sys.platform == 'win32') | |
|
2132 | 2132 | except IOError: |
|
2133 | 2133 | # If it ends with .py but doesn't already exist, assume we want |
|
2134 | 2134 | # a new file. |
@@ -3148,7 +3148,7 b' Defaulting color scheme to \'NoColor\'"""' | |||
|
3148 | 3148 | to be Python source and will show it with syntax highlighting. """ |
|
3149 | 3149 | |
|
3150 | 3150 | try: |
|
3151 | filename = get_py_filename(parameter_s) | |
|
3151 | filename = get_py_filename(parameter_s, sys.platform == 'win32') | |
|
3152 | 3152 | cont = file_read(filename) |
|
3153 | 3153 | except IOError: |
|
3154 | 3154 | try: |
@@ -63,14 +63,14 b' class py_file_finder(object):' | |||
|
63 | 63 | def __init__(self,test_filename): |
|
64 | 64 | self.test_filename = test_filename |
|
65 | 65 | |
|
66 | def __call__(self,name): | |
|
66 | def __call__(self,name,win32=False): | |
|
67 | 67 | from IPython.utils.path import get_py_filename |
|
68 | 68 | try: |
|
69 | return get_py_filename(name) | |
|
69 | return get_py_filename(name,win32=win32) | |
|
70 | 70 | except IOError: |
|
71 | 71 | test_dir = os.path.dirname(self.test_filename) |
|
72 | 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 | 76 | def _run_ns_sync(self,arg_s,runner=None): |
@@ -330,4 +330,16 b' def mute_warn():' | |||
|
330 | 330 | try: |
|
331 | 331 | yield |
|
332 | 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 b' def get_long_path_name(path):' | |||
|
82 | 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 | 86 | """Return a valid python filename in the current directory. |
|
87 | 87 | |
|
88 | 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 | 95 | name = os.path.expanduser(name) |
|
96 | if win32: | |
|
97 | if name.startswith(("'", '"')) and name.endswith(("'", '"')): | |
|
98 | name = name[1:-1] | |
|
92 | 99 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
93 | 100 | name += '.py' |
|
94 | 101 | if os.path.isfile(name): |
@@ -12,6 +12,8 b'' | |||
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | from __future__ import with_statement | |
|
16 | ||
|
15 | 17 | import os |
|
16 | 18 | import shutil |
|
17 | 19 | import sys |
@@ -27,6 +29,7 b' from nose import with_setup' | |||
|
27 | 29 | import IPython |
|
28 | 30 | from IPython.testing import decorators as dec |
|
29 | 31 | from IPython.testing.decorators import skip_if_not_win32, skip_win32 |
|
32 | from IPython.testing.tools import make_tempfile | |
|
30 | 33 | from IPython.utils import path, io |
|
31 | 34 | |
|
32 | 35 | # Platform-dependent imports |
@@ -83,7 +86,7 b' def setup_environment():' | |||
|
83 | 86 | each testfunction needs a pristine environment. |
|
84 | 87 | """ |
|
85 | 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 | 91 | if os.name == 'nt': |
|
89 | 92 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) |
@@ -92,7 +95,8 b' def setup_environment():' | |||
|
92 | 95 | def teardown_environment(): |
|
93 | 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 | 100 | reload(path) |
|
97 | 101 | |
|
98 | 102 | for key in env.keys(): |
@@ -401,4 +405,26 b' def test_not_writable_ipdir():' | |||
|
401 | 405 | io.stderr = stderr |
|
402 | 406 | nt.assert_true('WARNING' in pipe.getvalue()) |
|
403 | 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