python3.2 fixes: ResourceWarning: unclosed file
authorZac Medico <zmedico@gentoo.org>
Thu, 25 Aug 2011 02:25:59 +0000 (19:25 -0700)
committerZac Medico <zmedico@gentoo.org>
Thu, 25 Aug 2011 02:25:59 +0000 (19:25 -0700)
pym/portage/news.py
pym/portage/output.py
pym/portage/package/ebuild/_config/LocationsManager.py
pym/portage/repository/config.py
pym/portage/tests/ebuild/test_doebuild_spawn.py
pym/portage/util/__init__.py

index 866e5b0252a8af53fd277801797fd2f3864d3f5e..031e98c8c9fcb9eba00d7c8d89fcad0f61f246c3 100644 (file)
@@ -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):
index 0e8245f9a0e0451da6f26eb51151caa834d50346..763d74a7a4243ff34f82887835772efaffb7bd5c 100644 (file)
@@ -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);
index c2b115bb0ec1b5f195056e2d04e7d803253b8733..4072ffcae8b00776eb0d48fdbdb7f08677429812 100644 (file)
@@ -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:
index ac9793e4a6ec42de3bae4b60259abaa49d11739c..a12bd7bc5b89f3aeddc65a06538720ae3d953835 100644 (file)
@@ -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):
                """
index ed08b2a1f8b218478c887b36a0180858635cc6a5..cafb16d961ac53982df61b82541e2c4232ab4766 100644 (file)
@@ -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',):
index 4aa63d5e37a22a7a191ee99b46584dd5b06cbfb5..de3abdaa26739efb158860fa3eba874be6cc5b7e 100644 (file)
@@ -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