summaryrefslogtreecommitdiff
path: root/astroid/modutils.py
diff options
context:
space:
mode:
authorClaudiu Popa <cpopa@cloudbasesolutions.com>2014-11-17 00:34:50 +0200
committerClaudiu Popa <cpopa@cloudbasesolutions.com>2014-11-17 00:34:50 +0200
commitb1b9e5c5e9c12dbcfd6a12f7635f020e1df06a7b (patch)
tree91a6a36ddb998a9dcd7155d0fb4159f930415dee /astroid/modutils.py
parentcd4dc6e9b59700dabed80fcb7e616986c24c32a6 (diff)
downloadastroid-b1b9e5c5e9c12dbcfd6a12f7635f020e1df06a7b.tar.gz
Close all the streams returned by imp.find_module.
Diffstat (limited to 'astroid/modutils.py')
-rw-r--r--astroid/modutils.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/astroid/modutils.py b/astroid/modutils.py
index 23a476f..45fe898 100644
--- a/astroid/modutils.py
+++ b/astroid/modutils.py
@@ -176,6 +176,9 @@ def load_module_from_modpath(parts, path=None, use_sys=1):
if module is None:
mp_file, mp_filename, mp_desc = imp.find_module(part, path)
module = imp.load_module(curname, mp_file, mp_filename, mp_desc)
+ # mp_file still needs to be closed.
+ if mp_file:
+ mp_file.close()
if prevmodule:
setattr(prevmodule, part, module)
_file = getattr(module, '__file__', '')
@@ -492,7 +495,11 @@ def is_relative(modname, from_file):
if from_file in sys.path:
return False
try:
- imp.find_module(modname.split('.')[0], [from_file])
+ stream, _, _ = imp.find_module(modname.split('.')[0], [from_file])
+
+ # Close the stream to avoid ResourceWarnings.
+ if stream:
+ stream.close()
return True
except ImportError:
return False
@@ -591,12 +598,17 @@ def _module_file(modpath, path=None):
# >>> imp.find_module('posix')
# (None, None, ('', '', 6))
try:
- _, mp_filename, mp_desc = imp.find_module(modname, path)
+ stream, mp_filename, mp_desc = imp.find_module(modname, path)
except ImportError:
if checkeggs:
return _search_zip(modpath, pic)[:2]
raise
else:
+ # Don't forget to close the stream to avoid
+ # spurious ResourceWarnings.
+ if stream:
+ stream.close()
+
if checkeggs and mp_filename:
fullabspath = [_cache_normalize_path(x) for x in _path]
try: