From a12c63842b28e29f3bc6718e6d940d5b697f010f Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Wed, 24 Aug 2011 19:25:59 -0700 Subject: python3.2 fixes: ResourceWarning: unclosed file --- pym/portage/news.py | 7 ++++--- pym/portage/output.py | 12 +++++++++--- pym/portage/package/ebuild/_config/LocationsManager.py | 10 +++++++--- pym/portage/repository/config.py | 9 +++++++-- pym/portage/tests/ebuild/test_doebuild_spawn.py | 4 ++-- pym/portage/util/__init__.py | 13 +++++++++---- 6 files changed, 38 insertions(+), 17 deletions(-) diff --git a/pym/portage/news.py b/pym/portage/news.py index 866e5b025..031e98c8c 100644 --- a/pym/portage/news.py +++ b/pym/portage/news.py @@ -250,10 +250,11 @@ class NewsItem(object): return self._valid def parse(self): - lines = io.open(_unicode_encode(self.path, + f = io.open(_unicode_encode(self.path, encoding=_encodings['fs'], errors='strict'), - mode='r', encoding=_encodings['content'], errors='replace' - ).readlines() + mode='r', encoding=_encodings['content'], errors='replace') + lines = f.readlines() + f.close() self.restrictions = {} invalids = [] for i, line in enumerate(lines): diff --git a/pym/portage/output.py b/pym/portage/output.py index 0e8245f9a..763d74a7a 100644 --- a/pym/portage/output.py +++ b/pym/portage/output.py @@ -162,11 +162,14 @@ def _parse_color_map(config_root='/', onerror=None): if token[0] in quotes and token[0] == token[-1]: token = token[1:-1] return token + + f = None try: - lineno=0 - for line in io.open(_unicode_encode(myfile, + f = io.open(_unicode_encode(myfile, encoding=_encodings['fs'], errors='strict'), - mode='r', encoding=_encodings['content'], errors='replace'): + mode='r', encoding=_encodings['content'], errors='replace') + lineno = 0 + for line in f: lineno += 1 commenter_pos = line.find("#") @@ -226,6 +229,9 @@ def _parse_color_map(config_root='/', onerror=None): elif e.errno == errno.EACCES: raise PermissionDenied(myfile) raise + finally: + if f is not None: + f.close() def nc_len(mystr): tmp = re.sub(esc_seq + "^m]+m", "", mystr); diff --git a/pym/portage/package/ebuild/_config/LocationsManager.py b/pym/portage/package/ebuild/_config/LocationsManager.py index c2b115bb0..4072ffcae 100644 --- a/pym/portage/package/ebuild/_config/LocationsManager.py +++ b/pym/portage/package/ebuild/_config/LocationsManager.py @@ -89,11 +89,12 @@ class LocationsManager(object): def _addProfile(self, currentPath): parentsFile = os.path.join(currentPath, "parent") eapi_file = os.path.join(currentPath, "eapi") + f = None try: - eapi = io.open(_unicode_encode(eapi_file, + f = io.open(_unicode_encode(eapi_file, encoding=_encodings['fs'], errors='strict'), - mode='r', encoding=_encodings['content'], errors='replace' - ).readline().strip() + mode='r', encoding=_encodings['content'], errors='replace') + eapi = f.readline().strip() except IOError: pass else: @@ -102,6 +103,9 @@ class LocationsManager(object): "Profile contains unsupported " "EAPI '%s': '%s'") % \ (eapi, os.path.realpath(eapi_file),)) + finally: + if f is not None: + f.close() if os.path.exists(parentsFile): parents = grabfile(parentsFile) if not parents: diff --git a/pym/portage/repository/config.py b/pym/portage/repository/config.py index ac9793e4a..a12bd7bc5 100644 --- a/pym/portage/repository/config.py +++ b/pym/portage/repository/config.py @@ -137,14 +137,19 @@ class RepoConfig(object): Returns repo_name, missing. """ repo_name_path = os.path.join(repo_path, REPO_NAME_LOC) + f = None try: - return io.open( + f = io.open( _unicode_encode(repo_name_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], - errors='replace').readline().strip(), False + errors='replace') + return f.readline().strip(), False except EnvironmentError: return "x-" + os.path.basename(repo_path), True + finally: + if f is not None: + f.close() def info_string(self): """ diff --git a/pym/portage/tests/ebuild/test_doebuild_spawn.py b/pym/portage/tests/ebuild/test_doebuild_spawn.py index ed08b2a1f..cafb16d96 100644 --- a/pym/portage/tests/ebuild/test_doebuild_spawn.py +++ b/pym/portage/tests/ebuild/test_doebuild_spawn.py @@ -1,4 +1,4 @@ -# Copyright 2010 Gentoo Foundation +# Copyright 2010-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 from portage import os @@ -50,7 +50,7 @@ class DoebuildSpawnTestCase(TestCase): os.makedirs(settings[x]) # Create a fake environment, to pretend as if the ebuild # has been sourced already. - open(os.path.join(settings['T'], 'environment'), 'wb') + open(os.path.join(settings['T'], 'environment'), 'wb').close() scheduler = PollScheduler().sched_iface for phase in ('_internal_test',): diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py index 4aa63d5e3..de3abdaa2 100644 --- a/pym/portage/util/__init__.py +++ b/pym/portage/util/__init__.py @@ -534,16 +534,18 @@ def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True): else: expand_map = {} mykeys = {} + f = None try: # NOTE: shlex doesn't support unicode objects with Python 2 # (produces spurious \0 characters). if sys.hexversion < 0x3000000: - content = open(_unicode_encode(mycfg, - encoding=_encodings['fs'], errors='strict'), 'rb').read() + f = open(_unicode_encode(mycfg, + encoding=_encodings['fs'], errors='strict'), 'rb') else: - content = open(_unicode_encode(mycfg, + f = open(_unicode_encode(mycfg, encoding=_encodings['fs'], errors='strict'), mode='r', - encoding=_encodings['content'], errors='replace').read() + encoding=_encodings['content'], errors='replace') + content = f.read() except IOError as e: if e.errno == PermissionDenied.errno: raise PermissionDenied(mycfg) @@ -552,6 +554,9 @@ def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True): if e.errno not in (errno.EISDIR,): raise return None + finally: + if f is not None: + f.close() # Workaround for avoiding a silent error in shlex that is # triggered by a source statement at the end of the file -- cgit v1.2.3-1-g7c22