diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2014-11-17 00:12:14 +0200 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2014-11-17 00:12:14 +0200 |
commit | 44bb1b1cd4e8c140ceadb22e7382e075e0b0c928 (patch) | |
tree | 5b33a94f26e8ddf1bbdcc0388c52f2115cc2265d | |
parent | bb9152569e2ad9cb7a469fe082f2a4340636eb08 (diff) | |
download | astroid-44bb1b1cd4e8c140ceadb22e7382e075e0b0c928.tar.gz |
Close the file opened by open_source_file, to prevent ResourceWarnings.
-rw-r--r-- | astroid/builder.py | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/astroid/builder.py b/astroid/builder.py index 142825c..692261e 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -115,7 +115,7 @@ class AstroidBuilder(InspectBuilder): path is expected to be a python source file """ try: - _, encoding, data = open_source_file(path) + stream, encoding, data = open_source_file(path) except IOError as exc: msg = 'Unable to load file %r (%s)' % (path, exc) raise AstroidBuildingException(msg) @@ -123,15 +123,16 @@ class AstroidBuilder(InspectBuilder): raise AstroidBuildingException(exc) except LookupError as exc: # unknown encoding raise AstroidBuildingException(exc) - # get module name if necessary - if modname is None: - try: - modname = '.'.join(modpath_from_file(path)) - except ImportError: - modname = splitext(basename(path))[0] - # build astroid representation - module = self._data_build(data, modname, path) - return self._post_build(module, encoding) + with stream: + # get module name if necessary + if modname is None: + try: + modname = '.'.join(modpath_from_file(path)) + except ImportError: + modname = splitext(basename(path))[0] + # build astroid representation + module = self._data_build(data, modname, path) + return self._post_build(module, encoding) def string_build(self, data, modname='', path=None): """build astroid from source code string and return rebuilded astroid""" |