python3.2 fixes: ResourceWarning: unclosed file
authorZac Medico <zmedico@gentoo.org>
Thu, 25 Aug 2011 21:48:45 +0000 (14:48 -0700)
committerZac Medico <zmedico@gentoo.org>
Thu, 25 Aug 2011 21:50:39 +0000 (14:50 -0700)
bin/repoman
pym/_emerge/EbuildPhase.py
pym/_emerge/PollScheduler.py
pym/_emerge/SpawnProcess.py
pym/portage/elog/messages.py
pym/portage/package/ebuild/prepare_build_dirs.py
pym/portage/util/ExtractKernelVersion.py

index 10f603ea24afd2979085baa3584cbfae98524d4a..e0494d95e3361058384aa5f31d4e482366197baf 100755 (executable)
@@ -1228,11 +1228,13 @@ for x in scanlist:
 
                if not (y in ("ChangeLog", "metadata.xml") or y.endswith(".ebuild")):
                        continue
+               f = None
                try:
                        line = 1
-                       for l in io.open(_unicode_encode(os.path.join(checkdir, y),
+                       f = io.open(_unicode_encode(os.path.join(checkdir, y),
                                encoding=_encodings['fs'], errors='strict'),
-                               mode='r', encoding=_encodings['repo.content']):
+                               mode='r', encoding=_encodings['repo.content'])
+                       for l in f:
                                line +=1
                except UnicodeDecodeError as ue:
                        stats["file.UTF8"] += 1
@@ -1242,6 +1244,9 @@ for x in scanlist:
                        if l2 != 0:
                                s = s[s.rfind("\n") + 1:]
                        fails["file.UTF8"].append("%s/%s: line %i, just after: '%s'" % (checkdir, y, line, s))
+               finally:
+                       if f is not None:
+                               f.close()
 
        if vcs in ("git", "hg") and check_ebuild_notadded:
                if vcs == "git":
index f53570a5b8d6918c1bc8f1510388ff20b44c3318..ed0be0b0e8d35ecb1f5382b7ea51e4a185e6b776 100644 (file)
@@ -273,13 +273,15 @@ class EbuildPhase(CompositeTask):
                temp_file = open(_unicode_encode(temp_log,
                        encoding=_encodings['fs'], errors='strict'), 'rb')
 
-               log_file = self._open_log(log_path)
+               log_file, log_file_real = self._open_log(log_path)
 
                for line in temp_file:
                        log_file.write(line)
 
                temp_file.close()
                log_file.close()
+               if log_file_real is not log_file:
+                       log_file_real.close()
                os.unlink(temp_log)
 
        def _open_log(self, log_path):
@@ -287,11 +289,12 @@ class EbuildPhase(CompositeTask):
                f = open(_unicode_encode(log_path,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='ab')
+               f_real = f
 
                if log_path.endswith('.gz'):
                        f =  gzip.GzipFile(filename='', mode='ab', fileobj=f)
 
-               return f
+               return (f, f_real)
 
        def _die_hooks(self):
                self.returncode = None
index a2b5c2466f8434cb5a69f4ba1d11832a2fa7e9b6..fd9dfc0affb0cb8a6aeea638ebbb233affe03c7f 100644 (file)
@@ -333,6 +333,7 @@ class PollScheduler(object):
                                f = open(_unicode_encode(log_path,
                                        encoding=_encodings['fs'], errors='strict'),
                                        mode='ab')
+                               f_real = f
                        except IOError as e:
                                if e.errno not in (errno.ENOENT, errno.ESTALE):
                                        raise
@@ -349,6 +350,8 @@ class PollScheduler(object):
 
                                f.write(_unicode_encode(msg))
                                f.close()
+                               if f_real is not f:
+                                       f_real.close()
 
 _can_poll_device = None
 
index 099407eda8292bc71d7e98e4a5272585f94cdc54..065146c7518bed5ccf60e2868ee5f40176d30615 100644 (file)
@@ -26,7 +26,7 @@ class SpawnProcess(SubProcess):
                "path_lookup", "pre_exec")
 
        __slots__ = ("args",) + \
-               _spawn_kwarg_names + ("_selinux_type",)
+               _spawn_kwarg_names + ("_log_file_real", "_selinux_type",)
 
        _file_names = ("log", "process", "stdout")
        _files_dict = slot_dict_class(_file_names, prefix="")
@@ -84,6 +84,7 @@ class SpawnProcess(SubProcess):
                        files.log = open(_unicode_encode(logfile,
                                encoding=_encodings['fs'], errors='strict'), mode='ab')
                        if logfile.endswith('.gz'):
+                               self._log_file_real = files.log
                                files.log = gzip.GzipFile(filename='', mode='ab',
                                        fileobj=files.log)
 
@@ -238,3 +239,9 @@ class SpawnProcess(SubProcess):
 
                self._unregister_if_appropriate(event)
 
+       def _unregister(self):
+               super(SpawnProcess, self)._unregister()
+               if self._log_file_real is not None:
+                       # Avoid "ResourceWarning: unclosed file" since python 3.2.
+                       self._log_file_real.close()
+                       self._log_file_real = None
index 6c1580a3785eb9b16f047ea30a5c917e6b5b1132..50e0ab10a9deb8a75f84917dac7bf545a81619c6 100644 (file)
@@ -43,9 +43,10 @@ def collect_ebuild_messages(path):
                        logentries[msgfunction] = []
                lastmsgtype = None
                msgcontent = []
-               for l in io.open(_unicode_encode(filename,
+               f = io.open(_unicode_encode(filename,
                        encoding=_encodings['fs'], errors='strict'),
-                       mode='r', encoding=_encodings['repo.content'], errors='replace'):
+                       mode='r', encoding=_encodings['repo.content'], errors='replace')
+               for l in f:
                        if not l:
                                continue
                        try:
@@ -65,6 +66,7 @@ def collect_ebuild_messages(path):
                                        logentries[msgfunction].append((lastmsgtype, msgcontent))
                                msgcontent = [msg]
                        lastmsgtype = msgtype
+               f.close()
                if msgcontent:
                        logentries[msgfunction].append((lastmsgtype, msgcontent))
 
index 12c80ed293499b9f8c3c67b61b5ea2fb06e1926a..6de486acff0106f3587cd698ba724d2db2c6e350 100644 (file)
@@ -119,11 +119,13 @@ def _adjust_perms_msg(settings, msg):
        background = settings.get("PORTAGE_BACKGROUND") == "1"
        log_path = settings.get("PORTAGE_LOG_FILE")
        log_file = None
+       log_file_real = None
 
        if background and log_path is not None:
                try:
                        log_file = open(_unicode_encode(log_path,
                                encoding=_encodings['fs'], errors='strict'), mode='ab')
+                       log_file_real = log_file
                except IOError:
                        def write(msg):
                                pass
@@ -140,6 +142,8 @@ def _adjust_perms_msg(settings, msg):
        finally:
                if log_file is not None:
                        log_file.close()
+                       if log_file_real is not log_file:
+                               log_file_real.close()
 
 def _prepare_features_dirs(mysettings):
 
index 5cb9747e68eae6d144633352411d16651660c773..49957f5fb03fbc81fd197f2e8febb1641ab2a9a8 100644 (file)
@@ -37,6 +37,8 @@ def ExtractKernelVersion(base_dir):
                return (None, str(details))
        except IOError as details:
                return (None, str(details))
+       finally:
+               f.close()
 
        lines = [l.strip() for l in lines]