Handle bad __all__ for module completions...
Thomas Kluyver -
r22618:12e05563
Not Reviewed
Show More
Add another comment
TODOs: 0 unresolved 0 Resolved
COMMENTS: 0 General 0 Inline
@@ -0,0 +1,14
1 """Module with bad __all__
2
3 To test https://github.com/ipython/ipython/issues/9678
4 """
5
6 def evil():
7 pass
8
9 def puppies():
10 pass
11
12 __all__ = [evil, # Bad
13 'puppies', # Good
14 ]
@@ -153,7 +153,6 def is_importable(module, attr, only_modules):
153 else:
153 else:
154 return not(attr[:2] == '__' and attr[-2:] == '__')
154 return not(attr[:2] == '__' and attr[-2:] == '__')
155
155
156
157 def try_import(mod, only_modules=False):
156 def try_import(mod, only_modules=False):
158 try:
157 try:
159 m = __import__(mod)
158 m = __import__(mod)
@@ -173,9 +172,8 def try_import(mod, only_modules=False):
173 completions.extend(getattr(m, '__all__', []))
172 completions.extend(getattr(m, '__all__', []))
174 if m_is_init:
173 if m_is_init:
175 completions.extend(module_list(os.path.dirname(m.__file__)))
174 completions.extend(module_list(os.path.dirname(m.__file__)))
176 completions = set(completions)
175 completions = {c for c in completions if isinstance(c, string_types)}
177 if '__init__' in completions:
176 completions.discard('__init__')
178 completions.remove('__init__')
179 return list(completions)
177 return list(completions)
180
178
181
179
@@ -145,3 +145,19 def test_import_invalid_module():
145 nt.assert_equal(intersection, set())
145 nt.assert_equal(intersection, set())
146
146
147 assert valid_module_names.issubset(s), valid_module_names.intersection(s)
147 assert valid_module_names.issubset(s), valid_module_names.intersection(s)
148
149
150 def test_bad_module_all():
151 """Test module with invalid __all__
152
153 https://github.com/ipython/ipython/issues/9678
154 """
155 testsdir = os.path.dirname(__file__)
156 sys.path.insert(0, testsdir)
157 try:
158 results = module_completion('from bad_all import ')
159 nt.assert_in('puppies', results)
160 for r in results:
161 nt.assert_is_instance(r, py3compat.string_types)
162 finally:
163 sys.path.remove(testsdir)
Comments 0
You need to be logged in to leave comments. Login now