Skip to content

Commit e9651ae

Browse files
committed
Immediately close file object
1 parent 3ae9b10 commit e9651ae

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

bpython/importcompletion.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
SUFFIXES = importlib.machinery.all_suffixes()
4545
else:
46+
import imp
47+
4648
SUFFIXES = [suffix for suffix, mode, type in imp.get_suffixes()]
4749

4850
# The cached list of all known modules
@@ -158,20 +160,23 @@ def find_modules(path):
158160
# Workaround for issue #166
159161
continue
160162
try:
163+
is_package = False
161164
with warnings.catch_warnings():
162165
warnings.simplefilter("ignore", ImportWarning)
163166
fo, pathname, _ = imp.find_module(name, [path])
167+
if fo is not None:
168+
fo.close()
169+
else:
170+
# Yay, package
171+
is_package = True
164172
except (ImportError, IOError, SyntaxError):
165173
continue
166174
except UnicodeEncodeError:
167175
# Happens with Python 3 when there is a filename in some
168176
# invalid encoding
169177
continue
170178
else:
171-
if fo is not None:
172-
fo.close()
173-
else:
174-
# Yay, package
179+
if is_package:
175180
for subname in find_modules(pathname):
176181
if subname != "__init__":
177182
yield "%s.%s" % (name, subname)

0 commit comments

Comments
 (0)