pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/ipython/ipython/commit/e0059071af154d5c998828dfd334e76a1baf5d34

60c69660fa.css" /> Merge pull request #4895 from takluyver/i3459 · ipython/ipython@e005907 · GitHub
Skip to content

Commit e005907

Browse files
committed
Merge pull request #4895 from takluyver/i3459
Improvements to %run completions
2 parents 8e33b80 + 88f0b3d commit e005907

2 files changed

Lines changed: 57 additions & 24 deletions

File tree

IPython/core/completerlib.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,11 @@ def magic_run_completer(self, event):
260260
"""Complete files that end in .py or .ipy or .ipynb for the %run command.
261261
"""
262262
comps = arg_split(event.line, strict=False)
263-
relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"")
263+
# relpath should be the current token that we need to complete.
264+
if (len(comps) > 1) and (not event.line.endswith(' ')):
265+
relpath = comps[-1].strip("'\"")
266+
else:
267+
relpath = ''
264268

265269
#print("\nev=", event) # dbg
266270
#print("rp=", relpath) # dbg
@@ -270,20 +274,23 @@ def magic_run_completer(self, event):
270274
isdir = os.path.isdir
271275
relpath, tilde_expand, tilde_val = expand_user(relpath)
272276

273-
dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)]
274-
275277
# Find if the user has already typed the first filename, after which we
276278
# should complete on all files, since after the first one other files may
277279
# be arguments to the input script.
278280

279281
if any(magic_run_re.match(c) for c in comps):
280-
pys = [f.replace('\\','/') for f in lglob('*')]
282+
matches = [f.replace('\\','/') + ('/' if isdir(f) else '')
283+
for f in lglob(relpath+'*')]
281284
else:
285+
dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)]
282286
pys = [f.replace('\\','/')
283287
for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') +
284288
lglob(relpath+'*.ipynb') + lglob(relpath + '*.pyw')]
289+
290+
matches = dirs + pys
291+
285292
#print('run comp:', dirs+pys) # dbg
286-
return [compress_user(p, tilde_expand, tilde_val) for p in dirs+pys]
293+
return [compress_user(p, tilde_expand, tilde_val) for p in matches]
287294

288295

289296
def cd_completer(self, event):

IPython/core/tests/test_completerlib.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import unittest
1616
from os.path import join
1717

18+
import nose.tools as nt
19+
1820
from IPython.core.completerlib import magic_run_completer, module_completion
1921
from IPython.utils import py3compat
2022
from IPython.utils.tempdir import TemporaryDirectory
@@ -29,11 +31,17 @@ def __init__(self, line):
2931
# Test functions begin
3032
#-----------------------------------------------------------------------------
3133
class Test_magic_run_completer(unittest.TestCase):
34+
files = [u"aao.py", u"a.py", u"b.py", u"aao.txt"]
35+
dirs = [u"adir/", "bdir/"]
36+
3237
def setUp(self):
3338
self.BASETESTDIR = tempfile.mkdtemp()
34-
for fil in [u"aao.py", u"a.py", u"b.py"]:
39+
for fil in self.files:
3540
with open(join(self.BASETESTDIR, fil), "w") as sfile:
3641
sfile.write("pass\n")
42+
for d in self.dirs:
43+
os.mkdir(join(self.BASETESTDIR, d))
44+
3745
self.oldpath = py3compat.getcwd()
3846
os.chdir(self.BASETESTDIR)
3947

@@ -47,7 +55,7 @@ def test_1(self):
4755
event = MockEvent(u"%run a")
4856
mockself = None
4957
match = set(magic_run_completer(mockself, event))
50-
self.assertEqual(match, set([u"a.py", u"aao.py"]))
58+
self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})
5159

5260
def test_2(self):
5361
"""Test magic_run_completer, should match one alterntive
@@ -62,23 +70,23 @@ def test_3(self):
6270
event = MockEvent(u'%run "a')
6371
mockself = None
6472
match = set(magic_run_completer(mockself, event))
65-
self.assertEqual(match, set([u"a.py", u"aao.py"]))
66-
67-
def test_import_invalid_module(self):
68-
"""Testing of issue https://github.com/ipython/ipython/issues/1107"""
69-
invalid_module_names = set(['foo-bar', 'foo:bar', '10foo'])
70-
valid_module_names = set(['foobar'])
71-
with TemporaryDirectory() as tmpdir:
72-
sys.path.insert( 0, tmpdir )
73-
for name in invalid_module_names | valid_module_names:
74-
filename = os.path.join(tmpdir, name + '.py')
75-
open(filename, 'w').close()
76-
77-
s = set( module_completion('import foo') )
78-
intersection = s.intersection(invalid_module_names)
79-
self.assertFalse(intersection, intersection)
80-
81-
assert valid_module_names.issubset(s), valid_module_names.intersection(s)
73+
self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})
74+
75+
def test_completion_more_args(self):
76+
event = MockEvent(u'%run a.py ')
77+
match = set(magic_run_completer(None, event))
78+
self.assertEqual(match, set(self.files + self.dirs))
79+
80+
def test_completion_in_dir(self):
81+
# Github issue #3459
82+
event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a')))
83+
print(repr(event.line))
84+
match = set(magic_run_completer(None, event))
85+
# We specifically use replace here rather than normpath, because
86+
# at one point there were duplicates 'adir' and 'adir/', and normpath
87+
# would hide the failure for that.
88+
self.assertEqual(match, {join(self.BASETESTDIR, f).replace('\\','/')
89+
for f in (u'a.py', u'aao.py', u'aao.txt', u'adir/')})
8290

8391
class Test_magic_run_completer_nonascii(unittest.TestCase):
8492
@onlyif_unicode_paths
@@ -119,3 +127,21 @@ def test_3(self):
119127
mockself = None
120128
match = set(magic_run_completer(mockself, event))
121129
self.assertEqual(match, set([u"a.py", u"aaø.py"]))
130+
131+
# module_completer:
132+
133+
def test_import_invalid_module():
134+
"""Testing of issue https://github.com/ipython/ipython/issues/1107"""
135+
invalid_module_names = set(['foo-bar', 'foo:bar', '10foo'])
136+
valid_module_names = set(['foobar'])
137+
with TemporaryDirectory() as tmpdir:
138+
sys.path.insert( 0, tmpdir )
139+
for name in invalid_module_names | valid_module_names:
140+
filename = os.path.join(tmpdir, name + '.py')
141+
open(filename, 'w').close()
142+
143+
s = set( module_completion('import foo') )
144+
intersection = s.intersection(invalid_module_names)
145+
nt.assert_equal(intersection, set())
146+
147+
assert valid_module_names.issubset(s), valid_module_names.intersection(s)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy